-
Notifications
You must be signed in to change notification settings - Fork 0
/
pperf.c
899 lines (777 loc) · 28.5 KB
/
pperf.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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
#include "pperf.h"
#include <stdbool.h>
//#define ESTIMATE_LATENCY
//#define PC_ONLY
#ifdef DEBUG
#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug_printf(...)
#endif
#if !defined(__amd64__) && !defined(__aarch64__) && !defined(__riscv)
#error "Architecture not supported!"
#endif
//Older Kernel do not have that option
#ifndef PTRACE_O_EXITKILL
#define PTRACE_O_EXITKILL 0
#endif
#define PTRACE_WAIT(target, status) do { \
do { \
target = waitpid(-1, &status, __WALL); \
} while(target == -1 && errno == EAGAIN)
#define PTRACE_OTHER_CONTINUE(target, signal) do { \
_ptrace_return = ptrace(PTRACE_CONT, target, NULL, signal); \
} while (_ptrace_return == -1L && (errno == EBUSY || errno == EFAULT || errno == ESRCH))
#define PTRACE_CONTINUE(target, signal) \
_ptrace_return = ptrace(PTRACE_CONT, target, NULL, signal)
#ifndef TRACEE_INTERUPT_SIGNAL
#define TRACEE_INTERUPT_SIGNAL SIGUSR2
#endif
unsigned int getOnlineCPUIds(unsigned int **onlineCPUs) {
char *buf = NULL;
size_t n = 0;
unsigned int c = 0;
unsigned int id = 0;
FILE *fd = fopen("/proc/cpuinfo","r");
if (fd == NULL)
return 0;
while (getline(&buf, &n, fd) != -1) {
if (strstr(buf, "processor") == buf) {
if (sscanf(buf, "processor%255s%u", buf, &id) == 2) {
if (*onlineCPUs == NULL) {
*onlineCPUs = malloc(c * sizeof(unsigned int));
} else {
*onlineCPUs = realloc(*onlineCPUs, c * sizeof(unsigned int));
}
if (*onlineCPUs == NULL) {
return 0;
}
(*onlineCPUs)[c++] = id;
}
}
}
if (buf != NULL) {
free(buf);
}
return c;
}
struct task {
uint32_t tid;
uint64_t pc;
uint64_t cputime;
} __attribute__((packed));
struct trackTask {
pid_t tid;
bool thread;
FILE *schedstat;
};
int getCPUTimeFromSchedstat(FILE *schedstat, uint64_t *cputime) {
#ifdef PC_ONLY
return 0;
#endif
schedstat = freopen(NULL, "r", schedstat);
if (schedstat == NULL)
return 1;
if (fscanf(schedstat, "%lu", cputime) == 1)
return 0;
return 1;
}
struct taskList {
pid_t root;
uint32_t count;
uint32_t allocCount;
struct task *trace;
struct trackTask *tasks;
};
struct taskList tasks = {};
int addTask(pid_t const task) {
static char schedfile[1024] = {};
if (tasks.allocCount == 0) {
tasks.count = 0;
tasks.allocCount = 1;
tasks.trace = (struct task *) malloc(sizeof(struct task));
tasks.tasks = (struct trackTask *) malloc(sizeof(struct trackTask));
if (tasks.trace == NULL || tasks.tasks == NULL)
return 1;
} else if (tasks.count == tasks.allocCount) {
tasks.allocCount *= 2;
tasks.trace = (struct task *) realloc(tasks.trace, tasks.allocCount * sizeof(struct task));
tasks.tasks = (struct trackTask *) realloc(tasks.tasks, tasks.allocCount * sizeof(struct trackTask));
if (tasks.trace == NULL || tasks.tasks == NULL)
return 1;
}
snprintf(schedfile, 1024, "/proc/%d/task/%d/schedstat", tasks.root, task);
tasks.tasks[tasks.count].tid = task;
tasks.tasks[tasks.count].thread = tasks.root != task;
tasks.tasks[tasks.count].schedstat = fopen(schedfile, "r");
if (tasks.tasks[tasks.count].schedstat == NULL) {
snprintf(schedfile, 1024, "/proc/%d/task/%d/schedstat", task, task);
tasks.tasks[tasks.count].thread = false;
tasks.tasks[tasks.count].schedstat = fopen(schedfile, "r");
if (tasks.tasks[tasks.count].schedstat == NULL)
return 1;
}
tasks.trace[tasks.count].tid = task;
tasks.count++;
return 0;
}
int removeTaskIndex(uint32_t const i) {
if (i < tasks.count) {
fclose(tasks.tasks[i].schedstat);
tasks.count--;
for (unsigned int j = i; j < tasks.count; j++) {
tasks.trace[j].tid = tasks.trace[j+1].tid;
memcpy(&tasks.tasks[j], &tasks.tasks[j+1], sizeof(struct trackTask));
}
return 0;
}
return 1;
}
int removeTask(pid_t const task) {
for (unsigned int i = 0; i < tasks.count; i++) {
if (tasks.tasks[i].tid == task) {
fclose(tasks.tasks[i].schedstat);
tasks.count--;
for (unsigned int j = i; j < tasks.count; j++) {
tasks.trace[j].tid = tasks.trace[j+1].tid;
memcpy(&tasks.tasks[j], &tasks.tasks[j+1], sizeof(struct trackTask));
}
return 0;
}
}
return 1;
}
int taskExists(pid_t const task) {
for (unsigned int i = 0; i < tasks.count; i++) {
if (tasks.tasks[i].tid == task)
return 1;
}
return 0;
}
void groupStopNonThreadTasks() {
for (unsigned int i = 0; i < tasks.count; i++) {
if (!tasks.tasks[i].thread) {
kill(tasks.tasks[i].tid, SIGSTOP);
}
}
}
bool isNonThreadTask(pid_t const task) {
for (unsigned int i = 0; i < tasks.count; i++) {
if (tasks.tasks[i].tid == task) {
return !tasks.tasks[i].thread;
}
}
return false;
}
void help(char const *exec, char const opt, char const *optarg) {
FILE *out = stdout;
if (opt != 0) {
out = stderr;
if (optarg) {
fprintf(out, "Invalid parameter - %c %s\n", opt, optarg);
} else {
fprintf(out, "Invalid parameter - %c\n", opt);
}
}
fprintf(out, "%s [options] -- <command> [arguments]\n", exec);
fprintf(out, "\n");
fprintf(out, "Compiled with PMU\n");
fprintf(out, "%s\n", pmuAbout());
fprintf(out, "\n");
fprintf(out, "Options:\n");
fprintf(out, " -o, --output <file> write to file\n");
fprintf(out, " -p, --pmu-arg <pmu> pmu argument\n");
fprintf(out, " -f, --frequency <hertz> sampling frequency\n");
fprintf(out, " -r, --randomize randomize start sample\n");
fprintf(out, " --core-isolation sample on isolated core\n");
fprintf(out, " --fifo <priority> set fifo scheduler with priority\n");
fprintf(out, " --rr <priority> set rr scheduler with priority\n");
fprintf(out, " -v, --verbose verbsoe output at the end\n");
fprintf(out, " -h, --help shows help\n");
fprintf(out, "\n");
fprintf(out, "Example: %s -o /tmp/map -f 1000 -v -- sleep 10\n", exec);
}
struct timerData {
int active;
timer_t timer;
struct itimerspec time;
struct timespec samplingInterval;
struct sigaction signalOldAction;
struct sigaction signalAction;
};
struct callbackData {
pid_t tid;
struct timespec lastInterrupt;
};
static struct callbackData _callback_data;
void timerCallback(int sig) {
(void) sig;
int r;
do {
r = kill(_callback_data.tid, TRACEE_INTERUPT_SIGNAL);
} while (r == -1 && errno == EAGAIN);
debug_printf("[%d] send %d\n", _callback_data.tid, TRACEE_INTERUPT_SIGNAL);
clock_gettime(CLOCK_REALTIME, &_callback_data.lastInterrupt);
}
int pauseTimer(struct timerData *timer) {
if (timer->active == 0)
return 0;
timer->time.it_value.tv_sec = 0;
timer->time.it_value.tv_nsec = 0;
debug_printf("[DEBUG] timer paused\n");
if (timer_settime(timer->timer, 0, &timer->time, NULL) != 0)
return 1;
return 0;
}
int scheduleInterruptNow(struct timerData *timer) {
if (timer->active == 0)
return 0;
timer->time.it_value.tv_sec = 0;
timer->time.it_value.tv_nsec = 1;
debug_printf("[DEBUG] next timer now\n");
if (timer_settime(timer->timer, 0, &timer->time, NULL) != 0)
return 1;
return 0;
}
int scheduleInterruptIn(struct timerData *timer, struct timespec interrupt) {
if (timer->active == 0)
return 0;
timer->time.it_value.tv_nsec = interrupt.tv_nsec;
timer->time.it_value.tv_sec = interrupt.tv_sec;
debug_printf("[DEBUG] next timer in %llu us\n", timespecToMicroseconds(&timer->time.it_value));
if (timespecToNanoseconds(&timer->time.it_value) == 0)
return scheduleInterruptNow(timer);
if (timer_settime(timer->timer, 0, &timer->time, NULL) != 0)
return 1;
return 0;
}
int scheduleNextInterrupt(struct timerData *timer) {
static struct timespec nextPlannedInterrupt;
static struct timespec currentTime;
if (timer->active == 0)
return 0;
clock_gettime(CLOCK_REALTIME, ¤tTime);
timespecAdd(&nextPlannedInterrupt, &_callback_data.lastInterrupt, &timer->samplingInterval);
timespecSub(&timer->time.it_value, &nextPlannedInterrupt, ¤tTime);
debug_printf("[DEBUG] next timer in %llu ns\n", timespecToNanoseconds(&timer->time.it_value));
if (timespecToNanoseconds(&timer->time.it_value) == 0)
return scheduleInterruptNow(timer);
if (timer_settime(timer->timer, 0, &timer->time, NULL) != 0)
return 1;
return 0;
}
int startTimer(struct timerData *timer) {
//If sampling interval is 0, no need for a timer
if (timer->samplingInterval.tv_sec == 0 && timer->samplingInterval.tv_nsec == 0)
return 0;
//If already active, abort
if (timer->active)
goto start_error;
//Setup SIGNAL action
if (sigfillset(&timer->signalAction.sa_mask) != 0)
goto start_error;
timer->signalAction.sa_flags = SA_RESTART;
timer->signalAction.sa_handler = &timerCallback;
if (sigaction(SIGALRM, &timer->signalAction, &timer->signalOldAction) != 0)
goto start_error;
if (timer_create(CLOCK_REALTIME, NULL, &timer->timer) != 0)
goto start_error;
timer->active = 1;
return 0;
start_error:
return -1;
}
int stopTimer(struct timerData *timer) {
if (timer->active == 0)
return 0;
if (timer_delete(timer->timer) != 0)
goto stop_error;
if (sigaction(SIGALRM, &timer->signalOldAction, NULL) != 0)
goto stop_error;
timer->active = 0;
return 0;
stop_error:
return -1;
}
int main(int const argc, char **argv) {
FILE *output = NULL;
char **argsStart = NULL;
char *pmuArg = NULL;
bool verboseOutput = 0;
bool coreIsolation = 0;
bool randomize = 0;
unsigned int *onlineCPUs = NULL;
unsigned int nCPUs = 0;
double samplingFrequency = 1000;
int rr = 0;
int fifo = 0;
static struct option const long_options[] = {
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{"randomize", no_argument, 0, 'r'},
{"core-isolation", no_argument, 0, 'i'},
{"pmu-arg", required_argument, 0, 'p'},
{"fifo", required_argument, 0, 'x'},
{"rr", required_argument, 0, 'y'},
{"frequency", required_argument, 0, 'f'},
{"output", required_argument, 0, 'o'},
{0, 0, 0, 0}
};
static char const * short_options = "hvf:o:p:r";
while (1) {
char *endptr;
int c;
int option_index = 0;
size_t len = 0; (void) len;
unsigned int aLen;
c = getopt_long (argc, argv, short_options, long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 0:
break;
case 'h':
help(argv[0],0, NULL);
return 0;
case 'p':
aLen = strlen(optarg);
pmuArg = malloc((aLen + 1) * sizeof(char));
memset(pmuArg, '\0', aLen + 1);
memcpy(pmuArg, optarg, aLen);
break;
case 'x':
fifo = strtol(optarg, &endptr, 10);
if (endptr == optarg || fifo < 1 || fifo > 99) {
help(argv[0], c, optarg);
return 1;
}
break;
case 'y':
rr = strtol(optarg, &endptr, 10);
if (endptr == optarg || rr < 1 || rr > 99) {
help(argv[0], c, optarg);
return 1;
}
break;
case 'f':
samplingFrequency = strtod(optarg, &endptr);
if (endptr == optarg) {
help(argv[0], c, optarg);
return 1;
}
break;
case 'o':
len = strlen(optarg);
if (strlen(optarg) == 0) {
help(argv[0], c ,optarg);
return 1;
}
output = fopen(optarg, "w+");
if (output == NULL) {
help(argv[0], c, optarg);
return 1;
}
break;
case 'r':
randomize = true;
break;
case 'i':
coreIsolation = true;
break;
case 'v':
verboseOutput = true;
break;
default:
abort();
}
}
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--") == 0 && (i + 1) < argc) {
argsStart = &argv[i + 1];
break;
}
}
if (argsStart == NULL) {
help(argv[0], ' ', "no command specified");
return 1;
}
if (rr != 0)
fifo = 0;
int prio = rr + fifo;
struct sched_param param = {};
int useSched = SCHED_RR;
param.sched_priority = prio;
if (fifo != 0) {
useSched = SCHED_FIFO;
}
int ret = 0; // this application return code
long rp = 0; // ptrace return code
//Init PMU
if (pmuInit(pmuArg) != 0) {
goto pmuError;
}
if (pmuArg != NULL) {
free(pmuArg);
}
pid_t samplingTarget = 0;
pid_t rootIntrTarget = 0;
int intrStatus = 0;
struct VMMaps processMaps = {};
//Set scheduler if one was chosen
if (prio != 0) {
if (sched_setscheduler(0, useSched, ¶m) != 0) {
fprintf(stderr, "ERROR: (%d) could not set scheduler %d with priority %d\n", errno, useSched, prio);
ret = 1; goto exit;
}
}
//Core isolation feature
if (coreIsolation) {
nCPUs = getOnlineCPUIds(&onlineCPUs);
if (nCPUs == 1 && verboseOutput) {
fprintf(stdout, "[VERBOSE] CPU isolation does not work on a single core system\n");
}
if (nCPUs > 0) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(onlineCPUs[nCPUs -1], &mask);
if (sched_setaffinity(0, sizeof(mask), &mask) == -1) {
fprintf(stderr, "ERROR: could not set cpu mask for sampler\n");
ret = 1; goto exit;
}
} else {
fprintf(stderr, "ERROR: no online cpu cores were detected\n");
ret = 1; goto exit;
}
}
//Fork Process
do {
samplingTarget = fork();
} while (samplingTarget == -1 && errno == EAGAIN);
if (samplingTarget == -1) {
fprintf(stderr, "ERROR: could not fork!\n");
ret = 1; goto exit;
}
//Enable PTRACE of forked process and replace it with target application
if (samplingTarget == 0) {
//Set scheduler for sampling target
if (prio != 0) {
if (sched_setscheduler(0, useSched, ¶m) != 0) {
fprintf(stderr, "ERROR: (%d) could not set scheduler %d with priority %d\n", errno, useSched, prio);
return 1;
}
}
//Core isolation feature
if (coreIsolation) {
if (nCPUs > 1) {
cpu_set_t mask;
CPU_ZERO(&mask);
for (long i = 0; i < nCPUs - 1; i++) {
CPU_SET(onlineCPUs[i], &mask);
}
if (sched_setaffinity(0, sizeof(mask), &mask) == -1) {
fprintf(stderr, "ERROR: could not set cpu mask for target\n");
return 1;
}
}
}
if (ptrace(PTRACE_TRACEME, NULL, NULL, NULL) == -1) {
fprintf(stderr,"ERROR: ptrace traceme failed!\n");
return 1;
}
if (execvp(argsStart[0], argsStart) != 0) {
fprintf(stderr, "ERROR: failed to execute");
for (unsigned int i = 0; argsStart[i] != NULL; i++) {
fprintf(stderr, " %s", argsStart[i]);
}
fprintf(stderr,"\n");
return 1;
}
}
//Saving the sampling target as root process
tasks.root = samplingTarget;
//PTRACE sends SIGTRACE to a newly ptraced process (in this case our target),
//which we are waiting for
do {
rootIntrTarget = waitpid(samplingTarget, &intrStatus, __WALL);
} while (rootIntrTarget == -1 && errno == EINTR);
//If something went wrong, break here
if (WIFEXITED(intrStatus)) {
fprintf(stderr,"ERROR: unexpected process termination\n");
ret = 2; goto exit;
}
//The PID of the target must matched with that one which we waited for
if (samplingTarget != rootIntrTarget) {
fprintf(stderr, "ERROR: unexpected pid stopped\n");
ret = 2; goto exitWithTarget;
}
//Set PTRACE options to trace thread creation, target kills and exits
if (ptrace(PTRACE_SETOPTIONS, samplingTarget, NULL, PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXIT | PTRACE_O_EXITKILL) == -1) {
fprintf(stderr, "ERROR: Could not set ptrace options!\n");
ret = 1; goto exitWithTarget;
}
//Check if we can read the process virtual memory maps
struct VMMaps targetMap = {};
getProcessVMMaps(&targetMap, samplingTarget, 1);
if (targetMap.count == 0) {
fprintf(stderr, "ERROR: could not detect process vmmap\n");
ret = 1; goto exitWithTarget;
}
#ifdef DEBUG
dumpVMMaps("[DEBUG] VMMap ", targetMap);
#endif
freeVMMaps(&targetMap);
//Leave space for the profile header
if (output != NULL) {
// Leave place for Magic Number, Wall Time, Latency Time, Samples, PMU Data Size, VMMap Count
fseek(output, 3 * sizeof(uint32_t) + 3 * sizeof(uint64_t), SEEK_SET);
}
//Necessary structs for reading out the programm counter
static struct user_regs_struct regs = {};
static struct iovec rvec = { .iov_base = ®s, .iov_len = sizeof(regs) };
//Setting the target pid for the timer callback to send signals to
_callback_data.tid = samplingTarget;
//Add the target in our task structure, which will keep track of it and its threads
if (addTask(samplingTarget)) {
fprintf(stderr, "ERROR: could not add %d internal task structure\n", samplingTarget);
ret = 1; goto exitWithTarget;
}
//statistics
uint64_t samples = 0;
uint64_t interrupts = 0;
//timer data
struct timerData timer = {};
struct timespec timeDiff = {};
struct timespec currentTime = {};
frequencyToTimespec(&timer.samplingInterval, samplingFrequency);
struct timespec samplerStartTime = {};
clock_gettime(CLOCK_REALTIME, &samplerStartTime);
#ifndef ESTIMATE_LATENCY
struct timespec latencyStartTime = {};
struct timespec totalLatencyWallTime = {};
#else
clock_t latencyCpuStartTime = clock();
#endif
struct timespec sampleWallTime = {};
uint32_t const sizePMUData = pmuDataSize();
struct PMUData *samplePMUData = malloc(sizePMUData);
pmuRead(samplePMUData);
uint64_t sampleTime = 0;
uint64_t cputime;
if (startTimer(&timer) != 0) {
fprintf(stderr, "ERROR: could not start sampling timer\n");
ret = 1; goto exitWithTarget;
}
// first sample as soon as possible
if (randomize) {
srandom(time(NULL));
scheduleInterruptIn(&timer, NanosecondsToTimespec(timespecToNanoseconds(&timer.samplingInterval) * ((double) random() / RAND_MAX)));
} else {
scheduleInterruptNow(&timer);
}
long r;
do {
r = ptrace(PTRACE_CONT, samplingTarget, NULL, NULL);
} while (r == -1L && (errno == EBUSY || errno == EFAULT || errno == ESRCH));
do {
bool groupStop = false;
unsigned int stopCount = 0;
while(tasks.count > 0) {
int status;
int signal;
pid_t intrTarget;
do {
intrTarget = waitpid(-1, &status, __WALL);
} while (intrTarget == -1 && errno == EAGAIN);
if (WIFEXITED(status)) {
if (tasks.count == 1 || intrTarget == samplingTarget) {
debug_printf("[%d] last tracee died\n", intrTarget);
goto exitSampler;
} else {
if (removeTask(intrTarget)) {
fprintf(stderr, "ERROR: could not remove task %d from internal structure\n", intrTarget);
ret = 1; goto exitWithTarget;
}
debug_printf("[%d] tracee died\n", intrTarget);
if (groupStop && stopCount >= tasks.count) {
// We waited for this thread to stop
// but it died, so grab that sample
break;
}
continue;
}
}
if (!WIFSTOPPED(status)) {
fprintf(stderr, "unexpected process state of tid %d\n", intrTarget);
ret = 1; goto exitWithTarget;
}
signal = WSTOPSIG(status);
if (signal == TRACEE_INTERUPT_SIGNAL && !groupStop) {
debug_printf("[%d] initiate group stop\n", intrTarget);
groupStopNonThreadTasks();
signal = SIGSTOP;
groupStop = true;
stopCount = 0;
#ifndef ESTIMATE_LATENCY
clock_gettime(CLOCK_REALTIME, &latencyStartTime);
#endif
} else if (signal == SIGSTOP) {
signal = 0;
if (!taskExists(intrTarget)) {
debug_printf("[%d] new child detected\n", intrTarget);
if (addTask(intrTarget)) {
fprintf(stderr, "ERROR: could not add task %d to internal structure\n", intrTarget);
ret = 1; goto exitWithTarget;
}
}
if (groupStop) {
debug_printf("[%d] group stop\n", intrTarget);
if (++stopCount == tasks.count) {
break;
} else {
continue;
}
}
} else {
int eventStatus = status >> 16;
if (signal == SIGTRAP && eventStatus == PTRACE_EVENT_EXIT && isNonThreadTask(intrTarget)) {
if (isNonThreadTask(intrTarget)) {
debug_printf("[%d] non-thread tracee exits, record vmmaps\n", intrTarget);
getProcessVMMaps(&processMaps, intrTarget, 0);
}
debug_printf("[%d] tracee exits\n", intrTarget);
signal = 0;
} else if (signal == SIGTRAP && (eventStatus == PTRACE_EVENT_CLONE ||
eventStatus == PTRACE_EVENT_FORK ||
eventStatus == PTRACE_EVENT_VFORK)) {
debug_printf("[%d] tracee event %d\n", intrTarget, status >> 16);
signal = 0;
} else {
debug_printf("[%d] untraced signal %d, with event status %d\n", intrTarget, signal, eventStatus);
interrupts++;
}
}
rp = ptrace(PTRACE_CONT, intrTarget, NULL, signal);
if (rp == -1 && errno == ESRCH) {
debug_printf("[%d] death on ptrace cont\n", intrTarget);
if (removeTask(intrTarget)) {
fprintf(stderr, "ERROR: could not remove task %d from internal structure\n", intrTarget);
ret = 1; goto exitWithTarget;
}
} else {
debug_printf("[%d] continued with signal %d\n", intrTarget, signal);
}
}
clock_gettime(CLOCK_REALTIME, &sampleWallTime);
#ifndef PC_ONLY
pmuRead(samplePMUData);
debug_printf("[sample] PMU Data Read\n");
#endif
unsigned int i = 0;
while (i < tasks.count) {
rp = ptrace(PTRACE_GETREGSET, tasks.tasks[i].tid, NT_PRSTATUS, &rvec);
if (rp == -1 && errno == ESRCH) {
debug_printf("[%d] death on ptrace regs\n", tasks.tasks[i].tid);
if (removeTaskIndex(i)) {
fprintf(stderr, "ERROR: could not remove task %u from internal structure\n", tasks.tasks[i].tid);
ret = 1; goto exitWithTarget;
}
continue;
}
#ifdef __aarch64__
tasks.trace[i].pc = regs.pc;
#endif
#ifdef __riscv
tasks.trace[i].pc = regs.pc;
#endif
#ifdef __amd64__
tasks.trace[i].pc = regs.rip;
#endif
if (getCPUTimeFromSchedstat(tasks.tasks[i].schedstat, &cputime)) {
fprintf(stderr, "ERROR: could not read cputime of tid %u\n", tasks.tasks[i].tid);
ret = 1; goto exitWithTarget;
}
tasks.trace[i].cputime = cputime;
debug_printf("[%d] pc: 0x%lx, cputime: %lu\n", tasks.trace[i].tid, tasks.trace[i].pc, tasks.trace[i].cputime);
i++;
}
if (output != NULL ) {
sampleTime = timespecToMicroseconds(&sampleWallTime);
fwrite((void *) &sampleTime, sizeof(uint64_t), 1, output);
fwrite((void *) samplePMUData, sizePMUData, 1, output);
fwrite((void *) &tasks.count, sizeof(uint32_t), 1, output);
fwrite((void *) tasks.trace, sizeof(struct task), tasks.count, output);
}
samples++;
groupStop = false;
scheduleNextInterrupt(&timer);
#ifndef ESTIMATE_LATENCY
clock_gettime(CLOCK_REALTIME, ¤tTime);
timespecSub(&timeDiff, ¤tTime, &latencyStartTime);
timespecAddStore(&totalLatencyWallTime, &timeDiff);
#endif
i = 0;
while(i < tasks.count) {
rp = ptrace(PTRACE_CONT, tasks.tasks[i].tid, NULL, NULL);
if (rp == -1 && errno == ESRCH) {
debug_printf("[%d] death on ptrace cont after sample\n", tasks.tasks[i].tid);
if (removeTaskIndex(i)) {
fprintf(stderr, "ERROR: could not remove task %u from internal structure\n", tasks.tasks[i].tid);
ret = 1; goto exitWithTarget;
}
}
i++;
}
} while(tasks.count > 0);
exitSampler: ;
#ifndef ESTIMATE_LATENCY
uint64_t totalWallLatencyUs = timespecToMicroseconds(&totalLatencyWallTime);
#else
uint64_t totalWallLatencyUs = (clock() - latencyCpuStartTime) * 1000000 / CLOCKS_PER_SEC;
#endif
clock_gettime(CLOCK_REALTIME, ¤tTime);
timespecSub(&timeDiff, ¤tTime, &samplerStartTime);
uint64_t totalWallTimeUs = timespecToMicroseconds(&timeDiff);
if (stopTimer(&timer) != 0) {
fprintf(stderr, "Could not stop sampling timer\n");
ret = 1; goto exit;
}
if (processMaps.count == 0) {
fprintf(stderr, "No process map was read, process exit was not reported!\n");
ret = 1; goto exit;
}
#ifdef DEBUG
dumpVMMaps("[DEBUG] Final VMMap ", processMaps);
#endif
if (output != NULL) {
//Write VMMap
fwrite((void *) processMaps.maps, sizeof(struct VMMap), processMaps.count, output);
//HEADER
uint32_t const magic = (uint32_t) pmuWhat();
fseek(output, 0, SEEK_SET);
fwrite((void *) &magic, sizeof(uint32_t), 1, output);
fwrite((void *) &totalWallTimeUs, sizeof(uint64_t), 1, output);
fwrite((void *) &totalWallLatencyUs, sizeof(uint64_t), 1, output);
fwrite((void *) &samples, sizeof(uint64_t), 1, output);
fwrite((void *) &sizePMUData, sizeof(uint32_t), 1, output);
fwrite((void *) &processMaps.count, sizeof(uint32_t), 1, output);
}
freeVMMaps(&processMaps);
if (verboseOutput) {
printf("[VERBOSE] time : %10lu us (ideal), %10lu us (actual)\n", totalWallTimeUs - totalWallLatencyUs, totalWallTimeUs);
printf("[VERBOSE] interrupts : %10lu (total), %10lu (foreign) \n", interrupts + samples, interrupts );
printf("[VERBOSE] samples : %10llu (ideal), %10lu (actual) \n", (timespecToMicroseconds(&timer.samplingInterval) > 0) ? totalWallTimeUs / timespecToMicroseconds(&timer.samplingInterval) : 0, samples);
printf("[VERBOSE] latency : %10lu us (total), %10lu us (sample)\n", totalWallLatencyUs, (samples > 0) ? totalWallLatencyUs / samples : 0);
printf("[VERBOSE] frequency : %10.2f Hz (ideal), %10.2f Hz (actual)\n", samplingFrequency, (samples > 0) ? 1000000.0 / ((double) totalWallTimeUs / samples) : 0);
}
ret = 0; goto exit;
exitWithTarget:
kill(samplingTarget, SIGKILL);
ptrace(PTRACE_DETACH, samplingTarget, NULL, NULL, NULL);
exit:
pmuRelease();
pmuError:
if (output != NULL) {
fclose(output);
}
return ret;
}