blob: f6c580e3ed843eb5dad552de0c8edbe53adafe30 [file] [log] [blame]
Namhyung Kim6e344a92014-04-25 12:28:13 +09001#include "perf.h"
2#include "util/debug.h"
3#include "util/symbol.h"
4#include "util/sort.h"
5#include "util/evsel.h"
6#include "util/evlist.h"
7#include "util/machine.h"
8#include "util/thread.h"
9#include "tests/hists_common.h"
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030010#include <linux/kernel.h>
Namhyung Kim6e344a92014-04-25 12:28:13 +090011
12static struct {
13 u32 pid;
14 const char *comm;
15} fake_threads[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090016 { FAKE_PID_PERF1, "perf" },
17 { FAKE_PID_PERF2, "perf" },
18 { FAKE_PID_BASH, "bash" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090019};
20
21static struct {
22 u32 pid;
23 u64 start;
24 const char *filename;
25} fake_mmap_info[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090026 { FAKE_PID_PERF1, FAKE_MAP_PERF, "perf" },
27 { FAKE_PID_PERF1, FAKE_MAP_LIBC, "libc" },
28 { FAKE_PID_PERF1, FAKE_MAP_KERNEL, "[kernel]" },
29 { FAKE_PID_PERF2, FAKE_MAP_PERF, "perf" },
30 { FAKE_PID_PERF2, FAKE_MAP_LIBC, "libc" },
31 { FAKE_PID_PERF2, FAKE_MAP_KERNEL, "[kernel]" },
32 { FAKE_PID_BASH, FAKE_MAP_BASH, "bash" },
33 { FAKE_PID_BASH, FAKE_MAP_LIBC, "libc" },
34 { FAKE_PID_BASH, FAKE_MAP_KERNEL, "[kernel]" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090035};
36
37struct fake_sym {
38 u64 start;
39 u64 length;
40 const char *name;
41};
42
43static struct fake_sym perf_syms[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090044 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
45 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "run_command" },
46 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "cmd_record" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090047};
48
49static struct fake_sym bash_syms[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090050 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
51 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "xmalloc" },
52 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "xfree" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090053};
54
55static struct fake_sym libc_syms[] = {
56 { 700, 100, "malloc" },
57 { 800, 100, "free" },
58 { 900, 100, "realloc" },
Namhyung Kima1891aa2014-05-23 14:59:57 +090059 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "malloc" },
60 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "free" },
61 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "realloc" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090062};
63
64static struct fake_sym kernel_syms[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090065 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "schedule" },
66 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "page_fault" },
67 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "sys_perf_event_open" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090068};
69
70static struct {
71 const char *dso_name;
72 struct fake_sym *syms;
73 size_t nr_syms;
74} fake_symbols[] = {
75 { "perf", perf_syms, ARRAY_SIZE(perf_syms) },
76 { "bash", bash_syms, ARRAY_SIZE(bash_syms) },
77 { "libc", libc_syms, ARRAY_SIZE(libc_syms) },
78 { "[kernel]", kernel_syms, ARRAY_SIZE(kernel_syms) },
79};
80
81struct machine *setup_fake_machine(struct machines *machines)
82{
83 struct machine *machine = machines__find(machines, HOST_KERNEL_ID);
84 size_t i;
85
86 if (machine == NULL) {
87 pr_debug("Not enough memory for machine setup\n");
88 return NULL;
89 }
90
91 for (i = 0; i < ARRAY_SIZE(fake_threads); i++) {
92 struct thread *thread;
93
94 thread = machine__findnew_thread(machine, fake_threads[i].pid,
95 fake_threads[i].pid);
96 if (thread == NULL)
97 goto out;
98
99 thread__set_comm(thread, fake_threads[i].comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300100 thread__put(thread);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900101 }
102
103 for (i = 0; i < ARRAY_SIZE(fake_mmap_info); i++) {
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300104 struct perf_sample sample = {
105 .cpumode = PERF_RECORD_MISC_USER,
106 };
Namhyung Kim6e344a92014-04-25 12:28:13 +0900107 union perf_event fake_mmap_event = {
108 .mmap = {
Namhyung Kim6e344a92014-04-25 12:28:13 +0900109 .pid = fake_mmap_info[i].pid,
110 .tid = fake_mmap_info[i].pid,
111 .start = fake_mmap_info[i].start,
Namhyung Kima1891aa2014-05-23 14:59:57 +0900112 .len = FAKE_MAP_LENGTH,
Namhyung Kim6e344a92014-04-25 12:28:13 +0900113 .pgoff = 0ULL,
114 },
115 };
116
117 strcpy(fake_mmap_event.mmap.filename,
118 fake_mmap_info[i].filename);
119
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300120 machine__process_mmap_event(machine, &fake_mmap_event, &sample);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900121 }
122
123 for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
124 size_t k;
125 struct dso *dso;
126
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300127 dso = machine__findnew_dso(machine, fake_symbols[i].dso_name);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900128 if (dso == NULL)
129 goto out;
130
131 /* emulate dso__load() */
132 dso__set_loaded(dso, MAP__FUNCTION);
133
134 for (k = 0; k < fake_symbols[i].nr_syms; k++) {
135 struct symbol *sym;
136 struct fake_sym *fsym = &fake_symbols[i].syms[k];
137
138 sym = symbol__new(fsym->start, fsym->length,
139 STB_GLOBAL, fsym->name);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300140 if (sym == NULL) {
141 dso__put(dso);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900142 goto out;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300143 }
Namhyung Kim6e344a92014-04-25 12:28:13 +0900144
145 symbols__insert(&dso->symbols[MAP__FUNCTION], sym);
146 }
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300147
148 dso__put(dso);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900149 }
150
151 return machine;
152
153out:
154 pr_debug("Not enough memory for machine setup\n");
155 machine__delete_threads(machine);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900156 return NULL;
157}
Namhyung Kim4e754e12014-05-12 10:06:18 +0900158
159void print_hists_in(struct hists *hists)
160{
161 int i = 0;
162 struct rb_root *root;
163 struct rb_node *node;
164
Jiri Olsa52225032016-05-03 13:54:42 +0200165 if (hists__has(hists, need_collapse))
Namhyung Kim4e754e12014-05-12 10:06:18 +0900166 root = &hists->entries_collapsed;
167 else
168 root = hists->entries_in;
169
170 pr_info("----- %s --------\n", __func__);
171 node = rb_first(root);
172 while (node) {
173 struct hist_entry *he;
174
175 he = rb_entry(node, struct hist_entry, rb_node_in);
176
177 if (!he->filtered) {
178 pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
179 i, thread__comm_str(he->thread),
180 he->ms.map->dso->short_name,
181 he->ms.sym->name, he->stat.period);
182 }
183
184 i++;
185 node = rb_next(node);
186 }
187}
188
189void print_hists_out(struct hists *hists)
190{
191 int i = 0;
192 struct rb_root *root;
193 struct rb_node *node;
194
195 root = &hists->entries;
196
197 pr_info("----- %s --------\n", __func__);
198 node = rb_first(root);
199 while (node) {
200 struct hist_entry *he;
201
202 he = rb_entry(node, struct hist_entry, rb_node);
203
204 if (!he->filtered) {
Namhyung Kim0506aec2014-05-23 18:04:42 +0900205 pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
Namhyung Kimf21d1812014-05-12 14:43:18 +0900206 i, thread__comm_str(he->thread), he->thread->tid,
Namhyung Kim4e754e12014-05-12 10:06:18 +0900207 he->ms.map->dso->short_name,
Namhyung Kim0506aec2014-05-23 18:04:42 +0900208 he->ms.sym->name, he->stat.period,
209 he->stat_acc ? he->stat_acc->period : 0);
Namhyung Kim4e754e12014-05-12 10:06:18 +0900210 }
211
212 i++;
213 node = rb_next(node);
214 }
215}