ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/process_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/process_stats.c (file contents):
Revision 1.13 by ats, Sun Oct 19 02:03:02 2003 UTC vs.
Revision 1.24 by pajs, Sun Mar 28 18:22:58 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 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.
6 > * 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   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library 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.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser 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.
16 > * 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 > *
21 > * $Id$
22   */
23  
24   #ifdef HAVE_CONFIG_H
# Line 31 | Line 34
34   #include <string.h>
35   #endif
36  
34 #include "tools.h"
37   #ifdef SOLARIS
38   #include <procfs.h>
39   #include <limits.h>
# Line 39 | Line 41
41   #define MAX_FILE_LENGTH PATH_MAX
42   #endif
43   #ifdef LINUX
44 < #include <linux/limits.h>
44 > #include <limits.h>
45   #define PROC_LOCATION "/proc"
46   #define MAX_FILE_LENGTH PATH_MAX
47   #endif
48   #ifdef ALLBSD
49 < #include <kvm.h>
49 > #include <stdlib.h>
50   #include <sys/param.h>
51   #include <sys/sysctl.h>
52 < #endif
51 < #ifdef FREEBSD
52 > #if defined(FREEBSD) || defined(DFBSD)
53   #include <sys/user.h>
54 + #else
55 + #include <sys/proc.h>
56   #endif
57 + #endif
58  
59 < process_stat_t *get_process_stats(){
60 <
61 <        static process_stat_t process_stat;
62 <
59 > 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) || defined(LINUX)
64 <        DIR *proc_dir;
65 <        struct dirent *dir_entry;
66 <        char filename[MAX_FILE_LENGTH];
67 <        FILE *f;
64 < #endif
65 < #ifdef LINUX
66 <        char *line_ptr;
67 < #endif
64 >        DIR *proc_dir;
65 >        struct dirent *dir_entry;
66 >        char filename[MAX_FILE_LENGTH];
67 >        FILE *f;
68   #ifdef SOLARIS
69          psinfo_t process_info;
70   #endif
71 #ifdef ALLBSD
72        struct kinfo_proc *kp_stats;    
73        kvm_t *kvmd;
74        int procs;
75 #endif
71  
72 <        process_stat.sleeping=0;
73 <        process_stat.running=0;
74 <        process_stat.zombie=0;
80 <        process_stat.stopped=0;
81 <        process_stat.total=0;
72 >        if((proc_dir=opendir(PROC_LOCATION))==NULL){
73 >                return NULL;
74 >        }
75  
76 < #if defined(SOLARIS) || defined(LINUX)
77 <        if((proc_dir=opendir(PROC_LOCATION))==NULL){
85 <                return NULL;
86 <        }
76 >        while((dir_entry=readdir(proc_dir))!=NULL){
77 >                if(atoi(dir_entry->d_name) == 0) continue;
78  
88        while((dir_entry=readdir(proc_dir))!=NULL){
89                if(atoi(dir_entry->d_name) == 0) continue;
90
79   #ifdef SOLARIS
80 <                snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
80 >                snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name);
81   #endif
82 < #ifdef LINUX
83 <                snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name);
84 < #endif
85 <
86 <                if((f=fopen(filename, "r"))==NULL){
99 <                        /* Open failed.. Process since vanished, or the path was too long.
100 <                         * Ah well, move onwards to the next one */
101 <                        continue;
102 <                }
103 <
82 >                if((f=fopen(filename, "r"))==NULL){
83 >                        /* Open failed.. Process since vanished, or the path was too long.
84 >                         * Ah well, move onwards to the next one */
85 >                        continue;
86 >                }
87   #ifdef SOLARIS
88 <                fread(&process_info, sizeof(psinfo_t), 1, f);
106 <                if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++;
107 <                if(process_info.pr_lwp.pr_state==2) process_stat.running++;
108 <                if(process_info.pr_lwp.pr_state==3) process_stat.zombie++;
109 <                if(process_info.pr_lwp.pr_state==4) process_stat.stopped++;
110 <                if(process_info.pr_lwp.pr_state==6) process_stat.running++;
111 < #endif
112 < #ifdef LINUX
113 <                if((line_ptr=f_read_line(f, "State:"))==NULL){
114 <                        fclose(f);
115 <                        continue;
116 <                }
117 <                if((line_ptr=strchr(line_ptr, '\t'))==NULL){
118 <                        fclose(f);
119 <                        continue;
120 <                }
121 <                line_ptr++;
122 <                if(line_ptr=='\0'){
123 <                        fclose(f);
124 <                        continue;
125 <                }
88 >                fread(&process_info, sizeof(psinfo_t), 1, f);
89  
90 <                if(*line_ptr=='S') process_stat.sleeping++;
91 <                if(*line_ptr=='R') process_stat.running++;
92 <                if(*line_ptr=='Z') process_stat.zombie++;
93 <                if(*line_ptr=='T') process_stat.stopped++;
94 <                if(*line_ptr=='D') process_stat.stopped++;
95 < #endif  
90 >                proc_state = realloc(proc_state, (1+proc_state_size)*sizeof(proc_state_t));
91 >                proc_state_ptr = proc_state+proc_state_size;
92 >                
93 >                proc_state_ptr->pid = process_info.pr_pid;
94 >                proc_state_ptr->parent = process_info.pr_ppid;
95 >                proc_state_ptr->pgid = process_info.pr_pgid;
96 >                proc_state_ptr->uid = process_info.pr_uid;
97 >                proc_state_ptr->euid = process_info.pr_euid;
98 >                proc_state_ptr->gid = process_info.pr_gid;
99 >                proc_state_ptr->egid = process_info.pr_egid;
100 >                proc_state_ptr->proc_size = (process_info.pr_size) * 1024;
101 >                proc_state_ptr->proc_resident = (process_info.pr_rssize) * 1024;
102 >                proc_state_ptr->time_spent = process_info.pr_time.tv_sec;
103 >                proc_state_ptr->cpu_percent = (process_info.pr_pctcpu * 100.0) / 0x8000;
104 >                proc_state_ptr->process_name = strdup(process_info.pr_fname);
105 >                proc_state_ptr->proctitle = strdup(process_info.pr_psargs);
106  
107 <                fclose(f);
108 <        }
109 <        closedir(proc_dir);
107 >                if(process_info.pr_lwp.pr_state==1) proc_state_ptr->state = SLEEPING;
108 >                if(process_info.pr_lwp.pr_state==2) proc_state_ptr->state = RUNNING;
109 >                if(process_info.pr_lwp.pr_state==3) proc_state_ptr->state = ZOMBIE;
110 >                if(process_info.pr_lwp.pr_state==4) proc_state_ptr->state = STOPPED;
111 >                if(process_info.pr_lwp.pr_state==6) proc_state_ptr->state = RUNNING;
112   #endif
138 #ifdef ALLBSD
139        if((kvmd = get_kvm()) == NULL){
140                return NULL;
141        }
113  
114 <        kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs);
144 <
145 <        while(procs--){
146 < #ifdef FREEBSD5
147 <                if (kp_stats[procs].ki_stat == SSLEEP) process_stat.sleeping++;
148 <                if (kp_stats[procs].ki_stat == SWAIT) process_stat.sleeping++;
149 <                if (kp_stats[procs].ki_stat == SLOCK) process_stat.sleeping++;
150 <                if (kp_stats[procs].ki_stat == SRUN) process_stat.running++;
151 <                if (kp_stats[procs].ki_stat == SIDL) process_stat.running++;
152 <                if (kp_stats[procs].ki_stat == SZOMB) process_stat.zombie++;
153 <                if (kp_stats[procs].ki_stat == SSTOP) process_stat.stopped++;
154 <                
155 < #else
156 <                if (kp_stats[procs].kp_proc.p_stat == SSLEEP) process_stat.sleeping++;  
157 <                if (kp_stats[procs].kp_proc.p_stat == SRUN) process_stat.running++;
158 <                if (kp_stats[procs].kp_proc.p_stat == SIDL) process_stat.running++;
159 <                if (kp_stats[procs].kp_proc.p_stat == SZOMB) process_stat.zombie++;
160 <                if (kp_stats[procs].kp_proc.p_stat == SSTOP) process_stat.stopped++;
114 >                proc_state_size++;
115   #endif
162        }
163 #endif
116  
165        process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped;
117  
118 <        return &process_stat;
118 >                fclose(f);
119 >        }
120 >        closedir(proc_dir);
121 >        *ps = proc_state;
122 >
123 >        return proc_state_size;
124   }
125 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines