1 // SPDX-License-Identifier: GPL-2.0
4 #include "util/debug.h"
5 #include "util/symbol.h"
7 #include "util/evsel.h"
8 #include "util/evlist.h"
9 #include "util/machine.h"
10 #include "util/thread.h"
11 #include "tests/hists_common.h"
12 #include <linux/kernel.h>
18 { FAKE_PID_PERF1, "perf" },
19 { FAKE_PID_PERF2, "perf" },
20 { FAKE_PID_BASH, "bash" },
27 } fake_mmap_info[] = {
28 { FAKE_PID_PERF1, FAKE_MAP_PERF, "perf" },
29 { FAKE_PID_PERF1, FAKE_MAP_LIBC, "libc" },
30 { FAKE_PID_PERF1, FAKE_MAP_KERNEL, "[kernel]" },
31 { FAKE_PID_PERF2, FAKE_MAP_PERF, "perf" },
32 { FAKE_PID_PERF2, FAKE_MAP_LIBC, "libc" },
33 { FAKE_PID_PERF2, FAKE_MAP_KERNEL, "[kernel]" },
34 { FAKE_PID_BASH, FAKE_MAP_BASH, "bash" },
35 { FAKE_PID_BASH, FAKE_MAP_LIBC, "libc" },
36 { FAKE_PID_BASH, FAKE_MAP_KERNEL, "[kernel]" },
45 static struct fake_sym perf_syms[] = {
46 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
47 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "run_command" },
48 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "cmd_record" },
51 static struct fake_sym bash_syms[] = {
52 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
53 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "xmalloc" },
54 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "xfree" },
57 static struct fake_sym libc_syms[] = {
58 { 700, 100, "malloc" },
60 { 900, 100, "realloc" },
61 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "malloc" },
62 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "free" },
63 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "realloc" },
66 static struct fake_sym kernel_syms[] = {
67 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "schedule" },
68 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "page_fault" },
69 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "sys_perf_event_open" },
74 struct fake_sym *syms;
77 { "perf", perf_syms, ARRAY_SIZE(perf_syms) },
78 { "bash", bash_syms, ARRAY_SIZE(bash_syms) },
79 { "libc", libc_syms, ARRAY_SIZE(libc_syms) },
80 { "[kernel]", kernel_syms, ARRAY_SIZE(kernel_syms) },
83 struct machine *setup_fake_machine(struct machines *machines)
85 struct machine *machine = machines__find(machines, HOST_KERNEL_ID);
88 if (machine == NULL) {
89 pr_debug("Not enough memory for machine setup\n");
93 for (i = 0; i < ARRAY_SIZE(fake_threads); i++) {
94 struct thread *thread;
96 thread = machine__findnew_thread(machine, fake_threads[i].pid,
101 thread__set_comm(thread, fake_threads[i].comm, 0);
105 for (i = 0; i < ARRAY_SIZE(fake_mmap_info); i++) {
106 struct perf_sample sample = {
107 .cpumode = PERF_RECORD_MISC_USER,
109 union perf_event fake_mmap_event = {
111 .pid = fake_mmap_info[i].pid,
112 .tid = fake_mmap_info[i].pid,
113 .start = fake_mmap_info[i].start,
114 .len = FAKE_MAP_LENGTH,
119 strcpy(fake_mmap_event.mmap.filename,
120 fake_mmap_info[i].filename);
122 machine__process_mmap_event(machine, &fake_mmap_event, &sample);
125 for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
129 dso = machine__findnew_dso(machine, fake_symbols[i].dso_name);
133 /* emulate dso__load() */
134 dso__set_loaded(dso);
136 for (k = 0; k < fake_symbols[i].nr_syms; k++) {
138 struct fake_sym *fsym = &fake_symbols[i].syms[k];
140 sym = symbol__new(fsym->start, fsym->length,
141 STB_GLOBAL, STT_FUNC, fsym->name);
147 symbols__insert(&dso->symbols, sym);
156 pr_debug("Not enough memory for machine setup\n");
157 machine__delete_threads(machine);
161 void print_hists_in(struct hists *hists)
164 struct rb_root *root;
165 struct rb_node *node;
167 if (hists__has(hists, need_collapse))
168 root = &hists->entries_collapsed;
170 root = hists->entries_in;
172 pr_info("----- %s --------\n", __func__);
173 node = rb_first(root);
175 struct hist_entry *he;
177 he = rb_entry(node, struct hist_entry, rb_node_in);
180 pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
181 i, thread__comm_str(he->thread),
182 he->ms.map->dso->short_name,
183 he->ms.sym->name, he->stat.period);
187 node = rb_next(node);
191 void print_hists_out(struct hists *hists)
194 struct rb_root *root;
195 struct rb_node *node;
197 root = &hists->entries;
199 pr_info("----- %s --------\n", __func__);
200 node = rb_first(root);
202 struct hist_entry *he;
204 he = rb_entry(node, struct hist_entry, rb_node);
207 pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
208 i, thread__comm_str(he->thread), he->thread->tid,
209 he->ms.map->dso->short_name,
210 he->ms.sym->name, he->stat.period,
211 he->stat_acc ? he->stat_acc->period : 0);
215 node = rb_next(node);