/
time-helper.c
171 lines (141 loc) · 3.61 KB
/
time-helper.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
#define _GNU_SOURCE // for timersub()
#include <assert.h>
#include <getopt.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> // exit()
#include <sys/resource.h> // getrusage()
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
void die_errno(const char *message) {
perror(message);
exit(1);
}
void die(const char *message) {
fprintf(stderr, "time-helper: %s\n", message);
exit(1);
}
typedef struct Spec_t {
char* out_path;
bool append;
char delimiter; // delimiter should be tab or ,
bool verbose; // whether to show verbose logging
bool x; // %x status
bool e; // %e elapsed
bool U; // %U user time
bool S; // %S system time
bool M; // %M maxrss
int argc;
char **argv;
} Spec;
// Write CSV/TSV cells of different types
void int_cell(FILE* f, char delimiter, int val) {
if (delimiter != 0) { // NUL is invalid delimiter
fprintf(f, "%c%d", delimiter, val);
} else {
fprintf(f, "%d", val);
}
}
void time_cell(FILE* f, char delimiter, struct timeval *val) {
fprintf(f, "%c%ld.%06ld", delimiter, val->tv_sec, val->tv_usec);
}
int time_helper(Spec *spec, FILE* f) {
char *prog = spec->argv[0];
struct timeval start;
struct timeval end;
int status = 0;
switch (fork()) {
case -1:
die_errno("fork");
break;
case 0: // child exec
if (execvp(prog, spec->argv) < 0) {
fprintf(stderr, "time-helper: error executing '%s'\n", prog);
die_errno("execvp");
}
assert(0); // execvp() never returns
default: // parent measures elapsed time of child
if (gettimeofday(&start, NULL) < 0) {
die_errno("gettimeofday");
}
wait(&status);
if (gettimeofday(&end, NULL) < 0) {
die_errno("gettimeofday");
}
break;
}
//fprintf(stderr, "done waiting\n");
struct timeval elapsed;
timersub(&end, &start, &elapsed);
struct rusage usage;
getrusage(RUSAGE_CHILDREN, &usage);
struct timeval *user = &usage.ru_utime;
struct timeval *sys = &usage.ru_stime;
int exit_status = -1;
if (WIFEXITED(status)) {
exit_status = WEXITSTATUS(status);
}
char d = spec->delimiter;
// NO delimiter at first!
if (spec->x) { int_cell(f, 0, exit_status); }
if (spec->e) { time_cell(f, d, &elapsed); }
if (spec->U) { time_cell(f, d, &usage.ru_utime); }
if (spec->S) { time_cell(f, d, &usage.ru_stime); }
if (spec->M) { int_cell(f, d, usage.ru_maxrss); }
return exit_status;
}
int main(int argc, char **argv) {
Spec spec = {0};
spec.out_path = "/dev/null"; // default value
// http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
// + means to be strict about flag parsing.
char c;
while ((c = getopt(argc, argv, "+o:ad:vxeUSM")) != -1) {
switch (c) {
case 'o':
spec.out_path = optarg;
break;
case 'a':
spec.append = true;
break;
case 'd':
spec.delimiter = optarg[0];
break;
case 'v':
spec.verbose = true;
break;
case 'x':
spec.x = true;
break;
case 'e':
spec.e = true;
break;
case 'U':
spec.U = true;
break;
case 'S':
spec.S = true;
break;
case 'M':
spec.M = true;
break;
case '?': // getopt library will print error
return 2;
default:
abort(); // should never happen
}
}
int a = optind; // index into argv
if (a == argc) {
die("expected a command to run");
}
spec.argv = argv + a;
spec.argc = argc - a;
char* mode = spec.append ? "a" : "w";
FILE* f = fopen(spec.out_path, mode);
int exit_status = time_helper(&spec, f);
fclose(f);
return exit_status;
}