ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/process_stats.c
Revision: 1.14
Committed: Mon Nov 10 21:07:04 2003 UTC (20 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.13: +4 -0 lines
Log Message:
Add support for cygwin. This is a bit limited, there's a few things that
can't be retrieved on cygwin such as load averages, diskio, network io,
and process stats. The package compiles and runs, and both saidar and
statgrab work.

Taken from a patch submitted by Ron Arts <raarts@netland.nl>. Thanks Ron!

File Contents

# Content
1 /*
2 * i-scream central monitoring system
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
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program 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.
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "statgrab.h"
26 #if defined(SOLARIS) || defined(LINUX)
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31 #include <string.h>
32 #endif
33
34 #include "tools.h"
35 #ifdef SOLARIS
36 #include <procfs.h>
37 #include <limits.h>
38 #define PROC_LOCATION "/proc"
39 #define MAX_FILE_LENGTH PATH_MAX
40 #endif
41 #ifdef LINUX
42 #ifdef CYGWIN
43 #include <limits.h>
44 #else
45 #include <linux/limits.h>
46 #endif
47 #define PROC_LOCATION "/proc"
48 #define MAX_FILE_LENGTH PATH_MAX
49 #endif
50 #ifdef ALLBSD
51 #include <kvm.h>
52 #include <sys/param.h>
53 #include <sys/sysctl.h>
54 #endif
55 #ifdef FREEBSD
56 #include <sys/user.h>
57 #endif
58
59 process_stat_t *get_process_stats(){
60
61 static process_stat_t process_stat;
62
63 #if defined(SOLARIS) || defined(LINUX)
64 DIR *proc_dir;
65 struct dirent *dir_entry;
66 char filename[MAX_FILE_LENGTH];
67 FILE *f;
68 #endif
69 #ifdef LINUX
70 char *line_ptr;
71 #endif
72 #ifdef SOLARIS
73 psinfo_t process_info;
74 #endif
75 #ifdef ALLBSD
76 struct kinfo_proc *kp_stats;
77 kvm_t *kvmd;
78 int procs;
79 #endif
80
81 process_stat.sleeping=0;
82 process_stat.running=0;
83 process_stat.zombie=0;
84 process_stat.stopped=0;
85 process_stat.total=0;
86
87 #if defined(SOLARIS) || defined(LINUX)
88 if((proc_dir=opendir(PROC_LOCATION))==NULL){
89 return NULL;
90 }
91
92 while((dir_entry=readdir(proc_dir))!=NULL){
93 if(atoi(dir_entry->d_name) == 0) continue;
94
95 #ifdef SOLARIS
96 snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
97 #endif
98 #ifdef LINUX
99 snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name);
100 #endif
101
102 if((f=fopen(filename, "r"))==NULL){
103 /* Open failed.. Process since vanished, or the path was too long.
104 * Ah well, move onwards to the next one */
105 continue;
106 }
107
108 #ifdef SOLARIS
109 fread(&process_info, sizeof(psinfo_t), 1, f);
110 if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++;
111 if(process_info.pr_lwp.pr_state==2) process_stat.running++;
112 if(process_info.pr_lwp.pr_state==3) process_stat.zombie++;
113 if(process_info.pr_lwp.pr_state==4) process_stat.stopped++;
114 if(process_info.pr_lwp.pr_state==6) process_stat.running++;
115 #endif
116 #ifdef LINUX
117 if((line_ptr=f_read_line(f, "State:"))==NULL){
118 fclose(f);
119 continue;
120 }
121 if((line_ptr=strchr(line_ptr, '\t'))==NULL){
122 fclose(f);
123 continue;
124 }
125 line_ptr++;
126 if(line_ptr=='\0'){
127 fclose(f);
128 continue;
129 }
130
131 if(*line_ptr=='S') process_stat.sleeping++;
132 if(*line_ptr=='R') process_stat.running++;
133 if(*line_ptr=='Z') process_stat.zombie++;
134 if(*line_ptr=='T') process_stat.stopped++;
135 if(*line_ptr=='D') process_stat.stopped++;
136 #endif
137
138 fclose(f);
139 }
140 closedir(proc_dir);
141 #endif
142 #ifdef ALLBSD
143 if((kvmd = get_kvm()) == NULL){
144 return NULL;
145 }
146
147 kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs);
148
149 while(procs--){
150 #ifdef FREEBSD5
151 if (kp_stats[procs].ki_stat == SSLEEP) process_stat.sleeping++;
152 if (kp_stats[procs].ki_stat == SWAIT) process_stat.sleeping++;
153 if (kp_stats[procs].ki_stat == SLOCK) process_stat.sleeping++;
154 if (kp_stats[procs].ki_stat == SRUN) process_stat.running++;
155 if (kp_stats[procs].ki_stat == SIDL) process_stat.running++;
156 if (kp_stats[procs].ki_stat == SZOMB) process_stat.zombie++;
157 if (kp_stats[procs].ki_stat == SSTOP) process_stat.stopped++;
158
159 #else
160 if (kp_stats[procs].kp_proc.p_stat == SSLEEP) process_stat.sleeping++;
161 if (kp_stats[procs].kp_proc.p_stat == SRUN) process_stat.running++;
162 if (kp_stats[procs].kp_proc.p_stat == SIDL) process_stat.running++;
163 if (kp_stats[procs].kp_proc.p_stat == SZOMB) process_stat.zombie++;
164 if (kp_stats[procs].kp_proc.p_stat == SSTOP) process_stat.stopped++;
165 #endif
166 }
167 #endif
168
169 process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped;
170
171 return &process_stat;
172 }