blob: 00b8dc50f3dba8ffa1cbcc75938d15718741d75b [file] [log] [blame]
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03001#include <inttypes.h>
Namhyung Kim6e344a92014-04-25 12:28:13 +09002#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"
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030011#include <linux/kernel.h>
Namhyung Kim6e344a92014-04-25 12:28:13 +090012
13static struct {
14 u32 pid;
15 const char *comm;
16} fake_threads[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090017 { FAKE_PID_PERF1, "perf" },
18 { FAKE_PID_PERF2, "perf" },
19 { FAKE_PID_BASH, "bash" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090020};
21
22static struct {
23 u32 pid;
24 u64 start;
25 const char *filename;
26} fake_mmap_info[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090027 { 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]" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090036};
37
38struct fake_sym {
39 u64 start;
40 u64 length;
41 const char *name;
42};
43
44static struct fake_sym perf_syms[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090045 { 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" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090048};
49
50static struct fake_sym bash_syms[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090051 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
52 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "xmalloc" },
53 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "xfree" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090054};
55
56static struct fake_sym libc_syms[] = {
57 { 700, 100, "malloc" },
58 { 800, 100, "free" },
59 { 900, 100, "realloc" },
Namhyung Kima1891aa2014-05-23 14:59:57 +090060 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "malloc" },
61 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "free" },
62 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "realloc" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090063};
64
65static struct fake_sym kernel_syms[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090066 { 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" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090069};
70
71static 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
82struct 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);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300101 thread__put(thread);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900102 }
103
104 for (i = 0; i < ARRAY_SIZE(fake_mmap_info); i++) {
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300105 struct perf_sample sample = {
106 .cpumode = PERF_RECORD_MISC_USER,
107 };
Namhyung Kim6e344a92014-04-25 12:28:13 +0900108 union perf_event fake_mmap_event = {
109 .mmap = {
Namhyung Kim6e344a92014-04-25 12:28:13 +0900110 .pid = fake_mmap_info[i].pid,
111 .tid = fake_mmap_info[i].pid,
112 .start = fake_mmap_info[i].start,
Namhyung Kima1891aa2014-05-23 14:59:57 +0900113 .len = FAKE_MAP_LENGTH,
Namhyung Kim6e344a92014-04-25 12:28:13 +0900114 .pgoff = 0ULL,
115 },
116 };
117
118 strcpy(fake_mmap_event.mmap.filename,
119 fake_mmap_info[i].filename);
120
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300121 machine__process_mmap_event(machine, &fake_mmap_event, &sample);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900122 }
123
124 for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
125 size_t k;
126 struct dso *dso;
127
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300128 dso = machine__findnew_dso(machine, fake_symbols[i].dso_name);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900129 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);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300141 if (sym == NULL) {
142 dso__put(dso);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900143 goto out;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300144 }
Namhyung Kim6e344a92014-04-25 12:28:13 +0900145
146 symbols__insert(&dso->symbols[MAP__FUNCTION], sym);
147 }
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300148
149 dso__put(dso);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900150 }
151
152 return machine;
153
154out:
155 pr_debug("Not enough memory for machine setup\n");
156 machine__delete_threads(machine);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900157 return NULL;
158}
Namhyung Kim4e754e12014-05-12 10:06:18 +0900159
160void print_hists_in(struct hists *hists)
161{
162 int i = 0;
163 struct rb_root *root;
164 struct rb_node *node;
165
Jiri Olsa52225032016-05-03 13:54:42 +0200166 if (hists__has(hists, need_collapse))
Namhyung Kim4e754e12014-05-12 10:06:18 +0900167 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
190void 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) {
Namhyung Kim0506aec2014-05-23 18:04:42 +0900206 pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
Namhyung Kimf21d1812014-05-12 14:43:18 +0900207 i, thread__comm_str(he->thread), he->thread->tid,
Namhyung Kim4e754e12014-05-12 10:06:18 +0900208 he->ms.map->dso->short_name,
Namhyung Kim0506aec2014-05-23 18:04:42 +0900209 he->ms.sym->name, he->stat.period,
210 he->stat_acc ? he->stat_acc->period : 0);
Namhyung Kim4e754e12014-05-12 10:06:18 +0900211 }
212
213 i++;
214 node = rb_next(node);
215 }
216}