ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/process_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/process_stats.c (file contents):
Revision 1.24 by pajs, Sun Mar 28 18:22:58 2004 UTC vs.
Revision 1.25 by pajs, Tue Mar 30 13:41:31 2004 UTC

# Line 42 | Line 42
42   #endif
43   #ifdef LINUX
44   #include <limits.h>
45 + #include <unistd.h>
46 + #include <sys/stat.h>
47 + #include <fcntl.h>
48   #define PROC_LOCATION "/proc"
49   #define MAX_FILE_LENGTH PATH_MAX
50   #endif
# Line 68 | Line 71 | int get_proc_snapshot(proc_state_t **ps){
71   #ifdef SOLARIS
72          psinfo_t process_info;
73   #endif
74 + #ifdef LINUX
75 +        char s;
76 +        /* If someone has a executable of 4k filename length, they deserve to get it truncated :) */
77 +        char ps_name[4096];
78 +        char *ptr;
79 +        static char *psargs = NULL;
80 +        static int psarg_size = 0;
81 +        unsigned long stime, utime;
82 +        int x;
83 +        int fn;
84 +        int toread;
85 +        ssize_t size;
86 +        int t_read;
87 + #endif
88  
89          if((proc_dir=opendir(PROC_LOCATION))==NULL){
90 <                return NULL;
90 >                return -1;
91          }
92  
93          while((dir_entry=readdir(proc_dir))!=NULL){
# Line 79 | Line 96 | int get_proc_snapshot(proc_state_t **ps){
96   #ifdef SOLARIS
97                  snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
98   #endif
99 + #ifdef LINUX
100 +                snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/stat", dir_entry->d_name);
101 + #endif
102                  if((f=fopen(filename, "r"))==NULL){
103                          /* Open failed.. Process since vanished, or the path was too long.
104                           * Ah well, move onwards to the next one */
# Line 86 | Line 106 | int get_proc_snapshot(proc_state_t **ps){
106                  }
107   #ifdef SOLARIS
108                  fread(&process_info, sizeof(psinfo_t), 1, f);
109 + #endif
110  
111                  proc_state = realloc(proc_state, (1+proc_state_size)*sizeof(proc_state_t));
112                  proc_state_ptr = proc_state+proc_state_size;
113 <                
113 > #ifdef SOLARIS          
114                  proc_state_ptr->pid = process_info.pr_pid;
115                  proc_state_ptr->parent = process_info.pr_ppid;
116                  proc_state_ptr->pgid = process_info.pr_pgid;
# Line 109 | Line 130 | int get_proc_snapshot(proc_state_t **ps){
130                  if(process_info.pr_lwp.pr_state==3) proc_state_ptr->state = ZOMBIE;
131                  if(process_info.pr_lwp.pr_state==4) proc_state_ptr->state = STOPPED;
132                  if(process_info.pr_lwp.pr_state==6) proc_state_ptr->state = RUNNING;
133 + #endif
134 + #ifdef LINUX
135 +                x = fscanf(f, "%d %4096s %c %d %d %*d %*d %*d %*lu %*lu %*lu %*lu %*lu %lu %lu %*ld %*ld %*ld %d %*ld %*ld %*lu %llu %llu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*d %*d\n", &(proc_state_ptr->pid), ps_name, &s, &(proc_state_ptr->parent), &(proc_state_ptr->pgid), &utime, &stime, &(proc_state_ptr->nice), &(proc_state_ptr->proc_size), &(proc_state_ptr->proc_resident));
136 +                proc_state_ptr->proc_resident = proc_state_ptr->proc_resident * getpagesize();
137 +                if(s == 'S') proc_state_ptr->state = SLEEPING;
138 +                if(s == 'R') proc_state_ptr->state = RUNNING;
139 +                if(s == 'Z') proc_state_ptr->state = ZOMBIE;
140 +                if(s == 'T') proc_state_ptr->state = STOPPED;
141 +                if(s == 'D') proc_state_ptr->state = STOPPED;
142 +        
143 +                /* pa_name[0] should = '(' */
144 +                ptr = strchr(&ps_name[1], ')');
145 +                if(ptr !=NULL) *ptr='\0';
146 +                proc_state_ptr->process_name = strdup(&ps_name[1]);
147 +
148 +                /* Need to do cpu */
149 +                
150 +
151 +                /* proctitle */
152 +                snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/cmdline", dir_entry->d_name);
153 +
154 +                if((fn=open(filename, O_RDONLY)) == -1){
155 +                        /* Open failed.. Process since vanished, or the path was too long.
156 +                         * Ah well, move onwards to the next one */
157 +                        continue;
158 +                }
159 + #define         PSARG_START_SIZE 128
160 +                if(psargs == NULL){
161 +                        psargs = malloc(PSARG_START_SIZE);
162 +                        psarg_size = PSARG_START_SIZE;
163 +                }
164 +                ptr = psargs;  
165 +                t_read = 0;
166 +                toread = psarg_size;
167 +                while((size = read(fn, ptr, toread)) == toread){
168 +                        psargs = realloc(psargs, (psarg_size + PSARG_START_SIZE));
169 +                        ptr = psargs+psarg_size;
170 +                        t_read = psarg_size;
171 +                        psarg_size+=PSARG_START_SIZE;
172 +                        toread = PSARG_START_SIZE;
173 +                }
174 +                if(size != -1) t_read+=size;
175 +
176 +                ptr = psargs;
177 +                for(x=0; x<t_read; x++){
178 +                        if (*ptr == '\0') *ptr = ' ';
179 +                        ptr++;
180 +                }
181 +                /*  for safety sake */
182 +                psargs[t_read] = '\0';
183 +
184 +                proc_state_ptr->proctitle = strdup(psargs);
185 +
186   #endif
187  
188                  proc_state_size++;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines