-
Notifications
You must be signed in to change notification settings - Fork 0
/
wspy.c
448 lines (418 loc) · 12.4 KB
/
wspy.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
/*
* wspy.c - workload spy - main program
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <sys/wait.h>
#include "wspy.h"
#if AMDGPU
#include "gpu_info.h"
#endif
#include "error.h"
int aflag = 0;
int oflag = 0;
int sflag = 0;
int vflag = 0;
int xflag = 1;
int csvflag = 0;
int interval = 0;
int treeflag = 0;
int tree_cmdline = 0;
int tree_open = 0;
int trace_syscall = 0;
FILE *treefile = NULL;
FILE *outfile = NULL;
unsigned int counter_mask = COUNTER_IPC;
int num_procs;
int clocks_per_second;
int command_line_argc;
char **command_line_argv;
int parse_options(int argc,char *const argv[]){
FILE *fp;
int opt;
int i;
int value;
unsigned int lev;
static struct option long_options[] = {
{ "branch", no_argument, 0, 4 },
{ "no-branch", no_argument, 0, 5 },
{ "csv", no_argument, 0, 3 },
{ "cache1", no_argument, 0, 39 },
{ "no-cache1", no_argument, 0, 40 },
{ "cache2", no_argument, 0, 6 },
{ "no-cache2", no_argument, 0, 7 },
{ "cache3", no_argument, 0, 8 },
{ "no-cache3", no_argument, 0, 9 },
{ "dcache", no_argument, 0, 10 },
{ "no-cache", no_argument, 0, 11 },
{ "float",no_argument,0,33 },
{ "icache", no_argument, 0, 12 },
{ "no-icache", no_argument, 0, 13 },
{ "interval", required_argument, 0, 34 },
{ "ipc", no_argument, 0, 14 },
{ "no-ipc", no_argument, 0, 15 },
{ "memory", no_argument, 0, 16 },
{ "no-memory", no_argument, 0, 17 },
{ "opcache", no_argument, 0, 18 },
{ "no-opcache", no_argument, 0, 19 },
{ "per-core", no_argument, 0, 20 },
{ "rusage", no_argument, 0, 21 },
{ "no-rusage", no_argument, 0, 22 },
{ "software", no_argument, 0, 23 },
{ "no-software", no_argument, 0, 24 },
{ "system", no_argument, 0, 36 },
{ "no-system", no_argument, 0, 37 },
{ "tlb", no_argument, 0, 25 },
{ "no-tlb", no_argument, 0, 26 },
{ "topdown", no_argument, 0, 27 }, // (t)
{ "no-topdown", no_argument, 0, 28 },
{ "topdown2", no_argument, 0, 29 }, //
{ "no-topdown2", no_argument, 0, 30 },
{ "topdown-frontend",no_argument, 0, 42 },
{ "no-topdown-frontend",no_argument, 0, 43 },
{ "topdown-backend",no_argument,0,46 },
{ "no-topdown-backend",no_argument,0,47 },
{ "topdown-optlb",no_argument,0,44 },
{ "no-topdown-optlb",no_argument,0,45 },
{ "tree", required_argument, 0, 31 }, //
{ "tree-cmdline",no_argument,0,35 },
{ "tree-open",no_argument,0, 38 },
{ "tree-vmsize",no_argument,0,41 },
{ "verbose", no_argument, 0, 32 },
{ 0,0,0,0 },
};
while ((opt = getopt_long(argc,argv,"+abcio:rsStv",long_options,NULL)) != -1){
switch (opt){
case 3: //--csv
csvflag = 1;
break;
case 4: // --branch
case 'b':
counter_mask |= COUNTER_BRANCH;
break;
case 5: // --no-branch
counter_mask &= (~COUNTER_BRANCH);
break;
case 39: // --cache1
counter_mask |= COUNTER_L1CACHE;
break;
case 40:
counter_mask &= (~COUNTER_L1CACHE);
break;
case 6: // --cache2
case 'c':
counter_mask |= COUNTER_L2CACHE;
break;
case 7: // --no-cache2
counter_mask &= (~COUNTER_L2CACHE);
break;
case 8: // --cache3
counter_mask |= COUNTER_L3CACHE;
break;
case 9: // --no-cache3
counter_mask &= (~COUNTER_L3CACHE);
break;
case 10: // --dcache
counter_mask |= COUNTER_DCACHE;
break;
case 11: // --no-dcache
counter_mask &= (~COUNTER_DCACHE);
break;
case 12: // --icache
counter_mask |= COUNTER_ICACHE;
break;
case 13: // --no-icache
counter_mask &= (~COUNTER_ICACHE);
break;
case 14: // --ipc
case 'i':
counter_mask |= COUNTER_IPC;
break;
case 15: // --no-ipc
counter_mask &= (~COUNTER_IPC);
break;
case 16: // --memory
case 'm':
counter_mask |= COUNTER_MEMORY;
break;
case 17: // --no-memory
counter_mask &= (~COUNTER_MEMORY);
break;
case 18: // --opcache
counter_mask |= COUNTER_OPCACHE;
break;
case 19: // --no-opcache
counter_mask &= (~COUNTER_OPCACHE);
break;
case 20: // --per-core
case 'a':
aflag = 1;
break;
case 'o':
fp = fopen(optarg,"w");
if (!fp){
error("can not open file: %s\n",optarg);
} else {
outfile = fp;
oflag = 1;
}
break;
case 21: // --rusage
case 'r':
xflag = 1;
break;
case 22: // --no-rusage
xflag = 0;
break;
case 23: // --software
counter_mask |= COUNTER_SOFTWARE;
break;
case 24: // --no-software
counter_mask &= (~COUNTER_SOFTWARE);
break;
case 25: // --tlb
counter_mask |= COUNTER_TLB;
break;
case 26: // --no-tlb
counter_mask &= (~COUNTER_TLB);
break;
case 27: // --topdown
case 't':
counter_mask |= COUNTER_TOPDOWN;
break;
case 28: // --no-topdown
counter_mask &= (~COUNTER_TOPDOWN);
break;
case 29: // --topdown2
counter_mask |= COUNTER_TOPDOWN2;
break;
case 30: // --no-topdown2
counter_mask &= (~COUNTER_TOPDOWN2);
break;
case 42: // --topdown-frontend
counter_mask |= COUNTER_TOPDOWN_FE;
break;
case 43: // --no-topdown-frontend
counter_mask &= (~COUNTER_TOPDOWN_FE);
break;
case 46: // --topdown-backend
counter_mask |= COUNTER_TOPDOWN_BE;
break;
case 47: // --no-topdown-backend
counter_mask &= (~COUNTER_TOPDOWN_BE);
break;
case 44: // --topdown-optlb
counter_mask |= COUNTER_TOPDOWN_OP;
break;
case 45: // --no-topdown-optlb
counter_mask &= (~COUNTER_TOPDOWN_OP);
break;
case 31: // --tree
if ((treefile = fopen(optarg,"w")) == NULL){
warning("unable to open tree file: %s, ignored\n",optarg);
} else {
treeflag = 1;
}
break;
case 32: // --verbose
case 'v':
vflag++;
if (vflag>1) set_error_level(ERROR_LEVEL_DEBUG2);
else set_error_level(ERROR_LEVEL_DEBUG);
break;
case 33: // --float
counter_mask |= COUNTER_FLOAT;
break;
case 34:
if ((sscanf(optarg,"%d",&value) == 1) && value > 0){
interval = value;
} else {
warning("invalid argument to --interval: %s, ignored\n",optarg);
}
break;
case 35:
tree_cmdline = 1;
break;
case 36: // --system
case 's':
sflag = 1;
break;
case 37:
sflag = 0;
break;
case 38:
tree_open = 1;
trace_syscall = 1;
break;
default:
return 1;
}
}
if (optind >= argc){
warning("missing command after options\n");
return 1;
}
command_line_argv = calloc(argc-optind+1,sizeof(char *));
command_line_argc = argc - optind;
for (i=0;i<command_line_argc;i++){
command_line_argv[i] = argv[i+optind];
}
return 0;
}
int main(int argc,char *const argv[],char *const envp[]){
int i;
int status;
struct rusage rusage;
struct counter_group *cgroup;
outfile = stdout;
num_procs = get_nprocs();
clocks_per_second = sysconf(_SC_CLK_TCK);
if (parse_options(argc,argv)){
fatal("usage: %s -[abcistv][-o <file>] <cmd><args>...\n"
"\t--per-core or -a - metrics per core\n"
"\t--rusage or -r - show getrusage(2) information\n"
"\t--tree <file> - create CSV of processes\n"
"\t--tree-cmdline - record full command lines\n"
"\t--tree-vmsize - virtual memory size\n"
"\t-o <file> - send output to file\n"
"\t--csv - create csv output\n"
"\t--interval <sec> - read every <sec> seconds\n"
"\t--verbose or -v - print verbose information\n"
"\n"
"\t--software or -s - software counters\n"
"\t--ipc or i - IPC counters\n"
"\t--branch or -b - branch counters\n"
"\t--dcache - L1 dcache counters\n"
"\t--icache - L1 icache counters\n"
"\t--cache2 or -c - L2 cache counters\n"
"\t--cache3 - L3 cache counters\n"
"\t--memory - memory counters\n"
"\t--opcache - opcache counters\n"
"\t--tlb - TLB counters\n"
"\t--topdown or -t - topdown counters, level 1\n"
"\t--topdown2 - topdown counters, level 2\n"
"\t--topdown-frontend - topdown related to fe\n"
"\t--topdown-optlb - topdown related to opcache, dtlb\n"
,argv[0]);
}
if (inventory_cpu() != 0){
fatal("unable to query CPU information\n");
}
check_nmi_watchdog();
#if AMDGPU
gpu_info_initialize();
#endif
// parse the event tables
setup_raw_events();
// set up either system-wide or core-specific counters
if (aflag){
for (i=0;i<cpu_info->num_cores;i++){
if (cpu_info->coreinfo[i].is_available &&
((cpu_info->coreinfo[i].vendor == CORE_AMD_ZEN)||(cpu_info->coreinfo[i].vendor == CORE_AMD_ZEN5)||(cpu_info->coreinfo[i].vendor == CORE_INTEL_CORE))){
setup_counter_groups(&cpu_info->coreinfo[i].core_specific_counters);
}
}
} else {
setup_counter_groups(&cpu_info->systemwide_counters);
}
// software counters are only system-wide
if (counter_mask & COUNTER_SOFTWARE){
if (cgroup = software_counter_group("software")){
cgroup->next = cpu_info->systemwide_counters;
cpu_info->systemwide_counters = cgroup;
}
}
// set up core-specific and system-wide counters
for (i=0;i<cpu_info->num_cores;i++){
if (cpu_info->coreinfo[i].core_specific_counters)
setup_counters(cpu_info->coreinfo[i].core_specific_counters);
}
setup_counters(cpu_info->systemwide_counters);
// start core-specific and system-wide counters
for (i=0;i<cpu_info->num_cores;i++){
if (cpu_info->coreinfo[i].core_specific_counters)
start_counters(cpu_info->coreinfo[i].core_specific_counters);
}
start_counters(cpu_info->systemwide_counters);
signal(SIGINT,SIG_IGN);
// create CSV headers
if (csvflag){
if (interval){
fprintf(outfile,"time,");
}
if (sflag) print_system(PRINT_CSV_HEADER);
if (!interval && xflag) print_usage(NULL,PRINT_CSV_HEADER);
print_metrics(cpu_info->systemwide_counters,PRINT_CSV_HEADER);
fprintf(outfile,"\n");
}
// let the child start after two seconds
sleep(2);
if (sflag) read_system();
clock_gettime(CLOCK_REALTIME,&start_time);
if (launch_child(command_line_argc,command_line_argv,envp)){
fatal("unable to launch %s\n",command_line_argv[0]);
}
if (interval){
signal(SIGALRM,timer_callback);
alarm(interval);
}
write(child_pipe[1],"start\n",6);
if (treeflag){
ptrace_setup(child_pid);
fprintf(treefile,"0.000 %d root\n",child_pid);
ptrace_loop();
getrusage(RUSAGE_CHILDREN,&rusage);
} else {
// without ptrace, this process waits to complete
wait4(child_pid,&status,0,&rusage);
}
is_still_running = 0;
clock_gettime(CLOCK_REALTIME,&finish_time);
read_system();
// -----
// stop core-specific and system-wide counters
for (i=0;i<cpu_info->num_cores;i++){
if (cpu_info->coreinfo[i].core_specific_counters)
read_counters(cpu_info->coreinfo[i].core_specific_counters,1);
}
read_counters(cpu_info->systemwide_counters,1);
if (csvflag){
if (interval){
double elapsed;
elapsed = finish_time.tv_sec + finish_time.tv_nsec / 1000000000.0 -
start_time.tv_sec - start_time.tv_nsec / 1000000000.0;
fprintf(outfile,"%4.1f,",elapsed);
}
if (sflag) print_system(PRINT_CSV);
if (xflag && !interval) print_usage(&rusage,PRINT_CSV);
print_metrics(cpu_info->systemwide_counters,PRINT_CSV);
fprintf(outfile,"\n");
} else {
if (sflag) print_system(PRINT_NORMAL);
if (xflag) print_usage(&rusage,PRINT_NORMAL);
print_metrics(cpu_info->systemwide_counters,PRINT_NORMAL);
}
for (i=0;i<cpu_info->num_cores;i++){
if (cpu_info->coreinfo[i].core_specific_counters){
if (csvflag){
print_usage(&rusage,PRINT_CSV);
} else {
fprintf(outfile,"##### core %2d #######################\n",i);
}
print_metrics(cpu_info->coreinfo[i].core_specific_counters,csvflag?PRINT_CSV:PRINT_NORMAL);
if (csvflag) fprintf(outfile,"\n");
}
}
if (oflag) fclose(outfile);
// -----
#if AMDGPU
gpu_info_finalize();
#endif
return 0;
}