ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/process_stats.c
Revision: 1.20
Committed: Sat Feb 14 16:12:46 2004 UTC (20 years, 3 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.19: +19 -8 lines
Log Message:
Look at all the process states on all the BSD's. Tidly done using ifdef's.

File Contents

# User Rev Content
1 tdb 1.17 /*
2 pajs 1.1 * i-scream central monitoring system
3 tdb 1.8 * http://www.i-scream.org
4 tdb 1.17 * Copyright (C) 2000-2004 i-scream
5 pajs 1.1 *
6 tdb 1.17 * This library is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public
8     * License as published by the Free Software Foundation; either
9     * version 2.1 of the License, or (at your option) any later version.
10 pajs 1.1 *
11 tdb 1.17 * This library is distributed in the hope that it will be useful,
12 pajs 1.1 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 tdb 1.17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15 pajs 1.1 *
16 tdb 1.17 * You should have received a copy of the GNU Lesser General Public
17     * License along with this library; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19     * 02111-1307 USA
20 tdb 1.18 *
21 tdb 1.20 * $Id: process_stats.c,v 1.19 2004/02/14 02:56:11 tdb Exp $
22 pajs 1.1 */
23    
24     #ifdef HAVE_CONFIG_H
25     #include "config.h"
26     #endif
27    
28 pajs 1.6 #include "statgrab.h"
29     #if defined(SOLARIS) || defined(LINUX)
30 pajs 1.1 #include <stdio.h>
31     #include <stdlib.h>
32 pajs 1.5 #include <sys/types.h>
33     #include <dirent.h>
34     #include <string.h>
35 pajs 1.6 #endif
36 pajs 1.1
37 ats 1.13 #include "tools.h"
38 pajs 1.1 #ifdef SOLARIS
39     #include <procfs.h>
40     #include <limits.h>
41 pajs 1.5 #define PROC_LOCATION "/proc"
42     #define MAX_FILE_LENGTH PATH_MAX
43     #endif
44     #ifdef LINUX
45 ats 1.16 #include <limits.h>
46 pajs 1.1 #define PROC_LOCATION "/proc"
47     #define MAX_FILE_LENGTH PATH_MAX
48     #endif
49 ats 1.13 #ifdef ALLBSD
50 tdb 1.19 #include <stdlib.h>
51 pajs 1.6 #include <sys/param.h>
52     #include <sys/sysctl.h>
53 tdb 1.19 #include <sys/proc.h>
54 ats 1.13 #endif
55     #ifdef FREEBSD
56 pajs 1.6 #include <sys/user.h>
57     #endif
58 pajs 1.1
59     process_stat_t *get_process_stats(){
60    
61     static process_stat_t process_stat;
62    
63 pajs 1.6 #if defined(SOLARIS) || defined(LINUX)
64 pajs 1.1 DIR *proc_dir;
65     struct dirent *dir_entry;
66     char filename[MAX_FILE_LENGTH];
67     FILE *f;
68 pajs 1.6 #endif
69 pajs 1.5 #ifdef LINUX
70     char *line_ptr;
71     #endif
72     #ifdef SOLARIS
73     psinfo_t process_info;
74     #endif
75 ats 1.13 #ifdef ALLBSD
76 tdb 1.19 int mib[3];
77     size_t size;
78 pajs 1.6 struct kinfo_proc *kp_stats;
79 tdb 1.19 int procs, i;
80 pajs 1.6 #endif
81 pajs 1.1
82     process_stat.sleeping=0;
83     process_stat.running=0;
84     process_stat.zombie=0;
85     process_stat.stopped=0;
86 pajs 1.5 process_stat.total=0;
87 pajs 1.1
88 pajs 1.7 #if defined(SOLARIS) || defined(LINUX)
89 pajs 1.1 if((proc_dir=opendir(PROC_LOCATION))==NULL){
90     return NULL;
91     }
92    
93     while((dir_entry=readdir(proc_dir))!=NULL){
94     if(atoi(dir_entry->d_name) == 0) continue;
95 pajs 1.5
96     #ifdef SOLARIS
97 pajs 1.1 snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
98 pajs 1.5 #endif
99     #ifdef LINUX
100     snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name);
101     #endif
102 pajs 1.1
103     if((f=fopen(filename, "r"))==NULL){
104     /* Open failed.. Process since vanished, or the path was too long.
105     * Ah well, move onwards to the next one */
106     continue;
107     }
108    
109 pajs 1.5 #ifdef SOLARIS
110 pajs 1.1 fread(&process_info, sizeof(psinfo_t), 1, f);
111     if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++;
112     if(process_info.pr_lwp.pr_state==2) process_stat.running++;
113     if(process_info.pr_lwp.pr_state==3) process_stat.zombie++;
114     if(process_info.pr_lwp.pr_state==4) process_stat.stopped++;
115     if(process_info.pr_lwp.pr_state==6) process_stat.running++;
116 pajs 1.5 #endif
117     #ifdef LINUX
118     if((line_ptr=f_read_line(f, "State:"))==NULL){
119     fclose(f);
120     continue;
121     }
122     if((line_ptr=strchr(line_ptr, '\t'))==NULL){
123     fclose(f);
124     continue;
125     }
126     line_ptr++;
127     if(line_ptr=='\0'){
128     fclose(f);
129     continue;
130     }
131    
132     if(*line_ptr=='S') process_stat.sleeping++;
133     if(*line_ptr=='R') process_stat.running++;
134     if(*line_ptr=='Z') process_stat.zombie++;
135     if(*line_ptr=='T') process_stat.stopped++;
136     if(*line_ptr=='D') process_stat.stopped++;
137     #endif
138    
139     fclose(f);
140 pajs 1.1 }
141     closedir(proc_dir);
142 pajs 1.6 #endif
143 ats 1.13 #ifdef ALLBSD
144 tdb 1.19 mib[0] = CTL_KERN;
145     mib[1] = KERN_PROC;
146     mib[2] = KERN_PROC_ALL;
147    
148     if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
149 pajs 1.6 return NULL;
150     }
151 tdb 1.19
152     procs = size / sizeof(struct kinfo_proc);
153 pajs 1.6
154 tdb 1.19 kp_stats = malloc(size);
155     if (kp_stats == NULL) {
156     return NULL;
157     }
158    
159     if (sysctl(mib, 3, kp_stats, &size, NULL, 0) < 0) {
160     return NULL;
161     }
162 pajs 1.1
163 tdb 1.19 for (i = 0; i < procs; i++) {
164 pajs 1.9 #ifdef FREEBSD5
165 tdb 1.19 switch (kp_stats[i].ki_stat) {
166 pajs 1.9 #else
167 tdb 1.19 switch (kp_stats[i].kp_proc.p_stat) {
168 pajs 1.9 #endif
169 tdb 1.20 case SIDL:
170     case SRUN:
171     #ifdef SONPROC
172     case SONPROC: /* NetBSD */
173     #endif
174     process_stat.running++;
175     break;
176 tdb 1.19 case SSLEEP:
177 tdb 1.20 #ifdef SWAIT
178     case SWAIT: /* FreeBSD 5 */
179     #endif
180     #ifdef SLOCK
181     case SLOCK: /* FreeBSD 5 */
182     #endif
183 tdb 1.19 process_stat.sleeping++;
184     break;
185 tdb 1.20 case SSTOP:
186     process_stat.stopped++;
187 tdb 1.19 break;
188     case SZOMB:
189 tdb 1.20 #ifdef SDEAD
190     case SDEAD: /* OpenBSD & NetBSD */
191     #endif
192 tdb 1.19 process_stat.zombie++;
193     break;
194     }
195 pajs 1.6 }
196 ats 1.15 #endif
197 tdb 1.19
198     free(kp_stats);
199 ats 1.15
200     #ifdef CYGWIN
201     return NULL;
202 pajs 1.6 #endif
203 pajs 1.4
204     process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped;
205 pajs 1.10
206 pajs 1.1 return &process_stat;
207     }