forked from OpenCloudOS/perf-prof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-trace.c
1304 lines (1162 loc) · 41.3 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;
struct timeline_node {
struct rb_node timeline_node;
u64 time;
struct rb_node key_node;
u64 key;
struct tp *tp;
u32 unneeded : 1;
struct list_head needed;
union perf_event *event;
};
struct timeline_stat {
u64 new;
u64 delete;
u64 unneeded;
u64 mem_bytes;
u64 unneeded_bytes;
} tl_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;
bool need_timeline;
bool nested;
bool impl_based_on_call;
struct callchain_ctx *cc;
struct perf_evlist *evlist;
struct env *env;
} ctx;
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->unneeded = 0;
b->event = new_event;
RB_CLEAR_NODE(&b->timeline_node);
RB_CLEAR_NODE(&b->key_node);
INIT_LIST_HEAD(&b->needed);
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 {
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;
else if (b->key > e->key)
return 1;
else if (b->key < e->key)
return -1;
else
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->unneeded = e->unneeded;
b->event = new_event;
RB_CLEAR_NODE(&b->timeline_node);
RB_CLEAR_NODE(&b->key_node);
INIT_LIST_HEAD(&b->needed);
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);
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_before = 0UL;
u64 unneeded = 0, backup = 0;
/*
* When there are events lost, events cannot be paired.
* Therefore, actively release some old events.
**/
if (lost) {
struct rb_node *last = rb_last(&ctx.timeline.entries.rb_root);
struct timeline_node *tl_last = rb_entry_safe(last, struct timeline_node, timeline_node);
u64 interval = ctx.env->interval ? : 3000;
if (tl_last)
unneeded_before = tl_last->time - interval * 1000000UL;
} else 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 {
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;
}
while (next) {
tl = rb_entry(next, struct timeline_node, timeline_node);
// if lost: before `tl_last->time - interval` 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)) {
printf("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) {
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"
" mem_bytes = %lu\n"
" unneeded_bytes = %lu\n"
"BACKUP:\n"
" nr_entries = %u\n",
tl_stat.new, tl_stat.delete, tl_stat.unneeded,
tl_stat.mem_bytes, tl_stat.unneeded_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 = {
.keytype = monitor_instance_oncpu() ? K_CPU : K_THREAD,
.perins = env->perins,
.greater_than = env->greater_than,
.heatmap = env->heatmap,
.first_n = 10,
.sort_print = ctx.nested ? false : true,
};
bool key_attr = false;
bool untraced = false;
if (env->nr_events < (ctx.nested ? 1 : 2))
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) {
untraced = true;
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;
}
}
if (tp->key)
key_attr = true;
}
}
if (stacks) {
ctx.cc = callchain_ctx_new(CALLCHAIN_KERNEL, stdout);
base_profiler->pages *= 2;
} else
ctx.cc = NULL;
if (env->key || key_attr) {
options.keytype = K_CUSTOM;
if (!current_is_order()) {
fprintf(stderr, "WARN: Enable the --key parameter, it is recommended to enable the "
"--order parameter to order events.\n");
}
}
if (env->before_event1 &&
ctx.nr_ins > 1 &&
!using_order(base_profiler)) {
fprintf(stderr, "Enable --detail=-N, 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);
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;
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 = 1,
.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);
tp->evsel = evsel;
}
}
ctx.evlist = evlist;
return 0;
}
static int multi_trace_init(struct perf_evlist *evlist, struct env *env)
{
int i, j, k;
ctx.nested = 0;
if (__multi_trace_init(evlist, env) < 0)
return -1;
for (k = 0; k < ctx.nr_list - 1; 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 (j = 0; j < ctx.tp_list[k+1]->nr_tp; j++) {
struct tp *tp2 = &ctx.tp_list[k+1]->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 void multi_trace_interval(void)
{
int i, j, k;
int header = 0;
for (k = 0; k < ctx.nr_list - 1; 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 (j = 0; j < ctx.tp_list[k+1]->nr_tp; j++) {
struct tp *tp2 = &ctx.tp_list[k+1]->tp[j];
struct two_event *two;
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_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) {
ctx.class->remaining(two, left->event, left->key);
}
next = rb_next(next);
}
}
static void multi_trace_exit(struct perf_evlist *evlist)
{
multi_trace_handle_remaining();
multi_trace_interval();
monitor_ctx_exit();
}
static void multi_trace_sigusr1(int signum)
{
if (ctx.need_timeline)
timeline_stat();
}
static void multi_trace_lost(union perf_event *event, int ins)
{
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);
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, 0/*only kernel stack*/);
}
}
bool event_need_to_print(union perf_event *event, union perf_event *event1, union perf_event *event2)
{
struct multi_trace_type_header *e = (void *)event ->sample.array;
struct multi_trace_type_header *e1 = (void *)event1->sample.array;
struct multi_trace_type_header *e2 = (void *)event2->sample.array;
if (!(ctx.env->samecpu || ctx.env->samepid))
return true;
if (ctx.env->samecpu)
if (e->cpu_entry.cpu == e1->cpu_entry.cpu ||
e->cpu_entry.cpu == e2->cpu_entry.cpu)
return true;
if (ctx.env->samepid)
if (e->tid_entry.pid == e1->tid_entry.pid ||
e->tid_entry.pid == e2->tid_entry.pid)
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:
curr = iter->curr = iter->event1;
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_sample(union perf_event *event, int instance)
{
struct multi_trace_type_header *hdr = (void *)event->sample.array;
struct tp *tp = NULL, *tp1 = NULL;
struct timeline_node *tl_event = NULL;
struct perf_evsel *evsel;
int i, j;
bool need_find_prev, need_backup, need_remove_from_backup;
__u64 key;
evsel = perf_evlist__id_to_evsel(ctx.evlist, hdr->stream_id, NULL);
if (!evsel)
goto free_dup_event;
for (i = 0; i < ctx.nr_list; i++) {
tp1 = NULL;
for (j = 0; j < ctx.tp_list[i]->nr_tp; j++) {
tp = &ctx.tp_list[i]->tp[j];
if (tp->evsel == evsel)
goto found;
if (!tp->untraced)
tp1 = tp;
}
}
free_dup_event:
if (base_profiler->dup)
free(event);
return;
found:
if (ctx.env->verbose >= VERBOSE_EVENT) {
multi_trace_print(event, tp);
}
if (!ctx.nested) {
need_find_prev = i != 0;
need_backup = i != ctx.nr_list - 1;
need_remove_from_backup = 1;
// no need to use tp1
tp1 = NULL;
} else {
need_find_prev = ctx.impl_based_on_call || tp1 != NULL;
need_backup = tp1 == NULL;
need_remove_from_backup = tp1 != NULL;
}
// get key, include untraced events.
key = monitor_instance_oncpu() ? monitor_instance_cpu(instance) : monitor_instance_thread(instance);
// !untraced: tp->key || ctx.env->key
// untraced: tp->key
if (tp->key || (!tp->untraced && ctx.env->key)) {
struct tep_record record;
struct tep_handle *tep = tep__ref();
struct tep_event *e;
void *raw;
int size;
multi_trace_raw_size(event, &raw, &size, tp);
memset(&record, 0, sizeof(record));
record.ts = hdr->time/1000;
record.cpu = hdr->cpu_entry.cpu;
record.size = size;
record.data = raw;
e = tep_find_event_by_record(tep, &record);
if (tep_get_field_val(NULL, e, tp->key ?: ctx.env->key, &record, &key, 0) < 0) {
if (tep_get_common_field_val(NULL, e, tp->key ?: ctx.env->key, &record, &key, 0) < 0) {
tep__unref();
goto free_dup_event;
}
}
tep__unref();
}
if (tp->untraced)
goto untraced_processing;
// 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);
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;
if (ctx.need_timeline) {
struct event_iter iter;
if (ctx.env->before_event1) {
backup.time = prev->time - ctx.env->before_event1;
iter.start = rb_entry_safe(rblist__find_first(&ctx.timeline, &backup),
struct timeline_node, timeline_node);
} else
iter.start = prev;
iter.event1 = prev;
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.
if (ctx.need_timeline)
timeline_free_unneeded(false);
}
} 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);
}
}
}
untraced_processing:
// backup events to Timeline, include untraced events.
if (ctx.need_timeline) {
struct timeline_node backup = {
.time = hdr->time,
.key = key,
.tp = tp,
.unneeded = (!need_backup) || tp->untraced, // untraced means unneeded
.event = event,
};
struct rb_node *rbn;
bool need_free = false;
if (rblist__empty(&ctx.timeline) && backup.unneeded && ctx.env->before_event1 == 0)
rbn = NULL;
else
rbn = rblist__findnew(&ctx.timeline, &backup);
if (rbn) {
tl_event = rb_entry(rbn, struct timeline_node, timeline_node);
} else
goto free_dup_event;
// backup events, exclude untraced events.
if (need_backup && !tp->untraced) {
retry:
rbn = rblist__findnew(&ctx.backup, tl_event);
if (rbn) {
struct timeline_node *new;
new = rb_entry(rbn, struct timeline_node, key_node);
if (new != tl_event) {
/*
* 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 {
tl_event->unneeded = 1;
need_free = true;
}
} else
// Events at the last level are unneeded.
need_free = true;
if (need_free)
timeline_free_unneeded(false);
}
else
{
// backup events, exclude untraced events.
if (need_backup && !tp->untraced) {
struct timeline_node backup = {
.time = hdr->time,
.key = key,
.tp = tp,
.event = event,
};
if (base_profiler->dup) {
struct rb_node *rbn = rblist__findnew(&ctx.backup, &backup);
if (rbn) {
struct timeline_node *new;
new = rb_entry(rbn, struct timeline_node, key_node);
if (new->event != event) {
if (ctx.env->verbose >= VERBOSE_NOTICE)
multi_trace_print_title(new->event, new->tp, "EEXIST");
free(new->event);
new->event = event;
new->time = backup.time;
}
new->tp = tp;
} else
goto free_dup_event;
} else {
int err = rblist__add_node(&ctx.backup, &backup);
if (err == -EEXIST) {
struct rb_node *rbn = rblist__find(&ctx.backup, &backup);
struct timeline_node *new = rb_entry(rbn, struct timeline_node, key_node);
if (ctx.env->verbose >= VERBOSE_NOTICE)
multi_trace_print_title(new->event, new->tp, "EEXIST");
free(new->event);
new->event = memdup(event, event->header.size);
new->time = backup.time;
new->tp = tp;
} else if (err != 0)
goto free_dup_event;
}
} else
goto free_dup_event;
}
}
static void __help_events(struct help_ctx *hctx, const char *impl, bool *has_key)
{
int i, j;
struct env *env = hctx->env;
if (strcmp(impl, TWO_EVENT_SYSCALLS_IMPL) == 0) {
printf("-e raw_syscalls:sys_enter/./ -e raw_syscalls:sys_exit/./ ");
return;
}
for (i = 0; i < hctx->nr_list; i++) {
printf("-e \"");
for (j = 0; j < hctx->tp_list[i]->nr_tp; j++) {
struct tp *tp = &hctx->tp_list[i]->tp[j];
printf("%s:%s/%s/", tp->sys, tp->name, tp->filter&&tp->filter[0]?tp->filter:".");
if (!env->key || tp->key)
printf("key=%s/", tp->key?:".");
if (tp->key)
*has_key = true;
if (strcmp(impl, TWO_EVENT_MEM_PROFILE) == 0)
printf("ptr=%s/size=%s/", tp->mem_ptr?:".", tp->mem_size?:".");
if (strcmp(impl, TWO_EVENT_PAIR_IMPL) != 0)
printf("stack/");
if (tp->untraced)
printf("untraced/");
else
printf("[untraced/]");
if (j != hctx->tp_list[i]->nr_tp - 1)
printf(",");
}
printf("\" ");
}
}
static void __multi_trece_help(struct help_ctx *hctx, const char *common, const char *impl, bool impl_default)
{
struct env *env = hctx->env;
bool has_key = false;
if (!ctx.nested && strcmp(impl, TWO_EVENT_SYSCALLS_IMPL) && hctx->nr_list < 2)
return;
if (env->impl && strcmp(env->impl, impl))
return;
printf("%s ", common);
__help_events(hctx, impl, &has_key);
if (env->key)
printf("-k %s --order --order-mem . ", env->key);
else if (has_key)
printf("--order --order-mem . ");
if (!impl_default)
printf("--impl %s ", impl);
if (strcmp(impl, TWO_EVENT_DELAY_IMPL) == 0 ||
strcmp(impl, TWO_EVENT_SYSCALLS_IMPL) == 0 ||
strcmp(impl, TWO_EVENT_CALL_DELAY_IMPL) == 0) {
if (env->perins)
printf("--perins ");
if (env->greater_than)
printf("--than %lu ", env->greater_than);
if (env->detail) {
if (env->before_event1 || env->samecpu || env->samepid) {
int len = 0;
printf("--detail=");
if (env->before_event1)
len += printf("-%lu", env->before_event1);
if (env->samecpu)
len += printf("%ssamecpu", len > 0 ? "," : "");
if (env->samepid)
len += printf("%ssamepid", len > 0 ? "," : "");
printf(" ");
} else
printf("--detail ");
}
if (env->heatmap)
printf("--heatmap %s ", env->heatmap);
}
common_help(hctx, true, true, true, true, false, true, true);
if (!env->key && !has_key)
printf("[-k . --order --order-mem .] ");
else if (!env->key)
printf("[-k .] ");
if (strcmp(impl, TWO_EVENT_DELAY_IMPL) == 0 ||
strcmp(impl, TWO_EVENT_SYSCALLS_IMPL) == 0 ||
strcmp(impl, TWO_EVENT_CALL_DELAY_IMPL) == 0) {
if (!env->perins)
printf("[--perins] ");
if (!env->greater_than)
printf("[--than .] ");
if (!env->detail)
printf("[--detail[=-N,+N,samecpu,samepid]] ");
if (!env->heatmap)
printf("[--heatmap .] ");
}
common_help(hctx, false, true, true, true, false, true, true);
printf("\n");
}
#define NUM(ary) (sizeof(ary)/sizeof(ary[0]))
static void multi_trece_help(struct help_ctx *hctx)
{