-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctimer.cc
359 lines (320 loc) · 11.1 KB
/
ctimer.cc
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
/**
* Copyright (c) 2020 Leedehai. All rights reserved.
* Use of this source code is governed under the MIT LICENSE.txt file.
* -----
* The implementation of ctimer (see README.md for more info).
* Most could be in C, but I like C++'s grammar sugars like r-strings.
*/
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <numeric>
#include <string>
#include <signal.h>
#include <sys/errno.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
static bool g_verbose = false;
// NOTE stderr is unbuffered, while stdout is buffered.
#define VERBOSE(format, ...) \
if (g_verbose) { \
fprintf(stderr, "[verbose] "); \
fprintf(stderr, format, __VA_ARGS__); \
fprintf(stderr, "\n"); \
}
#define ERROR_STR(str) fprintf(stderr, "[Error] %s\n", str);
#define ERROR_FMT(format, ...) \
fprintf(stderr, "[Error] "); \
fprintf(stderr, format, __VA_ARGS__); \
fprintf(stderr, "\n");
#define CHECKED_SYSCALL(expr, description, error_action) \
if ((expr) == -1) { \
ERROR_FMT("syscall %s: %s", description, strerror(errno)); \
error_action; \
}
#define PARENT_ERR return 1;
#define CHILD_ERR raise(SIGQUIT);
enum ChildExit { kReturn, kSignal, kQuit, kTimeout, kUnknown };
static const char kStatsFilenameEnvVar[] = "CTIMER_STATS";
static const char kTimeoutEnvVar[] = "CTIMER_TIMEOUT";
static const char kDelimiterEnvVar[] = "CTIMER_DELIMITER";
// NOTE Do not make kEffectiveInfiniteTime a wider integer type, as Linux's
// itimerval struct only guarantees 32-bit or narrower integers.
static const unsigned int kEffectiveInfiniteTime =
0x7FFFFFFF; // In msec, over 24 days.
static const unsigned int kDefaultTimeoutMillisec = 1500;
static const char kHelpMessage[] = R"(usage: ctimer [-h] [-v] program [args ...]
ctimer: measure a program's processor time
positional arguments:
program path to the inspected program
args commandline arguments
optional arguments:
-h, --help print this help message and exit
-v, --verbose (dev) print verbosely
optional environment vairables:
%-16s file to write stats in JSON, default: (stdout)
%-16s processor time limit (ms), default: %d
%-16s delimiter encompassing the stats string
)";
static const char kReportJSONFormat[] = R"(%s{
"pid" : %d,
"maxrss_kb" : %ld,
"exit" : {
"type" : "%s",
"repr" : %s,
"desc" : "%s"
},
"times_ms" : {
"total" : %.3f,
"user" : %.3f,
"sys" : %.3f
}
}%s)";
/// Helper: print help.
static void PrintHelp() {
fprintf(stdout, kHelpMessage, kStatsFilenameEnvVar, kTimeoutEnvVar,
kDefaultTimeoutMillisec, kDelimiterEnvVar);
}
/// Helper: interpret exit type.
static const char* GetExitTypeString(ChildExit exit_type) {
switch (exit_type) {
case kReturn:
return "return";
case kSignal:
return "signal";
case kQuit:
return "quit";
case kTimeout:
return "timeout";
case kUnknown:
return "unknown";
default:
return "?";
}
}
/// Helper: return description of `exit_numeric_repr`.
static const char* GetExitReprString(ChildExit exit_type,
int exit_numeric_repr) {
switch (exit_type) {
case kReturn:
return "exit code";
case kSignal:
return strsignal(exit_numeric_repr);
case kQuit:
return "child error before exec";
case kTimeout:
return "child runtime limit (ms)";
case kUnknown:
return "unknown";
default:
return "?";
}
}
/// Helper: check whether a null-terminated string is all digits.
static bool IsShortDigitStr(const char* s, int maxCount) {
if (maxCount <= 0) {
return false;
}
short int count = 0;
while (*s) {
if (*s >= '0' && *s <= '9' && count < maxCount) {
++s;
++count;
} else {
return false;
}
}
return true;
}
/// Helper: compare flag string.
static bool MatchFlag(const char* in,
const char* short_flag,
const char* long_flag) {
if (!in) {
return false;
}
if (strcmp(short_flag, in) == 0 || strcmp(long_flag, in) == 0) {
return true;
}
return false;
}
/**** Main works ****/
struct WorkParams {
/// Argument count in command, including the program name.
int argc;
/// Limit of runtime on processor.
unsigned timeout_msec;
/// The inspected command: the program name, followed by
/// whatever args it has, then NULL(i.e.command[argc] == NULL).
char** command;
/// The file to write stats; write to stdout if NULL.
char* stats_filename;
/// Delimiter that encompasses the stats string.
char* delimiter;
};
/// Print stats; return 0 on success, 1 otherwise.
int ReportStats(ChildExit exit_type,
const WorkParams& params,
pid_t pid,
int exit_numeric_repr,
const rusage& rusage_obj) {
auto tv2msec = [](const timeval& tv) {
return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
};
double child_user_msec = tv2msec(rusage_obj.ru_utime);
double child_sys_msec = tv2msec(rusage_obj.ru_stime);
char exit_str_repr[16] = {0};
if (exit_numeric_repr >= 0) {
snprintf(exit_str_repr, sizeof(exit_str_repr), "%d", exit_numeric_repr);
} else {
snprintf(exit_str_repr, sizeof(exit_str_repr), "null");
}
// NOTE Only ru_utime, ru_stime are guaranteed by POSIX; other fields are
// implementation-dependent: https://pubs.opengroup.org/onlinepubs/9699919799/
// That said, ru_maxrss is present on Linux, macOS. See "man getrusage".
long maxrss = rusage_obj.ru_maxrss; // KB on Linux and iOS, B on macOS.
if (std::getenv("RUSAGE_SIZE_BYTES")) {
maxrss /= 1024;
}
char buffer[512] = {0};
int snprintf_ret =
snprintf(buffer, sizeof(buffer), kReportJSONFormat,
params.delimiter ? params.delimiter : "", pid,
maxrss,
GetExitTypeString(exit_type), exit_str_repr,
GetExitReprString(exit_type, exit_numeric_repr),
child_user_msec + child_sys_msec, child_user_msec,
child_sys_msec, params.delimiter ? params.delimiter : "");
if (snprintf_ret == -1) {
return 1;
}
int fprintf_ret;
if (!params.stats_filename) {
fprintf_ret = fprintf(stdout, "%s\n", buffer);
} else {
FILE* stats_file = fopen(params.stats_filename, "w");
if (!stats_file) {
ERROR_FMT("error at openning file %s", params.stats_filename);
return 1;
}
fprintf_ret = fprintf(stats_file, "%s\n", buffer);
}
return fprintf_ret != -1 ? 0 : 1;
}
/// Main works; return 0 on success, 1 otherwise.
int Work(const WorkParams& params) {
int t_sec = params.timeout_msec / 1000,
t_usec = 1000 * (params.timeout_msec % 1000);
itimerval interval = {/* to */ {0, 0}, /* from */ {t_sec, t_usec}};
pid_t child_pid = -1;
CHECKED_SYSCALL(child_pid = fork(), "fork", PARENT_ERR);
if (child_pid == 0) {
// Child process.
// ITIMER_PROF: decrements when the process executes OR
// when the system is executing on behalf of the process.
// POSIX call execvp() takes the environment variables from the
// parent process.
CHECKED_SYSCALL(setitimer(ITIMER_PROF, &interval, 0), "setitimer in child",
CHILD_ERR);
CHECKED_SYSCALL(execvp(params.command[0], params.command), "exec in child",
CHILD_ERR);
} else {
// Parent process.
VERBOSE("child forked; pid %d", child_pid);
int child_status = 0;
CHECKED_SYSCALL(waitpid(child_pid, &child_status, 0), "waitpid",
PARENT_ERR);
rusage rusage_obj;
memset(&rusage_obj, 0, sizeof(rusage_obj));
CHECKED_SYSCALL(getrusage(RUSAGE_CHILDREN, &rusage_obj), "getrusage",
PARENT_ERR);
if (WIFEXITED(child_status)) {
int exit_status = WEXITSTATUS(child_status);
VERBOSE("child %d exited with %d", child_pid, exit_status);
return ReportStats(kReturn, params, child_pid, exit_status, rusage_obj);
} else if (WIFSIGNALED(child_status)) {
int sig = WTERMSIG(child_status);
if (sig == SIGPROF) {
VERBOSE("child %d timeout, %d msec", child_pid, params.timeout_msec);
return ReportStats(kTimeout, params, child_pid, params.timeout_msec,
rusage_obj);
} else if (sig == SIGQUIT) {
VERBOSE("child %d quit", child_pid);
return ReportStats(kQuit, params, child_pid, -1, rusage_obj);
} else {
VERBOSE("child %d terminated by signal %d (%s)", child_pid, sig,
strsignal(sig));
return ReportStats(kSignal, params, child_pid, sig, rusage_obj);
}
} else {
VERBOSE("child exited abnormally without signal, pid = %d", child_pid);
return ReportStats(kUnknown, params, child_pid, -1, rusage_obj);
}
}
return 0;
}
int main(int argc, char* argv[]) {
if (argc == 1) {
ERROR_STR("arguments expected, use '-h' for help");
return 1;
}
WorkParams params;
// File to write stats; stdout if NULL.
params.stats_filename = getenv(kStatsFilenameEnvVar);
// Time limit for the program.
params.timeout_msec = kDefaultTimeoutMillisec;
if (char* timeout_env = getenv(kTimeoutEnvVar)) {
if (timeout_env[0] == '0' && timeout_env[1] == '\0') {
params.timeout_msec = kEffectiveInfiniteTime;
} else if (timeout_env[0] != '0' && IsShortDigitStr(timeout_env, 5)) {
params.timeout_msec = atoi(timeout_env);
} else {
ERROR_FMT("%s value '%s' is led by '0', not pure digits, or too long",
kTimeoutEnvVar, timeout_env);
return 1;
}
}
// Delimiter that encompasses the stats report.
params.delimiter = getenv(kDelimiterEnvVar);
if (params.delimiter && strlen(params.delimiter) >= 20) {
ERROR_FMT("delimiter string is too long (>=20): %s", params.delimiter);
return 1;
}
int command_start = -1;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
if (MatchFlag(argv[i], "-h", "--help")) {
PrintHelp();
return 0;
} else if (MatchFlag(argv[i], "-v", "--verbose")) {
g_verbose = true;
} else {
ERROR_FMT("option '%s' not recognized, use '-h' for help", argv[i]);
return 1;
}
} else {
command_start = i;
break;
}
}
if (command_start < 0) {
ERROR_STR("program name expected, use '-h' for help");
return 1;
}
params.argc = argc - command_start;
params.command = argv + command_start;
VERBOSE("stats output: %s",
params.stats_filename ? params.stats_filename : "(stdout)");
VERBOSE("timeout (ms): %d%s", params.timeout_msec,
params.timeout_msec == kEffectiveInfiniteTime ? " (infinite)" : "");
VERBOSE("command: %s",
std::accumulate(params.command + 1, params.command + params.argc,
std::string(params.command[0]),
[](const std::string& acc, const char* part) {
return acc + " " + part;
})
.c_str());
return Work(params);
}