0001
0002 #include <inttypes.h>
0003 #include "util/debug.h"
0004 #include "util/dso.h"
0005 #include "util/event.h" // struct perf_sample
0006 #include "util/map.h"
0007 #include "util/symbol.h"
0008 #include "util/sort.h"
0009 #include "util/evsel.h"
0010 #include "util/machine.h"
0011 #include "util/thread.h"
0012 #include "tests/hists_common.h"
0013 #include <linux/kernel.h>
0014 #include <linux/perf_event.h>
0015
0016 static struct {
0017 u32 pid;
0018 const char *comm;
0019 } fake_threads[] = {
0020 { FAKE_PID_PERF1, "perf" },
0021 { FAKE_PID_PERF2, "perf" },
0022 { FAKE_PID_BASH, "bash" },
0023 };
0024
0025 static struct {
0026 u32 pid;
0027 u64 start;
0028 const char *filename;
0029 } fake_mmap_info[] = {
0030 { FAKE_PID_PERF1, FAKE_MAP_PERF, "perf" },
0031 { FAKE_PID_PERF1, FAKE_MAP_LIBC, "libc" },
0032 { FAKE_PID_PERF1, FAKE_MAP_KERNEL, "[kernel]" },
0033 { FAKE_PID_PERF2, FAKE_MAP_PERF, "perf" },
0034 { FAKE_PID_PERF2, FAKE_MAP_LIBC, "libc" },
0035 { FAKE_PID_PERF2, FAKE_MAP_KERNEL, "[kernel]" },
0036 { FAKE_PID_BASH, FAKE_MAP_BASH, "bash" },
0037 { FAKE_PID_BASH, FAKE_MAP_LIBC, "libc" },
0038 { FAKE_PID_BASH, FAKE_MAP_KERNEL, "[kernel]" },
0039 };
0040
0041 struct fake_sym {
0042 u64 start;
0043 u64 length;
0044 const char *name;
0045 };
0046
0047 static struct fake_sym perf_syms[] = {
0048 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
0049 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "run_command" },
0050 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "cmd_record" },
0051 };
0052
0053 static struct fake_sym bash_syms[] = {
0054 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
0055 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "xmalloc" },
0056 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "xfree" },
0057 };
0058
0059 static struct fake_sym libc_syms[] = {
0060 { 700, 100, "malloc" },
0061 { 800, 100, "free" },
0062 { 900, 100, "realloc" },
0063 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "malloc" },
0064 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "free" },
0065 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "realloc" },
0066 };
0067
0068 static struct fake_sym kernel_syms[] = {
0069 { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "schedule" },
0070 { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "page_fault" },
0071 { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "sys_perf_event_open" },
0072 };
0073
0074 static struct {
0075 const char *dso_name;
0076 struct fake_sym *syms;
0077 size_t nr_syms;
0078 } fake_symbols[] = {
0079 { "perf", perf_syms, ARRAY_SIZE(perf_syms) },
0080 { "bash", bash_syms, ARRAY_SIZE(bash_syms) },
0081 { "libc", libc_syms, ARRAY_SIZE(libc_syms) },
0082 { "[kernel]", kernel_syms, ARRAY_SIZE(kernel_syms) },
0083 };
0084
0085 struct machine *setup_fake_machine(struct machines *machines)
0086 {
0087 struct machine *machine = machines__find(machines, HOST_KERNEL_ID);
0088 size_t i;
0089
0090 if (machine == NULL) {
0091 pr_debug("Not enough memory for machine setup\n");
0092 return NULL;
0093 }
0094
0095 for (i = 0; i < ARRAY_SIZE(fake_threads); i++) {
0096 struct thread *thread;
0097
0098 thread = machine__findnew_thread(machine, fake_threads[i].pid,
0099 fake_threads[i].pid);
0100 if (thread == NULL)
0101 goto out;
0102
0103 thread__set_comm(thread, fake_threads[i].comm, 0);
0104 thread__put(thread);
0105 }
0106
0107 for (i = 0; i < ARRAY_SIZE(fake_mmap_info); i++) {
0108 struct perf_sample sample = {
0109 .cpumode = PERF_RECORD_MISC_USER,
0110 };
0111 union perf_event fake_mmap_event = {
0112 .mmap = {
0113 .pid = fake_mmap_info[i].pid,
0114 .tid = fake_mmap_info[i].pid,
0115 .start = fake_mmap_info[i].start,
0116 .len = FAKE_MAP_LENGTH,
0117 .pgoff = 0ULL,
0118 },
0119 };
0120
0121 strcpy(fake_mmap_event.mmap.filename,
0122 fake_mmap_info[i].filename);
0123
0124 machine__process_mmap_event(machine, &fake_mmap_event, &sample);
0125 }
0126
0127 for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
0128 size_t k;
0129 struct dso *dso;
0130
0131 dso = machine__findnew_dso(machine, fake_symbols[i].dso_name);
0132 if (dso == NULL)
0133 goto out;
0134
0135
0136 dso__set_loaded(dso);
0137
0138 for (k = 0; k < fake_symbols[i].nr_syms; k++) {
0139 struct symbol *sym;
0140 struct fake_sym *fsym = &fake_symbols[i].syms[k];
0141
0142 sym = symbol__new(fsym->start, fsym->length,
0143 STB_GLOBAL, STT_FUNC, fsym->name);
0144 if (sym == NULL) {
0145 dso__put(dso);
0146 goto out;
0147 }
0148
0149 symbols__insert(&dso->symbols, sym);
0150 }
0151
0152 dso__put(dso);
0153 }
0154
0155 return machine;
0156
0157 out:
0158 pr_debug("Not enough memory for machine setup\n");
0159 machine__delete_threads(machine);
0160 return NULL;
0161 }
0162
0163 void print_hists_in(struct hists *hists)
0164 {
0165 int i = 0;
0166 struct rb_root_cached *root;
0167 struct rb_node *node;
0168
0169 if (hists__has(hists, need_collapse))
0170 root = &hists->entries_collapsed;
0171 else
0172 root = hists->entries_in;
0173
0174 pr_info("----- %s --------\n", __func__);
0175 node = rb_first_cached(root);
0176 while (node) {
0177 struct hist_entry *he;
0178
0179 he = rb_entry(node, struct hist_entry, rb_node_in);
0180
0181 if (!he->filtered) {
0182 pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
0183 i, thread__comm_str(he->thread),
0184 he->ms.map->dso->short_name,
0185 he->ms.sym->name, he->stat.period);
0186 }
0187
0188 i++;
0189 node = rb_next(node);
0190 }
0191 }
0192
0193 void print_hists_out(struct hists *hists)
0194 {
0195 int i = 0;
0196 struct rb_root_cached *root;
0197 struct rb_node *node;
0198
0199 root = &hists->entries;
0200
0201 pr_info("----- %s --------\n", __func__);
0202 node = rb_first_cached(root);
0203 while (node) {
0204 struct hist_entry *he;
0205
0206 he = rb_entry(node, struct hist_entry, rb_node);
0207
0208 if (!he->filtered) {
0209 pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
0210 i, thread__comm_str(he->thread), he->thread->tid,
0211 he->ms.map->dso->short_name,
0212 he->ms.sym->name, he->stat.period,
0213 he->stat_acc ? he->stat_acc->period : 0);
0214 }
0215
0216 i++;
0217 node = rb_next(node);
0218 }
0219 }