-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipestats.c
461 lines (367 loc) · 13.2 KB
/
pipestats.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include <getopt.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <stdint.h>
#include <ctype.h>
#include "units.h"
#include "time_estimate.h"
#define BUF_SIZE (4092)
typedef struct Stats {
unsigned long int total_bytes;
unsigned int bytes_since;
struct timeval last_report;
struct timeval start;
unsigned long long byte_count[256];
} Stats;
typedef struct Options {
double freq;
Unit unit;
int blocking;
int counts;
} Options;
Options options;
static volatile sig_atomic_t done = 0;
double elapsed_sec(struct timeval* end, struct timeval* start);
int read_options();
int setup(Stats* stats, struct timeval* report_interval);
void check_read_errors();
int check_write_errors();
void print_report(Stats* stats);
void print_final_report(Stats* stats);
void cleanup(int signal);
int main(int argc, char** argv) {
size_t buff_offset = 0;
int err = 0;
int bytes_read = 0;
struct timeval report_interval;
Stats stats;
char buff[BUF_SIZE];
done = 0;
if ((err = read_options(argc, argv)) != 0) {
return err;
}
if ((err = setup(&stats, &report_interval)) != 0) {
return err;
}
while (bytes_read > 0 || !done) {
print_report(&stats);
// Only read more if we've already written everything we already had.
if (bytes_read == 0) {
fd_set set;
// Use select so we don't wait for longer than report interval, but
// we do wait for some input at least that long (as opposed to just
// plain non-blocking io).
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
if (select(FD_SETSIZE, &set, NULL, NULL, &report_interval) > 0) {
bytes_read = fread(buff, 1, BUF_SIZE, stdin);
if (bytes_read == 0 && ferror(stdin) != 0) {
switch (errno) {
case EINTR:
case EBUSY:
case EDEADLK:
case EAGAIN:
case ETXTBSY:
// That's fine, keep going.
break;
default:
fprintf(stderr, "Got err %d during a read: %s\n",
errno, strerror(errno));
done = 1;
err = errno;
break;
}
} else {
int i;
stats.total_bytes += bytes_read;
stats.bytes_since += bytes_read;
if (options.counts) {
for (i=0; i < bytes_read; ++i) {
++stats.byte_count[(int) ((unsigned char) buff[i])];
}
}
}
}
}
if (bytes_read > 0) {
fd_set set;
FD_ZERO(&set);
FD_SET(STDOUT_FILENO, &set);
if (select(FD_SETSIZE, NULL, &set, NULL, &report_interval) > 0) {
int bytes_written = fwrite(buff + buff_offset, 1, bytes_read, stdout);
if (bytes_written == 0 && ferror(stdout) != 0) {
switch (errno) {
case EINTR:
case EBUSY:
case EDEADLK:
case EAGAIN:
case ETXTBSY:
break;
default:
// Can't write, so there's no point in continuing.
fprintf(stderr,
"Got err %d during a write: %s\n"
"Exiting with %d bytes still in buffer.\n",
errno, strerror(errno), bytes_read);
bytes_read = 0;
done = 1;
err = errno;
break;
}
}
if (bytes_written == bytes_read) {
bytes_read = 0;
buff_offset = 0;
} else if (bytes_written > 0) {
bytes_read -= bytes_written;
buff_offset += bytes_written;
}
}
} else {
// Make sure not to clear a done flag from some other reason.
done = done || feof(stdin);
// Good time to proactively ask for a flush, cuz maybe there isn't
// much happening (since we haven't read anything). But don't
// bother trying multiple times if it fails, it's just kinda a
// nice time.
fflush(stdout);
}
}
// Just for timing niceness, flush buffers before printing report.
done = 0;
while (!done && fflush(stdout) != 0) {
switch (errno) {
case EINTR:
case EBUSY:
case EDEADLK:
case EAGAIN:
case ETXTBSY:
break;
default:
fprintf(stderr, "Failed to flush stdout, err %d: %s\n",
errno, strerror(errno));
err = errno;
done = 1;
break;
}
}
print_final_report(&stats);
return err;
}
int read_options(int argc, char** argv) {
int opt = 0;
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"human", no_argument, NULL, 'H'},
{"freq", required_argument, NULL, 'f'},
{"blocking-io", no_argument, NULL, 'b'},
{"counts", no_argument, NULL, 'c'},
{0, 0, 0, 0}
};
// Init with defaults.
options.freq = 2.0;
options.unit = Human;
options.blocking = 0;
while (opt != -1) {
int option_index = 0;
opt = getopt_long(argc, argv, "hHBKMGf:bc", long_options, &option_index);
switch (opt) {
case -1:
break;
case 'h':
printf("usage: %s [options]\n"
"\n"
" -f/--freq SECONDS Report frequency, 0 to disable.\n"
" -H/--human Human units (adjust based on amount).\n"
" -[B|K|M|G] Use Bytes, Kilobytes, Megabytes, or Gigabytes.\n"
" -b/--blocking-io Use blocking io.\n"
" -c/--counts Report count per byte value at the end.\n"
"\n"
"pipestats reads from stdin, writes that input to stdout, "
"and reports stats about data transfered to stderr.\n",
argv[0]);
return -1;
break;
case 'c':
options.counts = 1;
break;
case 'H':
options.unit = Human;
break;
case 'B':
options.unit = Bytes;
break;
case 'K':
options.unit = Kilobytes;
break;
case 'M':
options.unit = Megabytes;
break;
case 'G':
options.unit = Gigabytes;
break;
case 'f':
options.freq = strtod(optarg, NULL);
if (options.freq < 0) {
fprintf(stderr, "ERROR: frequency must be >= 0\n");
return -1;
}
break;
case 'b':
options.blocking = 1;
break;
case '?':
return -1;
break;
default:
return -1;
break;
}
}
return 0;
}
int setup(Stats* stats, struct timeval* report_interval) {
struct sigaction cleanup_action;
double half_freq;
int abort_signals[] = {SIGHUP, SIGINT, SIGQUIT, SIGABRT, SIGPIPE, SIGTERM};
int i;
int err;
// Put stdin/stdout into non-blocking mode, so even if there's less than
// buffer size of data, we clean that out and report stats on it.
if (!options.blocking && fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1) {
fprintf(stderr,
"Warning: failed to put stdin in nonblocking mode. "
"Reporting might not be consistently on time.\n");
}
if (!options.blocking && fcntl(STDOUT_FILENO, F_SETFL, O_NONBLOCK) == -1) {
fprintf(stderr,
"Warning: failed to put stdout in nonblocking mode. "
"Reporting might not be consistently on time.\n");
}
if ((err = ferror(stdin)) != 0) {
fprintf(stderr,
"stdin is in error state %d: %s\nclearing and continuing\n",
err, strerror(err));
clearerr(stdin);
}
if ((err = ferror(stdout)) != 0) {
fprintf(stderr,
"stdout is in error state %d: %s\nclearing and continuing\n",
err, strerror(err));
clearerr(stdout);
}
// Timing for report.
half_freq = options.freq / 2.0;
report_interval->tv_sec = (int) half_freq;
report_interval->tv_usec =
(half_freq - ((int) half_freq)) * (1000 * 1000);
// Init stats.
memset(stats, 0, sizeof(Stats));
gettimeofday(&stats->start, NULL);
stats->last_report = stats->start;
// Set up handler for exiting, to print a final report, even if aborted.
memset(&cleanup_action, 0, sizeof(struct sigaction));
cleanup_action.sa_handler = &cleanup;
// Restart IO on interrupt, and reset sig handler to default after one delivery.
cleanup_action.sa_flags = SA_RESTART | SA_RESETHAND;
for (i=sizeof(abort_signals) / sizeof(abort_signals[0]) - 1; i >= 0; --i) {
if (sigaction(abort_signals[i], &cleanup_action, NULL) != 0) {
fprintf(stderr, "Failed to set cleanup handler for sig %d.\n",
i);
return -1;
}
}
return 0;
}
double elapsed_sec(struct timeval* end, struct timeval* start) {
double sec = end->tv_sec - start->tv_sec;
double usec = end->tv_usec - start->tv_usec;
return sec + usec / (1000 * 1000);
}
void print_report(Stats* stats) {
struct timeval now;
double elapsed;
if (options.freq <= 0) {
return;
}
gettimeofday(&now, NULL);
elapsed = elapsed_sec(&now, &stats->last_report);
if (elapsed >= options.freq) {
TimeEstimate time;
double milestone_amount;
const char* milestone_amount_unit;
double data_amount_since = adjust_unit(stats->bytes_since, options.unit);
const char* data_amount_since_unit = unit_name(stats->bytes_since, options.unit);
double data_amount_total = adjust_unit(stats->total_bytes, options.unit);
const char* data_amount_total_unit = unit_name(stats->total_bytes, options.unit);
// If stuff's moving, use current speed, to be optimistic about the
// current conditions. Otherwise use total avg speed, which is more
// realistic if things keep pausing.
estimate_time(&time,
stats->total_bytes,
stats->bytes_since > 0 ?
stats->bytes_since / elapsed :
stats->total_bytes / elapsed_sec(&now, &stats->start));
milestone_amount = adjust_unit(time.milestone_bytes, options.unit);
milestone_amount_unit = unit_name(time.milestone_bytes, options.unit);
fprintf(stderr,
"%3.2f %s/s"
", %3.2f %s total"
", %3.2f %s since last report"
", %3.2f %s until %3.2f %s"
"\n",
data_amount_since / elapsed, data_amount_since_unit,
data_amount_total, data_amount_total_unit,
data_amount_since, data_amount_since_unit,
time.time_remaining, time.time_unit,
milestone_amount, milestone_amount_unit);
stats->bytes_since = 0;
stats->last_report = now;
}
}
void print_final_report(Stats* stats) {
struct timeval now;
double elapsed;
int i;
double data_amount = adjust_unit(stats->total_bytes, options.unit);
const char* data_amount_unit = unit_name(stats->total_bytes, options.unit);
gettimeofday(&now, NULL);
elapsed = elapsed_sec(&now, &stats->start);
if (options.counts) {
fprintf(stderr, "Count of byte values:\n");
for (i=0; i < 256; ++i) {
int count = stats->byte_count[i];
double amount = adjust_unit(count, options.unit);
const char* unit = unit_name(count, options.unit);
fprintf(stderr, " %c 0x%02X: %6.2f%s %5.2f%%%s",
isprint(i) ? (char) i : ' ',
i,
amount,
unit,
100.0 * (double) count / stats->total_bytes,
i % 4 == 3 || i == 255 ? "\n" : "");
}
}
fprintf(stderr, "%3.2f %s (%lu bytes) total over %.2f sec, avg %.2f %s/s\n",
data_amount, data_amount_unit,
stats->total_bytes,
elapsed,
data_amount / elapsed, data_amount_unit);
}
void cleanup(int signal) {
// No need for handling a broken pipe, the app will show a message and/or
// abort if it's relevant.
if (signal != SIGPIPE) {
fprintf(stderr, "\nGot signal %s, aborting early.\n",
strsignal(signal));
done = 1;
}
}