forked from OpenCloudOS/perf-prof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-trace.c
1756 lines (1561 loc) · 57.2 KB
/
multi-trace.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 <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <linux/list.h>
#include <linux/string.h>
#include <linux/zalloc.h>
#include <linux/strlist.h>
#include <monitor.h>
#include <tep.h>
#include <trace_helpers.h>
#include <stack_helpers.h>
#include <two-event.h>
static profiler *base_profiler;
static profiler rundelay;
struct timeline_node {
struct rb_node timeline_node;
u64 time;
struct rb_node key_node;
u64 key;
struct tp *tp;
struct tp *tp1; // nested-trace, -e A,A_ret, A's tp.
u32 unneeded : 1,
need_find_prev : 1,
need_backup : 1,
need_remove_from_backup : 1,
maybe_unpaired : 1;
u64 seq;
struct list_head needed;
struct list_head pending;
union perf_event *event;
};
struct timeline_stat {
u64 new;
u64 delete;
u64 unneeded;
u64 pending;
u64 mem_bytes;
u64 unneeded_bytes;
u64 pending_bytes;
} tl_stat;
struct __dup_stat {
u64 nr_samples;
u64 nr_free;
} dup_stat;
struct __backup_stat {
u64 new;
u64 delete;
u64 mem_bytes;
} backup_stat;
static struct multi_trace_ctx {
int nr_ins;
int nr_list;
struct tp_list **tp_list;
struct two_event_impl *impl;
struct two_event_class *class;
struct rblist backup;
struct rblist timeline;
struct list_head needed_list; // need_timeline
struct list_head pending_list; // need_timeline
bool need_timeline;
bool nested;
bool impl_based_on_call;
u64 recent_time; // The most recent time for all known events.
u64 recent_lost_time;
u64 event_handled;
u64 sched_wakeup_unnecessary;
struct callchain_ctx *cc;
struct perf_evlist *evlist;
struct perf_thread_map *thread_map; // profiler rundelay
bool comm; // profiler rundelay
struct env *env;
} ctx;
static struct timeline_node *multi_trace_first_pending(struct timeline_node *tail);
static int perf_event_backup_node_cmp(struct rb_node *rbn, const void *entry)
{
struct timeline_node *b = container_of(rbn, struct timeline_node, key_node);
const struct timeline_node *e = entry;
if (b->key > e->key)
return 1;
else if (b->key < e->key)
return -1;
else
return 0;
}
static int perf_event_backup_node_find(const void *entry, const struct rb_node *rbn)
{
struct timeline_node *b = container_of(rbn, struct timeline_node, key_node);
const struct timeline_node *e = entry;
if (b->key > e->key)
return -1;
else if (b->key < e->key)
return 1;
else
return 0;
}
static struct rb_node *perf_event_backup_node_new(struct rblist *rlist, const void *new_entry)
{
if (ctx.need_timeline) {
struct timeline_node *b = (void *)new_entry;
/*
* With --order enabled, events are backed up in chronological order. Therefore, it
* can be directly added to the end of the queue `needed_list' without reordering.
**/
list_add_tail(&b->needed, &ctx.needed_list);
RB_CLEAR_NODE(&b->key_node);
return &b->key_node;
} else {
const struct timeline_node *e = new_entry;
union perf_event *event = e->event;
union perf_event *new_event = base_profiler->dup ? event : memdup(event, event->header.size);
struct timeline_node *b = malloc(sizeof(*b));
if (b && new_event) {
b->time = e->time;
b->key = e->key;
b->tp = e->tp;
b->tp1 = e->tp1;
b->unneeded = 0;
b->need_find_prev = e->need_find_prev;
b->need_backup = e->need_backup;
b->need_remove_from_backup = e->need_remove_from_backup;
b->maybe_unpaired = 0;
b->seq = e->seq;
b->event = new_event;
RB_CLEAR_NODE(&b->timeline_node);
RB_CLEAR_NODE(&b->key_node);
INIT_LIST_HEAD(&b->needed);
INIT_LIST_HEAD(&b->pending);
backup_stat.new ++;
backup_stat.mem_bytes += event->header.size;
return &b->key_node;
} else
return NULL;
}
}
static void perf_event_backup_node_delete(struct rblist *rblist, struct rb_node *rb_node)
{
struct timeline_node *b = container_of(rb_node, struct timeline_node, key_node);
if (ctx.need_timeline) {
b->unneeded = 1;
list_del_init(&b->needed);
tl_stat.unneeded ++;
tl_stat.unneeded_bytes += b->event->header.size;
} else {
backup_stat.delete ++;
backup_stat.mem_bytes -= b->event->header.size;
free(b->event);
free(b);
}
}
static int timeline_node_cmp(struct rb_node *rbn, const void *entry)
{
struct timeline_node *b = container_of(rbn, struct timeline_node, timeline_node);
const struct timeline_node *e = entry;
if (b->time > e->time)
return 1;
else if (b->time < e->time)
return -1;
if (b->key > e->key)
return 1;
else if (b->key < e->key)
return -1;
// The time and key values of different events may be equal, so they are sorted by seq.
if (b->seq > e->seq)
return 1;
else if (b->seq < e->seq)
return -1;
return 0;
}
static struct rb_node *timeline_node_new(struct rblist *rlist, const void *new_entry)
{
const struct timeline_node *e = new_entry;
union perf_event *event = e->event;
union perf_event *new_event = base_profiler->dup ? event : memdup(event, event->header.size);
struct timeline_node *b = malloc(sizeof(*b));
if (b && new_event) {
b->time = e->time;
b->key = e->key;
b->tp = e->tp;
b->tp1 = e->tp1;
b->unneeded = e->unneeded;
b->need_find_prev = e->need_find_prev;
b->need_backup = e->need_backup;
b->need_remove_from_backup = e->need_remove_from_backup;
b->maybe_unpaired = 0;
b->seq = e->seq;
b->event = new_event;
RB_CLEAR_NODE(&b->timeline_node);
RB_CLEAR_NODE(&b->key_node);
INIT_LIST_HEAD(&b->needed);
INIT_LIST_HEAD(&b->pending);
if (!b->tp->untraced) {
/*
* With --order enabled, events are backed up in chronological order. Therefore, it
* can be directly added to the end of the queue `pending_list' without reordering.
**/
list_add_tail(&b->pending, &ctx.pending_list);
b->unneeded = 0;
tl_stat.pending ++;
tl_stat.pending_bytes += event->header.size;
}
tl_stat.new ++;
if (b->unneeded) {
tl_stat.unneeded ++;
tl_stat.unneeded_bytes += event->header.size;
}
tl_stat.mem_bytes += event->header.size;
return &b->timeline_node;
} else
return NULL;
}
static void timeline_node_delete(struct rblist *rblist, struct rb_node *rb_node)
{
struct timeline_node *b = container_of(rb_node, struct timeline_node, timeline_node);
if (!list_empty(&b->pending)) {
list_del(&b->pending);
fprintf(stderr, "BUG: event is still in the pending list.\n");
}
tl_stat.delete ++;
tl_stat.mem_bytes -= b->event->header.size;
if (b->unneeded) {
tl_stat.unneeded --;
tl_stat.unneeded_bytes -= b->event->header.size;
}
free(b->event);
free(b);
}
static void timeline_free_unneeded(bool lost)
{
struct rb_node *next = rb_first_cached(&ctx.timeline.entries);
struct timeline_node *tl;
u64 unneeded_lost = 0UL;
u64 unneeded_before = 0UL;
u64 unneeded = 0, backup = 0;
/*
* When there are events lost, events cannot be paired.
* Therefore, actively release some old events.
**/
if (lost || ctx.recent_lost_time) {
u64 interval = ctx.env->interval ? : 3000;
// Any event before `ctx.recent_lost_time' may be unpaired. In the next interval, if event2
// is not received, the event is considered lost and removed from the timeline forcibly.
unneeded_lost = ctx.recent_time - interval * 1000000UL;
if (unneeded_lost >= ctx.recent_lost_time) {
unneeded_lost = 0UL;
ctx.recent_lost_time = 0UL;
}
}
if (ctx.env->before_event1) {
struct timeline_node *needed_first;
if (!list_empty(&ctx.needed_list))
needed_first = list_first_entry(&ctx.needed_list, struct timeline_node, needed);
else if (!list_empty(&ctx.pending_list))
needed_first = list_first_entry(&ctx.pending_list, struct timeline_node, pending);
else {
struct rb_node *unneeded_last = rb_last(&ctx.timeline.entries.rb_root);
needed_first = rb_entry_safe(unneeded_last, struct timeline_node, timeline_node);
}
if (needed_first && needed_first->time > ctx.env->before_event1)
unneeded_before = needed_first->time - ctx.env->before_event1;
}
if (unneeded_lost > unneeded_before)
unneeded_before = unneeded_lost;
while (next) {
tl = rb_entry(next, struct timeline_node, timeline_node);
// if lost: before `ctx.recent_lost_time` on the timeline
// elif before_event1: before `needed_first->time - before_event1` on the timeline
// else: unneeded
if ((unneeded_before == 0UL && tl->unneeded) ||
tl->time < unneeded_before) {
/*
* When there are events lost, the event is backed up but not consumed.
* Remove from ctx.backup.
**/
if (tl->unneeded == 0) {
if (RB_EMPTY_NODE(&tl->key_node)) {
fprintf(stderr, "BUG: rb key_node is empty\n");
} else
rblist__remove_node(&ctx.backup, &tl->key_node);
backup ++;
} else
unneeded ++;
rblist__remove_node(&ctx.timeline, next);
} else
break;
next = rb_first_cached(&ctx.timeline.entries);
}
if (lost || backup) {
print_time(stderr);
fprintf(stderr, "free unneeded %lu, backup %lu\n", unneeded, backup);
}
}
static void timeline_stat(void)
{
printf("TIMELINE:\n"
" new = %lu\n"
" delete = %lu\n"
" unneeded = %lu\n"
" pending = %lu\n"
" mem_bytes = %lu\n"
" unneeded_bytes = %lu\n"
" pending_bytes = %lu\n"
"BACKUP:\n"
" nr_entries = %u\n",
tl_stat.new, tl_stat.delete, tl_stat.unneeded, tl_stat.pending,
tl_stat.mem_bytes, tl_stat.unneeded_bytes, tl_stat.pending_bytes,
rblist__nr_entries(&ctx.backup));
}
static int monitor_ctx_init(struct env *env)
{
int i, j, stacks = 0;
struct tep_handle *tep;
struct two_event_options options = {
.keyname = monitor_instance_oncpu() ? "CPU" : "THREAD",
.perins = env->perins,
.comm = ctx.comm,
.only_print_greater_than = env->only_print_greater_than,
.greater_than = env->greater_than,
.lower_than = env->lower_than,
.heatmap = env->heatmap,
.first_n = 10,
.sort_print = ctx.nested ? false : true,
.env = env,
};
const char *keyname = NULL;
bool untraced = false;
int min_nr_events = 2;
if (ctx.nested)
min_nr_events = 1;
else if (env->cycle) {
if (!env->impl || !strcmp(env->impl, TWO_EVENT_DELAY_IMPL))
min_nr_events = 1;
else
env->cycle = 0;
}
if (env->nr_events < min_nr_events)
return -1;
base_profiler = current_base_profiler();
tep = tep__ref();
ctx.nr_ins = monitor_nr_instance();
ctx.nr_list = env->nr_events;
ctx.tp_list = calloc(ctx.nr_list, sizeof(*ctx.tp_list));
if (!ctx.tp_list)
return -1;
for (i = 0; i < ctx.nr_list; i++) {
ctx.tp_list[i] = tp_list_new(env->events[i]);
if (!ctx.tp_list[i]) {
return -1;
}
stacks += ctx.tp_list[i]->nr_need_stack;
for (j = 0; j < ctx.tp_list[i]->nr_tp; j++) {
struct tp *tp = &ctx.tp_list[i]->tp[j];
if (env->verbose)
printf("name %s id %d filter %s stack %d\n", tp->name, tp->id, tp->filter, tp->stack);
if (tp->untraced && !tp->trigger)
untraced = true;
if (tp->untraced) {
if ((env->samekey || env->samepid || env->samekey) &&
!tp_kernel(tp) && !tp->vcpu)
fprintf(stderr, "The event %s:%s needs the vm attr to convert the fields of the Guest events.\n",
tp->sys, tp->name);
continue;
}
if (env->key && !tp->key) {
struct tep_event *event = tep_find_event_by_name(tep, tp->sys, tp->name);
if (!tep_find_any_field(event, env->key)) {
fprintf(stderr, "Cannot find %s field at %s:%s\n", env->key, tp->sys, tp->name);
return -1;
}
tp->key_prog = tp_new_prog(tp, env->key);
tp->key = env->key;
}
if (tp->key && !keyname)
keyname = tp->key;
}
}
if (stacks) {
ctx.cc = callchain_ctx_new(callchain_flags(CALLCHAIN_KERNEL), stdout);
base_profiler->pages *= 2;
} else
ctx.cc = NULL;
if (keyname) {
options.keyname = keyname;
options.keylen = strlen(keyname);
if (options.keylen < 6)
options.keylen = 6;
if (!current_is_order()) {
fprintf(stderr, "WARN: Enable the --key parameter, it is recommended to enable the "
"--order parameter to order events.\n");
}
}
sched_init(ctx.nr_list, ctx.tp_list);
if (env->detail &&
ctx.nr_ins > 1 &&
!using_order(base_profiler)) {
fprintf(stderr, "Enable --detail, also need to enable --order.\n");
return -1;
}
if (env->impl && impl_based_on_call(env->impl))
ctx.impl_based_on_call = true;
if (ctx.impl_based_on_call && !ctx.nested) {
fprintf(stderr, "Only nested-trace can enable --impl %s.\n", env->impl);
return -1;
}
ctx.impl = impl_get(env->impl ?: TWO_EVENT_DELAY_IMPL);
if (!ctx.impl) {
fprintf(stderr, "--impl %s not implemented\n", env->impl);
return -1;
}
ctx.class = ctx.impl->class_new(ctx.impl, &options);
rblist__init(&ctx.backup);
ctx.backup.node_cmp = perf_event_backup_node_cmp;
ctx.backup.node_new = perf_event_backup_node_new;
ctx.backup.node_delete = perf_event_backup_node_delete;
rblist__init(&ctx.timeline);
ctx.timeline.node_cmp = timeline_node_cmp;
ctx.timeline.node_new = timeline_node_new;
ctx.timeline.node_delete = timeline_node_delete;
INIT_LIST_HEAD(&ctx.needed_list);
INIT_LIST_HEAD(&ctx.pending_list);
ctx.need_timeline = env->detail;
if (untraced && !env->detail) {
fprintf(stderr, "WARN: --detail parameter is not enabled. No need to add untrace events.\n");
}
if (!env->greater_than && env->detail) {
fprintf(stderr, "WARN: --than parameter is not enabled. No need to enable the "
"--detail parameter.\n");
}
ctx.env = env;
return 0;
}
static void monitor_ctx_exit(void)
{
int i;
while (multi_trace_first_pending(NULL)) ;
rblist__exit(&ctx.backup);
rblist__exit(&ctx.timeline);
ctx.impl->class_delete(ctx.class);
callchain_ctx_free(ctx.cc);
for (i = 0; i < ctx.nr_list; i++)
tp_list_free(ctx.tp_list[i]);
free(ctx.tp_list);
tep__unref();
}
static int __multi_trace_init(struct perf_evlist *evlist, struct env *env)
{
struct perf_event_attr attr = {
.type = PERF_TYPE_TRACEPOINT,
.config = 0,
.size = sizeof(struct perf_event_attr),
.sample_period = 1,
.sample_type = PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD | PERF_SAMPLE_RAW,
.read_format = PERF_FORMAT_ID,
.pinned = 1,
.disabled = 1,
.exclude_callchain_user = exclude_callchain_user(CALLCHAIN_KERNEL),
.exclude_callchain_kernel = exclude_callchain_kernel(CALLCHAIN_KERNEL),
.watermark = 1,
};
int i, j;
if (monitor_ctx_init(env) < 0)
return -1;
if (using_order(base_profiler)) {
base_profiler->dup = true;
}
reduce_wakeup_times(base_profiler, &attr);
for (i = 0; i < ctx.nr_list; i++) {
for (j = 0; j < ctx.tp_list[i]->nr_tp; j++) {
struct perf_evsel *evsel;
struct tp *tp = &ctx.tp_list[i]->tp[j];
attr.config = tp->id;
if (tp->stack)
attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
else
attr.sample_type &= (~PERF_SAMPLE_CALLCHAIN);
attr.sample_max_stack = tp->max_stack;
evsel = perf_evsel__new(&attr);
if (!evsel) {
return -1;
}
perf_evlist__add(evlist, evsel);
if (!tp_kernel(tp))
perf_evsel__keep_disable(evsel, true);
tp->evsel = evsel;
}
}
ctx.evlist = evlist;
return 0;
}
static int multi_trace_init(struct perf_evlist *evlist, struct env *env)
{
int i, j, k, n;
ctx.nested = 0;
if (__multi_trace_init(evlist, env) < 0)
return -1;
// env->cycle: from the last one back to the first.
for (k = 0; k < ctx.nr_list - !env->cycle; k++) {
for (i = 0; i < ctx.tp_list[k]->nr_tp; i++) {
struct tp *tp1 = &ctx.tp_list[k]->tp[i];
if (tp1->untraced)
continue;
// for handle remaining
if (!ctx.impl->object_new(ctx.class, tp1, NULL))
return -1;
n = (k+1) % ctx.nr_list;
for (j = 0; j < ctx.tp_list[n]->nr_tp; j++) {
struct tp *tp2 = &ctx.tp_list[n]->tp[j];
if (tp2->untraced)
continue;
if (!ctx.impl->object_new(ctx.class, tp1, tp2))
return -1;
}
}
}
return 0;
}
static int multi_trace_filter(struct perf_evlist *evlist, struct env *env)
{
int i, j, err;
for (i = 0; i < ctx.nr_list; i++) {
for (j = 0; j < ctx.tp_list[i]->nr_tp; j++) {
struct tp *tp = &ctx.tp_list[i]->tp[j];
if (tp->filter && tp->filter[0]) {
err = perf_evsel__apply_filter(tp->evsel, tp->filter);
if (err < 0)
return err;
}
}
}
return 0;
}
static unsigned long drop_mmap_events(struct perf_mmap *map)
{
union perf_event *event;
bool writable = false;
unsigned long dropped = 0;
if (perf_mmap__read_init(map) < 0)
return 0;
while ((event = perf_mmap__read_event(map, &writable)) != NULL) {
dropped ++;
perf_mmap__consume(map);
}
perf_mmap__read_done(map);
return dropped;
}
static void multi_trace_enabled(struct perf_evlist *evlist)
{
struct perf_mmap *map;
unsigned long dropped = 0;
/*
* Start sampling after the events is fully enabled.
*
* -e sched:sched_wakeup -e sched:sched_switch -C 0-95
* A sched_wakeup occurs on CPU0, possibly a paired sched_switch occurs on CPU95. When enabling,
* CPU0 is enabled first, and CPU95 is enabled last. It is possible that the sched_wakeup event
* is only sampled on CPU0, and the sched_switch event is not sampled on CPU95.
* It is possible that sched_wakeup will block the timeline to free unneeded events.
**/
perf_evlist__for_each_mmap(evlist, map, ctx.env->overwrite) {
dropped += drop_mmap_events(map);
}
if (ctx.env->verbose)
printf("Drop %lu events before starting sampling.\n", dropped);
}
static void multi_trace_handle_remaining(void)
{
struct rb_node *next = rb_first_cached(&ctx.backup.entries);
struct timeline_node *left;
struct two_event *two;
while (next) {
left = rb_entry(next, struct timeline_node, key_node);
two = ctx.impl->object_find(ctx.class, left->tp, NULL);
if (two) {
struct event_info info;
struct event_iter iter;
info.tp1 = left->tp;
info.tp2 = NULL;
info.key = left->key;
info.recent_time = ctx.recent_time;
if (ctx.need_timeline) {
if (ctx.env->before_event1) {
struct timeline_node backup = {
.time = left->time - ctx.env->before_event1,
.key = left->key,
.seq = 0,
};
iter.start = rb_entry_safe(rblist__find_first(&ctx.timeline, &backup),
struct timeline_node, timeline_node);
} else
iter.start = left;
iter.event1 = left;
iter.event2 = NULL;
iter.curr = iter.start;
}
if (info.recent_time - left->time > ctx.env->greater_than)
left->maybe_unpaired = 1;
if (ctx.class->remaining(two, left->event, &info, ctx.need_timeline ? &iter : NULL) == REMAINING_BREAK)
break;
}
next = rb_next(next);
}
}
static void multi_trace_interval(void)
{
int i, j, k, n;
int header = 0;
struct two_event *two;
multi_trace_handle_remaining();
// env->cycle: from the last one back to the first.
for (k = 0; k < ctx.nr_list - !ctx.env->cycle; k++) {
for (i = 0; i < ctx.tp_list[k]->nr_tp; i++) {
struct tp *tp1 = &ctx.tp_list[k]->tp[i];
if (tp1->untraced)
continue;
// for print remaining
two = ctx.impl->object_find(ctx.class, tp1, NULL);
if (!header) {
header = ctx.class->print_header(two);
}
ctx.class->print(two);
n = (k+1) % ctx.nr_list;
for (j = 0; j < ctx.tp_list[n]->nr_tp; j++) {
struct tp *tp2 = &ctx.tp_list[n]->tp[j];
if (tp2->untraced)
continue;
two = ctx.impl->object_find(ctx.class, tp1, tp2);
if (!header) {
header = ctx.class->print_header(two);
}
ctx.class->print(two);
}
}
}
}
static void multi_trace_exit(struct perf_evlist *evlist)
{
multi_trace_interval();
monitor_ctx_exit();
}
static void multi_trace_sigusr1(int signum)
{
if (ctx.need_timeline)
timeline_stat();
else {
if (base_profiler->dup)
printf("DUP STAT:\n"
" nr_samples = %lu\n"
" nr_free = %lu\n"
" nr_unfree = %lu\n",
dup_stat.nr_samples, dup_stat.nr_free + backup_stat.delete,
dup_stat.nr_samples - dup_stat.nr_free - backup_stat.delete);
printf("BACKUP:\n"
" new = %lu\n"
" delete = %lu\n"
" nr_entries = %u\n"
" mem_bytes = %lu\n",
backup_stat.new, backup_stat.delete, rblist__nr_entries(&ctx.backup),
backup_stat.mem_bytes);
}
printf("SPECIAL EVENT:\n");
printf(" sched:sched_wakeup unnecessary %lu\n", ctx.sched_wakeup_unnecessary);
}
static void multi_trace_lost(union perf_event *event, int ins, u64 lost_time)
{
lost_time = lost_time ? : ctx.recent_time;
if (ctx.recent_lost_time < lost_time)
ctx.recent_lost_time = lost_time;
print_lost_fn(event, ins);
if (ctx.need_timeline)
timeline_free_unneeded(true);
}
void multi_trace_raw_size(union perf_event *event, void **praw, int *psize, struct tp *tp)
{
if (tp->stack) {
struct multi_trace_type_callchain *data = (void *)event->sample.array;
struct {
__u32 size;
__u8 data[0];
} *raw = (void *)data->callchain.ips + data->callchain.nr * sizeof(__u64);
*praw = raw->data;
*psize = raw->size;
} else {
struct multi_trace_type_raw *raw = (void *)event->sample.array;
*praw = raw->raw.data;
*psize = raw->raw.size;
}
}
void multi_trace_print_title(union perf_event *event, struct tp *tp, const char *title)
{
struct multi_trace_type_callchain *data = (void *)event->sample.array;
void *raw;
int size;
multi_trace_raw_size(event, &raw, &size, tp);
if (title)
printf("%-27s", title);
else
print_time(stdout);
tp_print_marker(tp);
tep__update_comm(NULL, data->h.tid_entry.tid);
tep__print_event(data->h.time/1000, data->h.cpu_entry.cpu, raw, size);
if (tp->stack) {
print_callchain_common(ctx.cc, &data->callchain, data->h.tid_entry.pid);
}
}
bool event_need_to_print(union perf_event *event1, union perf_event *event2, struct event_info *info, struct event_iter *iter)
{
struct timeline_node *curr = iter->curr;
struct multi_trace_type_header *e = (void *)iter->event->sample.array;
struct multi_trace_type_header *e1 = (void *)event1->sample.array;
struct multi_trace_type_header *e2 = event2 ? (void *)event2->sample.array : NULL;
bool match;
if (!(ctx.env->samecpu || ctx.env->samepid || ctx.env->sametid || ctx.env->samekey))
return true;
// tp_kernel: Compare key values directly.
//!tp_kernel: Rely on vm attr to convert the fields of the Guest events.
match = tp_kernel(curr->tp) ? 1 : !!(curr->tp->vcpu);
if (ctx.env->samecpu && match)
if (e->cpu_entry.cpu == e1->cpu_entry.cpu ||
(e2 && e->cpu_entry.cpu == e2->cpu_entry.cpu))
return true;
if (ctx.env->samepid && match)
if (e->tid_entry.pid == e1->tid_entry.pid ||
(e2 && e->tid_entry.pid == e2->tid_entry.pid))
return true;
if (ctx.env->sametid && match)
if (e->tid_entry.tid == e1->tid_entry.tid ||
(e2 && e->tid_entry.tid == e2->tid_entry.tid))
return true;
if (ctx.env->samekey && match)
if ((!!curr->tp->key) == (!!info->tp1->key) &&
curr->key == info->key)
return true;
return false;
}
int event_iter_cmd(struct event_iter *iter, enum event_iter_cmd cmd)
{
struct timeline_node *curr;
struct rb_node *rbn;
if (!iter || cmd >= CMD_MAX)
return 0;
switch (cmd) {
case CMD_RESET:
curr = iter->curr = iter->start;
if (curr) {
iter->event = curr->event;
iter->tp = curr->tp;
}
break;
case CMD_EVENT1:
case CMD_EVENT2:
curr = iter->curr = (cmd == CMD_EVENT1 ? iter->event1 : iter->event2);
iter->event = curr->event;
iter->tp = curr->tp;
break;
case CMD_PREV:
case CMD_NEXT:
if (iter->curr == NULL)
return 0;
curr = iter->curr;
rbn = (cmd == CMD_PREV ? rb_prev : rb_next)(&curr->timeline_node);
iter->curr = rb_entry_safe(rbn, struct timeline_node, timeline_node);
if (!iter->curr)
return 0;
curr = iter->curr;
iter->event = curr->event;
iter->tp = curr->tp;
break;
case CMD_MAX:
default:
return 0;
}
return 1;
}
static struct rb_node *multi_trace_find_prev(struct timeline_node *backup)
{
struct rb_node *rbn;
rb_for_each(rbn, backup, &ctx.backup.entries.rb_root, perf_event_backup_node_find) {
if (!backup->tp)
return rbn;
else {
struct timeline_node *prev;
prev = container_of(rbn, struct timeline_node, key_node);
if (prev->tp == backup->tp)
return rbn;
}
}
return NULL;
}
static void multi_trace_tryto_call_two(struct timeline_node *tl_event, bool *need_free)
{
union perf_event *event = tl_event->event;
struct tp *tp = tl_event->tp;
struct tp *tp1 = tl_event->tp1;
bool need_find_prev = tl_event->need_find_prev;
bool need_remove_from_backup = tl_event->need_remove_from_backup;
u64 key = tl_event->key;
// find prev event, not include untraced
if (need_find_prev) {
struct timeline_node backup = {
.key = key,
.tp = tp1,
};
struct two_event *two;
struct rb_node *rbn = multi_trace_find_prev(&backup);
if (rbn) {
struct timeline_node *prev;
prev = container_of(rbn, struct timeline_node, key_node);
prev->maybe_unpaired = 0;
two = ctx.impl->object_find(ctx.class, prev->tp, tp);
if (two) {
struct event_info info;
info.tp1 = prev->tp;
info.tp2 = tp;
info.key = key;
info.recent_time = ctx.recent_time;
if (ctx.need_timeline) {
struct event_iter iter;
if (ctx.env->before_event1) {
backup.time = prev->time - ctx.env->before_event1;
backup.seq = 0;
iter.start = rb_entry_safe(rblist__find_first(&ctx.timeline, &backup),
struct timeline_node, timeline_node);
} else
iter.start = prev;
iter.event1 = prev;
iter.event2 = tl_event;
iter.curr = iter.start;
ctx.class->two(two, prev->event, event, &info, &iter);
} else
ctx.class->two(two, prev->event, event, &info, NULL);
}
if (need_remove_from_backup) {
rblist__remove_node(&ctx.backup, rbn);
// ctx.backup no longer references an event, prev.unneeded = 1,
// releasing unneeded events on the timeline in time.
*need_free = true;
}
} else if (ctx.impl_based_on_call) {
two = ctx.impl->object_find(ctx.class, tp, NULL);
if (two) {
// two(A, NULL), first call A.
struct event_info info;
info.tp1 = tp;
info.tp2 = NULL;
info.key = key;
ctx.class->two(two, event, NULL, &info, NULL);
}
}
}
}
static int multi_trace_tryto_backup(struct timeline_node *tl_event, bool *need_free)
{
bool need_backup = tl_event->need_backup;
unsigned int nr_entries;
struct rb_node *rbn;
int ret = -1;
// backup events, exclude untraced events.
if (need_backup) {
retry:
nr_entries = rblist__nr_entries(&ctx.backup);
rbn = rblist__findnew(&ctx.backup, tl_event);
if (rbn) {
if (nr_entries == rblist__nr_entries(&ctx.backup)) {
struct timeline_node *new;
new = rb_entry(rbn, struct timeline_node, key_node);
/*
* The same event occurs multiple times, only the last event is backed up.
* Previous events will be marked as unneeded and released on the timeline in time.
**/
if (ctx.env->verbose >= VERBOSE_NOTICE)
multi_trace_print_title(new->event, new->tp, "EEXIST");
rblist__remove_node(&ctx.backup, rbn);
*need_free = true;
/*
* tl_event->unneeded is equal to 0, but not added to ctx.backup, tl_event->key_node
* is empty, `timeline_free_unneeded' cannot be called immediately.
**/
goto retry;
} else
ret = 0;
} else {
tl_event->unneeded = 1;
*need_free = true;
}
} else
// Events at the last level are unneeded.
*need_free = true;