ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/process_stats.c
Revision: 1.18
Committed: Mon Jan 19 16:49:21 2004 UTC (20 years, 4 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_8_2, LIBSTATGRAB_0_8_1
Changes since 1.17: +2 -0 lines
Log Message:
A whole bunch of minor cosmetic changes.

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     * $Id$
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 pajs 1.6 #include <kvm.h>
51     #include <sys/param.h>
52     #include <sys/sysctl.h>
53 ats 1.13 #endif
54     #ifdef FREEBSD
55 pajs 1.6 #include <sys/user.h>
56     #endif
57 pajs 1.1
58     process_stat_t *get_process_stats(){
59    
60     static process_stat_t process_stat;
61    
62 pajs 1.6 #if defined(SOLARIS) || defined(LINUX)
63 pajs 1.1 DIR *proc_dir;
64     struct dirent *dir_entry;
65     char filename[MAX_FILE_LENGTH];
66     FILE *f;
67 pajs 1.6 #endif
68 pajs 1.5 #ifdef LINUX
69     char *line_ptr;
70     #endif
71     #ifdef SOLARIS
72     psinfo_t process_info;
73     #endif
74 ats 1.13 #ifdef ALLBSD
75 pajs 1.6 struct kinfo_proc *kp_stats;
76 ats 1.12 kvm_t *kvmd;
77 pajs 1.6 int procs;
78     #endif
79 pajs 1.1
80     process_stat.sleeping=0;
81     process_stat.running=0;
82     process_stat.zombie=0;
83     process_stat.stopped=0;
84 pajs 1.5 process_stat.total=0;
85 pajs 1.1
86 pajs 1.7 #if defined(SOLARIS) || defined(LINUX)
87 pajs 1.1 if((proc_dir=opendir(PROC_LOCATION))==NULL){
88     return NULL;
89     }
90    
91     while((dir_entry=readdir(proc_dir))!=NULL){
92     if(atoi(dir_entry->d_name) == 0) continue;
93 pajs 1.5
94     #ifdef SOLARIS
95 pajs 1.1 snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
96 pajs 1.5 #endif
97     #ifdef LINUX
98     snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name);
99     #endif
100 pajs 1.1
101     if((f=fopen(filename, "r"))==NULL){
102     /* Open failed.. Process since vanished, or the path was too long.
103     * Ah well, move onwards to the next one */
104     continue;
105     }
106    
107 pajs 1.5 #ifdef SOLARIS
108 pajs 1.1 fread(&process_info, sizeof(psinfo_t), 1, f);
109     if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++;
110     if(process_info.pr_lwp.pr_state==2) process_stat.running++;
111     if(process_info.pr_lwp.pr_state==3) process_stat.zombie++;
112     if(process_info.pr_lwp.pr_state==4) process_stat.stopped++;
113     if(process_info.pr_lwp.pr_state==6) process_stat.running++;
114 pajs 1.5 #endif
115     #ifdef LINUX
116     if((line_ptr=f_read_line(f, "State:"))==NULL){
117     fclose(f);
118     continue;
119     }
120     if((line_ptr=strchr(line_ptr, '\t'))==NULL){
121     fclose(f);
122     continue;
123     }
124     line_ptr++;
125     if(line_ptr=='\0'){
126     fclose(f);
127     continue;
128     }
129    
130     if(*line_ptr=='S') process_stat.sleeping++;
131     if(*line_ptr=='R') process_stat.running++;
132     if(*line_ptr=='Z') process_stat.zombie++;
133     if(*line_ptr=='T') process_stat.stopped++;
134     if(*line_ptr=='D') process_stat.stopped++;
135     #endif
136    
137     fclose(f);
138 pajs 1.1 }
139     closedir(proc_dir);
140 pajs 1.6 #endif
141 ats 1.13 #ifdef ALLBSD
142 ats 1.12 if((kvmd = get_kvm()) == NULL){
143 pajs 1.6 return NULL;
144     }
145    
146     kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs);
147 pajs 1.1
148 pajs 1.6 while(procs--){
149 pajs 1.9 #ifdef FREEBSD5
150     if (kp_stats[procs].ki_stat == SSLEEP) process_stat.sleeping++;
151 pajs 1.11 if (kp_stats[procs].ki_stat == SWAIT) process_stat.sleeping++;
152     if (kp_stats[procs].ki_stat == SLOCK) process_stat.sleeping++;
153 pajs 1.9 if (kp_stats[procs].ki_stat == SRUN) process_stat.running++;
154     if (kp_stats[procs].ki_stat == SIDL) process_stat.running++;
155     if (kp_stats[procs].ki_stat == SZOMB) process_stat.zombie++;
156     if (kp_stats[procs].ki_stat == SSTOP) process_stat.stopped++;
157 pajs 1.11
158 pajs 1.9 #else
159 pajs 1.6 if (kp_stats[procs].kp_proc.p_stat == SSLEEP) process_stat.sleeping++;
160     if (kp_stats[procs].kp_proc.p_stat == SRUN) process_stat.running++;
161     if (kp_stats[procs].kp_proc.p_stat == SIDL) process_stat.running++;
162     if (kp_stats[procs].kp_proc.p_stat == SZOMB) process_stat.zombie++;
163     if (kp_stats[procs].kp_proc.p_stat == SSTOP) process_stat.stopped++;
164 pajs 1.9 #endif
165 pajs 1.6 }
166 ats 1.15 #endif
167    
168     #ifdef CYGWIN
169     return NULL;
170 pajs 1.6 #endif
171 pajs 1.4
172     process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped;
173 pajs 1.10
174 pajs 1.1 return &process_stat;
175     }