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.1 by pajs, Tue Feb 18 19:28:30 2003 UTC vs.
Revision 1.12 by ats, Sun Oct 19 00:25:30 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 <stdio.h>
25   #include "statgrab.h"
26 + #if defined(SOLARIS) || defined(LINUX)
27 + #include <stdio.h>
28   #include <stdlib.h>
29 < #include <unistd.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 <sys/types.h>
34 #include <dirent.h>
35 #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 <sys/param.h>
49 + #include <sys/sysctl.h>
50 + #include <sys/user.h>
51 + #endif
52  
53   process_stat_t *get_process_stats(){
54  
55          static process_stat_t process_stat;
56  
57 <        psinfo_t process_info;
57 > #if defined(SOLARIS) || defined(LINUX)
58          DIR *proc_dir;
59          struct dirent *dir_entry;
60          char filename[MAX_FILE_LENGTH];
61          FILE *f;
62 + #endif
63 + #ifdef LINUX
64 +        char *line_ptr;
65 + #endif
66 + #ifdef SOLARIS
67 +        psinfo_t process_info;
68 + #endif
69 + #ifdef FREEBSD
70 +        struct kinfo_proc *kp_stats;    
71 +        kvm_t *kvmd;
72 +        int procs;
73 + #endif
74  
75          process_stat.sleeping=0;
76          process_stat.running=0;
77          process_stat.zombie=0;
78          process_stat.stopped=0;
79 <        process_stat.running=0;
79 >        process_stat.total=0;
80  
81 + #if defined(SOLARIS) || defined(LINUX)
82          if((proc_dir=opendir(PROC_LOCATION))==NULL){
83                  return NULL;
84          }
85  
86          while((dir_entry=readdir(proc_dir))!=NULL){
87                  if(atoi(dir_entry->d_name) == 0) continue;
88 +
89 + #ifdef SOLARIS
90                  snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
91 + #endif
92 + #ifdef LINUX
93 +                snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name);
94 + #endif
95  
64                printf("Filename : %s\n", filename);
96                  if((f=fopen(filename, "r"))==NULL){
97                          /* Open failed.. Process since vanished, or the path was too long.
98                           * Ah well, move onwards to the next one */
99                          continue;
100                  }
101  
102 + #ifdef SOLARIS
103                  fread(&process_info, sizeof(psinfo_t), 1, f);
72                fclose(f);
73
104                  if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++;
105                  if(process_info.pr_lwp.pr_state==2) process_stat.running++;
106                  if(process_info.pr_lwp.pr_state==3) process_stat.zombie++;
107                  if(process_info.pr_lwp.pr_state==4) process_stat.stopped++;
108                  if(process_info.pr_lwp.pr_state==6) process_stat.running++;
109 + #endif
110 + #ifdef LINUX
111 +                if((line_ptr=f_read_line(f, "State:"))==NULL){
112 +                        fclose(f);
113 +                        continue;
114 +                }
115 +                if((line_ptr=strchr(line_ptr, '\t'))==NULL){
116 +                        fclose(f);
117 +                        continue;
118 +                }
119 +                line_ptr++;
120 +                if(line_ptr=='\0'){
121 +                        fclose(f);
122 +                        continue;
123 +                }
124 +
125 +                if(*line_ptr=='S') process_stat.sleeping++;
126 +                if(*line_ptr=='R') process_stat.running++;
127 +                if(*line_ptr=='Z') process_stat.zombie++;
128 +                if(*line_ptr=='T') process_stat.stopped++;
129 +                if(*line_ptr=='D') process_stat.stopped++;
130 + #endif  
131 +
132 +                fclose(f);
133          }
80        
134          closedir(proc_dir);
135 + #endif
136 + #ifdef FREEBSD
137 +        if((kvmd = get_kvm()) == NULL){
138 +                return NULL;
139 +        }
140 +
141 +        kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs);
142 +
143 +        while(procs--){
144 + #ifdef FREEBSD5
145 +                if (kp_stats[procs].ki_stat == SSLEEP) process_stat.sleeping++;
146 +                if (kp_stats[procs].ki_stat == SWAIT) process_stat.sleeping++;
147 +                if (kp_stats[procs].ki_stat == SLOCK) process_stat.sleeping++;
148 +                if (kp_stats[procs].ki_stat == SRUN) process_stat.running++;
149 +                if (kp_stats[procs].ki_stat == SIDL) process_stat.running++;
150 +                if (kp_stats[procs].ki_stat == SZOMB) process_stat.zombie++;
151 +                if (kp_stats[procs].ki_stat == SSTOP) process_stat.stopped++;
152 +                
153 + #else
154 +                if (kp_stats[procs].kp_proc.p_stat == SSLEEP) process_stat.sleeping++;  
155 +                if (kp_stats[procs].kp_proc.p_stat == SRUN) process_stat.running++;
156 +                if (kp_stats[procs].kp_proc.p_stat == SIDL) process_stat.running++;
157 +                if (kp_stats[procs].kp_proc.p_stat == SZOMB) process_stat.zombie++;
158 +                if (kp_stats[procs].kp_proc.p_stat == SSTOP) process_stat.stopped++;
159 + #endif
160 +        }
161 + #endif
162 +
163 +        process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped;
164  
165          return &process_stat;
166   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines