forked from chazy/kvmperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avg.c
92 lines (81 loc) · 1.7 KB
/
avg.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
static int x86mode = 0;
static double get_power(char *line)
{
char *str, *str2 = NULL, *saveptr = NULL;
while (str = strtok_r(line, " \t", &saveptr)) {
line = NULL;
str2 = str;
}
if (str2)
return atof(str2);
else
return 0.0;
}
void usage(const char *arg0)
{
fprintf(stderr, "Usage: %s [-x] [input file]\n\n"
"Options:\n"
" -x: x86 data format\n"
" [input file]: input file name, stdin if ommitted",
arg0);
exit(EXIT_FAILURE);
}
int main(int argc, char * const argv[])
{
ssize_t ret;
size_t bufsize = 0;
FILE *stream;
char *lineptr = NULL;
char *fname;
double avg = 0;
long long datapoints = 1;
int opt;
while ((opt = getopt(argc, argv, "x")) != -1) {
switch (opt) {
case 'x':
x86mode = 1;
break;
default:
usage(argv[0]);
}
}
if (optind >= argc) {
stream = stdin;
} else {
/* first positional argument is file name */
fname = argv[optind];
stream = fopen(fname, "r");
if (!stream) {
fprintf(stderr, "Error opening file %s: %s",
fname, strerror(errno));
exit(1);
}
printf("%s\t", fname);
}
while (getline(&lineptr, &bufsize, stream) > 0) {
double line_power;
if (!x86mode && lineptr[0] == '#')
continue;
if (!x86mode && !strncmp(lineptr, "time", bufsize))
continue;
if (x86mode && (lineptr[0] < '0' || lineptr[0] > '9'))
continue;
line_power = get_power(lineptr);
if (line_power == 0.0)
continue;
avg = (line_power + (double)datapoints * avg) /
((double)datapoints + 1);
datapoints++;
}
if (stream == stdin)
printf("%f", avg);
else
printf("%f\n", avg);
return 0;
}