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 snprintf(filename, STRLEN, "/proc/%d/%s", pid, name);
00083
00084 fd = open(filename, O_RDONLY);
00085
00086 if ( fd < 0 ) {
00087
00088 return FALSE;
00089
00090 }
00091
00092 if ( (bytes = read(fd, buf, buf_size-1)) < 0 ) {
00093
00094 return FALSE;
00095
00096 }
00097
00098
00099 buf[bytes]='\0';
00100
00101 close(fd);
00102
00103 return TRUE;
00104
00105 }
00106
00111 double get_float_time(void) {
00112
00113 struct timeval t;
00114
00115 gettimeofday(&t, NULL);
00116 return (double) t.tv_sec * 10 + (double) t.tv_usec / 100000.0;
00117
00118 }
00119
00126 int get_process_info(ProcInfo_T p) {
00127 double temp_time;
00128
00129
00130
00131 temp_time = get_float_time();
00132
00133
00134
00135 if ( ! get_process_info_sysdep(p) ) {
00136
00137 return FALSE;
00138
00139 }
00140
00141
00142
00143 if ( p->time_prev == 0.0 ) {
00144
00145 p->cpu_percent= 0;
00146
00147 } else {
00148
00149
00150
00151 p->cpu_percent= (int) (( p->cputime - p->cputime_prev ) * 1000) /
00152 ( temp_time - p->time_prev );
00153
00154 }
00155
00156 p->time_prev = temp_time;
00157
00158
00159 return TRUE;
00160
00161 }
00162