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

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3 tdb 1.8 * http://www.i-scream.org
4     * Copyright (C) 2000-2003 i-scream
5 pajs 1.1 *
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 pajs 1.6 #include "statgrab.h"
26     #if defined(SOLARIS) || defined(LINUX)
27 pajs 1.1 #include <stdio.h>
28     #include <stdlib.h>
29 pajs 1.5 #include <sys/types.h>
30     #include <dirent.h>
31     #include <string.h>
32 pajs 1.6 #endif
33 pajs 1.1
34 ats 1.13 #include "tools.h"
35 pajs 1.1 #ifdef SOLARIS
36     #include <procfs.h>
37     #include <limits.h>
38 pajs 1.5 #define PROC_LOCATION "/proc"
39     #define MAX_FILE_LENGTH PATH_MAX
40     #endif
41     #ifdef LINUX
42 tdb 1.14 #ifdef CYGWIN
43     #include <limits.h>
44     #else
45 pajs 1.5 #include <linux/limits.h>
46 tdb 1.14 #endif
47 pajs 1.1 #define PROC_LOCATION "/proc"
48     #define MAX_FILE_LENGTH PATH_MAX
49     #endif
50 ats 1.13 #ifdef ALLBSD
51 pajs 1.6 #include <kvm.h>
52     #include <sys/param.h>
53     #include <sys/sysctl.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 pajs 1.6 struct kinfo_proc *kp_stats;
77 ats 1.12 kvm_t *kvmd;
78 pajs 1.6 int procs;
79     #endif
80 pajs 1.1
81     process_stat.sleeping=0;
82     process_stat.running=0;
83     process_stat.zombie=0;
84     process_stat.stopped=0;
85 pajs 1.5 process_stat.total=0;
86 pajs 1.1
87 pajs 1.7 #if defined(SOLARIS) || defined(LINUX)
88 pajs 1.1 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 pajs 1.5
95     #ifdef SOLARIS
96 pajs 1.1 snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
97 pajs 1.5 #endif
98     #ifdef LINUX
99     snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name);
100     #endif
101 pajs 1.1
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 pajs 1.5 #ifdef SOLARIS
109 pajs 1.1 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 pajs 1.5 #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 pajs 1.1 }
140     closedir(proc_dir);
141 pajs 1.6 #endif
142 ats 1.13 #ifdef ALLBSD
143 ats 1.12 if((kvmd = get_kvm()) == NULL){
144 pajs 1.6 return NULL;
145     }
146    
147     kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs);
148 pajs 1.1
149 pajs 1.6 while(procs--){
150 pajs 1.9 #ifdef FREEBSD5
151     if (kp_stats[procs].ki_stat == SSLEEP) process_stat.sleeping++;
152 pajs 1.11 if (kp_stats[procs].ki_stat == SWAIT) process_stat.sleeping++;
153     if (kp_stats[procs].ki_stat == SLOCK) process_stat.sleeping++;
154 pajs 1.9 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 pajs 1.11
159 pajs 1.9 #else
160 pajs 1.6 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 pajs 1.9 #endif
166 pajs 1.6 }
167     #endif
168 pajs 1.4
169     process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped;
170 pajs 1.10
171 pajs 1.1 return &process_stat;
172     }