Commit | Line | Data |
---|---|---|
1c15e888 C |
1 | /* @(#)stat.c 1.2 90/01/03 NFS Rev 2 Testsuite */ |
2 | /* 1.3 Lachman ONC Test Suite source */ | |
3 | ||
4 | #include <stdio.h> | |
5 | #include <math.h> | |
6 | ||
7 | /* | |
8 | * crunch through time stat files. This program will handle two | |
9 | * formats: BSD | |
10 | * 1.7 real 0.0 user 0.4 sys | |
11 | * and ATT | |
12 | * real 1.4 | |
13 | * user 0.0 | |
14 | * sys 0.2 | |
15 | * | |
16 | * ATT format may break out minutes -- 2:03.2 | |
17 | */ | |
18 | #define MAXINDEX 100 | |
19 | double real[MAXINDEX]; | |
20 | double user[MAXINDEX]; | |
21 | double sys[MAXINDEX]; | |
22 | char *Prog, *File; | |
23 | ||
24 | main(argc, argv) | |
25 | int argc; | |
26 | char *argv[]; | |
27 | { | |
28 | FILE *fp; | |
29 | int i, n; | |
30 | char c, *fmt; | |
31 | int attfmt = 0; /* set if using att time format */ | |
32 | ||
33 | Prog = argv[0]; | |
34 | if (argc == 1) { | |
35 | fprintf(stderr, "Usage: %s datafile\n", Prog); | |
36 | exit(1); | |
37 | } | |
38 | File = argv[1]; | |
39 | fp = fopen(File, "r"); | |
40 | if (fp == NULL) { | |
41 | fprintf(stderr, "%s: unable to open %s\n", | |
42 | Prog, File); | |
43 | exit(1); | |
44 | } | |
45 | if ((i = fgetc(fp)) == EOF) { | |
46 | fprintf(stderr, "%s: %s is empty\n", | |
47 | Prog, File); | |
48 | exit(1); | |
49 | } | |
50 | c = i & 0x7f; | |
51 | if (c == '\n' || c == '\r' || c == 'r') | |
52 | attfmt = 1; | |
53 | else | |
54 | fmt = "%F %*s %F %*s %F %*s"; /* BSD fmt */ | |
55 | if (ungetc(c, fp) == EOF) { | |
56 | fprintf(stderr, "%s: can't push char back to %s\n", | |
57 | Prog, File); | |
58 | exit(1); | |
59 | } | |
60 | ||
61 | if (attfmt) { | |
62 | for (n = 0; getattfmt(fp, n, 1); n++) { | |
63 | getattfmt(fp, n, 2); | |
64 | getattfmt(fp, n, 3); | |
65 | } | |
66 | } else { | |
67 | n = 0; | |
68 | while(fscanf(fp, fmt, &real[n], &user[n], &sys[n]) == 3) | |
69 | n++; | |
70 | } | |
71 | if (n == 0) { | |
72 | fprintf(stderr, "%s: no data in %s\n", | |
73 | Prog, File); | |
74 | #ifdef SVR3 | |
75 | exit(0); | |
76 | #else | |
77 | exit(1); | |
78 | #endif | |
79 | } | |
80 | stat(real, n); | |
81 | printf(" real"); | |
82 | stat(user, n); | |
83 | printf(" user"); | |
84 | stat(sys, n); | |
85 | printf(" sys\n"); | |
86 | ||
87 | exit(0); | |
88 | } | |
89 | ||
90 | /* | |
91 | * which: 1: real, 2:user, 3:sys | |
92 | * | |
93 | * returns 0 if no more data, else 1 | |
94 | */ | |
95 | getattfmt(fp, n, which) | |
96 | FILE *fp; | |
97 | int n, which; | |
98 | { | |
99 | char buf[BUFSIZ]; | |
100 | char *p; | |
101 | char *fmt; | |
102 | double *dp; | |
103 | int min, err = 0; | |
104 | ||
105 | if (n < 0 || n >= MAXINDEX) { | |
106 | fprintf(stderr, "%s: illegal index=%d in getattfmt\n", | |
107 | Prog, n); | |
108 | exit(1); | |
109 | } | |
110 | switch(which) { | |
111 | case 1: /* real */ | |
112 | dp = &real[n]; | |
113 | break; | |
114 | case 2: /* user */ | |
115 | dp = &user[n]; | |
116 | break; | |
117 | case 3: /* sys */ | |
118 | dp = &sys[n]; | |
119 | break; | |
120 | default: | |
121 | fprintf(stderr, "%s: illegal which=%d in getattfmt\n", | |
122 | Prog, which); | |
123 | exit(1); | |
124 | } | |
125 | ||
126 | while (fgets(buf, BUFSIZ, fp)) { | |
127 | /* null out newline */ | |
128 | for (p = buf; *p && *p != '\n'; p++) | |
129 | ; | |
130 | if (*p == '\n') | |
131 | *p = '\0'; | |
132 | ||
133 | /* look for blank line and skip it */ | |
134 | for (p = buf; *p && (*p == ' ' || *p == '\t'); p++) | |
135 | ; | |
136 | if (*p == '\0') | |
137 | continue; | |
138 | ||
139 | min = 0; | |
140 | for (p = buf; *p && *p != ':'; p++) | |
141 | ; | |
142 | if (*p == ':') { | |
143 | fmt = "%*s %d:%F"; | |
144 | if (sscanf(buf, fmt, &min, dp) != 2) | |
145 | err = 1; | |
146 | } else { | |
147 | fmt = "%*s %F"; | |
148 | if (sscanf(buf, fmt, dp) != 1) | |
149 | err = 1; | |
150 | } | |
151 | if (err) { | |
152 | fprintf(stderr, "%s: bad data format in %s (%s)\n", | |
153 | Prog, File, buf); | |
154 | exit(1); | |
155 | } | |
156 | if (min > 0) | |
157 | *dp += (double)(min * 60); | |
158 | return 1; | |
159 | } | |
160 | /* EOF */ | |
161 | if (which == 1) | |
162 | return 0; | |
163 | else { | |
164 | fprintf(stderr, "%s: premature EOF in %s\n", | |
165 | Prog, File); | |
166 | exit(1); | |
167 | } | |
168 | } | |
169 | ||
170 | stat(array, n) | |
171 | double array[]; | |
172 | int n; | |
173 | { | |
174 | double avg, sd; | |
175 | int i; | |
176 | ||
177 | avg = 0; | |
178 | for (i = 0; i < n; i++) | |
179 | avg += array[i]; | |
180 | avg = avg / (float) n; | |
181 | sd = 0; | |
182 | for (i = 0; i < n; i++) | |
183 | sd += (array[i] - avg)*(array[i] - avg); | |
184 | if (n > 1) { | |
185 | sd = sd / (float) (n - 1); | |
186 | sd = sqrt(sd); | |
187 | } else { | |
188 | sd = 0.0; | |
189 | } | |
190 | printf("\t%.1f (%.1f)", avg, sd); | |
191 | } |