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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/* @(#)stat.c 1.1 97/01/03 Connectathon Testsuite */
/* 1.3 Lachman ONC Test Suite source */
#include <stdio.h>
#include <math.h>
/*
* crunch through time stat files. This program will handle two
* formats: BSD
* 1.7 real 0.0 user 0.4 sys
* and ATT
* real 1.4
* user 0.0
* sys 0.2
*
* ATT format may break out minutes -- 2:03.2
*/
#define MAXINDEX 100
double real[MAXINDEX];
double user[MAXINDEX];
double sys[MAXINDEX];
char *Prog, *File;
int getattfmt();
void prtstat();
main(argc, argv)
int argc;
char *argv[];
{
FILE *fp;
int i, n;
char c, *fmt;
int attfmt = 0; /* set if using att time format */
Prog = argv[0];
if (argc == 1) {
fprintf(stderr, "Usage: %s datafile\n", Prog);
exit(1);
}
File = argv[1];
fp = fopen(File, "r");
if (fp == NULL) {
fprintf(stderr, "%s: unable to open %s\n",
Prog, File);
exit(1);
}
if ((i = fgetc(fp)) == EOF) {
fprintf(stderr, "%s: %s is empty\n",
Prog, File);
exit(1);
}
c = i & 0x7f;
if (c == '\n' || c == '\r' || c == 'r')
attfmt = 1;
else
fmt = "%lf %*s %lf %*s %lf %*s"; /* BSD fmt */
if (ungetc(c, fp) == EOF) {
fprintf(stderr, "%s: can't push char back to %s\n",
Prog, File);
exit(1);
}
if (attfmt) {
for (n = 0; getattfmt(fp, n, 1); n++) {
(void) getattfmt(fp, n, 2);
(void) getattfmt(fp, n, 3);
}
} else {
n = 0;
while (fscanf(fp, fmt, &real[n], &user[n], &sys[n]) == 3)
n++;
}
if (n == 0) {
fprintf(stderr, "%s: no data in %s\n",
Prog, File);
#ifdef SVR3
exit(0);
#else
exit(1);
#endif
}
prtstat(real, n);
printf(" real");
prtstat(user, n);
printf(" user");
prtstat(sys, n);
printf(" sys\n");
exit(0);
}
/*
* which: 1: real, 2:user, 3:sys
*
* returns 0 if no more data, else 1
*/
int
getattfmt(fp, n, which)
FILE *fp;
int n, which;
{
char buf[BUFSIZ];
char *p;
char *fmt;
double *dp;
int min, err = 0;
if (n < 0 || n >= MAXINDEX) {
fprintf(stderr, "%s: illegal index=%d in getattfmt\n",
Prog, n);
exit(1);
}
switch (which) {
case 1: /* real */
dp = &real[n];
break;
case 2: /* user */
dp = &user[n];
break;
case 3: /* sys */
dp = &sys[n];
break;
default:
fprintf(stderr, "%s: illegal which=%d in getattfmt\n",
Prog, which);
exit(1);
}
while (fgets(buf, BUFSIZ, fp)) {
/* null out newline */
for (p = buf; *p != '\0' && *p != '\n'; p++)
;
if (*p == '\n')
*p = '\0';
/* look for blank line and skip it */
for (p = buf; *p != '\0' && (*p == ' ' || *p == '\t'); p++)
;
if (*p == '\0')
continue;
min = 0;
for (p = buf; *p != '\0' && *p != ':'; p++)
;
if (*p == ':') {
fmt = "%*s %d:%lf";
if (sscanf(buf, fmt, &min, dp) != 2)
err = 1;
} else {
fmt = "%*s %lf";
if (sscanf(buf, fmt, dp) != 1)
err = 1;
}
if (err) {
fprintf(stderr, "%s: bad data format in %s (%s)\n",
Prog, File, buf);
exit(1);
}
if (min > 0)
*dp += (double)(min * 60);
return (1);
}
/* EOF */
if (which == 1)
return (0);
fprintf(stderr, "%s: premature EOF in %s\n", Prog, File);
exit(1);
}
void
prtstat(array, n)
double array[];
int n;
{
double avg, sd;
int i;
avg = 0;
for (i = 0; i < n; i++)
avg += array[i];
avg = avg / (float) n;
sd = 0;
for (i = 0; i < n; i++)
sd += (array[i] - avg)*(array[i] - avg);
if (n > 1) {
sd = sd / (float) (n - 1);
sd = sqrt(sd);
} else
sd = 0.0;
printf("\t%.1f (%.1f)", avg, sd);
}
|