-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstat.c
152 lines (123 loc) · 3.62 KB
/
stat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* stat.c
*
* 2006 Copyright (c) Evgeniy Polyakov <[email protected]>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "stat.h"
#ifdef DEBUG
#define ulog(f, a...) fprintf(stderr, f, ##a)
#else
#define ulog(f, a...)
#endif
#define ulog_err(f, a...) ulog(f ": %s [%d].\n", ##a, strerror(errno), errno)
unsigned long long stat_written = 0, stat_written_msg = 0;
static struct timeval tm1, tm2;
static unsigned long init_utime, init_stime;
int last_fd;
unsigned long syscall_recv, syscall_send;
static int cpu_test_usage(unsigned long *sys_utime, unsigned long *sys_stime)
{
FILE *f;
char path[64];
char *ptr, data[256], status;
char fmt[] = " %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu ";
int num, good_num = 13, err;
int ppid, pgrp, session, tty, tpgid;
unsigned long flags, minflt, cminflt, majflt, cmajflt, utime, stime;
snprintf(path, sizeof(path), "/proc/%d/stat", getpid());
f = fopen(path, "r");
if (!f) {
ulog_err("Failed to open %s", path);
return 1;
}
err = -EINVAL;
ptr = fgets(data, sizeof(data), f);
if (!ptr) {
ulog_err("Failed to read data");
goto err_out_close;
}
fclose(f);
data[sizeof(data) - 1] = '\0';
ptr = strrchr(data, ')');
if (!ptr) {
ulog("String '%s' is broken.\n", data);
goto err_out_exit;
}
ptr++;
if (!ptr || !*ptr) {
ulog("String '%s' is broken.\n", data);
goto err_out_exit;
}
num = sscanf(ptr, fmt, &status, &ppid, &pgrp,
&session, &tty, &tpgid, &flags,
&minflt, &cminflt, &majflt, &cmajflt,
&utime, &stime);
if (num != good_num) {
ulog("String '%s' is broken, num=%d, good_num=%d.\n", ptr, num, good_num);
goto err_out_exit;
}
*sys_utime = utime;
*sys_stime = stime;
return 0;
err_out_close:
fclose(f);
err_out_exit:
return -1;
}
void init_stat(void)
{
gettimeofday(&tm1, NULL);
print_stat();
}
void print_stat(void)
{
unsigned long stime, utime;
long diff, mdiff;
double speed, speed_msg;
stime = utime = 0;
gettimeofday(&tm2, NULL);
diff = (tm2.tv_sec - tm1.tv_sec)*1000000 + tm2.tv_usec - tm1.tv_usec;
mdiff = diff/1000;
if (!diff || !mdiff)
return;
speed = ((double)stat_written)*1000000.0/((double)diff*1024.0*1024.0);
speed_msg = ((double)stat_written_msg)*1000000.0/((double)diff);
cpu_test_usage(&utime, &stime);
utime -= init_utime;
stime -= init_stime;
/*
* USER_HZ is 100 tics/jiffies per second.
*
* ?time * 1000 / 100 is ?time in msecs.
*/
utime = utime*1000/mdiff;
stime = stime*1000/mdiff;
printf("Written %llu Mb, %llu messages, time %f sec, speed %f Mb/sec, %f msg/sec, CPU test_usage user: %3lu, kernel: %3lu, last_fd: %d, sc: recv: %lu, send: %lu.\n",
stat_written/1024/1024, stat_written_msg, ((double)diff)/1000000.0, speed, speed_msg, utime, stime, last_fd,
syscall_recv, syscall_send);
init_utime = utime;
init_stime = stime;
}