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.7 by pajs, Wed Jun 4 16:03:34 2003 UTC

# Line 22 | Line 22
22   #include "config.h"
23   #endif
24  
25 + #include "statgrab.h"
26 + #if defined(SOLARIS) || defined(LINUX)
27   #include <stdio.h>
28   #include <stdlib.h>
29 < #include <unistd.h>
30 < #include "statgrab.h"
29 > #include <sys/types.h>
30 > #include <dirent.h>
31 > #include <string.h>
32 > #endif
33  
34   #ifdef SOLARIS
35   #include <procfs.h>
36   #include <limits.h>
33 #include <dirent.h>
34 #include <string.h>
37   #define PROC_LOCATION "/proc"
38   #define MAX_FILE_LENGTH PATH_MAX
39   #endif
40 + #ifdef LINUX
41 + #include "tools.h"
42 + #include <linux/limits.h>
43 + #define PROC_LOCATION "/proc"
44 + #define MAX_FILE_LENGTH PATH_MAX
45 + #endif
46 + #ifdef FREEBSD
47 + #include <kvm.h>
48 + #include <fcntl.h>
49 + #include <sys/param.h>
50 + #include <sys/sysctl.h>
51 + #include <sys/user.h>
52 + #endif
53  
54   process_stat_t *get_process_stats(){
55  
56          static process_stat_t process_stat;
57  
58 <        psinfo_t process_info;
58 > #if defined(SOLARIS) || defined(LINUX)
59          DIR *proc_dir;
60          struct dirent *dir_entry;
61          char filename[MAX_FILE_LENGTH];
62          FILE *f;
63 + #endif
64 + #ifdef LINUX
65 +        char *line_ptr;
66 + #endif
67 + #ifdef SOLARIS
68 +        psinfo_t process_info;
69 + #endif
70 + #ifdef FREEBSD
71 +        struct kinfo_proc *kp_stats;    
72 +        kvm_t *kvmd = NULL;
73 +        int procs;
74 + #endif
75  
76          process_stat.sleeping=0;
77          process_stat.running=0;
78          process_stat.zombie=0;
79          process_stat.stopped=0;
80 <        process_stat.running=0;
80 >        process_stat.total=0;
81  
82 + #if defined(SOLARIS) || defined(LINUX)
83          if((proc_dir=opendir(PROC_LOCATION))==NULL){
84                  return NULL;
85          }
86  
87          while((dir_entry=readdir(proc_dir))!=NULL){
88                  if(atoi(dir_entry->d_name) == 0) continue;
89 +
90 + #ifdef SOLARIS
91                  snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
92 + #endif
93 + #ifdef LINUX
94 +                snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name);
95 + #endif
96  
97                  if((f=fopen(filename, "r"))==NULL){
98                          /* Open failed.. Process since vanished, or the path was too long.
# Line 66 | Line 100 | process_stat_t *get_process_stats(){
100                          continue;
101                  }
102  
103 + #ifdef SOLARIS
104                  fread(&process_info, sizeof(psinfo_t), 1, f);
70                fclose(f);
71
105                  if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++;
106                  if(process_info.pr_lwp.pr_state==2) process_stat.running++;
107                  if(process_info.pr_lwp.pr_state==3) process_stat.zombie++;
108                  if(process_info.pr_lwp.pr_state==4) process_stat.stopped++;
109                  if(process_info.pr_lwp.pr_state==6) process_stat.running++;
110 + #endif
111 + #ifdef LINUX
112 +                if((line_ptr=f_read_line(f, "State:"))==NULL){
113 +                        fclose(f);
114 +                        continue;
115 +                }
116 +                if((line_ptr=strchr(line_ptr, '\t'))==NULL){
117 +                        fclose(f);
118 +                        continue;
119 +                }
120 +                line_ptr++;
121 +                if(line_ptr=='\0'){
122 +                        fclose(f);
123 +                        continue;
124 +                }
125 +
126 +                if(*line_ptr=='S') process_stat.sleeping++;
127 +                if(*line_ptr=='R') process_stat.running++;
128 +                if(*line_ptr=='Z') process_stat.zombie++;
129 +                if(*line_ptr=='T') process_stat.stopped++;
130 +                if(*line_ptr=='D') process_stat.stopped++;
131 + #endif  
132 +
133 +                fclose(f);
134          }
78        
135          closedir(proc_dir);
136 + #endif
137 + #ifdef FREEBSD
138 +        if((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL){
139 +                return NULL;
140 +        }
141  
142 +        kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs);
143 +
144 +        while(procs--){
145 +                if (kp_stats[procs].kp_proc.p_stat == SSLEEP) process_stat.sleeping++;  
146 +                if (kp_stats[procs].kp_proc.p_stat == SRUN) process_stat.running++;
147 +                if (kp_stats[procs].kp_proc.p_stat == SIDL) process_stat.running++;
148 +                if (kp_stats[procs].kp_proc.p_stat == SZOMB) process_stat.zombie++;
149 +                if (kp_stats[procs].kp_proc.p_stat == SSTOP) process_stat.stopped++;
150 +        }
151 +
152 +        kvm_close(kvmd);
153 + #endif
154  
155          process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped;
156          return &process_stat;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines