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.4 by pajs, Sat Mar 1 16:39:25 2003 UTC vs.
Revision 1.5 by pajs, Fri Mar 7 15:26:30 2003 UTC

# Line 24 | Line 24
24  
25   #include <stdio.h>
26   #include <stdlib.h>
27 #include <unistd.h>
27   #include "statgrab.h"
28 + #include <sys/types.h>
29 + #include <dirent.h>
30 + #include <string.h>
31  
32   #ifdef SOLARIS
33   #include <procfs.h>
34   #include <limits.h>
33 #include <dirent.h>
34 #include <string.h>
35   #define PROC_LOCATION "/proc"
36   #define MAX_FILE_LENGTH PATH_MAX
37   #endif
38 + #ifdef LINUX
39 + #include "tools.h"
40 + #include <linux/limits.h>
41 + #define PROC_LOCATION "/proc"
42 + #define MAX_FILE_LENGTH PATH_MAX
43 + #endif
44  
45   process_stat_t *get_process_stats(){
46  
47          static process_stat_t process_stat;
48  
43        psinfo_t process_info;
49          DIR *proc_dir;
50          struct dirent *dir_entry;
51          char filename[MAX_FILE_LENGTH];
52          FILE *f;
53 + #ifdef LINUX
54 +        char *line_ptr;
55 + #endif
56 + #ifdef SOLARIS
57 +        psinfo_t process_info;
58 + #endif
59  
60          process_stat.sleeping=0;
61          process_stat.running=0;
62          process_stat.zombie=0;
63          process_stat.stopped=0;
64 <        process_stat.running=0;
64 >        process_stat.total=0;
65  
66          if((proc_dir=opendir(PROC_LOCATION))==NULL){
67                  return NULL;
# Line 58 | Line 69 | process_stat_t *get_process_stats(){
69  
70          while((dir_entry=readdir(proc_dir))!=NULL){
71                  if(atoi(dir_entry->d_name) == 0) continue;
72 +
73 + #ifdef SOLARIS
74                  snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
75 + #endif
76 + #ifdef LINUX
77 +                snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name);
78 + #endif
79  
80                  if((f=fopen(filename, "r"))==NULL){
81                          /* Open failed.. Process since vanished, or the path was too long.
# Line 66 | Line 83 | process_stat_t *get_process_stats(){
83                          continue;
84                  }
85  
86 + #ifdef SOLARIS
87                  fread(&process_info, sizeof(psinfo_t), 1, f);
70                fclose(f);
71
88                  if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++;
89                  if(process_info.pr_lwp.pr_state==2) process_stat.running++;
90                  if(process_info.pr_lwp.pr_state==3) process_stat.zombie++;
91                  if(process_info.pr_lwp.pr_state==4) process_stat.stopped++;
92                  if(process_info.pr_lwp.pr_state==6) process_stat.running++;
93 + #endif
94 + #ifdef LINUX
95 +                if((line_ptr=f_read_line(f, "State:"))==NULL){
96 +                        fclose(f);
97 +                        continue;
98 +                }
99 +                if((line_ptr=strchr(line_ptr, '\t'))==NULL){
100 +                        fclose(f);
101 +                        continue;
102 +                }
103 +                line_ptr++;
104 +                if(line_ptr=='\0'){
105 +                        fclose(f);
106 +                        continue;
107 +                }
108 +
109 +                if(*line_ptr=='S') process_stat.sleeping++;
110 +                if(*line_ptr=='R') process_stat.running++;
111 +                if(*line_ptr=='Z') process_stat.zombie++;
112 +                if(*line_ptr=='T') process_stat.stopped++;
113 +                if(*line_ptr=='D') process_stat.stopped++;
114 + #endif  
115 +
116 +                fclose(f);
117          }
78        
118          closedir(proc_dir);
119  
120  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines