33 #include <sys/times.h>
64 static int _get_prusage(pid_t pid)
66 char prusage_file[64];
72 memset((
void *)&pru, 0,
sizeof(prusage_t));
74 sprintf(prusage_file,
"/proc/%ld/usage", (
long)pid);
76 fd = open(prusage_file, O_RDONLY);
80 snprintf(gProcStats.
errstr, MAX_STATS_ERROR,
81 "Could not open process usage file: %s\n"
82 " -> %s\n", prusage_file, strerror(errno));
87 nread = read(fd, &pru,
sizeof(prusage_t));
91 snprintf(gProcStats.
errstr, MAX_STATS_ERROR,
92 "Could not read process usage file: %s\n"
93 " -> %s\n", prusage_file, strerror(errno));
98 else if (nread !=
sizeof(prusage_t)) {
100 snprintf(gProcStats.
errstr, MAX_STATS_ERROR,
101 "Could not read process usage file: %s\n"
102 " -> bytes read (%d) does not match expected size (%d)\n",
103 prusage_file, nread,
sizeof(prusage_t));
112 gProcStats.
total_rw_io = (
unsigned long)pru.pr_ioch;
115 run_time = (double)pru.pr_rtime.tv_sec
116 + ((
double)pru.pr_rtime.tv_nsec/1e9);
118 if (run_time > gProcStats.
run_time) {
134 static int _get_psinfo(pid_t pid)
136 char psinfo_file[64];
141 memset((
void *)&psi, 0,
sizeof(psinfo_t));
143 sprintf(psinfo_file,
"/proc/%ld/psinfo", (
long)pid);
145 fd = open(psinfo_file, O_RDONLY);
149 snprintf(gProcStats.
errstr, MAX_STATS_ERROR,
150 "Could not open process info file: %s\n"
151 " -> %s\n", psinfo_file, strerror(errno));
156 nread = read(fd, &psi,
sizeof(psinfo_t));
160 snprintf(gProcStats.
errstr, MAX_STATS_ERROR,
161 "Could not read process info file: %s\n"
162 " -> %s\n", psinfo_file, strerror(errno));
167 else if (nread !=
sizeof(psinfo_t)) {
169 snprintf(gProcStats.
errstr, MAX_STATS_ERROR,
170 "Could not read process info file: %s\n"
171 " -> bytes read (%d) does not match expected size (%d)\n",
172 psinfo_file, nread,
sizeof(psinfo_t));
180 strncpy(gProcStats.
exe_name, psi.pr_fname, 127);
183 gProcStats.
image_size = (
unsigned int)psi.pr_size;
186 if (psi.pr_rssize > gProcStats.
rss_size) {
187 gProcStats.
rss_size = (
unsigned int)psi.pr_rssize;
205 static int _get_process_status(pid_t pid)
207 char status_file[64];
210 unsigned int image_size;
211 unsigned int rss_size;
213 sprintf(status_file,
"/proc/%d/status", pid);
215 fp = fopen(status_file,
"r");
219 snprintf(gProcStats.
errstr, MAX_STATS_ERROR,
220 "Could not open process status file: %s\n"
221 " -> %s\n", status_file, strerror(errno));
226 while (fgets(line, 512, fp) != NULL) {
228 if (strstr(line,
"Name")) {
229 sscanf(line,
"%*s %s", gProcStats.
exe_name);
231 else if (strstr(line,
"VmSize")) {
232 sscanf(line,
"%*s %u", &image_size);
237 else if (strstr(line,
"VmRSS")) {
238 sscanf(line,
"%*s %u", &rss_size);
239 if (gProcStats.
rss_size < rss_size) {
247 snprintf(gProcStats.
errstr, MAX_STATS_ERROR,
248 "Could not read process status file: %s\n"
249 " -> %s\n", status_file, strerror(errno));
278 pid_t pid = getpid();
279 double ticks_per_sec = (double)sysconf(_SC_CLK_TCK);
285 gProcStats.
errstr[0] =
'\0';
289 if (times(&tms_buf) != (clock_t)-1) {
291 clock_ticks = tms_buf.tms_utime
294 + tms_buf.tms_cstime;
296 gProcStats.
cpu_time = clock_ticks / ticks_per_sec;
306 _get_process_status(pid);
331 if (proc_stats->
errstr[0] !=
'\0') {
332 fprintf(fp,
"%s\n", proc_stats->
errstr);
336 "Executable File Name: %s\n"
337 "Process Image Size: %u Kbytes\n"
338 "Resident Set Size: %u Kbytes\n"
339 "Total CPU Time: %g seconds\n",
347 "Total Read/Write IO: %lu bytes\n",
353 "Run Time: %.2f seconds\n",