]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - tools/perf/tests/hists_common.c
perf tools: Including missing inttypes.h header
[mirror_ubuntu-focal-kernel.git] / tools / perf / tests / hists_common.c
1 #include <inttypes.h>
2 #include "perf.h"
3 #include "util/debug.h"
4 #include "util/symbol.h"
5 #include "util/sort.h"
6 #include "util/evsel.h"
7 #include "util/evlist.h"
8 #include "util/machine.h"
9 #include "util/thread.h"
10 #include "tests/hists_common.h"
11 #include <linux/kernel.h>
12
13 static struct {
14 u32 pid;
15 const char *comm;
16 } fake_threads[] = {
17 { FAKE_PID_PERF1, "perf" },
18 { FAKE_PID_PERF2, "perf" },
19 { FAKE_PID_BASH, "bash" },
20 };
21
22 static struct {
23 u32 pid;
24 u64 start;
25 const char *filename;
26 } fake_mmap_info[] = {
27 { FAKE_PID_PERF1, FAKE_MAP_PERF, "perf" },
28 { FAKE_PID_PERF1, FAKE_MAP_LIBC, "libc" },
29 { FAKE_PID_PERF1, FAKE_MAP_KERNEL, "[kernel]" },
30 { FAKE_PID_PERF2, FAKE_MAP_PERF, "perf" },
31 { FAKE_PID_PERF2, FAKE_MAP_LIBC, "libc" },
32 { FAKE_PID_PERF2, FAKE_MAP_KERNEL, "[kernel]" },
33 { FAKE_PID_BASH, FAKE_MAP_BASH, "bash" },
34 { FAKE_PID_BASH, FAKE_MAP_LIBC, "libc" },
35 { FAKE_PID_BASH, FAKE_MAP_KERNEL, "[kernel]" },
36 };
37
38 struct fake_sym {
39 u64 start;
40 u64 length;
41 const char *name;
42 };
43
44 static struct fake_sym perf_syms[] = {
45 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
46 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "run_command" },
47 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "cmd_record" },
48 };
49
50 static struct fake_sym bash_syms[] = {
51 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
52 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "xmalloc" },
53 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "xfree" },
54 };
55
56 static struct fake_sym libc_syms[] = {
57 { 700, 100, "malloc" },
58 { 800, 100, "free" },
59 { 900, 100, "realloc" },
60 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "malloc" },
61 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "free" },
62 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "realloc" },
63 };
64
65 static struct fake_sym kernel_syms[] = {
66 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "schedule" },
67 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "page_fault" },
68 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "sys_perf_event_open" },
69 };
70
71 static struct {
72 const char *dso_name;
73 struct fake_sym *syms;
74 size_t nr_syms;
75 } fake_symbols[] = {
76 { "perf", perf_syms, ARRAY_SIZE(perf_syms) },
77 { "bash", bash_syms, ARRAY_SIZE(bash_syms) },
78 { "libc", libc_syms, ARRAY_SIZE(libc_syms) },
79 { "[kernel]", kernel_syms, ARRAY_SIZE(kernel_syms) },
80 };
81
82 struct machine *setup_fake_machine(struct machines *machines)
83 {
84 struct machine *machine = machines__find(machines, HOST_KERNEL_ID);
85 size_t i;
86
87 if (machine == NULL) {
88 pr_debug("Not enough memory for machine setup\n");
89 return NULL;
90 }
91
92 for (i = 0; i < ARRAY_SIZE(fake_threads); i++) {
93 struct thread *thread;
94
95 thread = machine__findnew_thread(machine, fake_threads[i].pid,
96 fake_threads[i].pid);
97 if (thread == NULL)
98 goto out;
99
100 thread__set_comm(thread, fake_threads[i].comm, 0);
101 thread__put(thread);
102 }
103
104 for (i = 0; i < ARRAY_SIZE(fake_mmap_info); i++) {
105 struct perf_sample sample = {
106 .cpumode = PERF_RECORD_MISC_USER,
107 };
108 union perf_event fake_mmap_event = {
109 .mmap = {
110 .pid = fake_mmap_info[i].pid,
111 .tid = fake_mmap_info[i].pid,
112 .start = fake_mmap_info[i].start,
113 .len = FAKE_MAP_LENGTH,
114 .pgoff = 0ULL,
115 },
116 };
117
118 strcpy(fake_mmap_event.mmap.filename,
119 fake_mmap_info[i].filename);
120
121 machine__process_mmap_event(machine, &fake_mmap_event, &sample);
122 }
123
124 for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
125 size_t k;
126 struct dso *dso;
127
128 dso = machine__findnew_dso(machine, fake_symbols[i].dso_name);
129 if (dso == NULL)
130 goto out;
131
132 /* emulate dso__load() */
133 dso__set_loaded(dso, MAP__FUNCTION);
134
135 for (k = 0; k < fake_symbols[i].nr_syms; k++) {
136 struct symbol *sym;
137 struct fake_sym *fsym = &fake_symbols[i].syms[k];
138
139 sym = symbol__new(fsym->start, fsym->length,
140 STB_GLOBAL, fsym->name);
141 if (sym == NULL) {
142 dso__put(dso);
143 goto out;
144 }
145
146 symbols__insert(&dso->symbols[MAP__FUNCTION], sym);
147 }
148
149 dso__put(dso);
150 }
151
152 return machine;
153
154 out:
155 pr_debug("Not enough memory for machine setup\n");
156 machine__delete_threads(machine);
157 return NULL;
158 }
159
160 void print_hists_in(struct hists *hists)
161 {
162 int i = 0;
163 struct rb_root *root;
164 struct rb_node *node;
165
166 if (hists__has(hists, need_collapse))
167 root = &hists->entries_collapsed;
168 else
169 root = hists->entries_in;
170
171 pr_info("----- %s --------\n", __func__);
172 node = rb_first(root);
173 while (node) {
174 struct hist_entry *he;
175
176 he = rb_entry(node, struct hist_entry, rb_node_in);
177
178 if (!he->filtered) {
179 pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
180 i, thread__comm_str(he->thread),
181 he->ms.map->dso->short_name,
182 he->ms.sym->name, he->stat.period);
183 }
184
185 i++;
186 node = rb_next(node);
187 }
188 }
189
190 void print_hists_out(struct hists *hists)
191 {
192 int i = 0;
193 struct rb_root *root;
194 struct rb_node *node;
195
196 root = &hists->entries;
197
198 pr_info("----- %s --------\n", __func__);
199 node = rb_first(root);
200 while (node) {
201 struct hist_entry *he;
202
203 he = rb_entry(node, struct hist_entry, rb_node);
204
205 if (!he->filtered) {
206 pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
207 i, thread__comm_str(he->thread), he->thread->tid,
208 he->ms.map->dso->short_name,
209 he->ms.sym->name, he->stat.period,
210 he->stat_acc ? he->stat_acc->period : 0);
211 }
212
213 i++;
214 node = rb_next(node);
215 }
216 }