-
Notifications
You must be signed in to change notification settings - Fork 0
/
nx.c
257 lines (232 loc) · 6.6 KB
/
nx.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
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <fcntl.h>
void usage(const char* prog_name)
{
fprintf(stderr, "usage: %s [-hc][-n <times>] command\n", prog_name);
exit(1);
}
typedef struct {
float real_time;;
float user_time;
float sys_time;
} CallStat, *CallStatP;
float timeval_to_sec(struct timeval* tv)
{
return (float)tv->tv_sec+0.000001*(float)tv->tv_usec;
}
typedef enum {
CSF_CSV,
CSF_HUMAN,
} CallStatFormat;
float minf(float a, float b)
{
return a<b ? a : b;
}
float maxf(float a, float b)
{
return a>b ? a : b;
}
void callstat_print_sep(FILE* fd, CallStatFormat fmt)
{
if (fmt==CSF_HUMAN){
fprintf(fd, "[---------|----------|----------|----------]\n");
}
}
void callstat_print_head(FILE* fd, CallStatFormat fmt)
{
callstat_print_sep(fd, fmt);
if (fmt==CSF_HUMAN) {
fprintf(fd, "| name | real | user | sys |\n");
}
if (fmt==CSF_CSV) {
fprintf(fd, "name, real, user, sys\n");
}
callstat_print_sep(fd, fmt);
}
void callstat_print(FILE* fd, CallStatP call_stat, const char* name,
CallStatFormat fmt)
{
if (fmt==CSF_CSV) {
fprintf(fd, "%s, %.3f, %.3f, %.3f\n", name, call_stat->real_time,
call_stat->user_time, call_stat->sys_time);
return;
}
if (fmt==CSF_HUMAN) {
fprintf(fd, "%10s %10.3f %10.3f %10.3f\n", name, call_stat->real_time,
call_stat->user_time, call_stat->sys_time);
return;
}
}
/*
global variables
*/
char* g_program_name=NULL;
int g_silent=0;
int g_num_repeats=10;
int g_output_format=CSF_HUMAN;
FILE* g_output_stream=NULL;
int parse_options(int argc, char **argv)
{
/*
if the program name is of the form <num>x, use <num> as the default number
of repeats
*/
char* end_ptr;
int x=strtol(argv[0], &end_ptr, 10);
if (*end_ptr=='x' && *(end_ptr+1)=='\0') {
g_num_repeats=x;
}
int opt=0;
g_output_stream=stdout;
while ((opt = getopt(argc, argv, "shcn:o:")) != -1) {
switch(opt) {
case 'n':
g_num_repeats=strtol(optarg, &end_ptr, 10);
if (*end_ptr!='\0') {
fprintf(stderr, "invalid integer literal for -n parameter.\n");
usage(g_program_name);
}
if (g_num_repeats<=0 || g_num_repeats>10000) {
fprintf(stderr, "Repeats must be in the range [1-10000].\n");
usage(g_program_name);
}
break;
case 's':
g_silent=1;
break;
case 'h':
g_output_format=CSF_HUMAN;
break;
case 'o':
if (!(g_output_stream=fopen(optarg, "w+"))) {
perror("Can't open file");
usage(g_program_name);
}
break;
case 'c':
g_output_format=CSF_CSV;
break;
case '?':
default:
usage(g_program_name);
}
}
if (argc-optind==0) {
usage(g_program_name);
}
return optind;
}
void collect_statistics(CallStatP call_stats)
{
int i=0;
CallStat min=call_stats[0], max=call_stats[0], mean=call_stats[0];
CallStat stddev;
memset(&stddev, 0, sizeof(CallStat));
for (i=1; i<g_num_repeats; ++i) {
min.real_time=minf(min.real_time, call_stats[i].real_time);
min.user_time=minf(min.user_time, call_stats[i].user_time);
min.sys_time=minf(min.sys_time, call_stats[i].sys_time);
max.real_time=maxf(max.real_time, call_stats[i].real_time);
max.user_time=maxf(max.user_time, call_stats[i].user_time);
max.sys_time=maxf(max.sys_time, call_stats[i].sys_time);
mean.real_time+=call_stats[i].real_time;
mean.user_time+=call_stats[i].user_time;
mean.sys_time+=call_stats[i].sys_time;
}
mean.real_time/=g_num_repeats;
mean.user_time/=g_num_repeats;
mean.sys_time/=g_num_repeats;
for (i=0; i<g_num_repeats; ++i) {
float d1=call_stats[i].real_time-mean.real_time;
stddev.real_time+=d1*d1;
float d2=call_stats[i].user_time-mean.user_time;
stddev.real_time+=d2*d2;
float d3=call_stats[i].sys_time-mean.sys_time;
stddev.sys_time+=d3*d3;
}
stddev.real_time=sqrt(stddev.real_time/g_num_repeats);
stddev.user_time=sqrt(stddev.user_time/g_num_repeats);
stddev.sys_time=sqrt(stddev.sys_time/g_num_repeats);
callstat_print(g_output_stream, &mean, "mean", g_output_format);
callstat_print(g_output_stream, &min, "min", g_output_format);
callstat_print(g_output_stream, &max, "max", g_output_format);
callstat_print(g_output_stream, &stddev, "stddev", g_output_format);
callstat_print_sep(g_output_stream, g_output_format);
}
void run_and_measure(int argc, char** argv, CallStatP call_stat_out)
{
int pid=0, status=0;
struct timeval before, after;
struct rusage ru;
gettimeofday(&before, NULL);
switch(pid = vfork()) {
case -1:
perror(g_program_name);
if (g_output_stream!=stdout) {
fclose(g_output_stream);
}
exit(1);
case 0:
if (g_silent) {
int stdout_err_fd=0;
if((stdout_err_fd = open("/dev/null", O_RDWR))==-1){
perror("can't redirect output/errors to /dev/null");
exit(-1);
}
dup2(stdout_err_fd, STDOUT_FILENO);
dup2(stdout_err_fd, STDERR_FILENO);
}
execvp(*argv, argv);
perror(*argv);
_exit((errno == ENOENT) ? 127 : 126);
}
while (wait3(&status, 0, &ru) != pid);
gettimeofday(&after, NULL);
if (!WIFEXITED(status)) {
fprintf(stderr, "Command terminated abnormally.\n");
exit(-1);
}
timersub(&after, &before, &after);
call_stat_out->real_time=timeval_to_sec(&after);
call_stat_out->user_time=timeval_to_sec(&ru.ru_utime);
call_stat_out->sys_time=timeval_to_sec(&ru.ru_stime);
}
int main(int argc, char **argv)
{
g_program_name=argv[0];
int consumed_args=parse_options(argc, argv);
/*
if we get here, all parameters have been parsed correctly. skip used
parameters. Everything that is left is the command we would like to
execute.
*/
argc-=consumed_args;
argv+=consumed_args;
CallStatP call_stats=(CallStatP)malloc(sizeof(CallStat)*g_num_repeats);
callstat_print_head(g_output_stream, g_output_format);
int i=0;
for (; i<g_num_repeats; ++i) {
run_and_measure(argc, argv, &call_stats[i]);
char num_buf[10];
snprintf(num_buf, 10, "%d", i+1);
callstat_print(g_output_stream, &call_stats[i], num_buf, g_output_format);
}
callstat_print_sep(g_output_stream, g_output_format);
/* collect statistics */
collect_statistics(call_stats);
if (g_output_stream!=stdout) {
fclose(g_output_stream);
}
free(call_stats);
return 0;
}