ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/process_stats.c
Revision: 1.22
Committed: Mon Feb 16 14:55:32 2004 UTC (20 years, 3 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_9
Changes since 1.21: +4 -3 lines
Log Message:
Add support for DragonFly BSD 1.0.
Also a minor tweak to the network interface code to make it more portable.

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.22 * $Id: process_stats.c,v 1.21 2004/02/14 16:43:55 ats 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.22 #if defined(FREEBSD) || defined(DFBSD)
54     #include <sys/user.h>
55     #else
56 tdb 1.19 #include <sys/proc.h>
57 ats 1.13 #endif
58 pajs 1.6 #endif
59 pajs 1.1
60     process_stat_t *get_process_stats(){
61    
62     static process_stat_t process_stat;
63    
64 pajs 1.6 #if defined(SOLARIS) || defined(LINUX)
65 pajs 1.1 DIR *proc_dir;
66     struct dirent *dir_entry;
67     char filename[MAX_FILE_LENGTH];
68     FILE *f;
69 pajs 1.6 #endif
70 pajs 1.5 #ifdef LINUX
71     char *line_ptr;
72     #endif
73     #ifdef SOLARIS
74     psinfo_t process_info;
75     #endif
76 ats 1.13 #ifdef ALLBSD
77 tdb 1.19 int mib[3];
78     size_t size;
79 pajs 1.6 struct kinfo_proc *kp_stats;
80 tdb 1.19 int procs, i;
81 pajs 1.6 #endif
82 pajs 1.1
83     process_stat.sleeping=0;
84     process_stat.running=0;
85     process_stat.zombie=0;
86     process_stat.stopped=0;
87 pajs 1.5 process_stat.total=0;
88 pajs 1.1
89 pajs 1.7 #if defined(SOLARIS) || defined(LINUX)
90 pajs 1.1 if((proc_dir=opendir(PROC_LOCATION))==NULL){
91     return NULL;
92     }
93    
94     while((dir_entry=readdir(proc_dir))!=NULL){
95     if(atoi(dir_entry->d_name) == 0) continue;
96 pajs 1.5
97     #ifdef SOLARIS
98 pajs 1.1 snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
99 pajs 1.5 #endif
100     #ifdef LINUX
101     snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name);
102     #endif
103 pajs 1.1
104     if((f=fopen(filename, "r"))==NULL){
105     /* Open failed.. Process since vanished, or the path was too long.
106     * Ah well, move onwards to the next one */
107     continue;
108     }
109    
110 pajs 1.5 #ifdef SOLARIS
111 pajs 1.1 fread(&process_info, sizeof(psinfo_t), 1, f);
112     if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++;
113     if(process_info.pr_lwp.pr_state==2) process_stat.running++;
114     if(process_info.pr_lwp.pr_state==3) process_stat.zombie++;
115     if(process_info.pr_lwp.pr_state==4) process_stat.stopped++;
116     if(process_info.pr_lwp.pr_state==6) process_stat.running++;
117 pajs 1.5 #endif
118     #ifdef LINUX
119     if((line_ptr=f_read_line(f, "State:"))==NULL){
120     fclose(f);
121     continue;
122     }
123     if((line_ptr=strchr(line_ptr, '\t'))==NULL){
124     fclose(f);
125     continue;
126     }
127     line_ptr++;
128     if(line_ptr=='\0'){
129     fclose(f);
130     continue;
131     }
132    
133     if(*line_ptr=='S') process_stat.sleeping++;
134     if(*line_ptr=='R') process_stat.running++;
135     if(*line_ptr=='Z') process_stat.zombie++;
136     if(*line_ptr=='T') process_stat.stopped++;
137     if(*line_ptr=='D') process_stat.stopped++;
138     #endif
139    
140     fclose(f);
141 pajs 1.1 }
142     closedir(proc_dir);
143 pajs 1.6 #endif
144 ats 1.13 #ifdef ALLBSD
145 tdb 1.19 mib[0] = CTL_KERN;
146     mib[1] = KERN_PROC;
147     mib[2] = KERN_PROC_ALL;
148    
149     if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
150 pajs 1.6 return NULL;
151     }
152 tdb 1.19
153     procs = size / sizeof(struct kinfo_proc);
154 pajs 1.6
155 tdb 1.19 kp_stats = malloc(size);
156     if (kp_stats == NULL) {
157     return NULL;
158     }
159    
160     if (sysctl(mib, 3, kp_stats, &size, NULL, 0) < 0) {
161 ats 1.21 free(kp_stats);
162 tdb 1.19 return NULL;
163     }
164 pajs 1.1
165 tdb 1.19 for (i = 0; i < procs; i++) {
166 pajs 1.9 #ifdef FREEBSD5
167 tdb 1.19 switch (kp_stats[i].ki_stat) {
168 pajs 1.9 #else
169 tdb 1.19 switch (kp_stats[i].kp_proc.p_stat) {
170 pajs 1.9 #endif
171 tdb 1.20 case SIDL:
172     case SRUN:
173     #ifdef SONPROC
174     case SONPROC: /* NetBSD */
175     #endif
176     process_stat.running++;
177     break;
178 tdb 1.19 case SSLEEP:
179 tdb 1.20 #ifdef SWAIT
180     case SWAIT: /* FreeBSD 5 */
181     #endif
182     #ifdef SLOCK
183     case SLOCK: /* FreeBSD 5 */
184     #endif
185 tdb 1.19 process_stat.sleeping++;
186     break;
187 tdb 1.20 case SSTOP:
188     process_stat.stopped++;
189 tdb 1.19 break;
190     case SZOMB:
191 tdb 1.20 #ifdef SDEAD
192     case SDEAD: /* OpenBSD & NetBSD */
193     #endif
194 tdb 1.19 process_stat.zombie++;
195     break;
196     }
197 pajs 1.6 }
198 ats 1.21
199     free(kp_stats);
200 ats 1.15 #endif
201    
202     #ifdef CYGWIN
203     return NULL;
204 pajs 1.6 #endif
205 pajs 1.4
206     process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped;
207 pajs 1.10
208 pajs 1.1 return &process_stat;
209     }