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.8 by tdb, Sun Aug 24 20:24:09 2003 UTC vs.
Revision 1.22 by tdb, Mon Feb 16 14:55:32 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 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
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
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 program is distributed in the hope that it will be useful,
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
14 < * GNU General Public License for more details.
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 General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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$
22   */
23  
24   #ifdef HAVE_CONFIG_H
# Line 31 | Line 34
34   #include <string.h>
35   #endif
36  
37 + #include "tools.h"
38   #ifdef SOLARIS
39   #include <procfs.h>
40   #include <limits.h>
# Line 38 | Line 42
42   #define MAX_FILE_LENGTH PATH_MAX
43   #endif
44   #ifdef LINUX
45 < #include "tools.h"
42 < #include <linux/limits.h>
45 > #include <limits.h>
46   #define PROC_LOCATION "/proc"
47   #define MAX_FILE_LENGTH PATH_MAX
48   #endif
49 < #ifdef FREEBSD
50 < #include <kvm.h>
48 < #include <fcntl.h>
49 > #ifdef ALLBSD
50 > #include <stdlib.h>
51   #include <sys/param.h>
52   #include <sys/sysctl.h>
53 + #if defined(FREEBSD) || defined(DFBSD)
54   #include <sys/user.h>
55 + #else
56 + #include <sys/proc.h>
57   #endif
58 + #endif
59  
60   process_stat_t *get_process_stats(){
61  
# Line 67 | Line 73 | process_stat_t *get_process_stats(){
73   #ifdef SOLARIS
74          psinfo_t process_info;
75   #endif
76 < #ifdef FREEBSD
76 > #ifdef ALLBSD
77 >        int mib[3];
78 >        size_t size;
79          struct kinfo_proc *kp_stats;    
80 <        kvm_t *kvmd = NULL;
73 <        int procs;
80 >        int procs, i;
81   #endif
82  
83          process_stat.sleeping=0;
# Line 134 | Line 141 | process_stat_t *get_process_stats(){
141          }
142          closedir(proc_dir);
143   #endif
144 < #ifdef FREEBSD
145 <        if((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL){
144 > #ifdef ALLBSD
145 >        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                  return NULL;
151          }
152 +        
153 +        procs = size / sizeof(struct kinfo_proc);
154  
155 <        kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs);
155 >        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 >                free(kp_stats);
162 >                return NULL;
163 >        }
164  
165 <        while(procs--){
166 <                if (kp_stats[procs].kp_proc.p_stat == SSLEEP) process_stat.sleeping++;  
167 <                if (kp_stats[procs].kp_proc.p_stat == SRUN) process_stat.running++;
168 <                if (kp_stats[procs].kp_proc.p_stat == SIDL) process_stat.running++;
169 <                if (kp_stats[procs].kp_proc.p_stat == SZOMB) process_stat.zombie++;
170 <                if (kp_stats[procs].kp_proc.p_stat == SSTOP) process_stat.stopped++;
165 >        for (i = 0; i < procs; i++) {
166 > #ifdef FREEBSD5
167 >                switch (kp_stats[i].ki_stat) {
168 > #else
169 >                switch (kp_stats[i].kp_proc.p_stat) {
170 > #endif
171 >                case SIDL:
172 >                case SRUN:
173 > #ifdef SONPROC
174 >                case SONPROC: /* NetBSD */
175 > #endif
176 >                        process_stat.running++;
177 >                        break;
178 >                case SSLEEP:
179 > #ifdef SWAIT
180 >                case SWAIT: /* FreeBSD 5 */
181 > #endif
182 > #ifdef SLOCK
183 >                case SLOCK: /* FreeBSD 5 */
184 > #endif
185 >                        process_stat.sleeping++;
186 >                        break;
187 >                case SSTOP:
188 >                        process_stat.stopped++;
189 >                        break;
190 >                case SZOMB:
191 > #ifdef SDEAD
192 >                case SDEAD: /* OpenBSD & NetBSD */
193 > #endif
194 >                        process_stat.zombie++;
195 >                        break;
196 >                }
197          }
198  
199 <        kvm_close(kvmd);
199 >        free(kp_stats);
200   #endif
201  
202 + #ifdef CYGWIN
203 +        return NULL;
204 + #endif
205 +
206          process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped;
207 +
208          return &process_stat;
209   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines