forked from OpenCloudOS/perf-prof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.c
1371 lines (1220 loc) · 46.7 KB
/
monitor.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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <signal.h>
#define _GNU_SOURCE
#include <unistd.h>
#include <linux/perf_event.h>
#include <sys/time.h>
#include <linux/kvm.h>
#include <sys/ioctl.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#if defined(__i386__) || defined(__x86_64__)
#include <cpuid.h>
#endif
#include <linux/thread_map.h>
#include <linux/cgroup.h>
#include <trace_helpers.h>
#include <monitor.h>
#include <tep.h>
#include <timer.h>
#include <stack_helpers.h>
static int daylight_active;
struct event_poll *main_epoll = NULL;
static int perf_event_fds;
struct monitor *monitors_list = NULL;
struct monitor *monitor = NULL;
void monitor_register(struct monitor *m)
{
m->next = monitors_list;
monitors_list = m;
}
struct monitor * monitor_find(char *name)
{
struct monitor *m = monitors_list;
while(m) {
if (!strcmp(m->name, name))
return m;
m = m->next;
}
return NULL;
}
struct monitor *monitor_next(struct monitor *m)
{
if (!m)
return monitors_list;
else
return m->next;
}
int monitor_nr_instance(void)
{
int nr_ins;
nr_ins = perf_cpu_map__nr(monitor->cpus);
if (perf_cpu_map__empty(monitor->cpus))
nr_ins = perf_thread_map__nr(monitor->threads);
return nr_ins;
}
int monitor_instance_cpu(int ins)
{
return perf_cpu_map__cpu(monitor->cpus, ins);
}
int monitor_instance_thread(int ins)
{
return perf_thread_map__pid(monitor->threads, ins);
}
int monitor_instance_oncpu(void)
{
return !perf_cpu_map__empty(monitor->cpus);
}
struct monitor *current_monitor(void)
{
return monitor;
}
int main_epoll_add(int fd, unsigned int events, void *ptr, handle_event handle)
{
return event_poll__add(main_epoll, fd, events, ptr, handle);
}
int main_epoll_del(int fd)
{
return event_poll__del(main_epoll, fd);
}
/******************************************************
perf-prof argc argv
******************************************************/
struct env env = {
.trigger_freq = 1000,
.freq = 100,
.irqs_disabled = -1,
.tif_need_resched = -1,
.exclude_pid = -1,
.nr_running_min = -1,
.nr_running_max = -1,
};
static volatile bool exiting;
static volatile bool child_finished;
const char *main_program_version = PROGRAME " 0.17";
enum {
LONG_OPT_start = 500,
LONG_OPT_than,
LONG_OPT_only_than,
LONG_OPT_lower,
LONG_OPT_order_mem,
LONG_OPT_detail,
LONG_OPT_period,
};
/**
* Parses a string into a number. The number stored at @ptr is
* potentially suffixed with K, M, G, T, P, E.
*/
static unsigned long memparse(const char *ptr, char **retptr)
{
char *endptr; /* local pointer to end of parsed string */
unsigned long ret = strtoul(ptr, &endptr, 10);
switch (*endptr) {
case 'E':
case 'e':
ret <<= 10;
/* fall through */
case 'P':
case 'p':
ret <<= 10;
/* fall through */
case 'T':
case 't':
ret <<= 10;
/* fall through */
case 'G':
case 'g':
ret <<= 10;
/* fall through */
case 'M':
case 'm':
ret <<= 10;
/* fall through */
case 'K':
case 'k':
ret <<= 10;
/* fall through */
case 'B':
case 'b':
endptr++;
default:
break;
}
if (retptr)
*retptr = endptr;
return ret;
}
/**
* Parses a string into ns. The number stored at @ptr is
* potentially suffixed with s, ms, us, ns.
*/
static unsigned long nsparse(const char *ptr, char **retptr)
{
char *endptr; /* local pointer to end of parsed string */
unsigned long ret = strtoul(ptr, &endptr, 10);
unsigned long tmp = ret;
switch (*endptr) {
case 'S':
case 's':
tmp *= 1000;
endptr--;
/* fall through */
case 'M':
case 'm':
tmp *= 1000;
/* fall through */
case 'U':
case 'u':
tmp *= 1000;
/* fall through */
case 'N':
case 'n':
endptr++;
if (*endptr == 's') {
endptr++;
ret = tmp;
}
default:
break;
}
if (retptr)
*retptr = endptr;
return ret;
}
static void detail_parse(const char *s)
{
if (strcmp(s, "samecpu") == 0)
env.samecpu = true;
else if (strcmp(s, "samepid") == 0)
env.samepid = true;
else if (strcmp(s, "sametid") == 0)
env.sametid = true;
else if (strcmp(s, "samekey") == 0)
env.samekey = true;
else if (s[0] == '-')
env.before_event1 = nsparse(s+1, NULL);
else
env.after_event2 = nsparse(s, NULL);
}
static int parse_arg(int key, char *arg)
{
switch (key) {
case 'e':
env.events = realloc(env.events, (env.nr_events + 1) * sizeof(*env.events));
env.events[env.nr_events] = strdup(arg);
if (env.nr_events == 0)
env.event = env.events[0];
env.nr_events ++;
break;
case LONG_OPT_only_than:
env.only_print_greater_than = true;
// fall through
case LONG_OPT_than:
env.greater_than = nsparse(arg, NULL);
break;
case LONG_OPT_lower:
env.lower_than = nsparse(arg, NULL);
break;
case LONG_OPT_order_mem:
env.order_mem = memparse(arg, NULL);
break;
case LONG_OPT_detail:
env.detail = true;
if (arg) {
char *ss = strdup(arg);
char *sep, *s = ss;
while ((sep = strchr(s, ',')) != NULL) {
*sep = '\0';
detail_parse(s);
s = sep + 1;
}
if (*s)
detail_parse(s);
free(ss);
}
break;
case LONG_OPT_period:
env.sample_period = nsparse(arg, NULL);
break;
case 'V':
printf("%s\n", main_program_version);
exit(0);
default:
break;
}
return 0;
}
static int parse_help_cb(const struct option *opt, const char *arg, int unset)
{
help();
return 0;
}
static int parse_arg_cb(const struct option *opt, const char *arg, int unset)
{
return parse_arg(opt->short_name, (char *)arg);
}
#define OPT_BOOL_NONEG(s, l, v, h) { .type = OPTION_BOOLEAN, .short_name = (s), .long_name = (l), .value = check_vtype(v, bool *), .help = (h), .flags = PARSE_OPT_NONEG }
#define OPT_INT_NONEG(s, l, v, a, h) { .type = OPTION_INTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, int *), .argh = (a), .help = (h), .flags = PARSE_OPT_NONEG }
#define OPT_UINT_NONEG(s, l, v, a, h) { .type = OPTION_UINTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned int *), .argh = (a), .help = (h), .flags = PARSE_OPT_NONEG }
#define OPT_LONG_NONEG(s, l, v, a, h) { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .argh = (a), .help = (h), .flags = PARSE_OPT_NONEG }
#define OPT_ULONG_NONEG(s, l, v, a, h) { .type = OPTION_ULONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned long *), .argh = (a), .help = (h), .flags = PARSE_OPT_NONEG }
#define OPT_U64_NONEG(s, l, v, a, h) { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .argh = (a), .help = (h), .flags = PARSE_OPT_NONEG }
#define OPT_STRDUP_NONEG(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = check_vtype(v, char **), .argh = (a), .help = (h), .flags = PARSE_OPT_NONEG | PARSE_OPT_NOEMPTY }
#define OPT_PARSE_NONEG(s, l, v, a, h) \
{ .type = OPTION_CALLBACK, .short_name = (BUILD_BUG_ON_ZERO(s==0) + s), .long_name = (l), .value = (v), .argh = (a), .help = (h), .flags = PARSE_OPT_NONEG, .callback = (parse_arg_cb) }
#define OPT_PARSE_NOARG(s, l, v, a, h) \
{ .type = OPTION_CALLBACK, .short_name = (BUILD_BUG_ON_ZERO(s==0) + s), .long_name = (l), .value = (v), .argh = (a), .help = (h), .flags = PARSE_OPT_NONEG | PARSE_OPT_NOARG, .callback = (parse_arg_cb) }
#define OPT_PARSE_OPTARG(s, l, v, a, h) \
{ .type = OPTION_CALLBACK, .short_name = (BUILD_BUG_ON_ZERO(s==0) + s), .long_name = (l), .value = (v), .argh = (a), .help = (h), .flags = PARSE_OPT_NONEG | PARSE_OPT_OPTARG, .callback = (parse_arg_cb) }
#define OPT_INT_OPTARG(s, l, v, d, a, h) \
{ .type = OPTION_INTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, int *), .argh = (a), .defval = (intptr_t)(d), .help = (h), .flags = PARSE_OPT_NONEG | PARSE_OPT_OPTARG }
#define OPT_HELP() \
{ .type = OPTION_CALLBACK, .short_name = ('h'), .long_name = ("help"), .help = ("Give this help list"), .flags = PARSE_OPT_NONEG | PARSE_OPT_NOARG, .callback = (parse_help_cb) }
struct option main_options[] = {
OPT_GROUP("OPTION:"),
OPT_STRDUP_NONEG('C', "cpus", &env.cpumask, "CPU[-CPU],...", "Monitor the specified CPU, Dflt: all cpu"),
OPT_STRDUP_NONEG('p', "pids", &env.pids, "PID,...", "Attach to processes"),
OPT_STRDUP_NONEG('t', "tids", &env.tids, "TID,...", "Attach to threads"),
OPT_STRDUP_NONEG( 0 , "cgroups", &env.cgroups, "cgroup,...", "Attach to cgroups, support regular expression."),
OPT_INT_NONEG ('i', "interval", &env.interval, "ms", "Interval, Unit: ms"),
OPT_STRDUP_NONEG('o', "output", &env.output, "file", "Output file name"),
OPT_BOOL_NONEG ( 0 , "order", &env.order, "Order events by timestamp."),
OPT_PARSE_NONEG (LONG_OPT_order_mem, "order-mem", &env.order_mem, "Bytes", "Maximum memory used by ordering events. Unit: GB/MB/KB/*B."),
OPT_INT_NONEG ('m', "mmap-pages", &env.mmap_pages, "pages", "Number of mmap data pages and AUX area tracing mmap pages"),
OPT_LONG_NONEG ('N', "exit-N", &env.exit_n, "N", "Exit after N events have been sampled."),
OPT_BOOL_NONEG ( 0 , "tsc", &env.tsc, "Convert perf time to tsc time."),
OPT_U64_NONEG ( 0 , "tsc-offset", &env.tsc_offset, NULL, "Sum with tsc-offset to get the final tsc time."),
OPT_PARSE_NOARG ('V', "version", NULL, NULL, "Version info"),
OPT__VERBOSITY(&env.verbose),
OPT_HELP(),
OPT_GROUP("FILTER OPTION:"),
OPT_BOOL_NONEG ('G', "exclude-host", &env.exclude_host, "Monitor GUEST, exclude host"),
OPT_BOOL_NONEG ( 0 , "exclude-guest", &env.exclude_guest, "exclude guest"),
OPT_BOOL_NONEG ( 0 , "exclude-user", &env.exclude_user, "exclude user"),
OPT_BOOL_NONEG ( 0 , "exclude-kernel", &env.exclude_kernel, "exclude kernel"),
OPT_BOOLEAN_SET ( 0 , "user-callchain", &env.user_callchain, &env.user_callchain_set, "include user callchains, no- prefix to exclude"),
OPT_BOOLEAN_SET ( 0 , "kernel-callchain", &env.kernel_callchain, &env.kernel_callchain_set, "include kernel callchains, no- prefix to exclude"),
OPT_INT_OPTARG ( 0 , "irqs_disabled", &env.irqs_disabled, 1, "0|1", "ebpf, irqs disabled or not."),
OPT_INT_OPTARG ( 0 , "tif_need_resched", &env.tif_need_resched, 1, "0|1", "ebpf, TIF_NEED_RESCHED is set or not."),
OPT_INT_NONEG ( 0 , "exclude_pid", &env.exclude_pid, "PID", "ebpf, exclude pid"),
OPT_INT_NONEG ( 0 , "nr_running_min", &env.nr_running_min, NULL, "ebpf, minimum number of running processes for CPU runqueue."),
OPT_INT_NONEG ( 0 , "nr_running_max", &env.nr_running_max, NULL, "ebpf, maximum number of running processes for CPU runqueue."),
OPT_GROUP("PROFILER OPTION:"),
OPT_PARSE_NONEG ('e', "event", NULL, "EVENT,...", "Event selector. use 'perf list tracepoint' to list available tp events.\n"
" EVENT,EVENT,...\n"
" EVENT: sys:name[/filter/ATTR/ATTR/.../]\n"
" filter: ftrace filter\n"
" ATTR:\n"
" stack: sample_type PERF_SAMPLE_CALLCHAIN\n"
" max-stack=int : sample_max_stack\n"
" alias=str: event alias\n"
" top-by=EXPR: add to top, sort by this field\n"
" top-add=EXPR: add to top\n"
" comm=EXPR: top, show COMM\n"
" ptr=EXPR: kmemleak, ptr field, Dflt: ptr=ptr\n"
" size=EXPR: kmemleak, size field, Dflt: size=bytes_alloc\n"
" num=EXPR: num-dist, num field\n"
" key=EXPR: key for multiple events: top, multi-trace\n"
" untraced: multi-trace, auxiliary, no two-event analysis\n"
" trigger: multi-trace, use events to trigger interval output\n"
" vm=uuid: get the mapping from Guest vcpu to Host tid\n"
" push=[IP]:PORT: push events to the local broadcast server IP:PORT\n"
" push=chardev: push events to chardev, e.g., /dev/virtio-ports/*\n"
" push=file: push events to file\n"
" pull=[IP]:PORT: pull events from server IP:PORT\n"
" pull=chardev: pull events from chardev\n"
" pull=file: pull events from file\n"
" EXPR:\n"
" C expression. See `"PROGRAME" expr -h` for more information."
),
OPT_INT_NONEG ('F', "freq", &env.freq, NULL, "Profile at this frequency, Dflt: 100, No profile: 0"),
OPT_STRDUP_NONEG('k', "key", &env.key, "str", "Key for series events"),
OPT_STRDUP_NONEG( 0 , "filter", &env.filter, "filter", "Event filter/comm filter"),
OPT_PARSE_NONEG (LONG_OPT_period, "period", &env.sample_period, "ns", "Sample period, Unit: s/ms/us/*ns"),
OPT_STRDUP_NONEG(0, "impl", &env.impl, "impl", "Implementation of two-event analysis class. Dflt: delay.\n"
" delay: latency distribution between two events\n"
" pair: determine if two events are paired\n"
" kmemprof: profile memory allocated and freed bytes\n"
" syscalls: syscall delay\n"
" call: analyze function calls, only for nested-trace.\n"
" call-delay: call + delay, only for nested-trace."),
OPT_BOOL_NONEG ('S', "interruptible", &env.interruptible, "TASK_INTERRUPTIBLE"),
OPT_BOOL_NONEG ('D', "uninterruptible", &env.uninterruptible, "TASK_UNINTERRUPTIBLE"),
OPT_PARSE_NONEG ( LONG_OPT_than, "than", &env.greater_than, "ns", "Greater than specified time, Unit: s/ms/us/*ns/percent"),
OPT_PARSE_NONEG ( LONG_OPT_only_than, "only-than", &env.greater_than,"ns", "Only print those that are greater than the specified time."),
OPT_PARSE_NONEG ( LONG_OPT_lower, "lower", &env.lower_than, "ns", "Lower than specified time, Unit: s/ms/us/*ns"),
OPT_STRDUP_NONEG( 0 , "alloc", &env.tp_alloc, "EVENT", "Memory alloc tracepoint/kprobe/uprobe"),
OPT_STRDUP_NONEG( 0 , "free", &env.tp_free, "EVENT", "Memory free tracepoint/kprobe/uprobe"),
OPT_BOOL_NONEG ( 0 , "syscalls", &env.syscalls, "Trace syscalls"),
OPT_BOOL_NONEG ( 0 , "perins", &env.perins, "Print per instance stat"),
OPT_BOOL_NONEG ('g', "call-graph", &env.callchain, "Enable call-graph recording"),
OPT_STRDUP_NONEG( 0 , "flame-graph", &env.flame_graph, "file", "Specify the folded stack file."),
OPT_STRDUP_NONEG( 0 , "heatmap", &env.heatmap, "file", "Specify the output latency file."),
OPT_PARSE_OPTARG( LONG_OPT_detail, "detail", NULL, "-N,+N,samecpu,samepid",
"More detailed information output.\n"
"For multi-trace profiler:\n"
" -N: Before event1, print events within N nanoseconds.\n"
" +N: After event2, print events within N nanoseconds.\n"
"samecpu: Only show events with the same cpu as event1 or event2.\n"
"samepid: Only show events with the same pid as event1 or event2.\n"
"sametid: Only show events with the same tid as event1 or event2.\n"
"samekey: Only show events with the same key as event1 or event2."),
OPT_INT_NONEG ('T', "trigger", &env.trigger_freq, NULL, "Trigger Threshold, Dflt: 1000, No trigger: 0"),
OPT_BOOL_NONEG ( 0 , "test", &env.test, "Split-lock test verification"),
OPT_STRDUP_NONEG( 0 , "symbols", &env.symbols, NULL, "Maps addresses to symbol names.\n"
"Similar to pprof --symbols."),
OPT_STRDUP_NONEG('d', "device", &env.device, "device", "Block device, /dev/sdx"),
OPT_INT_NONEG ( 0 , "ldlat", &env.ldlat, "cycles", "mem-loads latency, Unit: cycles"),
OPT_BOOL_NONEG ( 0 , "overwrite", &env.overwrite, "use overwrite mode"),
OPT_BOOL_NONEG ( 0 , "spte", &env.spte, "kvmmmu: enable kvmmmu:kvm_mmu_set_spte"),
OPT_BOOL_NONEG ( 0 , "mmio", &env.mmio, "kvmmmu: enable kvmmmu:mark_mmio_spte"),
OPT_BOOL_NONEG ( 0 , "only-comm", &env.only_comm, "top: only show comm but not key"),
OPT_BOOL_NONEG ( 0 , "cycle", &env.cycle, "multi-trace: event cycle, from the last one back to the first."),
OPT_END()
};
const char * const main_usage[] = {
PROGRAME " profiler [PROFILER OPTION...] [help] [cmd [args...]]",
PROGRAME " --symbols /path/to/bin",
"",
"Profiling based on perf_event and ebpf",
NULL
};
void help(void)
{
int argc = 2;
const char *argv[] = {PROGRAME, "--help"};
const char * const *usagestr = main_usage;
struct monitor *m = monitor;
if (m) {
if (monitor->argv && monitor->desc) {
argc = 0;
while (monitor->argv[argc++] != NULL);
parse_options(argc - 1, monitor->argv, main_options, monitor->desc, PARSE_OPT_INTERNAL_HELP_NO_ORDER);
} else
parse_options(argc, argv, main_options, main_usage, PARSE_OPT_INTERNAL_HELP_NO_ORDER);
}
fprintf(stderr, "\n Usage: %s\n", *usagestr++);
while (*usagestr && **usagestr)
fprintf(stderr, " or: %s\n", *usagestr++);
while (*usagestr) {
fprintf(stderr, "%s%s\n",
**usagestr ? " " : "",
*usagestr);
usagestr++;
}
fprintf(stderr, "\n Available Profilers:\n");
while((m = monitor_next(m))) {
fprintf(stderr, " %-20s", m->name);
if (m->desc && m->desc[2] && m->desc[2][0])
fprintf(stderr, " %s\n", m->desc[2]);
else
fprintf(stderr, "\n");
}
fprintf(stderr, "\n See '%s profiler -h' for more information on a specific profiler.\n\n", PROGRAME);
exit(129);
}
static void disable_help(void)
{
struct option *opts = main_options;
for (; opts->type != OPTION_END; opts++) {
if (opts->short_name == 'h') {
opts->short_name = 0;
opts->long_name = "disable-help";
}
}
}
#ifndef CONFIG_LIBBPF
static const char *LIBBPF_BUILD = "NO CONFIG_LIBBPF=y";
#endif
static int parse_main_options(int argc, char *argv[])
{
bool stop_at_non_option = true;
bool dashdash = false;
#ifndef CONFIG_LIBBPF
set_option_nobuild(main_options, 0, "irqs_disabled", LIBBPF_BUILD, true);
set_option_nobuild(main_options, 0, "tif_need_resched", LIBBPF_BUILD, true);
set_option_nobuild(main_options, 0, "exclude_pid", LIBBPF_BUILD, true);
set_option_nobuild(main_options, 0, "nr_running_min", LIBBPF_BUILD, true);
set_option_nobuild(main_options, 0, "nr_running_max", LIBBPF_BUILD, true);
#endif
while (argc > 0) {
argc = parse_options(argc, (const char **)argv, main_options, main_usage,
PARSE_OPT_NO_INTERNAL_HELP | PARSE_OPT_KEEP_DASHDASH |
(stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION :
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN));
if (argc && argv[0][0] != '-' && argv[0][1] != '-') {
struct monitor *m = monitor_find(argv[0]);
if (m != NULL) {
env.help_monitor = monitor;
monitor = m;
continue;
} else if (stop_at_non_option) {
stop_at_non_option = false;
disable_help();
continue;
}
}
// --
if (argc && argv[0][0] == '-' && argv[0][1] == '-') {
argc--;
memmove(argv, argv + 1, argc * sizeof(argv[0]));
argv[argc] = NULL;
dashdash = true;
}
break;
}
if (monitor == NULL && env.symbols == NULL)
help();
if (!dashdash) {
if (monitor && monitor->argc_init)
argc = monitor->argc_init(argc, argv);
else if (argc && env.verbose > 0) {
int i;
printf("Unparsed options:");
for (i = 0; i < argc; i ++)
printf(" %s", argv[i]);
printf("\n");
}
}
return argc;
}
static void sig_handler(int sig)
{
if (sig == SIGCHLD)
child_finished = 1;
exiting = 1;
}
static void sigusr2_handler(int sig)
{
static unsigned long utime = 0, stime = 0;
static struct timeval tv;
static bool init = 0;
struct rusage usage;
if (init == 0) {
tzset(); /* Now 'timezone' global is populated. */
daylight_active = daylight;
gettimeofday(&tv, NULL);
if (getrusage(RUSAGE_SELF, &usage) == 0) {
utime = usage.ru_utime.tv_sec * 1000000UL + usage.ru_utime.tv_usec;
stime = usage.ru_stime.tv_sec * 1000000UL + usage.ru_stime.tv_usec;
} else
return ;
init = 1;
return ;
}
if (getrusage(RUSAGE_SELF, &usage) == 0) {
unsigned long user, sys, us;
struct timeval t;
char timebuff[64];
struct tm result;
gettimeofday(&t, NULL);
us = t.tv_sec * 1000000UL + t.tv_usec - tv.tv_sec * 1000000UL - tv.tv_usec;
user = usage.ru_utime.tv_sec * 1000000UL + usage.ru_utime.tv_usec - utime;
sys = usage.ru_stime.tv_sec * 1000000UL + usage.ru_stime.tv_usec - stime;
nolocks_localtime(&result, t.tv_sec, timezone, daylight_active);
strftime(timebuff, sizeof(timebuff), "%Y-%m-%d %H:%M:%S", &result);
printf("%s.%06u CPU %%usr %.2f %%sys %.2f MAXRSS %luk\n", timebuff, (unsigned int)tv.tv_usec,
user*100.0/us, sys*100.0/us, usage.ru_maxrss);
utime += user;
stime += sys;
tv = t;
}
}
int get_present_cpus(void)
{
struct perf_cpu_map *cpumap = NULL;
FILE *f;
int cpus;
f = fopen("/sys/devices/system/cpu/present", "r");
if (!f)
f = fopen("/sys/devices/system/cpu/online", "r");
cpumap = perf_cpu_map__read(f);
fclose(f);
cpus = perf_cpu_map__max(cpumap);
perf_cpu_map__put(cpumap);
return cpus + 1;
}
int get_tsc_khz(void)
{
int kvm, vm, vcpu;
int tsc_khz = 0;
kvm = open("/dev/kvm", O_RDWR);
if (kvm < 0) {
fprintf(stderr, "open kvm failed\n");
return 0;
}
vm = ioctl(kvm, KVM_CREATE_VM, 0);
if (kvm < 0) {
fprintf(stderr, "create vm failed\n");
close(kvm);
return 0;
}
vcpu = ioctl(vm, KVM_CREATE_VCPU, 0);
if (vcpu < 0) {
fprintf(stderr, "create vm failed\n");
close(vm);
close(kvm);
return 0;
}
if (ioctl(vm, KVM_CHECK_EXTENSION, KVM_CAP_GET_TSC_KHZ) == 1) {
tsc_khz = ioctl(vcpu, KVM_GET_TSC_KHZ, 0);
if (tsc_khz < 0)
tsc_khz = 0;
}
close(vcpu);
close(vm);
close(kvm);
return tsc_khz;
}
int get_cpuinfo(struct cpuinfo_x86 *info)
{
#if defined(__i386__) || defined(__x86_64__)
__u32 eax, ebx, ecx, edx;
eax = ebx = ecx = edx = 0;
__get_cpuid(0, &eax, &ebx, &ecx, &edx);
if (info)
memset(info, 0, sizeof(*info));
if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) {
if (info && eax > 1) {
int family, model, stepping;
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
family = (eax >> 8) & 0xf;
model = (eax >> 4) & 0xf;
stepping = eax & 0xf;
if (family == 0x6 || family == 0xf)
model += ((eax > 16) & 0xf) << 4;
if (family == 0xf)
family += (eax >> 20) & 0xff;
info->vendor = X86_VENDOR_INTEL;
info->family = family;
info->model = model;
info->stepping = stepping;
}
return X86_VENDOR_INTEL;
} else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) {
if (info && eax > 1) {
int family, model, stepping;
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
family = (eax >> 8) & 0xf;
model = (eax >> 4) & 0xf;
stepping = eax & 0xf;
if (family == 0xf) {
model += ((eax > 16) & 0xf) << 4;
family += (eax >> 20) & 0xff;
}
info->vendor = X86_VENDOR_AMD;
info->family = family;
info->model = model;
info->stepping = stepping;
}
return X86_VENDOR_AMD;
} else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) {
return X86_VENDOR_HYGON;
} else
return -1;
#else
if (info)
memset(info, 0, sizeof(*info));
return -1;
#endif
}
#define CPUID_EXT_HYPERVISOR (1U << 31)
int in_guest(void)
{
#if defined(__i386__) || defined(__x86_64__)
__u32 eax, ebx, ecx, edx;
eax = ebx = ecx = edx = 0;
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
return !!(ecx & CPUID_EXT_HYPERVISOR);
#else
return 0;
#endif
}
int callchain_flags(int dflt_flags)
{
int flags = dflt_flags;
if (env.user_callchain_set) {
if (env.user_callchain)
flags |= CALLCHAIN_USER;
else
flags &= ~CALLCHAIN_USER;
}
if (env.kernel_callchain_set) {
if (env.kernel_callchain)
flags |= CALLCHAIN_KERNEL;
else
flags &= ~CALLCHAIN_KERNEL;
}
return flags;
}
int exclude_callchain_user(int dflt_flags)
{
int flags = callchain_flags(dflt_flags);
return flags & CALLCHAIN_USER ? 0 : 1;
}
int exclude_callchain_kernel(int dflt_flags)
{
int flags = callchain_flags(dflt_flags);
return flags & CALLCHAIN_KERNEL ? 0 : 1;
}
struct workload {
int cork_fd;
pid_t pid;
};
static int workload_prepare(struct workload *workload, char *argv[])
{
int child_ready_pipe[2], go_pipe[2];
char bf;
if (pipe(child_ready_pipe) < 0) {
perror("failed to create 'ready' pipe");
return -1;
}
if (pipe(go_pipe) < 0) {
perror("failed to create 'go' pipe");
goto out_close_ready_pipe;
}
workload->pid = fork();
if (workload->pid < 0) {
perror("failed to fork");
goto out_close_pipes;
}
if (!workload->pid) {
int ret;
signal(SIGTERM, SIG_DFL);
close(child_ready_pipe[0]);
close(go_pipe[1]);
fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
/*
* Change the name of this process not to confuse --exclude-perf users
* that sees 'perf' in the window up to the execvp() and thinks that
* perf samples are not being excluded.
*/
prctl(PR_SET_NAME, "perf-exec");
/*
* Tell the parent we're ready to go
*/
close(child_ready_pipe[1]);
/*
* Wait until the parent tells us to go.
*/
ret = read(go_pipe[0], &bf, 1);
/*
* The parent will ask for the execvp() to be performed by
* writing exactly one byte, in workload.cork_fd, usually via
* evlist__start_workload().
*
* For cancelling the workload without actually running it,
* the parent will just close workload.cork_fd, without writing
* anything, i.e. read will return zero and we just exit()
* here.
*/
if (ret != 1) {
if (ret == -1)
perror("unable to read pipe");
exit(ret);
}
execvp(argv[0], (char **)argv);
exit(-1);
}
close(child_ready_pipe[1]);
close(go_pipe[0]);
/*
* wait for child to settle
*/
if (read(child_ready_pipe[0], &bf, 1) == -1) {
perror("unable to read pipe");
goto out_close_pipes;
}
fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
workload->cork_fd = go_pipe[1];
close(child_ready_pipe[0]);
return 0;
out_close_pipes:
close(go_pipe[0]);
close(go_pipe[1]);
out_close_ready_pipe:
close(child_ready_pipe[0]);
close(child_ready_pipe[1]);
return -1;
}
static int workload_start(struct workload *workload)
{
if (workload->cork_fd > 0) {
char bf = 0;
int ret;
/*
* Remove the cork, let it rip!
*/
ret = write(workload->cork_fd, &bf, 1);
if (ret < 0)
perror("unable to write to pipe");
close(workload->cork_fd);
return ret;
}
return 0;
}
void print_time(FILE *fp)
{
char timebuff[64];
struct timeval tv;
struct tm *result;
gettimeofday(&tv, NULL);
result = localtime(&tv.tv_sec);
daylight_active = result->tm_isdst;
strftime(timebuff, sizeof(timebuff), "%Y-%m-%d %H:%M:%S", result);
fprintf(fp, "%s.%06u ", timebuff, (unsigned int)tv.tv_usec);
}
void print_lost_fn(union perf_event *event, int ins)
{
int oncpu;
if (env.exit_n) return;
oncpu = monitor_instance_oncpu();
print_time(stderr);
fprintf(stderr, "lost %llu events on %s #%d\n", event->lost.lost,
oncpu ? "CPU" : "thread",
oncpu ? monitor_instance_cpu(ins) : monitor_instance_thread(ins));
}
static void print_fork_exit_fn(union perf_event *event, int ins, int exit)
{
if (env.verbose >= VERBOSE_ALL) {
int oncpu = monitor_instance_oncpu();
print_time(stderr);
fprintf(stderr, "%s ppid %u ptid %u pid %u tid %u on %s #%d\n",
exit ? "exit" : "fork",
event->fork.ppid, event->fork.ptid,
event->fork.pid, event->fork.tid,
oncpu ? "CPU" : "thread",
oncpu ? monitor_instance_cpu(ins) : monitor_instance_thread(ins));
}
}
static void print_comm_fn(union perf_event *event, int ins)
{
if (env.verbose >= VERBOSE_ALL) {
int oncpu = monitor_instance_oncpu();
print_time(stderr);
fprintf(stderr, "comm pid %u tid %u %s on %s #%d\n",
event->comm.pid, event->comm.tid,
event->comm.comm,
oncpu ? "CPU" : "thread",
oncpu ? monitor_instance_cpu(ins) : monitor_instance_thread(ins));
}
}
static void print_throttle_unthrottle_fn(union perf_event *event, int ins, int unthrottle)
{
if (env.verbose >= VERBOSE_NOTICE) {
int oncpu = monitor_instance_oncpu();
print_time(stderr);
fprintf(stderr, "%llu.%06llu: %s events on %s #%d\n",
event->throttle.time / NSEC_PER_SEC, (event->throttle.time % NSEC_PER_SEC)/1000,
unthrottle ? "unthrottle" : "throttle",
oncpu ? "CPU" : "thread",
oncpu ? monitor_instance_cpu(ins) : monitor_instance_thread(ins));
}
}
static void print_context_switch_fn(union perf_event *event, int ins)
{
if (env.verbose >= VERBOSE_ALL) {
int oncpu = monitor_instance_oncpu();
print_time(stderr);
fprintf(stderr, "switch on %s #%d\n", oncpu ? "CPU" : "thread",
oncpu ? monitor_instance_cpu(ins) : monitor_instance_thread(ins));
}
}
static void print_context_switch_cpu_fn(union perf_event *event, int ins)
{
if (env.verbose >= VERBOSE_ALL) {
int oncpu = monitor_instance_oncpu();
print_time(stderr);
fprintf(stderr, "switch next pid %u tid %u on %s #%d\n",
event->context_switch.next_prev_pid, event->context_switch.next_prev_tid,
oncpu ? "CPU" : "thread",
oncpu ? monitor_instance_cpu(ins) : monitor_instance_thread(ins));
}
}
int perf_event_process_record(union perf_event *event, int instance, bool writable, bool converted)
{
static long sampled_events = 0;
switch (event->header.type) {
case PERF_RECORD_LOST:
if (monitor->lost)
monitor->lost(event, instance, 0);
else
print_lost_fn(event, instance);
break;
case PERF_RECORD_FORK:
if (monitor->fork)
monitor->fork(event, instance);
else
print_fork_exit_fn(event, instance, 0);
break;
case PERF_RECORD_COMM:
if (monitor->comm)
monitor->comm(event, instance);
else
print_comm_fn(event, instance);
break;
case PERF_RECORD_EXIT:
if (monitor->exit)
monitor->exit(event, instance);
else
print_fork_exit_fn(event, instance, 1);
break;
case PERF_RECORD_THROTTLE:
if (monitor->throttle)
monitor->throttle(event, instance);
else
print_throttle_unthrottle_fn(event, instance, 0);
break;
case PERF_RECORD_UNTHROTTLE:
if (monitor->unthrottle)
monitor->unthrottle(event, instance);
else
print_throttle_unthrottle_fn(event, instance, 1);
break;
case PERF_RECORD_SAMPLE:
if (likely(!env.exit_n) || ++sampled_events <= env.exit_n) {
if (monitor->sample)
monitor->sample(unlikely(converted) ? event : perf_event_convert(event, writable), instance);
}
if (unlikely(env.exit_n) && sampled_events >= env.exit_n)
exiting = 1;
break;
case PERF_RECORD_SWITCH:
if (monitor->context_switch)
monitor->context_switch(event, instance);
else
print_context_switch_fn(event, instance);
break;
case PERF_RECORD_SWITCH_CPU_WIDE: