Repository navigation
Expand file tree
/
Copy pathannotate.c
More file actions
3254 lines (2707 loc) · 79.6 KB
/
Copy pathannotate.c
File metadata and controls
3254 lines (2707 loc) · 79.6 KB
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
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
*
* Parts came from builtin-annotate.c, see those files for further
* copyright notes.
*/
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include "util.h" // hex_width()
#include "ui/ui.h"
#include "sort.h"
#include "build-id.h"
#include "color.h"
#include "config.h"
#include "disasm.h"
#include "dso.h"
#include "env.h"
#include "map.h"
#include "maps.h"
#include "symbol.h"
#include "srcline.h"
#include "units.h"
#include "debug.h"
#include "debuginfo.h"
#include "annotate.h"
#include "annotate-data.h"
#include "evsel.h"
#include "evlist.h"
#include "bpf-event.h"
#include "bpf-utils.h"
#include "block-range.h"
#include "string2.h"
#include "dwarf-regs.h"
#include "util/event.h"
#include "util/sharded_mutex.h"
#include "arch/common.h"
#include "namespaces.h"
#include "thread.h"
#include "hashmap.h"
#include "strbuf.h"
#include <regex.h>
#include <linux/bitops.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/zalloc.h>
#include <subcmd/parse-options.h>
#include <subcmd/run-command.h>
#include <math.h>
/* FIXME: For the HE_COLORSET */
#include "ui/browser.h"
/*
* FIXME: Using the same values as slang.h,
* but that header may not be available everywhere
*/
#define LARROW_CHAR ((unsigned char)',')
#define RARROW_CHAR ((unsigned char)'+')
#define DARROW_CHAR ((unsigned char)'.')
#define UARROW_CHAR ((unsigned char)'-')
#include <linux/ctype.h>
/* global annotation options */
struct annotation_options annotate_opts;
/* Data type collection debug statistics */
struct annotated_data_stat ann_data_stat;
LIST_HEAD(ann_insn_stat);
/* Pseudo data types */
struct annotated_data_type stackop_type = {
.self = {
.type_name = (char *)"(stack operation)",
.children = LIST_HEAD_INIT(stackop_type.self.children),
},
};
struct annotated_data_type canary_type = {
.self = {
.type_name = (char *)"(stack canary)",
.children = LIST_HEAD_INIT(canary_type.self.children),
},
};
#define NO_TYPE ((struct annotated_data_type *)-1UL)
/* symbol histogram: key = offset << 16 | evsel->core.idx */
static size_t sym_hist_hash(long key, void *ctx __maybe_unused)
{
return (key >> 16) + (key & 0xffff);
}
static bool sym_hist_equal(long key1, long key2, void *ctx __maybe_unused)
{
return key1 == key2;
}
static struct annotated_source *annotated_source__new(void)
{
struct annotated_source *src = zalloc(sizeof(*src));
if (src != NULL)
INIT_LIST_HEAD(&src->source);
return src;
}
static __maybe_unused void annotated_source__delete(struct annotated_source *src)
{
struct hashmap_entry *cur;
size_t bkt;
if (src == NULL)
return;
if (src->samples) {
hashmap__for_each_entry(src->samples, cur, bkt)
zfree(&cur->pvalue);
hashmap__free(src->samples);
}
zfree(&src->histograms);
free(src);
}
static int annotated_source__alloc_histograms(struct annotated_source *src,
int nr_hists)
{
src->nr_histograms = nr_hists;
src->histograms = calloc(nr_hists, sizeof(*src->histograms));
if (src->histograms == NULL)
return -1;
src->samples = hashmap__new(sym_hist_hash, sym_hist_equal, NULL);
if (IS_ERR(src->samples)) {
zfree(&src->histograms);
src->samples = NULL;
}
return src->histograms ? 0 : -1;
}
struct sym_hist_entry *
annotated_source__hist_entry(struct annotated_source *src, const struct evsel *evsel, u64 offset)
{
struct sym_hist_entry *entry;
long key = offset << 16 | evsel->core.idx;
if (!hashmap__find(src->samples, key, &entry))
return NULL;
return entry;
}
void symbol__annotate_zero_histograms(struct symbol *sym)
{
struct annotation *notes = symbol__annotation(sym);
annotation__lock(notes);
if (notes->src != NULL) {
memset(notes->src->histograms, 0,
notes->src->nr_histograms * sizeof(*notes->src->histograms));
hashmap__clear(notes->src->samples);
}
if (notes->branch && notes->branch->cycles_hist) {
memset(notes->branch->cycles_hist, 0,
symbol__size(sym) * sizeof(struct cyc_hist));
}
annotation__unlock(notes);
}
static int __symbol__account_cycles(struct cyc_hist *ch,
u64 start,
unsigned offset, unsigned cycles,
unsigned have_start)
{
/*
* For now we can only account one basic block per
* final jump. But multiple could be overlapping.
* Always account the longest one. So when
* a shorter one has been already seen throw it away.
*
* We separately always account the full cycles.
*/
ch[offset].num_aggr++;
ch[offset].cycles_aggr += cycles;
if (cycles > ch[offset].cycles_max)
ch[offset].cycles_max = cycles;
if (ch[offset].cycles_min) {
if (cycles && cycles < ch[offset].cycles_min)
ch[offset].cycles_min = cycles;
} else
ch[offset].cycles_min = cycles;
if (!have_start && ch[offset].have_start)
return 0;
if (ch[offset].num) {
if (have_start && (!ch[offset].have_start ||
ch[offset].start > start)) {
ch[offset].have_start = 0;
ch[offset].cycles = 0;
ch[offset].num = 0;
if (ch[offset].reset < 0xffff)
ch[offset].reset++;
} else if (have_start &&
ch[offset].start < start)
return 0;
}
if (ch[offset].num < NUM_SPARKS)
ch[offset].cycles_spark[ch[offset].num] = cycles;
ch[offset].have_start = have_start;
ch[offset].start = start;
ch[offset].cycles += cycles;
ch[offset].num++;
return 0;
}
static int __symbol__inc_addr_samples(struct map_symbol *ms,
struct annotated_source *src, u64 addr,
struct perf_sample *sample)
{
struct evsel *evsel = sample->evsel;
struct symbol *sym = ms->sym;
long hash_key;
u64 offset;
struct sym_hist *h;
struct sym_hist_entry *entry;
u64 weight = sample->weight ?: sample->ins_lat ?: sample->weight3;
pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map__unmap_ip(ms->map, addr));
if ((addr < sym->start || addr >= sym->end) &&
(addr != sym->end || sym->start != sym->end)) {
pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
__func__, __LINE__, sym->name, sym->start, addr, sym->end);
return -ERANGE;
}
offset = addr - sym->start;
h = annotated_source__histogram(src, evsel);
if (h == NULL) {
pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n",
__func__, __LINE__, sym->name, sym->start, addr, sym->end,
symbol__type(sym) == STT_FUNC);
return -ENOMEM;
}
hash_key = offset << 16 | evsel->core.idx;
if (!hashmap__find(src->samples, hash_key, &entry)) {
entry = zalloc(sizeof(*entry));
if (entry == NULL)
return -ENOMEM;
if (hashmap__add(src->samples, hash_key, entry) < 0)
return -ENOMEM;
}
h->nr_samples++;
h->period += sample->period;
entry->nr_samples++;
entry->period += sample->period;
if (sample->evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
entry->weight_sum[WEIGHT_WEIGHT] += sample->weight;
entry->weight_num[WEIGHT_WEIGHT]++;
if (sample->weight)
sym_hist__set_weight_mask(h, BIT(WEIGHT_WEIGHT));
}
if (sample->evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT) {
entry->weight_sum[WEIGHT_INSNLAT] += sample->ins_lat;
entry->weight_num[WEIGHT_INSNLAT]++;
entry->weight_sum[WEIGHT_WEIGHT3] += sample->weight3;
entry->weight_num[WEIGHT_WEIGHT3]++;
if (sample->ins_lat)
sym_hist__set_weight_mask(h, BIT(WEIGHT_INSNLAT));
if (sample->weight3)
sym_hist__set_weight_mask(h, BIT(WEIGHT_WEIGHT3));
}
pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64
" weight %" PRIu64 "\n",
sym->start, sym->name, addr, addr - sym->start, evsel->core.idx,
entry->nr_samples, entry->period, weight);
return 0;
}
struct annotated_branch *annotation__get_branch(struct annotation *notes)
{
if (notes == NULL)
return NULL;
if (notes->branch == NULL)
notes->branch = zalloc(sizeof(*notes->branch));
return notes->branch;
}
static struct annotated_branch *symbol__find_branch_hist(struct symbol *sym,
unsigned int br_cntr_nr)
{
struct annotation *notes = symbol__annotation(sym);
struct annotated_branch *branch;
const size_t size = symbol__size(sym);
branch = annotation__get_branch(notes);
if (branch == NULL)
return NULL;
if (branch->cycles_hist == NULL) {
branch->cycles_hist = calloc(size, sizeof(struct cyc_hist));
if (!branch->cycles_hist)
return NULL;
}
if (br_cntr_nr && branch->br_cntr == NULL) {
branch->br_cntr = calloc(br_cntr_nr * size, sizeof(u64));
if (!branch->br_cntr)
return NULL;
}
return branch;
}
struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists)
{
struct annotation *notes = symbol__annotation(sym);
if (notes->src == NULL) {
notes->src = annotated_source__new();
if (notes->src == NULL)
return NULL;
goto alloc_histograms;
}
if (notes->src->histograms == NULL) {
alloc_histograms:
annotated_source__alloc_histograms(notes->src, nr_hists);
}
return notes->src;
}
static int symbol__inc_addr_samples(struct map_symbol *ms,
u64 addr,
struct perf_sample *sample)
{
struct symbol *sym = ms->sym;
struct annotated_source *src;
if (sym == NULL)
return 0;
src = symbol__hists(sym, evlist__nr_entries(sample->evsel->evlist));
return src ? __symbol__inc_addr_samples(ms, src, addr, sample) : 0;
}
static int symbol__account_br_cntr(struct annotated_branch *branch,
struct evsel *evsel,
unsigned offset,
u64 br_cntr)
{
unsigned int br_cntr_nr = evsel__leader(evsel)->br_cntr_nr;
unsigned int base = evsel__leader(evsel)->br_cntr_idx;
unsigned int off = offset * evlist__nr_br_cntr(evsel->evlist);
u64 *branch_br_cntr = branch->br_cntr;
unsigned int i, mask, width;
if (!br_cntr || !branch_br_cntr)
return 0;
perf_env__find_br_cntr_info(evsel__env(evsel), NULL, &width);
mask = (1L << width) - 1;
for (i = 0; i < br_cntr_nr; i++) {
u64 cntr = (br_cntr >> i * width) & mask;
branch_br_cntr[off + i + base] += cntr;
if (cntr == mask)
branch_br_cntr[off + i + base] |= ANNOTATION__BR_CNTR_SATURATED_FLAG;
}
return 0;
}
static int symbol__account_cycles(u64 addr, u64 start, struct symbol *sym,
unsigned cycles, struct evsel *evsel,
u64 br_cntr)
{
struct annotated_branch *branch;
unsigned offset;
int ret;
if (sym == NULL)
return 0;
branch = symbol__find_branch_hist(sym, evlist__nr_br_cntr(evsel->evlist));
if (!branch)
return -ENOMEM;
if (addr < sym->start || addr >= sym->end)
return -ERANGE;
if (start) {
if (start < sym->start || start >= sym->end)
return -ERANGE;
if (start >= addr)
start = 0;
}
offset = addr - sym->start;
ret = __symbol__account_cycles(branch->cycles_hist,
start ? start - sym->start : 0,
offset, cycles,
!!start);
if (ret)
return ret;
return symbol__account_br_cntr(branch, evsel, offset, br_cntr);
}
int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
struct addr_map_symbol *start,
unsigned cycles,
struct evsel *evsel,
u64 br_cntr)
{
u64 saddr = 0;
int err;
if (!cycles)
return 0;
/*
* Only set start when IPC can be computed. We can only
* compute it when the basic block is completely in a single
* function.
* Special case the case when the jump is elsewhere, but
* it starts on the function start.
*/
if (start &&
(start->ms.sym == ams->ms.sym ||
(ams->ms.sym &&
start->addr == ams->ms.sym->start + map__start(ams->ms.map))))
saddr = start->al_addr;
if (saddr == 0)
pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
ams->addr,
start ? start->addr : 0,
ams->ms.sym ? ams->ms.sym->start + map__start(ams->ms.map) : 0,
saddr);
err = symbol__account_cycles(ams->al_addr, saddr, ams->ms.sym, cycles, evsel, br_cntr);
if (err)
pr_debug2("account_cycles failed %d\n", err);
return err;
}
struct annotation_line *annotated_source__get_line(struct annotated_source *src,
s64 offset)
{
struct annotation_line *al;
list_for_each_entry(al, &src->source, node) {
if (al->offset == offset)
return al;
}
return NULL;
}
static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end)
{
struct annotation_line *al;
unsigned n_insn = 0;
al = annotated_source__get_line(notes->src, start);
if (al == NULL)
return 0;
list_for_each_entry_from(al, ¬es->src->source, node) {
if (al->offset == -1)
continue;
if ((u64)al->offset > end)
break;
n_insn++;
}
return n_insn;
}
static void annotated_branch__delete(struct annotated_branch *branch)
{
if (branch) {
zfree(&branch->cycles_hist);
free(branch->br_cntr);
free(branch);
}
}
static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch)
{
unsigned n_insn;
unsigned int cover_insn = 0;
n_insn = annotation__count_insn(notes, start, end);
if (n_insn && ch->num && ch->cycles) {
struct annotation_line *al;
struct annotated_branch *branch;
float ipc = n_insn / ((double)ch->cycles / (double)ch->num);
/* Hide data when there are too many overlaps. */
if (ch->reset >= 0x7fff)
return;
al = annotated_source__get_line(notes->src, start);
if (al == NULL)
return;
list_for_each_entry_from(al, ¬es->src->source, node) {
if (al->offset == -1)
continue;
if ((u64)al->offset > end)
break;
if (al->cycles && al->cycles->ipc == 0.0) {
al->cycles->ipc = ipc;
cover_insn++;
}
}
branch = annotation__get_branch(notes);
if (cover_insn && branch) {
branch->hit_cycles += ch->cycles;
branch->hit_insn += n_insn * ch->num;
branch->cover_insn += cover_insn;
}
}
}
static int annotation__compute_ipc(struct annotation *notes, size_t size,
struct evsel *evsel)
{
unsigned int br_cntr_nr = evlist__nr_br_cntr(evsel->evlist);
int err = 0;
s64 offset;
if (!notes->branch || !notes->branch->cycles_hist)
return 0;
notes->branch->total_insn = annotation__count_insn(notes, 0, size - 1);
notes->branch->hit_cycles = 0;
notes->branch->hit_insn = 0;
notes->branch->cover_insn = 0;
annotation__lock(notes);
for (offset = size - 1; offset >= 0; --offset) {
struct cyc_hist *ch;
ch = ¬es->branch->cycles_hist[offset];
if (ch && ch->cycles) {
struct annotation_line *al;
al = annotated_source__get_line(notes->src, offset);
if (al && al->cycles == NULL) {
al->cycles = zalloc(sizeof(*al->cycles));
if (al->cycles == NULL) {
err = ENOMEM;
break;
}
}
if (ch->have_start)
annotation__count_and_fill(notes, ch->start, offset, ch);
if (al && ch->num_aggr) {
al->cycles->avg = ch->cycles_aggr / ch->num_aggr;
al->cycles->max = ch->cycles_max;
al->cycles->min = ch->cycles_min;
}
if (al && notes->branch->br_cntr) {
if (!al->br_cntr) {
al->br_cntr = calloc(br_cntr_nr, sizeof(u64));
if (!al->br_cntr) {
err = ENOMEM;
break;
}
}
al->num_aggr = ch->num_aggr;
al->br_cntr_nr = br_cntr_nr;
al->evsel = evsel;
memcpy(al->br_cntr, ¬es->branch->br_cntr[offset * br_cntr_nr],
br_cntr_nr * sizeof(u64));
}
}
}
if (err) {
while (++offset < (s64)size) {
struct cyc_hist *ch = ¬es->branch->cycles_hist[offset];
if (ch && ch->cycles) {
struct annotation_line *al;
al = annotated_source__get_line(notes->src, offset);
if (al) {
zfree(&al->cycles);
zfree(&al->br_cntr);
}
}
}
}
annotation__unlock(notes);
return 0;
}
int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample)
{
return symbol__inc_addr_samples(&ams->ms, ams->al_addr, sample);
}
int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, u64 ip)
{
return symbol__inc_addr_samples(&he->ms, ip, sample);
}
void annotation__exit(struct annotation *notes)
{
annotated_source__delete(notes->src);
annotated_branch__delete(notes->branch);
}
static struct sharded_mutex *sharded_mutex;
static void annotation__init_sharded_mutex(void)
{
/* As many mutexes as there are CPUs. */
sharded_mutex = sharded_mutex__new(cpu__max_present_cpu().cpu);
}
static size_t annotation__hash(const struct annotation *notes)
{
return (size_t)notes;
}
static struct mutex *annotation__get_mutex(const struct annotation *notes)
{
static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(&once, annotation__init_sharded_mutex);
if (!sharded_mutex)
return NULL;
return sharded_mutex__get_mutex(sharded_mutex, annotation__hash(notes));
}
void annotation__lock(struct annotation *notes)
NO_THREAD_SAFETY_ANALYSIS
{
struct mutex *mutex = annotation__get_mutex(notes);
if (mutex)
mutex_lock(mutex);
}
void annotation__unlock(struct annotation *notes)
NO_THREAD_SAFETY_ANALYSIS
{
struct mutex *mutex = annotation__get_mutex(notes);
if (mutex)
mutex_unlock(mutex);
}
bool annotation__trylock(struct annotation *notes)
{
struct mutex *mutex = annotation__get_mutex(notes);
if (!mutex)
return false;
return mutex_trylock(mutex);
}
void annotation_line__add(struct annotation_line *al, struct list_head *head)
{
list_add_tail(&al->node, head);
}
struct annotation_line *
annotation_line__next(struct annotation_line *pos, struct list_head *head)
{
list_for_each_entry_continue(pos, head, node)
if (pos->offset >= 0)
return pos;
return NULL;
}
static const char *annotate__address_color(struct block_range *br)
{
double cov = block_range__coverage(br);
if (cov >= 0) {
/* mark red for >75% coverage */
if (cov > 0.75)
return PERF_COLOR_RED;
/* mark dull for <1% coverage */
if (cov < 0.01)
return PERF_COLOR_NORMAL;
}
return PERF_COLOR_MAGENTA;
}
static const char *annotate__asm_color(struct block_range *br)
{
double cov = block_range__coverage(br);
if (cov >= 0) {
/* mark dull for <1% coverage */
if (cov < 0.01)
return PERF_COLOR_NORMAL;
}
return PERF_COLOR_BLUE;
}
static void annotate__branch_printf(struct block_range *br, u64 addr)
{
bool emit_comment = true;
if (!br)
return;
#if 1
if (br->is_target && br->start == addr) {
struct block_range *branch = br;
double p;
/*
* Find matching branch to our target.
*/
while (!branch->is_branch)
branch = block_range__next(branch);
p = 100 *(double)br->entry / branch->coverage;
if (p > 0.1) {
if (emit_comment) {
emit_comment = false;
printf("\t#");
}
/*
* The percentage of coverage joined at this target in relation
* to the next branch.
*/
printf(" +%.2f%%", p);
}
}
#endif
if (br->is_branch && br->end == addr) {
double p = 100*(double)br->taken / br->coverage;
if (p > 0.1) {
if (emit_comment) {
emit_comment = false;
printf("\t#");
}
/*
* The percentage of coverage leaving at this branch, and
* its prediction ratio.
*/
printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
}
}
}
static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width)
{
s64 offset = dl->al.offset;
const u64 addr = start + offset;
struct block_range *br;
br = block_range__find(addr);
color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr);
color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
annotate__branch_printf(br, addr);
return 0;
}
static struct annotated_data_type *
__hist_entry__get_data_type(struct hist_entry *he, const struct arch *arch,
struct debuginfo *dbg, struct disasm_line *dl,
int *type_offset);
static bool needs_type_info(struct annotated_data_type *data_type)
{
if (data_type == NULL || data_type == NO_TYPE)
return false;
if (verbose)
return true;
return (data_type != &stackop_type) && (data_type != &canary_type);
}
static const char *annotation__weight_mode_str(enum symbol__weight_mode mode,
const struct evsel *evsel)
{
switch (mode) {
case WEIGHT_NONE:
return "";
case WEIGHT_WEIGHT:
return "Weight";
case WEIGHT_INSNLAT:
return "InsnLat";
case WEIGHT_WEIGHT3:
switch (evsel__e_machine((struct evsel *)evsel, NULL)) {
case EM_PPC:
case EM_PPC64:
return "PCycleLat";
case EM_X86_64:
return "RetireLat";
default:
return "Weight3";
}
default:
return "";
}
}
static void annotation__column_title(char *buf, size_t size,
struct annotation *notes,
const struct evsel *evsel)
{
const char *base = symbol_conf.show_total_period ? "Period" :
symbol_conf.show_nr_samples ? "Samples" : "Percent";
int weight;
u8 weight_mask = annotation__weight_mask(notes, evsel);
scnprintf(buf, size, "%s", base);
for_each_weight(weight) {
if (weight_mask & BIT(weight))
scnprintf(buf + strlen(buf),
size - strlen(buf),
" %s",
annotation__weight_mode_str(weight, evsel));
}
}
static int
annotation_line__print(struct annotation_line *al, struct annotation_print_data *apd,
struct annotation_options *opts, int printed,
struct annotation_line *queue)
{
struct symbol *sym = apd->he->ms.sym;
struct disasm_line *dl = container_of(al, struct disasm_line, al);
struct annotation *notes = symbol__annotation(sym);
static const char *prev_line;
int max_lines = opts->max_lines;
int percent_type = opts->percent_type;
if (al->offset != -1) {
double max_percent = 0.0;
int i, nr_percent = 1;
const char *color;
for (i = 0; i < al->data_nr; i++) {
double percent;
percent = annotation_data__percent(&al->data[i],
percent_type);
if (percent > max_percent)
max_percent = percent;
}
if (al->data_nr > nr_percent)
nr_percent = al->data_nr;
if (max_percent < opts->min_pcnt)
return -1;
if (max_lines && printed >= max_lines)
return 1;
if (queue != NULL) {
struct annotation_options queue_opts = {
.max_lines = 1,
.percent_type = percent_type,
};
list_for_each_entry_from(queue, ¬es->src->source, node) {
if (queue == al)
break;
annotation_line__print(queue, apd, &queue_opts,
/*printed=*/0, /*queue=*/NULL);
}
}
color = get_percent_color(max_percent);
for (i = 0; i < nr_percent; i++) {
struct annotation_data *data = &al->data[i];
double percent;
int weight;
u8 weight_mask = annotation__weight_mask(notes, apd->evsel);
percent = annotation_data__percent(data, percent_type);
color = get_percent_color(percent);
if (symbol_conf.show_total_period)
color_fprintf(stdout, color, " %11" PRIu64,
data->he.period);
else if (symbol_conf.show_nr_samples)
color_fprintf(stdout, color, " %7" PRIu64,
data->he.nr_samples);
else
color_fprintf(stdout, color, " %7.2f", percent);
for_each_weight(weight) {
if (weight_mask & BIT(weight))
color_fprintf(stdout, color, " %7" PRIu64,
data->he.weight_num[weight] ?
data->he.weight_sum[weight] /
data->he.weight_num[weight]
: 0);
}
}
printf(" : ");
disasm_line__print(dl, notes->src->start, apd->addr_fmt_width);
if (opts->code_with_type && apd->dbg) {
struct annotated_data_type *data_type;
int offset = 0;
data_type = __hist_entry__get_data_type(apd->he, apd->arch,
apd->dbg, dl, &offset);
if (needs_type_info(data_type)) {
char buf[4096];
printf("\t\t# data-type: %s",
data_type->self.type_name);
if (data_type != &stackop_type &&
data_type != &canary_type)
printf(" +%#x", offset);
if (annotated_data_type__get_member_name(data_type,
buf,
sizeof(buf),
offset))
printf(" (%s)", buf);
}
}
/*
* Also color the filename and line if needed, with
* the same color than the percentage. Don't print it
* twice for close colored addr with the same filename:line
*/
if (al->path) {
if (!prev_line || strcmp(prev_line, al->path)) {
color_fprintf(stdout, color, " // %s", al->path);
prev_line = al->path;
}
}
printf("\n");
} else if (max_lines && printed >= max_lines)
return 1;
else {
int width = annotation__pcnt_width(notes, apd->evsel);
if (queue)
return -1;
if (!*al->line)
printf(" %*s:\n", width, " ");
else
printf(" %*s: %-*d %s\n", width, " ", apd->addr_fmt_width,
al->line_nr, al->line);
}
return 0;
}
static void calc_percent(struct annotation *notes,
struct evsel *evsel,
struct annotation_data *data,
s64 offset, s64 end)
{
struct hists *hists = evsel__hists(evsel);
struct sym_hist *sym_hist = annotation__histogram(notes, evsel);
unsigned int hits = 0;