-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·761 lines (729 loc) · 27.8 KB
/
main.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
#define _GNU_SOURCE 1
#include <asm/unistd.h>
#include <fcntl.h>
#include <linux/perf_event.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#define LEN 100
// Global variable
static int *fd, nfd = -1, go = 0, sample = 0;
FILE *outfd;
static long long *r;
static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags)
{
int ret;
ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
group_fd, flags);
return ret;
}
// Handler wake up signal
static void wakeup_handler(int signum, siginfo_t* info, void* context)
{
go = 1;
if (signum != SIGUSR1)
{
fprintf(stderr, "Wrong signal\n");
exit(EXIT_FAILURE);
}
}
// Handler overflow registers
static void perf_event_handler(int signum, siginfo_t* info, void* context)
{
int i;
ioctl(info->si_fd, PERF_EVENT_IOC_REFRESH, 1);
//Test that signal is correct
if (info->si_code == POLL_HUP){
//Read all counters and save into file
read(fd[0], r, sizeof(long long) * (nfd + 1));
for (i = 1; i <= r[0]; i++) fprintf(outfd, "%lld ", r[i]);
fprintf(outfd, "\n");
}
}
// Initialize signal struct
int initSig(struct sigaction *sa)
{
//Configure signal hanlder
memset(sa, 0, sizeof(struct sigaction));
sa->sa_sigaction = perf_event_handler;
sa->sa_flags = SA_SIGINFO | SA_RESTART;
//Setup signal handler
if (sigaction(SIGIO, sa, NULL) < 0)
{
fprintf(stderr, "Error setting up signal handler\n");
perror("sigaction");
return -1;
}
return 0;
}
// Init WakeUp Signal
int initWakeSig(struct sigaction *sa)
{
//Configure signal hanlder
memset(sa, 0, sizeof(struct sigaction));
sa->sa_sigaction = wakeup_handler;
sa->sa_flags = SA_SIGINFO | SA_RESTART;
//Setup signal handler
if (sigaction(SIGUSR1, sa, NULL) < 0)
{
fprintf(stderr, "Error setting up signal handler\n");
perror("sigaction");
return -1;
}
return 0;
}
void perfStruct (struct perf_event_attr *pe, char leader, int type, int config)
{
memset(pe, 0, sizeof(struct perf_event_attr));
pe->size = sizeof(struct perf_event_attr);
pe->disabled = 1;
//Exclude kernel and hipervisor from being measure
pe->exclude_kernel = 1;
pe->exclude_hv = 1;
if (leader)
{
pe->read_format = PERF_FORMAT_GROUP;
if (sample != 0) pe->sample_period = sample;
}
pe->type = type;
pe->config = config;
}
// Parse counter program options
int counterProgram(int *out, char *in)
{
if (strcmp(in, "PERF_COUNT_HW_CPU_CYCLES") == 0)
{
*out = PERF_TYPE_HARDWARE;
return PERF_COUNT_HW_CPU_CYCLES;
}
else if (strcmp(in, "PERF_COUNT_HW_INSTRUCTIONS") == 0)
{
*out = PERF_TYPE_HARDWARE;
return PERF_COUNT_HW_INSTRUCTIONS;
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_REFERENCES") == 0)
{
*out = PERF_TYPE_HARDWARE;
return PERF_COUNT_HW_CACHE_REFERENCES;
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_MISSES") == 0)
{
*out = PERF_TYPE_HARDWARE;
return PERF_COUNT_HW_CACHE_MISSES;
}
else if (strcmp(in, "PERF_COUNT_HW_BRANCH_INSTRUCTIONS") == 0)
{
*out = PERF_TYPE_HARDWARE;
return PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
}
else if (strcmp(in, "PERF_COUNT_HW_BRANCH_MISSES") == 0)
{
*out = PERF_TYPE_HARDWARE;
return PERF_COUNT_HW_BRANCH_MISSES;
}
else if (strcmp(in, "PERF_COUNT_HW_BUS_CYCLES") == 0)
{
*out = PERF_TYPE_HARDWARE;
return PERF_COUNT_HW_BUS_CYCLES;
}
else if (strcmp(in, "PERF_COUNT_HW_STALLED_CYCLES_FRONTEND") == 0)
{
*out = PERF_TYPE_HARDWARE;
return PERF_COUNT_HW_STALLED_CYCLES_FRONTEND;
}
else if (strcmp(in, "PERF_COUNT_HW_STALLED_CYCLES_BACKEND") == 0)
{
*out = PERF_TYPE_HARDWARE;
return PERF_COUNT_HW_STALLED_CYCLES_BACKEND;
}
else if (strcmp(in, "PERF_COUNT_HW_CPU_CYCLES") == 0)
{
*out = PERF_TYPE_HARDWARE;
return PERF_COUNT_HW_CPU_CYCLES;
}
else if (strcmp(in, "PERF_COUNT_SW_CPU_CLOCK") == 0)
{
*out = PERF_TYPE_SOFTWARE;
return PERF_COUNT_SW_CPU_CLOCK;
}
else if (strcmp(in, "PERF_COUNT_SW_TASK_CLOCK") == 0)
{
*out = PERF_TYPE_SOFTWARE;
return PERF_COUNT_SW_TASK_CLOCK;
}
else if (strcmp(in, "PERF_COUNT_SW_PAGE_FAULTS") == 0)
{
*out = PERF_TYPE_SOFTWARE;
return PERF_COUNT_SW_PAGE_FAULTS;
}
else if (strcmp(in, "PERF_COUNT_SW_CONTEXT_SWITCHES") == 0)
{
*out = PERF_TYPE_SOFTWARE;
return PERF_COUNT_SW_CONTEXT_SWITCHES;
}
else if (strcmp(in, "PERF_COUNT_SW_CPU_MIGRATIONS") == 0)
{
*out = PERF_TYPE_SOFTWARE;
return PERF_COUNT_SW_CPU_MIGRATIONS;
}
else if (strcmp(in, "PERF_COUNT_SW_PAGE_FAULTS_MIN") == 0)
{
*out = PERF_TYPE_SOFTWARE;
return PERF_COUNT_SW_PAGE_FAULTS_MIN;
}
else if (strcmp(in, "PERF_COUNT_SW_PAGE_FAULTS_MAJ") == 0)
{
*out = PERF_TYPE_SOFTWARE;
return PERF_COUNT_SW_PAGE_FAULTS_MAJ;
}
else if (strcmp(in, "PERF_COUNT_SW_ALIGNMENT_FAULTS") == 0)
{
*out = PERF_TYPE_SOFTWARE;
return PERF_COUNT_SW_ALIGNMENT_FAULTS;
}
else if (strcmp(in, "PERF_COUNT_SW_EMULATION_FAULTS") == 0)
{
*out = PERF_TYPE_SOFTWARE;
return PERF_COUNT_SW_EMULATION_FAULTS;
}
else if (strcmp(in, "PERF_COUNT_SW_DUMMY") == 0)
{
*out = PERF_TYPE_SOFTWARE;
return PERF_COUNT_SW_DUMMY;
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1D_ACCESS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1D)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1I_ACCESS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1I)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_LL_ACCESS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_LL)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_DTLB_ACCESS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_DTLB)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_ITLB_ACCESS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_ITLB)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_BPU_ACCESS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_BPU)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_NODE_ACCESS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_NODE)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1D_ACCESS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1D)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1I_ACCESS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1I)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_LL_ACCESS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_LL)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_DTLB_ACCESS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_DTLB)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_ITLB_ACCESS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_ITLB)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_BPU_ACCESS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_BPU)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_NODE_ACCESS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_NODE)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1D_MISS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1D)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1I_MISS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1I)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_LL_MISS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_LL)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_DTLB_MISS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_DTLB)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_ITLB_MISS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_ITLB)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_BPU_MISS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_BPU)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_NODE_MISS_R") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_NODE)
| (PERF_COUNT_HW_CACHE_OP_READ << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1D_MISS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1D)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1I_MISS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1I)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_LL_MISS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_LL)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_DTLB_MISS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_DTLB)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_ITLB_MISS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_ITLB)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_BPU_MISS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_BPU)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_NODE_MISS_W") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_NODE)
| (PERF_COUNT_HW_CACHE_OP_WRITE << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1D_ACCESS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1D)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1I_ACCESS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1I)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_LL_ACCESS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_LL)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_DTLB_ACCESS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_DTLB)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_ITLB_ACCESS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_ITLB)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_BPU_ACCESS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_BPU)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_NODE_ACCESS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_NODE)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1D_MISS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1D)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_L1I_MISS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_L1I)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_LL_MISS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_LL)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_DTLB_MISS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_DTLB)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_ITLB_MISS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_ITLB)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_BPU_MISS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_BPU)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else if (strcmp(in, "PERF_COUNT_HW_CACHE_NODE_MISS_P") == 0)
{
*out = PERF_TYPE_HW_CACHE;
return (PERF_COUNT_HW_CACHE_NODE)
| (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8)
| (PERF_COUNT_HW_CACHE_RESULT_MISS << 16);
}
else return -1;
}
// Prints all events options
void printEvents()
{
fprintf(stdout, "Events list (since Linux 3.1)\n");
fprintf(stdout, "\n");
fprintf(stdout, "PERF_COUNT_HW_CPU_CYCLES -- Total cycles (Affected by frequency scaling)\n");
fprintf(stdout, "PERF_COUNT_HW_INSTRUCTIONS -- Retired instructions\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_REFERENCES -- Cache access (Usually LLC)\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_MISSES -- Cache misses (Usually LLC)\n");
fprintf(stdout, "PERF_COUNT_HW_BRANCH_INSTRUCTIONS -- Retired branch instructions\n");
fprintf(stdout, "PERF_COUNT_HW_BRANCH_MISSES -- Mispredicted branch instructions\n");
fprintf(stdout, "PERF_COUNT_HW_BUS_CYCLES -- Bus cycles\n");
fprintf(stdout, "PERF_COUNT_HW_STALLED_CYCLES_FRONTEND -- Stalled cycles during issue\n");
fprintf(stdout, "PERF_COUNT_HW_STALLED_CYCLES_BACKEND -- Stalled cycles during retirement\n");
fprintf(stdout, "PERF_COUNT_HW_CPU_CYCLES -- Total cycles\n");
fprintf(stdout, "PERF_COUNT_SW_CPU_CLOCK -- CPU clock\n");
fprintf(stdout, "PERF_COUNT_SW_TASK_CLOCK -- Clock count specific to the task that is running\n");
fprintf(stdout, "PERF_COUNT_SW_PAGE_FAULTS -- Number of page faults\n");
fprintf(stdout, "PERF_COUNT_SW_CONTEXT_SWITCHES -- Number of context switches\n");
fprintf(stdout, "PERF_COUNT_SW_CPU_MIGRATIONS -- Number of times the process has to migrated to a new CPU\n");
fprintf(stdout, "PERF_COUNT_SW_PAGE_FAULTS_MIN -- Number of minor page faults\n");
fprintf(stdout, "PERF_COUNT_SW_PAGE_FAULTS_MAJ -- Number of major page faults\n");
fprintf(stdout, "PERF_COUNT_SW_ALIGNMENT_FAULTS -- Number of alignment faults\n");
fprintf(stdout, "PERF_COUNT_SW_EMULATION_FAULTS -- Number of emulation faults\n");
fprintf(stdout, "PERF_COUNT_SW_DUMMY -- Placeholder event that counts nothing\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1D_ACCESS_R -- Read accesses to L1 data cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1I_ACCESS_R -- Read accesses to L1 instruction cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_LL_ACCESS_R -- Read accesses to LLC cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_DTLB_ACCESS_R -- Read access to Data TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_ITLB_ACCESS_R -- Read access to Instruction TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_BPU_ACCESS_R -- Read accesses to branch prediction Unit\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_NODE_ACCESS_R -- Read accesses to local memory\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1D_ACCESS_W -- Write accesses to L1 data cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1I_ACCESS_W -- Write accesses to L1 instruction cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_LL_ACCESS_W -- Write accesses to LLC cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_DTLB_ACCESS_W -- Write accesses to Data TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_ITLB_ACCESS_W -- Write accesses to Instruction TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_BPU_ACCESS_W -- Write accesses to branch prediction Unit\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_NODE_ACCESS_W -- Write accesses to local memory\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1D_MISS_R -- Read misses to L1 data cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1I_MISS_R -- Read misses to L1 instruction cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_LL_MISS_R -- Read misses to LLC cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_DTLB_MISS_R -- Read misses to Data TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_ITLB_MISS_R -- Read misses to Istruction TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_BPU_MISS_R -- Read misses to branch prediction\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_NODE_MISS_R -- Read misses to local memory\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1D_MISS_W -- Write misses to L1 data cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1I_MISS_W -- Write misses to L1 instruction cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_LL_MISS_W -- Write misses to LLC cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_DTLB_MISS_W -- Write misses to Data TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_ITLB_MISS_W -- Write misses to Instruction TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_BPU_MISS_W -- Write misses to branch predicition Unit\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_NODE_MISS_W -- Write miesses to local memory\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1D_ACCESS_P -- Prefetch accesses to L1 data cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1I_ACCESS_P -- Prefetch accesses to L1 instruction cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_LL_ACCESS_P -- Prefetch accesses to LLC cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_DTLB_ACCESS_P -- Prefetch accesses to Data TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_ITLB_ACCESS_P -- Prefecth accesses to Instruction TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_BPU_ACCESS_P -- Prefetch accesses to branch prediction unit\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_NODE_ACCESS_P -- Prefetch accesses to local memory\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1D_MISS_P -- Prefecth misses to L1 data cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_L1I_MISS_P -- Prefetch misses to L1 instruction cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_LL_MISS_P -- Prefetch misses to LLC cache\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_DTLB_MISS_P -- Prefetch misses to Data TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_ITLB_MISS_P -- Prefetch misses to Instruction TLB\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_BPU_MISS_P -- Prefetch misses to branch prediction unit\n");
fprintf(stdout, "PERF_COUNT_HW_CACHE_NODE_MISS_P -- Prefetch misses to local memory\n");
}
// Measure HW counters
void measure(int argc, char **argv, char (*event)[LEN], int size)
{
int i, returnStatus, type, config;
sigset_t sigmask, oldmask;
pid_t pid;
struct perf_event_attr pe[nfd];
struct sigaction sa, wake;
// Block SIGUSR1, for safety reasons
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGUSR1);
sigfillset(&oldmask);
if ((pid = fork()) == 0)
{
// Wait until parent process is ready to measure
sigprocmask(SIG_BLOCK, &sigmask, &oldmask);
if (kill(getppid(), SIGUSR1) == -1)
{
fprintf(stderr, "Can't wakup child process\n");
exit(EXIT_FAILURE);
}
if (initWakeSig(&wake) == -1) exit(EXIT_FAILURE);
if (!go) sigsuspend(&oldmask);
// Launch extern program
execvp(argv[0], argv);
// Child should never reach this exit
exit(EXIT_FAILURE);
} else if (pid < 1)
{
fprintf(stderr, "Can't create a childre\n");
exit(EXIT_FAILURE);
}
// Wait until child process is ready (parent process always have to
// execute after child process)
sigprocmask(SIG_BLOCK, &sigmask, &oldmask);
if (initWakeSig(&wake) == -1) exit(EXIT_FAILURE);
if (!go) sigsuspend(&oldmask);
sigprocmask(SIG_UNBLOCK, &sigmask, NULL);
//TODO: improve signal and blocking between parent and child process in
//order to avoid use sleep (THIS DON'T GUARANTEE 100% THAT CHILDREN FINISH
//ITS SIGNAL COMUNICATION)
sleep(1);
// Init signal for overflow
if (initSig(&sa) == -1) exit(EXIT_FAILURE);
// Init structs for Perf
for (i = 0; i < nfd; i++)
{
// Print options
fprintf(outfd, "#%s ", event[i]);
config = counterProgram(&type, event[i]);
if (config == -1)
{
fprintf(stderr, "Wrong Event\n");
exit(EXIT_FAILURE);
}
// First event is the leader
if (!i) perfStruct(&pe[i], 1, type, config);
else perfStruct(&pe[i], 0, type, config);
}
fprintf(outfd, "\n");
// Init Perf Events
if((fd[0] = perf_event_open(&pe[0], pid, -1, -1, PERF_FLAG_FD_CLOEXEC)) == -1)
{
fprintf(stderr, "Error opening leader %llx\n", pe[0].config);
fprintf(stderr, "ERRNO: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
for (i = 1; i < nfd; i++)
{
if ((fd[i] = perf_event_open(&pe[i], pid, -1, fd[0], 0)) == -1)
{
fprintf(stderr, "Error opening leader %llx\n", pe[i].config);
fprintf(stderr, "ERRNO: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
//Setup event handler for overflow signals
fcntl(fd[0], F_SETFL, O_NONBLOCK | O_ASYNC);
fcntl(fd[0], F_SETSIG, SIGIO);
fcntl(fd[0], F_SETOWN, getpid());
for (i = 1; i < nfd; i++)
{
ioctl(fd[i], PERF_EVENT_IOC_RESET, 0);
ioctl(fd[i], PERF_EVENT_IOC_ENABLE, 0);
}
ioctl(fd[0], PERF_EVENT_IOC_RESET, 0);
ioctl(fd[0], PERF_EVENT_IOC_ENABLE, 0);
ioctl(fd[0], PERF_EVENT_IOC_REFRESH, 1);
//Send signal to extern process and wait until child end (WE ARE MEASURING!!)
if (kill(pid, SIGUSR1) == -1) fprintf(stderr, "Can't wake up child process\n");
else
{
wait(&returnStatus);
if (returnStatus != 0)
{
fprintf(stderr, "Program did not finish normally: %d, ERRNO: %s\n",
returnStatus, strerror(errno));
}
}
for (i = 0; i < nfd; i++) ioctl(fd[i], PERF_EVENT_IOC_DISABLE, 0);
// Print last values
read(fd[0], r, sizeof(long long) * (nfd + 1));
for (i = 1; i <= r[0]; i++) fprintf(outfd, "%lld ", r[i]);
fprintf(outfd, "\n");
// Close file descriptors
for (i = 0; i < nfd; i++) close(fd[i]);
}
int main(int argc, char **argv)
{
int c, i, maxC = 0;
char counter[LEN][LEN];
// Parse program arguments
while ((c = getopt(argc, argv, "ln:o:c:s:")) != -1)
{
switch(c)
{
case 'l':
printEvents();
goto exit;
case 'o':
outfd = fopen(optarg, "w");
break;
case 'n':
if ((nfd = atoi(optarg)) > 4)
fprintf(stderr, "Warning: more than 4 events is probably "
"not supported, depends on kernel version and CPU\n");
break;
case 'c':
optind--;
for (i = 0; optind < argc && *argv[optind] != '-'; optind++, i++)
strcpy (counter[i], argv[optind]);
maxC = i;
break;
case 'h':
fprintf(stdout, "./main -n [number of events] -o [output file] "
"-c [Counters] -s [Samples] -- Program args\n");
goto exit;
break;
case 's':
if ((sample = atoi(optarg)) < 1)
fprintf(stdout, "Sample must be greater than 0\n");
break;
}
}
if (strcmp(argv[optind - 1], "--") != 0)
{
fprintf(stderr, "./main -n [number of events] -o [output file] "
"-c [Counters] -s [Samples] -- Program args\n");
goto exit;
}
// Test if all variables are set
if (nfd == -1 || outfd == NULL || maxC == 0)
{
fprintf(stderr, "./main -n [number of events] -o [output file] "
"-c [Counters] -s [Samples] -- Program args\n");
goto exit;
}
// Allocate file descriptos
fd = malloc(sizeof(int) * nfd);
// Allocate struct for reading output
r = malloc(sizeof(long long) * (nfd + 1));
// Print basic information
measure(argc - optind, &argv[optind], counter, maxC);
exit:
if (outfd != NULL) fclose(outfd);
}