blob: 6b21746d6eec84b0bf443fc4cd05523be57f9878 [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"
10
11static struct {
12 u32 pid;
13 const char *comm;
14} fake_threads[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090015 { FAKE_PID_PERF1, "perf" },
16 { FAKE_PID_PERF2, "perf" },
17 { FAKE_PID_BASH, "bash" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090018};
19
20static struct {
21 u32 pid;
22 u64 start;
23 const char *filename;
24} fake_mmap_info[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090025 { FAKE_PID_PERF1, FAKE_MAP_PERF, "perf" },
26 { FAKE_PID_PERF1, FAKE_MAP_LIBC, "libc" },
27 { FAKE_PID_PERF1, FAKE_MAP_KERNEL, "[kernel]" },
28 { FAKE_PID_PERF2, FAKE_MAP_PERF, "perf" },
29 { FAKE_PID_PERF2, FAKE_MAP_LIBC, "libc" },
30 { FAKE_PID_PERF2, FAKE_MAP_KERNEL, "[kernel]" },
31 { FAKE_PID_BASH, FAKE_MAP_BASH, "bash" },
32 { FAKE_PID_BASH, FAKE_MAP_LIBC, "libc" },
33 { FAKE_PID_BASH, FAKE_MAP_KERNEL, "[kernel]" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090034};
35
36struct fake_sym {
37 u64 start;
38 u64 length;
39 const char *name;
40};
41
42static struct fake_sym perf_syms[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090043 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
44 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "run_command" },
45 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "cmd_record" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090046};
47
48static struct fake_sym bash_syms[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090049 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
50 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "xmalloc" },
51 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "xfree" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090052};
53
54static struct fake_sym libc_syms[] = {
55 { 700, 100, "malloc" },
56 { 800, 100, "free" },
57 { 900, 100, "realloc" },
Namhyung Kima1891aa2014-05-23 14:59:57 +090058 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "malloc" },
59 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "free" },
60 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "realloc" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090061};
62
63static struct fake_sym kernel_syms[] = {
Namhyung Kima1891aa2014-05-23 14:59:57 +090064 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "schedule" },
65 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "page_fault" },
66 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "sys_perf_event_open" },
Namhyung Kim6e344a92014-04-25 12:28:13 +090067};
68
69static struct {
70 const char *dso_name;
71 struct fake_sym *syms;
72 size_t nr_syms;
73} fake_symbols[] = {
74 { "perf", perf_syms, ARRAY_SIZE(perf_syms) },
75 { "bash", bash_syms, ARRAY_SIZE(bash_syms) },
76 { "libc", libc_syms, ARRAY_SIZE(libc_syms) },
77 { "[kernel]", kernel_syms, ARRAY_SIZE(kernel_syms) },
78};
79
80struct machine *setup_fake_machine(struct machines *machines)
81{
82 struct machine *machine = machines__find(machines, HOST_KERNEL_ID);
83 size_t i;
84
85 if (machine == NULL) {
86 pr_debug("Not enough memory for machine setup\n");
87 return NULL;
88 }
89
90 for (i = 0; i < ARRAY_SIZE(fake_threads); i++) {
91 struct thread *thread;
92
93 thread = machine__findnew_thread(machine, fake_threads[i].pid,
94 fake_threads[i].pid);
95 if (thread == NULL)
96 goto out;
97
98 thread__set_comm(thread, fake_threads[i].comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030099 thread__put(thread);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900100 }
101
102 for (i = 0; i < ARRAY_SIZE(fake_mmap_info); i++) {
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300103 struct perf_sample sample = {
104 .cpumode = PERF_RECORD_MISC_USER,
105 };
Namhyung Kim6e344a92014-04-25 12:28:13 +0900106 union perf_event fake_mmap_event = {
107 .mmap = {
Namhyung Kim6e344a92014-04-25 12:28:13 +0900108 .pid = fake_mmap_info[i].pid,
109 .tid = fake_mmap_info[i].pid,
110 .start = fake_mmap_info[i].start,
Namhyung Kima1891aa2014-05-23 14:59:57 +0900111 .len = FAKE_MAP_LENGTH,
Namhyung Kim6e344a92014-04-25 12:28:13 +0900112 .pgoff = 0ULL,
113 },
114 };
115
116 strcpy(fake_mmap_event.mmap.filename,
117 fake_mmap_info[i].filename);
118
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300119 machine__process_mmap_event(machine, &fake_mmap_event, &sample);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900120 }
121
122 for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
123 size_t k;
124 struct dso *dso;
125
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300126 dso = machine__findnew_dso(machine, fake_symbols[i].dso_name);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900127 if (dso == NULL)
128 goto out;
129
130 /* emulate dso__load() */
131 dso__set_loaded(dso, MAP__FUNCTION);
132
133 for (k = 0; k < fake_symbols[i].nr_syms; k++) {
134 struct symbol *sym;
135 struct fake_sym *fsym = &fake_symbols[i].syms[k];
136
137 sym = symbol__new(fsym->start, fsym->length,
138 STB_GLOBAL, fsym->name);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300139 if (sym == NULL) {
140 dso__put(dso);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900141 goto out;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300142 }
Namhyung Kim6e344a92014-04-25 12:28:13 +0900143
144 symbols__insert(&dso->symbols[MAP__FUNCTION], sym);
145 }
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300146
147 dso__put(dso);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900148 }
149
150 return machine;
151
152out:
153 pr_debug("Not enough memory for machine setup\n");
154 machine__delete_threads(machine);
Namhyung Kim6e344a92014-04-25 12:28:13 +0900155 return NULL;
156}
Namhyung Kim4e754e12014-05-12 10:06:18 +0900157
158void print_hists_in(struct hists *hists)
159{
160 int i = 0;
161 struct rb_root *root;
162 struct rb_node *node;
163
Jiri Olsa52225032016-05-03 13:54:42 +0200164 if (hists__has(hists, need_collapse))
Namhyung Kim4e754e12014-05-12 10:06:18 +0900165 root = &hists->entries_collapsed;
166 else
167 root = hists->entries_in;
168
169 pr_info("----- %s --------\n", __func__);
170 node = rb_first(root);
171 while (node) {
172 struct hist_entry *he;
173
174 he = rb_entry(node, struct hist_entry, rb_node_in);
175
176 if (!he->filtered) {
177 pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
178 i, thread__comm_str(he->thread),
179 he->ms.map->dso->short_name,
180 he->ms.sym->name, he->stat.period);
181 }
182
183 i++;
184 node = rb_next(node);
185 }
186}
187
188void print_hists_out(struct hists *hists)
189{
190 int i = 0;
191 struct rb_root *root;
192 struct rb_node *node;
193
194 root = &hists->entries;
195
196 pr_info("----- %s --------\n", __func__);
197 node = rb_first(root);
198 while (node) {
199 struct hist_entry *he;
200
201 he = rb_entry(node, struct hist_entry, rb_node);
202
203 if (!he->filtered) {
Namhyung Kim0506aec2014-05-23 18:04:42 +0900204 pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
Namhyung Kimf21d1812014-05-12 14:43:18 +0900205 i, thread__comm_str(he->thread), he->thread->tid,
Namhyung Kim4e754e12014-05-12 10:06:18 +0900206 he->ms.map->dso->short_name,
Namhyung Kim0506aec2014-05-23 18:04:42 +0900207 he->ms.sym->name, he->stat.period,
208 he->stat_acc ? he->stat_acc->period : 0);
Namhyung Kim4e754e12014-05-12 10:06:18 +0900209 }
210
211 i++;
212 node = rb_next(node);
213 }
214}