00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00031 #include <config.h>
00032
00033 #ifdef HAVE_SYS_TYPES_H
00034 #include <sys/types.h>
00035 #endif
00036
00037 #ifdef HAVE_SYS_STAT_H
00038 #include <sys/stat.h>
00039 #endif
00040
00041 #ifdef HAVE_STRING_H
00042 #include <string.h>
00043 #endif
00044
00045 #ifdef HAVE_SYS_TIME_H
00046 #include <sys/time.h>
00047 #endif
00048
00049 #ifdef HAVE_LOADAVG_H
00050 #include <sys/loadavg.h>
00051 #endif
00052
00053 #ifdef HAVE_STRING_H
00054 #include <string.h>
00055 #endif
00056
00057 #ifdef HAVE_FCNTL_H
00058 #include <fcntl.h>
00059 #endif
00060
00061 #include <stdio.h>
00062 #include <errno.h>
00063
00064
00065 #include "process.h"
00066
00067
00076 int read_proc_file(char *buf, int buf_size, char * name, int pid) {
00077
00078 int fd;
00079 char filename[STRLEN];
00080 int bytes;
00081
00082 ASSERT(buf);
00083 ASSERT(name);
00084
00085 snprintf(filename, STRLEN, "/proc/%d/%s", pid, name);
00086
00087 fd = open(filename, O_RDONLY);
00088
00089 if ( fd < 0 ) {
00090
00091 return FALSE;
00092
00093 }
00094
00095 if ( (bytes = read(fd, buf, buf_size-1)) < 0 ) {
00096
00097 return FALSE;
00098
00099 }
00100
00101
00102 buf[bytes]='\0';
00103
00104 close(fd);
00105
00106 return TRUE;
00107
00108 }
00109
00114 double get_float_time(void) {
00115
00116 struct timeval t;
00117
00118 gettimeofday(&t, NULL);
00119 return (double) t.tv_sec * 10 + (double) t.tv_usec / 100000.0;
00120
00121 }
00122
00129 int get_process_info(ProcInfo_T p) {
00130 double temp_time;
00131
00132
00133
00134 temp_time = get_float_time();
00135
00136
00137
00138 if ( ! get_process_info_sysdep(p) ) {
00139
00140 return FALSE;
00141
00142 }
00143
00144
00145
00146 if ( p->time_prev == 0.0 ) {
00147
00148 p->cpu_percent= 0;
00149
00150 } else {
00151
00152
00153
00154 p->cpu_percent= (int) (( p->cputime - p->cputime_prev ) * 1000) /
00155 ( temp_time - p->time_prev ) / num_cpus;
00156
00157 }
00158
00159 p->time_prev = temp_time;
00160
00161
00162 return TRUE;
00163
00164 }
00165