~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/tests/hists_common.c

Version: ~ [ linux-6.0 ] ~ [ linux-5.19.12 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.71 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.146 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.215 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.260 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.295 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.330 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <inttypes.h>
  3 #include "perf.h"
  4 #include "util/debug.h"
  5 #include "util/map.h"
  6 #include "util/symbol.h"
  7 #include "util/sort.h"
  8 #include "util/evsel.h"
  9 #include "util/evlist.h"
 10 #include "util/machine.h"
 11 #include "util/thread.h"
 12 #include "tests/hists_common.h"
 13 #include <linux/kernel.h>
 14 
 15 static struct {
 16         u32 pid;
 17         const char *comm;
 18 } fake_threads[] = {
 19         { FAKE_PID_PERF1, "perf" },
 20         { FAKE_PID_PERF2, "perf" },
 21         { FAKE_PID_BASH,  "bash" },
 22 };
 23 
 24 static struct {
 25         u32 pid;
 26         u64 start;
 27         const char *filename;
 28 } fake_mmap_info[] = {
 29         { FAKE_PID_PERF1, FAKE_MAP_PERF,   "perf" },
 30         { FAKE_PID_PERF1, FAKE_MAP_LIBC,   "libc" },
 31         { FAKE_PID_PERF1, FAKE_MAP_KERNEL, "[kernel]" },
 32         { FAKE_PID_PERF2, FAKE_MAP_PERF,   "perf" },
 33         { FAKE_PID_PERF2, FAKE_MAP_LIBC,   "libc" },
 34         { FAKE_PID_PERF2, FAKE_MAP_KERNEL, "[kernel]" },
 35         { FAKE_PID_BASH,  FAKE_MAP_BASH,   "bash" },
 36         { FAKE_PID_BASH,  FAKE_MAP_LIBC,   "libc" },
 37         { FAKE_PID_BASH,  FAKE_MAP_KERNEL, "[kernel]" },
 38 };
 39 
 40 struct fake_sym {
 41         u64 start;
 42         u64 length;
 43         const char *name;
 44 };
 45 
 46 static struct fake_sym perf_syms[] = {
 47         { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
 48         { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "run_command" },
 49         { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "cmd_record" },
 50 };
 51 
 52 static struct fake_sym bash_syms[] = {
 53         { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
 54         { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "xmalloc" },
 55         { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "xfree" },
 56 };
 57 
 58 static struct fake_sym libc_syms[] = {
 59         { 700, 100, "malloc" },
 60         { 800, 100, "free" },
 61         { 900, 100, "realloc" },
 62         { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "malloc" },
 63         { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "free" },
 64         { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "realloc" },
 65 };
 66 
 67 static struct fake_sym kernel_syms[] = {
 68         { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "schedule" },
 69         { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "page_fault" },
 70         { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "sys_perf_event_open" },
 71 };
 72 
 73 static struct {
 74         const char *dso_name;
 75         struct fake_sym *syms;
 76         size_t nr_syms;
 77 } fake_symbols[] = {
 78         { "perf", perf_syms, ARRAY_SIZE(perf_syms) },
 79         { "bash", bash_syms, ARRAY_SIZE(bash_syms) },
 80         { "libc", libc_syms, ARRAY_SIZE(libc_syms) },
 81         { "[kernel]", kernel_syms, ARRAY_SIZE(kernel_syms) },
 82 };
 83 
 84 struct machine *setup_fake_machine(struct machines *machines)
 85 {
 86         struct machine *machine = machines__find(machines, HOST_KERNEL_ID);
 87         size_t i;
 88 
 89         if (machine == NULL) {
 90                 pr_debug("Not enough memory for machine setup\n");
 91                 return NULL;
 92         }
 93 
 94         for (i = 0; i < ARRAY_SIZE(fake_threads); i++) {
 95                 struct thread *thread;
 96 
 97                 thread = machine__findnew_thread(machine, fake_threads[i].pid,
 98                                                  fake_threads[i].pid);
 99                 if (thread == NULL)
100                         goto out;
101 
102                 thread__set_comm(thread, fake_threads[i].comm, 0);
103                 thread__put(thread);
104         }
105 
106         for (i = 0; i < ARRAY_SIZE(fake_mmap_info); i++) {
107                 struct perf_sample sample = {
108                         .cpumode = PERF_RECORD_MISC_USER,
109                 };
110                 union perf_event fake_mmap_event = {
111                         .mmap = {
112                                 .pid = fake_mmap_info[i].pid,
113                                 .tid = fake_mmap_info[i].pid,
114                                 .start = fake_mmap_info[i].start,
115                                 .len = FAKE_MAP_LENGTH,
116                                 .pgoff = 0ULL,
117                         },
118                 };
119 
120                 strcpy(fake_mmap_event.mmap.filename,
121                        fake_mmap_info[i].filename);
122 
123                 machine__process_mmap_event(machine, &fake_mmap_event, &sample);
124         }
125 
126         for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
127                 size_t k;
128                 struct dso *dso;
129 
130                 dso = machine__findnew_dso(machine, fake_symbols[i].dso_name);
131                 if (dso == NULL)
132                         goto out;
133 
134                 /* emulate dso__load() */
135                 dso__set_loaded(dso);
136 
137                 for (k = 0; k < fake_symbols[i].nr_syms; k++) {
138                         struct symbol *sym;
139                         struct fake_sym *fsym = &fake_symbols[i].syms[k];
140 
141                         sym = symbol__new(fsym->start, fsym->length,
142                                           STB_GLOBAL, STT_FUNC, fsym->name);
143                         if (sym == NULL) {
144                                 dso__put(dso);
145                                 goto out;
146                         }
147 
148                         symbols__insert(&dso->symbols, sym);
149                 }
150 
151                 dso__put(dso);
152         }
153 
154         return machine;
155 
156 out:
157         pr_debug("Not enough memory for machine setup\n");
158         machine__delete_threads(machine);
159         return NULL;
160 }
161 
162 void print_hists_in(struct hists *hists)
163 {
164         int i = 0;
165         struct rb_root_cached *root;
166         struct rb_node *node;
167 
168         if (hists__has(hists, need_collapse))
169                 root = &hists->entries_collapsed;
170         else
171                 root = hists->entries_in;
172 
173         pr_info("----- %s --------\n", __func__);
174         node = rb_first_cached(root);
175         while (node) {
176                 struct hist_entry *he;
177 
178                 he = rb_entry(node, struct hist_entry, rb_node_in);
179 
180                 if (!he->filtered) {
181                         pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
182                                 i, thread__comm_str(he->thread),
183                                 he->ms.map->dso->short_name,
184                                 he->ms.sym->name, he->stat.period);
185                 }
186 
187                 i++;
188                 node = rb_next(node);
189         }
190 }
191 
192 void print_hists_out(struct hists *hists)
193 {
194         int i = 0;
195         struct rb_root_cached *root;
196         struct rb_node *node;
197 
198         root = &hists->entries;
199 
200         pr_info("----- %s --------\n", __func__);
201         node = rb_first_cached(root);
202         while (node) {
203                 struct hist_entry *he;
204 
205                 he = rb_entry(node, struct hist_entry, rb_node);
206 
207                 if (!he->filtered) {
208                         pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
209                                 i, thread__comm_str(he->thread), he->thread->tid,
210                                 he->ms.map->dso->short_name,
211                                 he->ms.sym->name, he->stat.period,
212                                 he->stat_acc ? he->stat_acc->period : 0);
213                 }
214 
215                 i++;
216                 node = rb_next(node);
217         }
218 }
219 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp