1#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[] = {
15	{ FAKE_PID_PERF1, "perf" },
16	{ FAKE_PID_PERF2, "perf" },
17	{ FAKE_PID_BASH,  "bash" },
18};
19
20static struct {
21	u32 pid;
22	u64 start;
23	const char *filename;
24} fake_mmap_info[] = {
25	{ 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]" },
34};
35
36struct fake_sym {
37	u64 start;
38	u64 length;
39	const char *name;
40};
41
42static struct fake_sym perf_syms[] = {
43	{ 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" },
46};
47
48static struct fake_sym bash_syms[] = {
49	{ FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
50	{ FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "xmalloc" },
51	{ FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "xfree" },
52};
53
54static struct fake_sym libc_syms[] = {
55	{ 700, 100, "malloc" },
56	{ 800, 100, "free" },
57	{ 900, 100, "realloc" },
58	{ FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "malloc" },
59	{ FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "free" },
60	{ FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "realloc" },
61};
62
63static struct fake_sym kernel_syms[] = {
64	{ 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" },
67};
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);
99		thread__put(thread);
100	}
101
102	for (i = 0; i < ARRAY_SIZE(fake_mmap_info); i++) {
103		union perf_event fake_mmap_event = {
104			.mmap = {
105				.header = { .misc = PERF_RECORD_MISC_USER, },
106				.pid = fake_mmap_info[i].pid,
107				.tid = fake_mmap_info[i].pid,
108				.start = fake_mmap_info[i].start,
109				.len = FAKE_MAP_LENGTH,
110				.pgoff = 0ULL,
111			},
112		};
113
114		strcpy(fake_mmap_event.mmap.filename,
115		       fake_mmap_info[i].filename);
116
117		machine__process_mmap_event(machine, &fake_mmap_event, NULL);
118	}
119
120	for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
121		size_t k;
122		struct dso *dso;
123
124		dso = machine__findnew_dso(machine, fake_symbols[i].dso_name);
125		if (dso == NULL)
126			goto out;
127
128		/* emulate dso__load() */
129		dso__set_loaded(dso, MAP__FUNCTION);
130
131		for (k = 0; k < fake_symbols[i].nr_syms; k++) {
132			struct symbol *sym;
133			struct fake_sym *fsym = &fake_symbols[i].syms[k];
134
135			sym = symbol__new(fsym->start, fsym->length,
136					  STB_GLOBAL, fsym->name);
137			if (sym == NULL) {
138				dso__put(dso);
139				goto out;
140			}
141
142			symbols__insert(&dso->symbols[MAP__FUNCTION], sym);
143		}
144
145		dso__put(dso);
146	}
147
148	return machine;
149
150out:
151	pr_debug("Not enough memory for machine setup\n");
152	machine__delete_threads(machine);
153	machine__delete(machine);
154	return NULL;
155}
156
157void print_hists_in(struct hists *hists)
158{
159	int i = 0;
160	struct rb_root *root;
161	struct rb_node *node;
162
163	if (sort__need_collapse)
164		root = &hists->entries_collapsed;
165	else
166		root = hists->entries_in;
167
168	pr_info("----- %s --------\n", __func__);
169	node = rb_first(root);
170	while (node) {
171		struct hist_entry *he;
172
173		he = rb_entry(node, struct hist_entry, rb_node_in);
174
175		if (!he->filtered) {
176			pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
177				i, thread__comm_str(he->thread),
178				he->ms.map->dso->short_name,
179				he->ms.sym->name, he->stat.period);
180		}
181
182		i++;
183		node = rb_next(node);
184	}
185}
186
187void print_hists_out(struct hists *hists)
188{
189	int i = 0;
190	struct rb_root *root;
191	struct rb_node *node;
192
193	root = &hists->entries;
194
195	pr_info("----- %s --------\n", __func__);
196	node = rb_first(root);
197	while (node) {
198		struct hist_entry *he;
199
200		he = rb_entry(node, struct hist_entry, rb_node);
201
202		if (!he->filtered) {
203			pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
204				i, thread__comm_str(he->thread), he->thread->tid,
205				he->ms.map->dso->short_name,
206				he->ms.sym->name, he->stat.period,
207				he->stat_acc ? he->stat_acc->period : 0);
208		}
209
210		i++;
211		node = rb_next(node);
212	}
213}
214