blob: bc912c68f49a56aa92ad26a27dc8cc34b2566fa2 [file] [log] [blame]
Li Zefanba77c9e2009-11-20 15:53:25 +08001#include "builtin.h"
2#include "perf.h"
3
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03004#include "util/evlist.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03005#include "util/evsel.h"
Li Zefanba77c9e2009-11-20 15:53:25 +08006#include "util/util.h"
7#include "util/cache.h"
8#include "util/symbol.h"
9#include "util/thread.h"
10#include "util/header.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080013
14#include "util/parse-options.h"
15#include "util/trace-event.h"
16
17#include "util/debug.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080018
19#include <linux/rbtree.h>
20
21struct alloc_stat;
22typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
23
Robert Richterefad1412011-12-07 10:02:54 +010024static const char *input_name;
Li Zefanba77c9e2009-11-20 15:53:25 +080025
Li Zefanba77c9e2009-11-20 15:53:25 +080026static int alloc_flag;
27static int caller_flag;
28
Li Zefanba77c9e2009-11-20 15:53:25 +080029static int alloc_lines = -1;
30static int caller_lines = -1;
31
Li Zefan7707b6b2009-11-24 13:25:48 +080032static bool raw_ip;
33
Li Zefan29b3e152009-11-24 13:26:10 +080034static char default_sort_order[] = "frag,hit,bytes";
35
Li Zefan7d0d3942009-11-24 13:26:31 +080036static int *cpunode_map;
37static int max_cpu_num;
38
Li Zefanba77c9e2009-11-20 15:53:25 +080039struct alloc_stat {
Li Zefan079d3f62009-11-24 13:26:55 +080040 u64 call_site;
41 u64 ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +080042 u64 bytes_req;
43 u64 bytes_alloc;
44 u32 hit;
Li Zefan079d3f62009-11-24 13:26:55 +080045 u32 pingpong;
46
47 short alloc_cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +080048
49 struct rb_node node;
50};
51
52static struct rb_root root_alloc_stat;
53static struct rb_root root_alloc_sorted;
54static struct rb_root root_caller_stat;
55static struct rb_root root_caller_sorted;
56
57static unsigned long total_requested, total_allocated;
Li Zefan7d0d3942009-11-24 13:26:31 +080058static unsigned long nr_allocs, nr_cross_allocs;
Li Zefanba77c9e2009-11-20 15:53:25 +080059
Li Zefan7d0d3942009-11-24 13:26:31 +080060#define PATH_SYS_NODE "/sys/devices/system/node"
61
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030062static int init_cpunode_map(void)
Li Zefan7d0d3942009-11-24 13:26:31 +080063{
64 FILE *fp;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030065 int i, err = -1;
Li Zefan7d0d3942009-11-24 13:26:31 +080066
67 fp = fopen("/sys/devices/system/cpu/kernel_max", "r");
68 if (!fp) {
69 max_cpu_num = 4096;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030070 return 0;
Li Zefan7d0d3942009-11-24 13:26:31 +080071 }
72
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030073 if (fscanf(fp, "%d", &max_cpu_num) < 1) {
74 pr_err("Failed to read 'kernel_max' from sysfs");
75 goto out_close;
76 }
77
Li Zefan7d0d3942009-11-24 13:26:31 +080078 max_cpu_num++;
79
80 cpunode_map = calloc(max_cpu_num, sizeof(int));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030081 if (!cpunode_map) {
82 pr_err("%s: calloc failed\n", __func__);
83 goto out_close;
84 }
85
Li Zefan7d0d3942009-11-24 13:26:31 +080086 for (i = 0; i < max_cpu_num; i++)
87 cpunode_map[i] = -1;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030088
89 err = 0;
90out_close:
Li Zefan7d0d3942009-11-24 13:26:31 +080091 fclose(fp);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030092 return err;
Li Zefan7d0d3942009-11-24 13:26:31 +080093}
94
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030095static int setup_cpunode_map(void)
Li Zefan7d0d3942009-11-24 13:26:31 +080096{
97 struct dirent *dent1, *dent2;
98 DIR *dir1, *dir2;
99 unsigned int cpu, mem;
100 char buf[PATH_MAX];
101
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300102 if (init_cpunode_map())
103 return -1;
Li Zefan7d0d3942009-11-24 13:26:31 +0800104
105 dir1 = opendir(PATH_SYS_NODE);
106 if (!dir1)
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300107 return -1;
Li Zefan7d0d3942009-11-24 13:26:31 +0800108
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500109 while ((dent1 = readdir(dir1)) != NULL) {
110 if (dent1->d_type != DT_DIR ||
111 sscanf(dent1->d_name, "node%u", &mem) < 1)
Li Zefan7d0d3942009-11-24 13:26:31 +0800112 continue;
113
114 snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name);
115 dir2 = opendir(buf);
116 if (!dir2)
117 continue;
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500118 while ((dent2 = readdir(dir2)) != NULL) {
119 if (dent2->d_type != DT_LNK ||
120 sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
Li Zefan7d0d3942009-11-24 13:26:31 +0800121 continue;
122 cpunode_map[cpu] = mem;
123 }
Namhyung Kim8442da12012-01-08 02:25:28 +0900124 closedir(dir2);
Li Zefan7d0d3942009-11-24 13:26:31 +0800125 }
Namhyung Kim8442da12012-01-08 02:25:28 +0900126 closedir(dir1);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300127 return 0;
Li Zefan7d0d3942009-11-24 13:26:31 +0800128}
129
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300130static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
131 int bytes_req, int bytes_alloc, int cpu)
Li Zefanba77c9e2009-11-20 15:53:25 +0800132{
133 struct rb_node **node = &root_alloc_stat.rb_node;
134 struct rb_node *parent = NULL;
135 struct alloc_stat *data = NULL;
136
Li Zefanba77c9e2009-11-20 15:53:25 +0800137 while (*node) {
138 parent = *node;
139 data = rb_entry(*node, struct alloc_stat, node);
140
141 if (ptr > data->ptr)
142 node = &(*node)->rb_right;
143 else if (ptr < data->ptr)
144 node = &(*node)->rb_left;
145 else
146 break;
147 }
148
149 if (data && data->ptr == ptr) {
150 data->hit++;
151 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +0800152 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +0800153 } else {
154 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300155 if (!data) {
156 pr_err("%s: malloc failed\n", __func__);
157 return -1;
158 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800159 data->ptr = ptr;
Li Zefan079d3f62009-11-24 13:26:55 +0800160 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800161 data->hit = 1;
162 data->bytes_req = bytes_req;
163 data->bytes_alloc = bytes_alloc;
164
165 rb_link_node(&data->node, parent, node);
166 rb_insert_color(&data->node, &root_alloc_stat);
167 }
Li Zefan079d3f62009-11-24 13:26:55 +0800168 data->call_site = call_site;
169 data->alloc_cpu = cpu;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300170 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800171}
172
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300173static int insert_caller_stat(unsigned long call_site,
Li Zefanba77c9e2009-11-20 15:53:25 +0800174 int bytes_req, int bytes_alloc)
175{
176 struct rb_node **node = &root_caller_stat.rb_node;
177 struct rb_node *parent = NULL;
178 struct alloc_stat *data = NULL;
179
Li Zefanba77c9e2009-11-20 15:53:25 +0800180 while (*node) {
181 parent = *node;
182 data = rb_entry(*node, struct alloc_stat, node);
183
184 if (call_site > data->call_site)
185 node = &(*node)->rb_right;
186 else if (call_site < data->call_site)
187 node = &(*node)->rb_left;
188 else
189 break;
190 }
191
192 if (data && data->call_site == call_site) {
193 data->hit++;
194 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +0800195 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +0800196 } else {
197 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300198 if (!data) {
199 pr_err("%s: malloc failed\n", __func__);
200 return -1;
201 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800202 data->call_site = call_site;
Li Zefan079d3f62009-11-24 13:26:55 +0800203 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800204 data->hit = 1;
205 data->bytes_req = bytes_req;
206 data->bytes_alloc = bytes_alloc;
207
208 rb_link_node(&data->node, parent, node);
209 rb_insert_color(&data->node, &root_caller_stat);
210 }
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300211
212 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800213}
214
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300215static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300216 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800217{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300218 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
219 call_site = perf_evsel__intval(evsel, sample, "call_site");
220 int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
221 bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800222
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300223 if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300224 insert_caller_stat(call_site, bytes_req, bytes_alloc))
225 return -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800226
227 total_requested += bytes_req;
228 total_allocated += bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800229
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300230 nr_allocs++;
231 return 0;
232}
233
234static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
235 struct perf_sample *sample)
236{
237 int ret = perf_evsel__process_alloc_event(evsel, sample);
238
239 if (!ret) {
240 int node1 = cpunode_map[sample->cpu],
241 node2 = perf_evsel__intval(evsel, sample, "node");
242
Li Zefan7d0d3942009-11-24 13:26:31 +0800243 if (node1 != node2)
244 nr_cross_allocs++;
245 }
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300246
247 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +0800248}
249
Li Zefan079d3f62009-11-24 13:26:55 +0800250static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
251static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
252
253static struct alloc_stat *search_alloc_stat(unsigned long ptr,
254 unsigned long call_site,
255 struct rb_root *root,
256 sort_fn_t sort_fn)
257{
258 struct rb_node *node = root->rb_node;
259 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
260
261 while (node) {
262 struct alloc_stat *data;
263 int cmp;
264
265 data = rb_entry(node, struct alloc_stat, node);
266
267 cmp = sort_fn(&key, data);
268 if (cmp < 0)
269 node = node->rb_left;
270 else if (cmp > 0)
271 node = node->rb_right;
272 else
273 return data;
274 }
275 return NULL;
276}
277
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300278static int perf_evsel__process_free_event(struct perf_evsel *evsel,
279 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800280{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300281 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
Li Zefan079d3f62009-11-24 13:26:55 +0800282 struct alloc_stat *s_alloc, *s_caller;
283
Li Zefan079d3f62009-11-24 13:26:55 +0800284 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
285 if (!s_alloc)
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300286 return 0;
Li Zefan079d3f62009-11-24 13:26:55 +0800287
Arnaldo Carvalho de Melo22ad7982012-08-07 10:56:43 -0300288 if ((short)sample->cpu != s_alloc->alloc_cpu) {
Li Zefan079d3f62009-11-24 13:26:55 +0800289 s_alloc->pingpong++;
290
291 s_caller = search_alloc_stat(0, s_alloc->call_site,
292 &root_caller_stat, callsite_cmp);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300293 if (!s_caller)
294 return -1;
Li Zefan079d3f62009-11-24 13:26:55 +0800295 s_caller->pingpong++;
296 }
297 s_alloc->alloc_cpu = -1;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300298
299 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800300}
301
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300302typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
303 struct perf_sample *sample);
Li Zefanba77c9e2009-11-20 15:53:25 +0800304
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300305static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200306 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200307 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300308 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200309 struct machine *machine)
Li Zefanba77c9e2009-11-20 15:53:25 +0800310{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200311 struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800312
Li Zefanba77c9e2009-11-20 15:53:25 +0800313 if (thread == NULL) {
314 pr_debug("problem processing %d event, skipping it.\n",
315 event->header.type);
316 return -1;
317 }
318
319 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
320
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300321 if (evsel->handler.func != NULL) {
322 tracepoint_handler f = evsel->handler.func;
323 return f(evsel, sample);
324 }
325
326 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800327}
328
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300329static struct perf_tool perf_kmem = {
330 .sample = process_sample_event,
331 .comm = perf_event__process_comm,
332 .ordered_samples = true,
Li Zefanba77c9e2009-11-20 15:53:25 +0800333};
334
Li Zefanba77c9e2009-11-20 15:53:25 +0800335static double fragmentation(unsigned long n_req, unsigned long n_alloc)
336{
337 if (n_alloc == 0)
338 return 0.0;
339 else
340 return 100.0 - (100.0 * n_req / n_alloc);
341}
342
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200343static void __print_result(struct rb_root *root, struct perf_session *session,
344 int n_lines, int is_caller)
Li Zefanba77c9e2009-11-20 15:53:25 +0800345{
346 struct rb_node *next;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300347 struct machine *machine;
Li Zefanba77c9e2009-11-20 15:53:25 +0800348
Li Zefan079d3f62009-11-24 13:26:55 +0800349 printf("%.102s\n", graph_dotted_line);
350 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
Pekka Enberg47103272010-01-19 19:23:23 +0200351 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
Li Zefan079d3f62009-11-24 13:26:55 +0800352 printf("%.102s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800353
354 next = rb_first(root);
355
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300356 machine = perf_session__find_host_machine(session);
357 if (!machine) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800358 pr_err("__print_result: couldn't find kernel information\n");
359 return;
360 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800361 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200362 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
363 node);
364 struct symbol *sym = NULL;
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300365 struct map *map;
Li Zefan079d3f62009-11-24 13:26:55 +0800366 char buf[BUFSIZ];
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200367 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800368
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200369 if (is_caller) {
370 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +0800371 if (!raw_ip)
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -0300372 sym = machine__find_kernel_function(machine, addr, &map, NULL);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200373 } else
374 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800375
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200376 if (sym != NULL)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200377 snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300378 addr - map->unmap_ip(map, sym->start));
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200379 else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200380 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
Li Zefan079d3f62009-11-24 13:26:55 +0800381 printf(" %-34s |", buf);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200382
Pekka Enberg47103272010-01-19 19:23:23 +0200383 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n",
Li Zefan079d3f62009-11-24 13:26:55 +0800384 (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +0800385 (unsigned long)data->bytes_alloc / data->hit,
386 (unsigned long long)data->bytes_req,
387 (unsigned long)data->bytes_req / data->hit,
388 (unsigned long)data->hit,
Li Zefan079d3f62009-11-24 13:26:55 +0800389 (unsigned long)data->pingpong,
Li Zefanba77c9e2009-11-20 15:53:25 +0800390 fragmentation(data->bytes_req, data->bytes_alloc));
391
392 next = rb_next(next);
393 }
394
395 if (n_lines == -1)
Li Zefan079d3f62009-11-24 13:26:55 +0800396 printf(" ... | ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +0800397
Li Zefan079d3f62009-11-24 13:26:55 +0800398 printf("%.102s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800399}
400
401static void print_summary(void)
402{
403 printf("\nSUMMARY\n=======\n");
404 printf("Total bytes requested: %lu\n", total_requested);
405 printf("Total bytes allocated: %lu\n", total_allocated);
406 printf("Total bytes wasted on internal fragmentation: %lu\n",
407 total_allocated - total_requested);
408 printf("Internal fragmentation: %f%%\n",
409 fragmentation(total_requested, total_allocated));
Li Zefan7d0d3942009-11-24 13:26:31 +0800410 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
Li Zefanba77c9e2009-11-20 15:53:25 +0800411}
412
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200413static void print_result(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +0800414{
415 if (caller_flag)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200416 __print_result(&root_caller_sorted, session, caller_lines, 1);
Li Zefanba77c9e2009-11-20 15:53:25 +0800417 if (alloc_flag)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200418 __print_result(&root_alloc_sorted, session, alloc_lines, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +0800419 print_summary();
420}
421
Li Zefan29b3e152009-11-24 13:26:10 +0800422struct sort_dimension {
423 const char name[20];
424 sort_fn_t cmp;
425 struct list_head list;
426};
427
428static LIST_HEAD(caller_sort);
429static LIST_HEAD(alloc_sort);
430
Li Zefanba77c9e2009-11-20 15:53:25 +0800431static void sort_insert(struct rb_root *root, struct alloc_stat *data,
Li Zefan29b3e152009-11-24 13:26:10 +0800432 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800433{
434 struct rb_node **new = &(root->rb_node);
435 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +0800436 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +0800437
438 while (*new) {
439 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +0800440 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800441
442 this = rb_entry(*new, struct alloc_stat, node);
443 parent = *new;
444
Li Zefan29b3e152009-11-24 13:26:10 +0800445 list_for_each_entry(sort, sort_list, list) {
446 cmp = sort->cmp(data, this);
447 if (cmp)
448 break;
449 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800450
451 if (cmp > 0)
452 new = &((*new)->rb_left);
453 else
454 new = &((*new)->rb_right);
455 }
456
457 rb_link_node(&data->node, parent, new);
458 rb_insert_color(&data->node, root);
459}
460
461static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
Li Zefan29b3e152009-11-24 13:26:10 +0800462 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800463{
464 struct rb_node *node;
465 struct alloc_stat *data;
466
467 for (;;) {
468 node = rb_first(root);
469 if (!node)
470 break;
471
472 rb_erase(node, root);
473 data = rb_entry(node, struct alloc_stat, node);
Li Zefan29b3e152009-11-24 13:26:10 +0800474 sort_insert(root_sorted, data, sort_list);
Li Zefanba77c9e2009-11-20 15:53:25 +0800475 }
476}
477
478static void sort_result(void)
479{
Li Zefan29b3e152009-11-24 13:26:10 +0800480 __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
481 __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
Li Zefanba77c9e2009-11-20 15:53:25 +0800482}
483
484static int __cmd_kmem(void)
485{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200486 int err = -EINVAL;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300487 struct perf_session *session;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300488 const struct perf_evsel_str_handler kmem_tracepoints[] = {
489 { "kmem:kmalloc", perf_evsel__process_alloc_event, },
490 { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
491 { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
492 { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
493 { "kmem:kfree", perf_evsel__process_free_event, },
494 { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
495 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300496
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300497 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_kmem);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200498 if (session == NULL)
499 return -ENOMEM;
Li Zefanba77c9e2009-11-20 15:53:25 +0800500
Arnaldo Carvalho de Meloe727ca72010-04-01 19:12:13 -0300501 if (perf_session__create_kernel_maps(session) < 0)
502 goto out_delete;
503
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200504 if (!perf_session__has_traces(session, "kmem record"))
505 goto out_delete;
506
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300507 if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
508 pr_err("Initializing perf session tracepoint handlers failed\n");
509 return -1;
510 }
511
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200512 setup_pager();
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300513 err = perf_session__process_events(session, &perf_kmem);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200514 if (err != 0)
515 goto out_delete;
516 sort_result();
517 print_result(session);
518out_delete:
519 perf_session__delete(session);
520 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +0800521}
522
523static const char * const kmem_usage[] = {
Li Zefan90b86a92009-12-10 15:21:57 +0800524 "perf kmem [<options>] {record|stat}",
Li Zefanba77c9e2009-11-20 15:53:25 +0800525 NULL
526};
527
Li Zefanba77c9e2009-11-20 15:53:25 +0800528static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
529{
530 if (l->ptr < r->ptr)
531 return -1;
532 else if (l->ptr > r->ptr)
533 return 1;
534 return 0;
535}
536
Li Zefan29b3e152009-11-24 13:26:10 +0800537static struct sort_dimension ptr_sort_dimension = {
538 .name = "ptr",
539 .cmp = ptr_cmp,
540};
541
Li Zefanba77c9e2009-11-20 15:53:25 +0800542static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
543{
544 if (l->call_site < r->call_site)
545 return -1;
546 else if (l->call_site > r->call_site)
547 return 1;
548 return 0;
549}
550
Li Zefan29b3e152009-11-24 13:26:10 +0800551static struct sort_dimension callsite_sort_dimension = {
552 .name = "callsite",
553 .cmp = callsite_cmp,
554};
555
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200556static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
557{
558 if (l->hit < r->hit)
559 return -1;
560 else if (l->hit > r->hit)
561 return 1;
562 return 0;
563}
564
Li Zefan29b3e152009-11-24 13:26:10 +0800565static struct sort_dimension hit_sort_dimension = {
566 .name = "hit",
567 .cmp = hit_cmp,
568};
569
Li Zefanba77c9e2009-11-20 15:53:25 +0800570static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
571{
572 if (l->bytes_alloc < r->bytes_alloc)
573 return -1;
574 else if (l->bytes_alloc > r->bytes_alloc)
575 return 1;
576 return 0;
577}
578
Li Zefan29b3e152009-11-24 13:26:10 +0800579static struct sort_dimension bytes_sort_dimension = {
580 .name = "bytes",
581 .cmp = bytes_cmp,
582};
583
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200584static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
585{
586 double x, y;
587
588 x = fragmentation(l->bytes_req, l->bytes_alloc);
589 y = fragmentation(r->bytes_req, r->bytes_alloc);
590
591 if (x < y)
592 return -1;
593 else if (x > y)
594 return 1;
595 return 0;
596}
597
Li Zefan29b3e152009-11-24 13:26:10 +0800598static struct sort_dimension frag_sort_dimension = {
599 .name = "frag",
600 .cmp = frag_cmp,
601};
602
Li Zefan079d3f62009-11-24 13:26:55 +0800603static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
604{
605 if (l->pingpong < r->pingpong)
606 return -1;
607 else if (l->pingpong > r->pingpong)
608 return 1;
609 return 0;
610}
611
612static struct sort_dimension pingpong_sort_dimension = {
613 .name = "pingpong",
614 .cmp = pingpong_cmp,
615};
616
Li Zefan29b3e152009-11-24 13:26:10 +0800617static struct sort_dimension *avail_sorts[] = {
618 &ptr_sort_dimension,
619 &callsite_sort_dimension,
620 &hit_sort_dimension,
621 &bytes_sort_dimension,
622 &frag_sort_dimension,
Li Zefan079d3f62009-11-24 13:26:55 +0800623 &pingpong_sort_dimension,
Li Zefan29b3e152009-11-24 13:26:10 +0800624};
625
626#define NUM_AVAIL_SORTS \
627 (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
628
629static int sort_dimension__add(const char *tok, struct list_head *list)
630{
631 struct sort_dimension *sort;
632 int i;
633
634 for (i = 0; i < NUM_AVAIL_SORTS; i++) {
635 if (!strcmp(avail_sorts[i]->name, tok)) {
636 sort = malloc(sizeof(*sort));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300637 if (!sort) {
638 pr_err("%s: malloc failed\n", __func__);
639 return -1;
640 }
Li Zefan29b3e152009-11-24 13:26:10 +0800641 memcpy(sort, avail_sorts[i], sizeof(*sort));
642 list_add_tail(&sort->list, list);
643 return 0;
644 }
645 }
646
647 return -1;
648}
649
650static int setup_sorting(struct list_head *sort_list, const char *arg)
651{
652 char *tok;
653 char *str = strdup(arg);
654
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300655 if (!str) {
656 pr_err("%s: strdup failed\n", __func__);
657 return -1;
658 }
Li Zefan29b3e152009-11-24 13:26:10 +0800659
660 while (true) {
661 tok = strsep(&str, ",");
662 if (!tok)
663 break;
664 if (sort_dimension__add(tok, sort_list) < 0) {
665 error("Unknown --sort key: '%s'", tok);
Namhyung Kim1b228592012-01-08 02:25:29 +0900666 free(str);
Li Zefan29b3e152009-11-24 13:26:10 +0800667 return -1;
668 }
669 }
670
671 free(str);
672 return 0;
673}
674
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300675static int parse_sort_opt(const struct option *opt __maybe_unused,
676 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800677{
Li Zefanba77c9e2009-11-20 15:53:25 +0800678 if (!arg)
679 return -1;
680
Li Zefanba77c9e2009-11-20 15:53:25 +0800681 if (caller_flag > alloc_flag)
Li Zefan29b3e152009-11-24 13:26:10 +0800682 return setup_sorting(&caller_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800683 else
Li Zefan29b3e152009-11-24 13:26:10 +0800684 return setup_sorting(&alloc_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800685
686 return 0;
687}
688
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300689static int parse_caller_opt(const struct option *opt __maybe_unused,
690 const char *arg __maybe_unused,
691 int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800692{
Li Zefan90b86a92009-12-10 15:21:57 +0800693 caller_flag = (alloc_flag + 1);
694 return 0;
695}
Li Zefanba77c9e2009-11-20 15:53:25 +0800696
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300697static int parse_alloc_opt(const struct option *opt __maybe_unused,
698 const char *arg __maybe_unused,
699 int unset __maybe_unused)
Li Zefan90b86a92009-12-10 15:21:57 +0800700{
701 alloc_flag = (caller_flag + 1);
Li Zefanba77c9e2009-11-20 15:53:25 +0800702 return 0;
703}
704
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300705static int parse_line_opt(const struct option *opt __maybe_unused,
706 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800707{
708 int lines;
709
710 if (!arg)
711 return -1;
712
713 lines = strtoul(arg, NULL, 10);
714
715 if (caller_flag > alloc_flag)
716 caller_lines = lines;
717 else
718 alloc_lines = lines;
719
720 return 0;
721}
722
723static const struct option kmem_options[] = {
724 OPT_STRING('i', "input", &input_name, "file",
725 "input file name"),
Li Zefan90b86a92009-12-10 15:21:57 +0800726 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
727 "show per-callsite statistics",
728 parse_caller_opt),
729 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
730 "show per-allocation statistics",
731 parse_alloc_opt),
Li Zefan29b3e152009-11-24 13:26:10 +0800732 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
Li Zefan079d3f62009-11-24 13:26:55 +0800733 "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
Li Zefanba77c9e2009-11-20 15:53:25 +0800734 parse_sort_opt),
735 OPT_CALLBACK('l', "line", NULL, "num",
Li Zefan90b86a92009-12-10 15:21:57 +0800736 "show n lines",
Li Zefanba77c9e2009-11-20 15:53:25 +0800737 parse_line_opt),
Li Zefan7707b6b2009-11-24 13:25:48 +0800738 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
Li Zefanba77c9e2009-11-20 15:53:25 +0800739 OPT_END()
740};
741
742static const char *record_args[] = {
743 "record",
744 "-a",
745 "-R",
Li Zefanba77c9e2009-11-20 15:53:25 +0800746 "-f",
747 "-c", "1",
748 "-e", "kmem:kmalloc",
749 "-e", "kmem:kmalloc_node",
750 "-e", "kmem:kfree",
751 "-e", "kmem:kmem_cache_alloc",
752 "-e", "kmem:kmem_cache_alloc_node",
753 "-e", "kmem:kmem_cache_free",
754};
755
756static int __cmd_record(int argc, const char **argv)
757{
758 unsigned int rec_argc, i, j;
759 const char **rec_argv;
760
761 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
762 rec_argv = calloc(rec_argc + 1, sizeof(char *));
763
Chris Samuelce47dc52010-11-13 13:35:06 +1100764 if (rec_argv == NULL)
765 return -ENOMEM;
766
Li Zefanba77c9e2009-11-20 15:53:25 +0800767 for (i = 0; i < ARRAY_SIZE(record_args); i++)
768 rec_argv[i] = strdup(record_args[i]);
769
770 for (j = 1; j < (unsigned int)argc; j++, i++)
771 rec_argv[i] = argv[j];
772
773 return cmd_record(i, rec_argv, NULL);
774}
775
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300776int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800777{
Li Zefanba77c9e2009-11-20 15:53:25 +0800778 argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
779
Li Zefan90b86a92009-12-10 15:21:57 +0800780 if (!argc)
Li Zefanba77c9e2009-11-20 15:53:25 +0800781 usage_with_options(kmem_usage, kmem_options);
782
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200783 symbol__init();
784
Li Zefan90b86a92009-12-10 15:21:57 +0800785 if (!strncmp(argv[0], "rec", 3)) {
786 return __cmd_record(argc, argv);
787 } else if (!strcmp(argv[0], "stat")) {
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300788 if (setup_cpunode_map())
789 return -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800790
Li Zefan90b86a92009-12-10 15:21:57 +0800791 if (list_empty(&caller_sort))
792 setup_sorting(&caller_sort, default_sort_order);
793 if (list_empty(&alloc_sort))
794 setup_sorting(&alloc_sort, default_sort_order);
Li Zefan7d0d3942009-11-24 13:26:31 +0800795
Li Zefan90b86a92009-12-10 15:21:57 +0800796 return __cmd_kmem();
Pekka Enbergb00eca82010-01-19 19:26:11 +0200797 } else
798 usage_with_options(kmem_usage, kmem_options);
Li Zefan90b86a92009-12-10 15:21:57 +0800799
800 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800801}
802