-
Notifications
You must be signed in to change notification settings - Fork 12
/
multi-trace.c
2469 lines (2209 loc) · 83.7 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>
#include <tp_struct.h>
#define ENABLED_MAX ULLONG_MAX
#define ENABLED_TP_MAX (ULLONG_MAX-1)
struct multi_trace_ctx;
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;
u32 ins;
u64 seq;
union {
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;
};
struct __dup_stat {
u64 nr_samples;
u64 nr_free;
};
struct __backup_stat {
u64 new;
u64 delete;
u64 mem_bytes;
};
enum lost_affect {
LOST_AFFECT_ALL_EVENT,
LOST_AFFECT_INS_EVENT,
};
struct lost_node {
struct list_head lost_link;
int ins;
bool reclaim;
u64 start_time;
u64 end_time;
u64 lost;
};
struct multi_trace_ctx {
struct prof_dev *dev;
int oncpu;
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 *perins_list;
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 event_handled;
u64 sched_wakeup_unnecessary;
struct callchain_ctx *cc;
struct perf_thread_map *thread_map; // profiler rundelay
bool comm; // profiler rundelay, syscalls
bool rundelay; // profiler rundelay
int level; // level = sched_init()
/* lost */
enum lost_affect lost_affect;
struct list_head timeline_lost_list; // LOST_AFFECT_ALL_EVENT. struct lost_node
struct list_head *perins_lost_list; // LOST_AFFECT_INS_EVENT. struct lost_node
/* syscalls: exit, exit_group */
struct perf_evsel *extra_evsel;
void (*extra_sample)(struct prof_dev *dev, union perf_event *event, int instance);
/* stat */
struct timeline_stat tl_stat;
struct __dup_stat dup_stat;
struct __backup_stat backup_stat;
};
static struct timeline_node *multi_trace_first_pending(struct prof_dev *dev, 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)
{
struct multi_trace_ctx *ctx = container_of(rlist, struct multi_trace_ctx, backup);
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 = ctx->dev->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->ins = e->ins;
b->seq = e->seq;
b->event = ctx->dev->dup ? new_event : perf_event_get(new_event);
RB_CLEAR_NODE(&b->timeline_node);
RB_CLEAR_NODE(&b->key_node);
INIT_LIST_HEAD(&b->needed);
/*
* The events for each instance are time-ordered. Therefore, it can be directly added
* to the end of the queue without reordering.
**/
list_add_tail(&b->needed, &ctx->perins_list[b->ins]);
ctx->backup_stat.new ++;
ctx->backup_stat.mem_bytes += event->header.size;
return &b->key_node;
} else {
if (b) free(b);
if (new_event && new_event != event) free(new_event);
return NULL;
}
}
}
static void perf_event_backup_node_delete(struct rblist *rblist, struct rb_node *rb_node)
{
struct multi_trace_ctx *ctx = container_of(rblist, struct multi_trace_ctx, backup);
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);
ctx->tl_stat.unneeded ++;
ctx->tl_stat.unneeded_bytes += b->event->header.size;
} else {
list_del(&b->needed);
ctx->backup_stat.delete ++;
ctx->backup_stat.mem_bytes -= b->event->header.size;
perf_event_put(b->event);
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)
{
struct multi_trace_ctx *ctx = container_of(rlist, struct multi_trace_ctx, timeline);
const struct timeline_node *e = new_entry;
union perf_event *event = e->event;
union perf_event *new_event = ctx->dev->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->ins = e->ins;
b->seq = e->seq;
b->event = ctx->dev->dup ? new_event : perf_event_get(new_event);
RB_CLEAR_NODE(&b->timeline_node);
RB_CLEAR_NODE(&b->key_node);
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;
ctx->tl_stat.pending ++;
ctx->tl_stat.pending_bytes += event->header.size;
}
ctx->tl_stat.new ++;
if (b->unneeded) {
ctx->tl_stat.unneeded ++;
ctx->tl_stat.unneeded_bytes += event->header.size;
}
ctx->tl_stat.mem_bytes += event->header.size;
return &b->timeline_node;
} else {
if (b) free(b);
if (new_event && new_event != event) free(new_event);
return NULL;
}
}
static void timeline_node_delete(struct rblist *rblist, struct rb_node *rb_node)
{
struct multi_trace_ctx *ctx = container_of(rblist, struct multi_trace_ctx, timeline);
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");
}
ctx->tl_stat.delete ++;
ctx->tl_stat.mem_bytes -= b->event->header.size;
if (b->unneeded) {
ctx->tl_stat.unneeded --;
ctx->tl_stat.unneeded_bytes -= b->event->header.size;
}
perf_event_put(b->event);
free(b->event);
free(b);
}
static void timeline_free_unneeded(struct prof_dev *dev)
{
struct env *env = dev->env;
struct multi_trace_ctx *ctx = dev->private;
struct rb_node *next = rb_first_cached(&ctx->timeline.entries);
struct timeline_node *tl;
u64 unneeded_before = 0UL;
u64 unneeded = 0, backup = 0;
if (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 > env->before_event1)
unneeded_before = needed_first->time - env->before_event1;
}
while (next) {
tl = rb_entry(next, struct timeline_node, timeline_node);
// if 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 backed-up event is deleted in time,
* see multi_trace_event_lost().
* Do a safety check here.
**/
if (unlikely(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 (unlikely(backup)) {
print_time(stderr);
fprintf(stderr, "free unneeded %lu, backup %lu\n", unneeded, backup);
}
}
static void timeline_stat(struct multi_trace_ctx *ctx)
{
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",
ctx->tl_stat.new, ctx->tl_stat.delete, ctx->tl_stat.unneeded, ctx->tl_stat.pending,
ctx->tl_stat.mem_bytes, ctx->tl_stat.unneeded_bytes, ctx->tl_stat.pending_bytes,
rblist__nr_entries(&ctx->backup));
}
static void monitor_ctx_exit(struct prof_dev *dev);
static int monitor_ctx_init(struct prof_dev *dev)
{
struct env *env = dev->env;
struct multi_trace_ctx *ctx = dev->private;
int i, j, stacks = 0;
struct tep_handle *tep;
int oncpu = prof_dev_ins_oncpu(dev);
struct two_event_options options = {
.keyname = oncpu ? "CPU" : "THREAD",
.perins = env->perins,
.comm = ctx->comm,
.rundelay = strcmp(dev->prof->name, "rundelay") == 0,
.only_print_greater_than = env->only_print_greater_than,
.greater_than = env->greater_than,
.lower_than = env->lower_than,
.hide_than = env->hide_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;
int nr_pull = 0, nr_real_nonpull_tp = 0;
int nr_ringbuffer = 0;
ctx->dev = dev;
ctx->oncpu = oncpu;
ctx->rundelay = options.rundelay;
INIT_LIST_HEAD(&ctx->needed_list);
INIT_LIST_HEAD(&ctx->pending_list);
INIT_LIST_HEAD(&ctx->timeline_lost_list);
tep = tep__ref();
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)
goto failed;
ctx->nr_ins = prof_dev_nr_ins(dev);
ctx->nr_list = env->nr_events;
ctx->tp_list = calloc(ctx->nr_list, sizeof(*ctx->tp_list));
if (!ctx->tp_list)
goto failed;
for (i = 0; i < ctx->nr_list; i++) {
struct tp *tp;
ctx->tp_list[i] = tp_list_new(dev, env->events[i]);
if (!ctx->tp_list[i]) {
goto failed;
}
stacks += ctx->tp_list[i]->nr_need_stack;
for_each_real_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->receive)
nr_pull += env->detail ? 1 : !tp->untraced;
else
nr_real_nonpull_tp += env->detail ? 1 : !tp->untraced;
if (tp->untraced) {
if ((env->samekey || env->samepid || env->sametid) &&
!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);
goto failed;
}
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(dev, CALLCHAIN_KERNEL), stdout);
dev->pages *= 2;
} else
ctx->cc = NULL;
// Each pull event comes from an independent channel and is regarded as a different
// ringbuffer. Need to order.
nr_ringbuffer = nr_pull;
if (keyname) {
options.keyname = keyname;
options.keylen = strlen(keyname);
if (options.keylen < 6)
options.keylen = 6;
// All events are ordered only when nr_ins==1.
nr_ringbuffer += ctx->nr_ins;
if (nr_ringbuffer > 1 && !using_order(dev)) {
fprintf(stderr, "Enable --key or pull= attr, also need to enable --order.\n");
goto failed;
}
} else {
// Use instance as key, cpu or pid.
if (!ctx->oncpu) {
ctx->comm = 1;
options.comm = 1;
}
/* Use instance as key.
* When --detail is enabled, the events of all instance need to be ordered for
* detailed output. Each instance accumulates a ringbuffer.
*
* Do not enable --detail, and use the instance as the key. All events participating
* in latency tracing are ordered on a single instance. No matter how many instances
* there are, ringbuffer is only + 1. Because correctness can be guaranteed without
* enabling --order.
*/
nr_ringbuffer += env->detail ? ctx->nr_ins : !!nr_real_nonpull_tp;
if (nr_ringbuffer > 1 && !using_order(dev)) {
fprintf(stderr, "Enable --detail or pull= attr, also need to enable --order.\n");
goto failed;
}
}
ctx->level = sched_init(ctx->nr_list, ctx->tp_list);
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);
goto failed;
}
ctx->impl = impl_get(env->impl ?: TWO_EVENT_DELAY_IMPL);
if (!ctx->impl) {
fprintf(stderr, "--impl %s not implemented\n", env->impl);
goto failed;
}
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;
ctx->perins_list = malloc(ctx->nr_ins * sizeof(struct list_head));
if (ctx->perins_list) {
for (i = 0; i < ctx->nr_ins; i++)
INIT_LIST_HEAD(&ctx->perins_list[i]);
} else
goto failed;
if (keyname) {
ctx->lost_affect = LOST_AFFECT_ALL_EVENT;
} else {
// use instance as key, cpu or pid.
ctx->lost_affect = LOST_AFFECT_INS_EVENT;
ctx->perins_lost_list = malloc(ctx->nr_ins * sizeof(struct list_head));
if (ctx->perins_lost_list) {
for (i = 0; i < ctx->nr_ins; i++)
INIT_LIST_HEAD(&ctx->perins_lost_list[i]);
} else
goto failed;
}
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->lower_than) && env->detail) {
fprintf(stderr, "WARN: --than parameter is not enabled. No need to enable the "
"--detail parameter.\n");
}
return 0;
failed:
monitor_ctx_exit(dev);
return -1;
}
static void monitor_ctx_exit(struct prof_dev *dev)
{
struct multi_trace_ctx *ctx = dev->private;
struct lost_node *lost, *next;
int i;
while (multi_trace_first_pending(dev, NULL)) ;
perf_thread_map__put(ctx->thread_map);
rblist__exit(&ctx->backup);
rblist__exit(&ctx->timeline);
if (ctx->perins_lost_list) {
for (i = 0; i < ctx->nr_ins; i++)
list_for_each_entry_safe(lost, next, &ctx->perins_lost_list[i], lost_link)
free(lost);
free(ctx->perins_lost_list);
} else
list_for_each_entry_safe(lost, next, &ctx->timeline_lost_list, lost_link)
free(lost);
free(ctx->perins_list);
if (ctx->impl && ctx->class)
ctx->impl->class_delete(ctx->class);
callchain_ctx_free(ctx->cc);
if (ctx->tp_list) {
int i;
for (i = 0; i < ctx->nr_list; i++)
tp_list_free(ctx->tp_list[i]);
free(ctx->tp_list);
}
tep__unref();
free(ctx);
}
static int __multi_trace_init(struct prof_dev *dev)
{
struct perf_evlist *evlist = dev->evlist;
struct multi_trace_ctx *ctx = dev->private;
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_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(dev, CALLCHAIN_KERNEL),
.exclude_callchain_kernel = exclude_callchain_kernel(dev, CALLCHAIN_KERNEL),
.watermark = 1,
};
int i, j;
if (monitor_ctx_init(dev) < 0)
return -1;
if (using_order(dev)) {
dev->dup = true;
}
reduce_wakeup_times(dev, &attr);
for (i = 0; i < ctx->nr_list; i++) {
struct tp *tp;
for_each_real_tp(ctx->tp_list[i], tp, j) {
struct perf_evsel *evsel;
if (tp->stack)
attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
else
attr.sample_type &= (~PERF_SAMPLE_CALLCHAIN);
evsel = tp_evsel_new(tp, &attr);
if (!evsel) {
goto failed;
}
perf_evlist__add(evlist, evsel);
}
for_each_dev_tp(ctx->tp_list[i], tp, j) {
struct prof_dev *source_dev = tp->source_dev;
if (source_dev &&
prof_dev_forward(source_dev, dev) == 0) {
// The target is responsible for whether to print title
source_dev->print_title = false;
if (!tp->untraced) {
fprintf(stderr, "%s can only be untraced.\n", source_dev->prof->name);
ctx->tp_list[i]->nr_untraced ++;
}
tp->untraced = true;
/*
* source_dev uses an independent ringbuffer, and order must be enabled
* when forwarding to multi-trace.
*
* perf-prof multi-trace -e kvm:kvm_exit -e 'kvm:kvm_entry,task-state/-m 256/untraced/' \
* -t 210673 -m 128 -i 1000 --than 80us --detail=sametid
*/
if (ctx->need_timeline &&
!using_order(dev)) {
fprintf(stderr, "Enable --detail and %s//, also need to enable --order.\n", source_dev->prof->name);
goto failed;
}
}
}
}
return 0;
failed:
monitor_ctx_exit(dev);
return -1;
}
static int multi_trace_init(struct prof_dev *dev)
{
int i, j, k, n;
struct env *env = dev->env;
struct multi_trace_ctx *ctx = dev->private;
if (!ctx) {
dev->private = ctx = zalloc(sizeof(*ctx));
if (!ctx)
return -1;
}
ctx->nested = 0;
if (__multi_trace_init(dev) < 0)
return -1;
// env->cycle: from the last one back to the first.
for (k = 0; k < ctx->nr_list - !env->cycle; k++) {
struct tp *tp1, *tp2;
for_each_real_tp(ctx->tp_list[k], tp1, i) {
if (tp1->untraced)
continue;
// for handle remaining
if (!ctx->impl->object_new(ctx->class, tp1, NULL))
goto failed;
n = (k+1) % ctx->nr_list;
for_each_real_tp(ctx->tp_list[n], tp2, j) {
if (tp2->untraced)
continue;
if (!ctx->impl->object_new(ctx->class, tp1, tp2))
goto failed;
}
}
}
return 0;
failed:
monitor_ctx_exit(dev);
return -1;
}
static int multi_trace_filter(struct prof_dev *dev)
{
struct multi_trace_ctx *ctx = dev->private;
int i, err;
for (i = 0; i < ctx->nr_list; i++) {
if ((err = tp_list_apply_filter(dev, ctx->tp_list[i])) < 0)
return err;
}
return 0;
}
static void multi_trace_enabled(struct prof_dev *dev)
{
/*
* 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.
**/
/*
* See prof_dev_atomic_enable().
*/
}
static int multi_trace_call_remaining(struct prof_dev *dev, struct timeline_node *left, remaining_reason rr)
{
struct env *env = dev->env;
struct multi_trace_ctx *ctx = dev->private;
struct two_event *two;
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;
info.rr = rr;
if (ctx->need_timeline) {
if (env->before_event1) {
struct timeline_node backup = {
.time = left->time - 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 > env->greater_than)
left->maybe_unpaired = 1;
return ctx->class->remaining(two, left->event, &info, ctx->need_timeline ? &iter : NULL);
}
return REMAINING_CONTINUE;
}
static void multi_trace_handle_remaining(struct prof_dev *dev, remaining_reason rr)
{
struct multi_trace_ctx *ctx = dev->private;
struct rb_node *next = rb_first_cached(&ctx->backup.entries);
struct timeline_node *left;
while (next) {
left = rb_entry(next, struct timeline_node, key_node);
if (multi_trace_call_remaining(dev, left, rr) == REMAINING_BREAK)
break;
next = rb_next(next);
}
}
static void multi_trace_interval(struct prof_dev *dev)
{
struct multi_trace_ctx *ctx = dev->private;
int i, j, k, n;
int header = 0;
struct two_event *two;
// env->cycle: from the last one back to the first.
for (k = 0; k < ctx->nr_list - !dev->env->cycle; k++) {
struct tp *tp1, *tp2;
for_each_real_tp(ctx->tp_list[k], tp1, 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_each_real_tp(ctx->tp_list[n], tp2, 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 prof_dev *dev)
{
multi_trace_interval(dev);
monitor_ctx_exit(dev);
}
static void multi_trace_flush(struct prof_dev *dev, enum profdev_flush how)
{
if (how == PROF_DEV_FLUSH_FINAL) {
struct multi_trace_ctx *ctx = dev->private;
multi_trace_handle_remaining(dev, REMAINING_EXIT);
while (multi_trace_first_pending(dev, NULL)) ;
rblist__exit(&ctx->backup);
rblist__exit(&ctx->timeline);
}
}
static u64 multi_trace_minevtime(struct prof_dev *dev)
{
struct multi_trace_ctx *ctx = dev->private;
u64 minevtime = ULLONG_MAX;
if (dev->env->greater_than || dev->env->lower_than) {
struct rb_node *rbn;
struct timeline_node *node = NULL, *tmp;
if (ctx->need_timeline) {
rbn = rb_first_cached(&ctx->timeline.entries);
node = rb_entry_safe(rbn, struct timeline_node, timeline_node);
} else {
int i;
for (i = 0; i < ctx->nr_ins; i++) {
tmp = list_first_entry_or_null(&ctx->perins_list[i], struct timeline_node, needed);
if (tmp && (!node || tmp->time < node->time))
node = tmp;
}
}
if (node && node->time < minevtime)
minevtime = node->time;
}
if (dev->env->perins && ctx->comm) {
u64 mintime = 0;
if (dev->env->interval && ctx->recent_time > dev->env->interval * NSEC_PER_MSEC)
mintime = ctx->recent_time - dev->env->interval * NSEC_PER_MSEC;
if (mintime < minevtime)
minevtime = mintime;
}
return minevtime;
}
static void multi_trace_sigusr(struct prof_dev *dev, int signum)
{
struct multi_trace_ctx *ctx = dev->private;
if (signum != SIGUSR1)
return;
if (ctx->need_timeline)
timeline_stat(ctx);
else {
if (dev->dup)
printf("DUP STAT:\n"
" nr_samples = %lu\n"
" nr_free = %lu\n"
" nr_unfree = %lu\n",
ctx->dup_stat.nr_samples, ctx->dup_stat.nr_free + ctx->backup_stat.delete,
ctx->dup_stat.nr_samples - ctx->dup_stat.nr_free - ctx->backup_stat.delete);
printf("BACKUP:\n"
" new = %lu\n"
" delete = %lu\n"
" nr_entries = %u\n"
" mem_bytes = %lu\n",
ctx->backup_stat.new, ctx->backup_stat.delete, rblist__nr_entries(&ctx->backup),
ctx->backup_stat.mem_bytes);
}
printf("SPECIAL EVENT:\n");
printf(" sched:sched_wakeup unnecessary %lu\n", ctx->sched_wakeup_unnecessary);
}
static inline void reclaim(struct prof_dev *dev, u64 key, remaining_reason rr)
{
struct multi_trace_ctx *ctx = dev->private;
struct rb_node *node, *next;
struct timeline_node backup = {
.key = key,
};
int remaining = REMAINING_CONTINUE;
// Remove all events with the same key.
node = rb_find_first(&backup, &ctx->backup.entries.rb_root, perf_event_backup_node_find);
while (node) {
next = rb_next_match(&backup, node, perf_event_backup_node_find);
if (remaining == REMAINING_CONTINUE) {
struct timeline_node *left = rb_entry(node, struct timeline_node, key_node);
remaining = multi_trace_call_remaining(dev, left, rr);
}
rblist__remove_node(&ctx->backup, node);
node = next;
}
}
static inline void lost_reclaim(struct prof_dev *dev, int ins)
{
struct multi_trace_ctx *ctx = dev->private;
if (ctx->lost_affect == LOST_AFFECT_INS_EVENT) {
u64 key = ctx->oncpu ? prof_dev_ins_cpu(dev, ins) : prof_dev_ins_thread(dev, ins);
reclaim(dev, key, REMAINING_LOST);
} else {
multi_trace_handle_remaining(dev, REMAINING_LOST);
rblist__exit(&ctx->backup);
}
}
static void multi_trace_print_lost(struct prof_dev *dev, union perf_event *event, int ins)
{
struct multi_trace_ctx *ctx = dev->private;
struct lost_node *lost;
if (event)
return print_lost_fn(dev, event, ins);
if (ctx->lost_affect == LOST_AFFECT_INS_EVENT)
lost = list_first_entry(&ctx->perins_lost_list[ins], struct lost_node, lost_link);
else
lost = list_first_entry(&ctx->timeline_lost_list, struct lost_node, lost_link);
print_time(stderr);
fprintf(stderr, "%s: lost %lu events on %s #%d", dev->prof->name, lost->lost,
ctx->oncpu ? "CPU" : "thread",
ctx->oncpu ? prof_dev_ins_cpu(dev, lost->ins) : prof_dev_ins_thread(dev, lost->ins));
if (dev->env->greater_than || dev->env->lower_than)
fprintf(stderr, " (%lu.%06lu, %lu.%06lu)\n", lost->start_time/NSEC_PER_SEC, (lost->start_time%NSEC_PER_SEC)/1000,
lost->end_time/NSEC_PER_SEC, (lost->end_time%NSEC_PER_SEC)/1000);
else
fprintf(stderr, "\n");
}
static void multi_trace_lost(struct prof_dev *dev, union perf_event *event, int ins, u64 lost_start, u64 lost_end)
{
struct multi_trace_ctx *ctx = dev->private;
struct lost_node *pos;
struct lost_node *lost;