ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/process_stats.c
Revision: 1.26
Committed: Tue Mar 30 22:10:04 2004 UTC (20 years, 1 month ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.25: +88 -5 lines
Log Message:
The startings of the BSD code. Only works on FreeBSD 5 so far.

Doesn't yet do command line arguments, or CPU percentages correctly.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org
4 * Copyright (C) 2000-2004 i-scream
5 *
6 * 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 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * 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 *
21 * $Id: process_stats.c,v 1.25 2004/03/30 13:41:31 pajs Exp $
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include "statgrab.h"
29 #if defined(SOLARIS) || defined(LINUX)
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <dirent.h>
34 #include <string.h>
35 #endif
36
37 #ifdef SOLARIS
38 #include <procfs.h>
39 #include <limits.h>
40 #define PROC_LOCATION "/proc"
41 #define MAX_FILE_LENGTH PATH_MAX
42 #endif
43 #ifdef LINUX
44 #include <limits.h>
45 #include <unistd.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #define PROC_LOCATION "/proc"
49 #define MAX_FILE_LENGTH PATH_MAX
50 #endif
51 #ifdef ALLBSD
52 #include <stdlib.h>
53 #include <sys/param.h>
54 #include <sys/sysctl.h>
55 #if defined(FREEBSD) || defined(DFBSD)
56 #include <sys/user.h>
57 #else
58 #include <sys/proc.h>
59 #endif
60 #endif
61
62 int get_proc_snapshot(proc_state_t **ps){
63 proc_state_t *proc_state = NULL;
64 proc_state_t *proc_state_ptr;
65 int proc_state_size = 0;
66 #ifdef ALLBSD
67 int mib[3];
68 size_t size;
69 struct kinfo_proc *kp_stats;
70 int procs, i;
71 #endif
72 #if defined(SOLARIS) || defined(LINUX)
73 DIR *proc_dir;
74 struct dirent *dir_entry;
75 char filename[MAX_FILE_LENGTH];
76 FILE *f;
77 #ifdef SOLARIS
78 psinfo_t process_info;
79 #endif
80 #ifdef LINUX
81 char s;
82 /* If someone has a executable of 4k filename length, they deserve to get it truncated :) */
83 char ps_name[4096];
84 char *ptr;
85 static char *psargs = NULL;
86 static int psarg_size = 0;
87 unsigned long stime, utime;
88 int x;
89 int fn;
90 int toread;
91 ssize_t size;
92 int t_read;
93 #endif
94
95 if((proc_dir=opendir(PROC_LOCATION))==NULL){
96 return -1;
97 }
98
99 while((dir_entry=readdir(proc_dir))!=NULL){
100 if(atoi(dir_entry->d_name) == 0) continue;
101
102 #ifdef SOLARIS
103 snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
104 #endif
105 #ifdef LINUX
106 snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/stat", dir_entry->d_name);
107 #endif
108 if((f=fopen(filename, "r"))==NULL){
109 /* Open failed.. Process since vanished, or the path was too long.
110 * Ah well, move onwards to the next one */
111 continue;
112 }
113 #ifdef SOLARIS
114 fread(&process_info, sizeof(psinfo_t), 1, f);
115 #endif
116
117 proc_state = realloc(proc_state, (1+proc_state_size)*sizeof(proc_state_t));
118 proc_state_ptr = proc_state+proc_state_size;
119 #ifdef SOLARIS
120 proc_state_ptr->pid = process_info.pr_pid;
121 proc_state_ptr->parent = process_info.pr_ppid;
122 proc_state_ptr->pgid = process_info.pr_pgid;
123 proc_state_ptr->uid = process_info.pr_uid;
124 proc_state_ptr->euid = process_info.pr_euid;
125 proc_state_ptr->gid = process_info.pr_gid;
126 proc_state_ptr->egid = process_info.pr_egid;
127 proc_state_ptr->proc_size = (process_info.pr_size) * 1024;
128 proc_state_ptr->proc_resident = (process_info.pr_rssize) * 1024;
129 proc_state_ptr->time_spent = process_info.pr_time.tv_sec;
130 proc_state_ptr->cpu_percent = (process_info.pr_pctcpu * 100.0) / 0x8000;
131 proc_state_ptr->process_name = strdup(process_info.pr_fname);
132 proc_state_ptr->proctitle = strdup(process_info.pr_psargs);
133
134 if(process_info.pr_lwp.pr_state==1) proc_state_ptr->state = SLEEPING;
135 if(process_info.pr_lwp.pr_state==2) proc_state_ptr->state = RUNNING;
136 if(process_info.pr_lwp.pr_state==3) proc_state_ptr->state = ZOMBIE;
137 if(process_info.pr_lwp.pr_state==4) proc_state_ptr->state = STOPPED;
138 if(process_info.pr_lwp.pr_state==6) proc_state_ptr->state = RUNNING;
139 #endif
140 #ifdef LINUX
141 x = fscanf(f, "%d %4096s %c %d %d %*d %*d %*d %*lu %*lu %*lu %*lu %*lu %lu %lu %*ld %*ld %*ld %d %*ld %*ld %*lu %llu %llu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*d %*d\n", &(proc_state_ptr->pid), ps_name, &s, &(proc_state_ptr->parent), &(proc_state_ptr->pgid), &utime, &stime, &(proc_state_ptr->nice), &(proc_state_ptr->proc_size), &(proc_state_ptr->proc_resident));
142 proc_state_ptr->proc_resident = proc_state_ptr->proc_resident * getpagesize();
143 if(s == 'S') proc_state_ptr->state = SLEEPING;
144 if(s == 'R') proc_state_ptr->state = RUNNING;
145 if(s == 'Z') proc_state_ptr->state = ZOMBIE;
146 if(s == 'T') proc_state_ptr->state = STOPPED;
147 if(s == 'D') proc_state_ptr->state = STOPPED;
148
149 /* pa_name[0] should = '(' */
150 ptr = strchr(&ps_name[1], ')');
151 if(ptr !=NULL) *ptr='\0';
152 proc_state_ptr->process_name = strdup(&ps_name[1]);
153
154 /* Need to do cpu */
155
156
157 /* proctitle */
158 snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/cmdline", dir_entry->d_name);
159
160 if((fn=open(filename, O_RDONLY)) == -1){
161 /* Open failed.. Process since vanished, or the path was too long.
162 * Ah well, move onwards to the next one */
163 continue;
164 }
165 #define PSARG_START_SIZE 128
166 if(psargs == NULL){
167 psargs = malloc(PSARG_START_SIZE);
168 psarg_size = PSARG_START_SIZE;
169 }
170 ptr = psargs;
171 t_read = 0;
172 toread = psarg_size;
173 while((size = read(fn, ptr, toread)) == toread){
174 psargs = realloc(psargs, (psarg_size + PSARG_START_SIZE));
175 ptr = psargs+psarg_size;
176 t_read = psarg_size;
177 psarg_size+=PSARG_START_SIZE;
178 toread = PSARG_START_SIZE;
179 }
180 if(size != -1) t_read+=size;
181
182 ptr = psargs;
183 for(x=0; x<t_read; x++){
184 if (*ptr == '\0') *ptr = ' ';
185 ptr++;
186 }
187 /* for safety sake */
188 psargs[t_read] = '\0';
189
190 proc_state_ptr->proctitle = strdup(psargs);
191
192 #endif
193
194 proc_state_size++;
195
196 fclose(f);
197 }
198 closedir(proc_dir);
199 #endif
200
201 #ifdef ALLBSD
202 mib[0] = CTL_KERN;
203 mib[1] = KERN_PROC;
204 mib[2] = KERN_PROC_ALL;
205
206 if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
207 return NULL;
208 }
209
210 procs = size / sizeof(struct kinfo_proc);
211
212 kp_stats = malloc(size);
213 if (kp_stats == NULL) {
214 return NULL;
215 }
216
217 if (sysctl(mib, 3, kp_stats, &size, NULL, 0) < 0) {
218 free(kp_stats);
219 return NULL;
220 }
221
222 for (i = 0; i < procs; i++) {
223 proc_state = realloc(proc_state, (1+proc_state_size)*sizeof(proc_state_t));
224 proc_state_ptr = proc_state+proc_state_size;
225
226 proc_state_ptr->process_name = strdup(kp_stats[i].ki_comm);
227 /* Seems we don't have access to that bit of memory */
228 /*proc_state_ptr->proctitle = strdup(kp_stats[i].ki_args->ar_args);*/
229
230 proc_state_ptr->pid = kp_stats[i].ki_pid;
231 proc_state_ptr->parent = kp_stats[i].ki_ppid;
232 proc_state_ptr->pgid = kp_stats[i].ki_pgid;
233
234 proc_state_ptr->uid = kp_stats[i].ki_ruid;
235 proc_state_ptr->euid = kp_stats[i].ki_uid;
236 proc_state_ptr->gid = kp_stats[i].ki_rgid;
237 proc_state_ptr->egid = kp_stats[i].ki_svgid;
238
239 proc_state_ptr->proc_size = kp_stats[i].ki_size;
240 /* This is in pages */
241 proc_state_ptr->proc_resident = kp_stats[i].ki_rssize * getpagesize();
242 /* This seems to be in microseconds */
243 proc_state_ptr->time_spent = kp_stats[i].ki_runtime / 1000000;
244 proc_state_ptr->cpu_percent = kp_stats[i].ki_pctcpu;
245 proc_state_ptr->nice = kp_stats[i].ki_nice;
246
247 switch (kp_stats[i].ki_stat) {
248 case SIDL:
249 case SRUN:
250 #ifdef SONPROC
251 case SONPROC: /* NetBSD */
252 #endif
253 proc_state_ptr->state = RUNNING;
254 break;
255 case SSLEEP:
256 #ifdef SWAIT
257 case SWAIT: /* FreeBSD 5 */
258 #endif
259 #ifdef SLOCK
260 case SLOCK: /* FreeBSD 5 */
261 #endif
262 proc_state_ptr->state = SLEEPING;
263 break;
264 case SSTOP:
265 proc_state_ptr->state = STOPPED;
266 break;
267 case SZOMB:
268 #ifdef SDEAD
269 case SDEAD: /* OpenBSD & NetBSD */
270 #endif
271 proc_state_ptr->state = ZOMBIE;
272 break;
273 }
274 proc_state_size++;
275 }
276
277 free(kp_stats);
278 #endif
279
280 *ps = proc_state;
281 return proc_state_size;
282 }