ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/process_stats.c
Revision: 1.23
Committed: Sun Mar 28 18:04:14 2004 UTC (20 years, 1 month ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.22: +49 -143 lines
Log Message:
The start of the rework for process_stats to give more information, and
to become a standard proc interface.

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.23 * $Id: process_stats.c,v 1.22 2004/02/16 14:55:32 tdb 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.1 #define PROC_LOCATION "/proc"
46     #define MAX_FILE_LENGTH PATH_MAX
47     #endif
48 ats 1.13 #ifdef ALLBSD
49 tdb 1.19 #include <stdlib.h>
50 pajs 1.6 #include <sys/param.h>
51     #include <sys/sysctl.h>
52 tdb 1.22 #if defined(FREEBSD) || defined(DFBSD)
53     #include <sys/user.h>
54     #else
55 tdb 1.19 #include <sys/proc.h>
56 ats 1.13 #endif
57 pajs 1.6 #endif
58 pajs 1.1
59 pajs 1.23 int get_proc_snapshot(proc_state_t **ps){
60     proc_state_t *proc_state = NULL;
61     proc_state_t *proc_state_ptr;
62     int proc_state_size = 0;
63     #if defined(SOLARIS)
64     DIR *proc_dir;
65     struct dirent *dir_entry;
66     char filename[MAX_FILE_LENGTH];
67     FILE *f;
68 pajs 1.5 psinfo_t process_info;
69 pajs 1.1
70 pajs 1.23 if((proc_dir=opendir(PROC_LOCATION))==NULL){
71     return NULL;
72     }
73    
74     while((dir_entry=readdir(proc_dir))!=NULL){
75     if(atoi(dir_entry->d_name) == 0) continue;
76    
77     snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
78    
79     if((f=fopen(filename, "r"))==NULL){
80     /* Open failed.. Process since vanished, or the path was too long.
81     * Ah well, move onwards to the next one */
82     continue;
83     }
84     fread(&process_info, sizeof(psinfo_t), 1, f);
85 pajs 1.1
86 pajs 1.23 proc_state = realloc(proc_state, (1+proc_state_size)*sizeof(proc_state_t));
87 pajs 1.5
88 pajs 1.23 proc_state_ptr = proc_state+proc_state_size;
89    
90     proc_state_ptr->pid = process_info.pr_pid;
91     proc_state_ptr->parent = process_info.pr_ppid;
92     proc_state_ptr->pgid = process_info.pr_pgid;
93     proc_state_ptr->uid = process_info.pr_uid;
94     proc_state_ptr->euid = process_info.pr_euid;
95     proc_state_ptr->gid = process_info.pr_gid;
96     proc_state_ptr->egid = process_info.pr_egid;
97     proc_state_ptr->proc_size = (process_info.pr_size) * 1024;
98     proc_state_ptr->proc_resident = (process_info.pr_rssize) * 1024;
99     proc_state_ptr->time_spent = process_info.pr_time.tv_sec;
100     proc_state_ptr->cpu_percent = (process_info.pr_pctcpu * 100.0) / 0x8000;
101     proc_state_ptr->process_name = strdup(process_info.pr_fname);
102     proc_state_ptr->proctitle = strdup(process_info.pr_psargs);
103    
104     proc_state_size++;
105 pajs 1.5 #endif
106 pajs 1.1
107    
108 pajs 1.23 fclose(f);
109     }
110     closedir(proc_dir);
111     *ps = proc_state;
112 ats 1.21
113 pajs 1.23 return proc_state_size;
114     }
115 ats 1.15