ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/process_stats.c
Revision: 1.25
Committed: Tue Mar 30 13:41:31 2004 UTC (20 years, 1 month ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.24: +77 -3 lines
Log Message:
Basic framework for linux.. Doesn't do CPU on a per process basis, as i'm
not sure the best way to approach that yet.

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 pajs 1.25 * $Id: process_stats.c,v 1.24 2004/03/28 18:22:58 pajs 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     #ifdef SOLARIS
38     #include <procfs.h>
39     #include <limits.h>
40 pajs 1.5 #define PROC_LOCATION "/proc"
41     #define MAX_FILE_LENGTH PATH_MAX
42     #endif
43     #ifdef LINUX
44 ats 1.16 #include <limits.h>
45 pajs 1.25 #include <unistd.h>
46     #include <sys/stat.h>
47     #include <fcntl.h>
48 pajs 1.1 #define PROC_LOCATION "/proc"
49     #define MAX_FILE_LENGTH PATH_MAX
50     #endif
51 ats 1.13 #ifdef ALLBSD
52 tdb 1.19 #include <stdlib.h>
53 pajs 1.6 #include <sys/param.h>
54     #include <sys/sysctl.h>
55 tdb 1.22 #if defined(FREEBSD) || defined(DFBSD)
56     #include <sys/user.h>
57     #else
58 tdb 1.19 #include <sys/proc.h>
59 ats 1.13 #endif
60 pajs 1.6 #endif
61 pajs 1.1
62 pajs 1.23 int get_proc_snapshot(proc_state_t **ps){
63     proc_state_t *proc_state = NULL;
64     proc_state_t *proc_state_ptr;
65     int proc_state_size = 0;
66 pajs 1.24 #if defined(SOLARIS) || defined(LINUX)
67 pajs 1.23 DIR *proc_dir;
68     struct dirent *dir_entry;
69     char filename[MAX_FILE_LENGTH];
70     FILE *f;
71 pajs 1.24 #ifdef SOLARIS
72 pajs 1.5 psinfo_t process_info;
73 pajs 1.24 #endif
74 pajs 1.25 #ifdef LINUX
75     char s;
76     /* If someone has a executable of 4k filename length, they deserve to get it truncated :) */
77     char ps_name[4096];
78     char *ptr;
79     static char *psargs = NULL;
80     static int psarg_size = 0;
81     unsigned long stime, utime;
82     int x;
83     int fn;
84     int toread;
85     ssize_t size;
86     int t_read;
87     #endif
88 pajs 1.1
89 pajs 1.23 if((proc_dir=opendir(PROC_LOCATION))==NULL){
90 pajs 1.25 return -1;
91 pajs 1.23 }
92    
93     while((dir_entry=readdir(proc_dir))!=NULL){
94     if(atoi(dir_entry->d_name) == 0) continue;
95    
96 pajs 1.24 #ifdef SOLARIS
97 pajs 1.23 snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
98 pajs 1.24 #endif
99 pajs 1.25 #ifdef LINUX
100     snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/stat", dir_entry->d_name);
101     #endif
102 pajs 1.23 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 pajs 1.24 #ifdef SOLARIS
108 pajs 1.23 fread(&process_info, sizeof(psinfo_t), 1, f);
109 pajs 1.25 #endif
110 pajs 1.1
111 pajs 1.23 proc_state = realloc(proc_state, (1+proc_state_size)*sizeof(proc_state_t));
112     proc_state_ptr = proc_state+proc_state_size;
113 pajs 1.25 #ifdef SOLARIS
114 pajs 1.23 proc_state_ptr->pid = process_info.pr_pid;
115     proc_state_ptr->parent = process_info.pr_ppid;
116     proc_state_ptr->pgid = process_info.pr_pgid;
117     proc_state_ptr->uid = process_info.pr_uid;
118     proc_state_ptr->euid = process_info.pr_euid;
119     proc_state_ptr->gid = process_info.pr_gid;
120     proc_state_ptr->egid = process_info.pr_egid;
121     proc_state_ptr->proc_size = (process_info.pr_size) * 1024;
122     proc_state_ptr->proc_resident = (process_info.pr_rssize) * 1024;
123     proc_state_ptr->time_spent = process_info.pr_time.tv_sec;
124     proc_state_ptr->cpu_percent = (process_info.pr_pctcpu * 100.0) / 0x8000;
125     proc_state_ptr->process_name = strdup(process_info.pr_fname);
126     proc_state_ptr->proctitle = strdup(process_info.pr_psargs);
127 pajs 1.24
128     if(process_info.pr_lwp.pr_state==1) proc_state_ptr->state = SLEEPING;
129     if(process_info.pr_lwp.pr_state==2) proc_state_ptr->state = RUNNING;
130     if(process_info.pr_lwp.pr_state==3) proc_state_ptr->state = ZOMBIE;
131     if(process_info.pr_lwp.pr_state==4) proc_state_ptr->state = STOPPED;
132     if(process_info.pr_lwp.pr_state==6) proc_state_ptr->state = RUNNING;
133 pajs 1.25 #endif
134     #ifdef LINUX
135     x = fscanf(f, "%d %4096s %c %d %d %*d %*d %*d %*lu %*lu %*lu %*lu %*lu %lu %lu %*ld %*ld %*ld %d %*ld %*ld %*lu %llu %llu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*d %*d\n", &(proc_state_ptr->pid), ps_name, &s, &(proc_state_ptr->parent), &(proc_state_ptr->pgid), &utime, &stime, &(proc_state_ptr->nice), &(proc_state_ptr->proc_size), &(proc_state_ptr->proc_resident));
136     proc_state_ptr->proc_resident = proc_state_ptr->proc_resident * getpagesize();
137     if(s == 'S') proc_state_ptr->state = SLEEPING;
138     if(s == 'R') proc_state_ptr->state = RUNNING;
139     if(s == 'Z') proc_state_ptr->state = ZOMBIE;
140     if(s == 'T') proc_state_ptr->state = STOPPED;
141     if(s == 'D') proc_state_ptr->state = STOPPED;
142    
143     /* pa_name[0] should = '(' */
144     ptr = strchr(&ps_name[1], ')');
145     if(ptr !=NULL) *ptr='\0';
146     proc_state_ptr->process_name = strdup(&ps_name[1]);
147    
148     /* Need to do cpu */
149    
150    
151     /* proctitle */
152     snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/cmdline", dir_entry->d_name);
153    
154     if((fn=open(filename, O_RDONLY)) == -1){
155     /* Open failed.. Process since vanished, or the path was too long.
156     * Ah well, move onwards to the next one */
157     continue;
158     }
159     #define PSARG_START_SIZE 128
160     if(psargs == NULL){
161     psargs = malloc(PSARG_START_SIZE);
162     psarg_size = PSARG_START_SIZE;
163     }
164     ptr = psargs;
165     t_read = 0;
166     toread = psarg_size;
167     while((size = read(fn, ptr, toread)) == toread){
168     psargs = realloc(psargs, (psarg_size + PSARG_START_SIZE));
169     ptr = psargs+psarg_size;
170     t_read = psarg_size;
171     psarg_size+=PSARG_START_SIZE;
172     toread = PSARG_START_SIZE;
173     }
174     if(size != -1) t_read+=size;
175    
176     ptr = psargs;
177     for(x=0; x<t_read; x++){
178     if (*ptr == '\0') *ptr = ' ';
179     ptr++;
180     }
181     /* for safety sake */
182     psargs[t_read] = '\0';
183    
184     proc_state_ptr->proctitle = strdup(psargs);
185    
186 pajs 1.24 #endif
187 pajs 1.23
188     proc_state_size++;
189 pajs 1.5 #endif
190 pajs 1.1
191    
192 pajs 1.23 fclose(f);
193     }
194     closedir(proc_dir);
195     *ps = proc_state;
196 ats 1.21
197 pajs 1.23 return proc_state_size;
198     }
199 ats 1.15