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.3 by pajs, Sat Mar 1 02:12:59 2003 UTC vs.
Revision 1.13 by ats, Sun Oct 19 02:03:02 2003 UTC

# Line 1 | Line 1
1   /*
2   * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
3 > * http://www.i-scream.org
4 > * Copyright (C) 2000-2003 i-scream
5   *
6   * This program is free software; you can redistribute it and/or
7   * modify it under the terms of the GNU General Public License
# 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 + #include "tools.h"
35   #ifdef SOLARIS
36   #include <procfs.h>
37   #include <limits.h>
33 #include <dirent.h>
34 #include <string.h>
38   #define PROC_LOCATION "/proc"
39   #define MAX_FILE_LENGTH PATH_MAX
40   #endif
41 + #ifdef LINUX
42 + #include <linux/limits.h>
43 + #define PROC_LOCATION "/proc"
44 + #define MAX_FILE_LENGTH PATH_MAX
45 + #endif
46 + #ifdef ALLBSD
47 + #include <kvm.h>
48 + #include <sys/param.h>
49 + #include <sys/sysctl.h>
50 + #endif
51 + #ifdef FREEBSD
52 + #include <sys/user.h>
53 + #endif
54  
55   process_stat_t *get_process_stats(){
56  
57          static process_stat_t process_stat;
58  
59 <        psinfo_t process_info;
59 > #if defined(SOLARIS) || defined(LINUX)
60          DIR *proc_dir;
61          struct dirent *dir_entry;
62          char filename[MAX_FILE_LENGTH];
63          FILE *f;
64 + #endif
65 + #ifdef LINUX
66 +        char *line_ptr;
67 + #endif
68 + #ifdef SOLARIS
69 +        psinfo_t process_info;
70 + #endif
71 + #ifdef ALLBSD
72 +        struct kinfo_proc *kp_stats;    
73 +        kvm_t *kvmd;
74 +        int procs;
75 + #endif
76  
77          process_stat.sleeping=0;
78          process_stat.running=0;
79          process_stat.zombie=0;
80          process_stat.stopped=0;
81 <        process_stat.running=0;
81 >        process_stat.total=0;
82  
83 + #if defined(SOLARIS) || defined(LINUX)
84          if((proc_dir=opendir(PROC_LOCATION))==NULL){
85                  return NULL;
86          }
87  
88          while((dir_entry=readdir(proc_dir))!=NULL){
89                  if(atoi(dir_entry->d_name) == 0) continue;
90 +
91 + #ifdef SOLARIS
92                  snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
93 + #endif
94 + #ifdef LINUX
95 +                snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name);
96 + #endif
97  
98                  if((f=fopen(filename, "r"))==NULL){
99                          /* Open failed.. Process since vanished, or the path was too long.
# Line 66 | Line 101 | process_stat_t *get_process_stats(){
101                          continue;
102                  }
103  
104 + #ifdef SOLARIS
105                  fread(&process_info, sizeof(psinfo_t), 1, f);
70                fclose(f);
71
106                  if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++;
107                  if(process_info.pr_lwp.pr_state==2) process_stat.running++;
108                  if(process_info.pr_lwp.pr_state==3) process_stat.zombie++;
109                  if(process_info.pr_lwp.pr_state==4) process_stat.stopped++;
110                  if(process_info.pr_lwp.pr_state==6) process_stat.running++;
111 + #endif
112 + #ifdef LINUX
113 +                if((line_ptr=f_read_line(f, "State:"))==NULL){
114 +                        fclose(f);
115 +                        continue;
116 +                }
117 +                if((line_ptr=strchr(line_ptr, '\t'))==NULL){
118 +                        fclose(f);
119 +                        continue;
120 +                }
121 +                line_ptr++;
122 +                if(line_ptr=='\0'){
123 +                        fclose(f);
124 +                        continue;
125 +                }
126 +
127 +                if(*line_ptr=='S') process_stat.sleeping++;
128 +                if(*line_ptr=='R') process_stat.running++;
129 +                if(*line_ptr=='Z') process_stat.zombie++;
130 +                if(*line_ptr=='T') process_stat.stopped++;
131 +                if(*line_ptr=='D') process_stat.stopped++;
132 + #endif  
133 +
134 +                fclose(f);
135          }
78        
136          closedir(proc_dir);
137 + #endif
138 + #ifdef ALLBSD
139 +        if((kvmd = get_kvm()) == NULL){
140 +                return NULL;
141 +        }
142 +
143 +        kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs);
144 +
145 +        while(procs--){
146 + #ifdef FREEBSD5
147 +                if (kp_stats[procs].ki_stat == SSLEEP) process_stat.sleeping++;
148 +                if (kp_stats[procs].ki_stat == SWAIT) process_stat.sleeping++;
149 +                if (kp_stats[procs].ki_stat == SLOCK) process_stat.sleeping++;
150 +                if (kp_stats[procs].ki_stat == SRUN) process_stat.running++;
151 +                if (kp_stats[procs].ki_stat == SIDL) process_stat.running++;
152 +                if (kp_stats[procs].ki_stat == SZOMB) process_stat.zombie++;
153 +                if (kp_stats[procs].ki_stat == SSTOP) process_stat.stopped++;
154 +                
155 + #else
156 +                if (kp_stats[procs].kp_proc.p_stat == SSLEEP) process_stat.sleeping++;  
157 +                if (kp_stats[procs].kp_proc.p_stat == SRUN) process_stat.running++;
158 +                if (kp_stats[procs].kp_proc.p_stat == SIDL) process_stat.running++;
159 +                if (kp_stats[procs].kp_proc.p_stat == SZOMB) process_stat.zombie++;
160 +                if (kp_stats[procs].kp_proc.p_stat == SSTOP) process_stat.stopped++;
161 + #endif
162 +        }
163 + #endif
164 +
165 +        process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped;
166  
167          return &process_stat;
168   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines