blob: 84b82397a28ea8cf1f8d20b9f7cc164191b85454 [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"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020016#include "util/data.h"
Don Zickus4b627952014-04-07 14:55:23 -040017#include "util/cpumap.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080018
19#include "util/debug.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080020
21#include <linux/rbtree.h>
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -030022#include <linux/string.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080023
24struct alloc_stat;
25typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
26
Li Zefanba77c9e2009-11-20 15:53:25 +080027static int alloc_flag;
28static int caller_flag;
29
Li Zefanba77c9e2009-11-20 15:53:25 +080030static int alloc_lines = -1;
31static int caller_lines = -1;
32
Li Zefan7707b6b2009-11-24 13:25:48 +080033static bool raw_ip;
34
Li Zefanba77c9e2009-11-20 15:53:25 +080035struct alloc_stat {
Li Zefan079d3f62009-11-24 13:26:55 +080036 u64 call_site;
37 u64 ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +080038 u64 bytes_req;
39 u64 bytes_alloc;
40 u32 hit;
Li Zefan079d3f62009-11-24 13:26:55 +080041 u32 pingpong;
42
43 short alloc_cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +080044
45 struct rb_node node;
46};
47
48static struct rb_root root_alloc_stat;
49static struct rb_root root_alloc_sorted;
50static struct rb_root root_caller_stat;
51static struct rb_root root_caller_sorted;
52
53static unsigned long total_requested, total_allocated;
Li Zefan7d0d3942009-11-24 13:26:31 +080054static unsigned long nr_allocs, nr_cross_allocs;
Li Zefanba77c9e2009-11-20 15:53:25 +080055
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030056static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
57 int bytes_req, int bytes_alloc, int cpu)
Li Zefanba77c9e2009-11-20 15:53:25 +080058{
59 struct rb_node **node = &root_alloc_stat.rb_node;
60 struct rb_node *parent = NULL;
61 struct alloc_stat *data = NULL;
62
Li Zefanba77c9e2009-11-20 15:53:25 +080063 while (*node) {
64 parent = *node;
65 data = rb_entry(*node, struct alloc_stat, node);
66
67 if (ptr > data->ptr)
68 node = &(*node)->rb_right;
69 else if (ptr < data->ptr)
70 node = &(*node)->rb_left;
71 else
72 break;
73 }
74
75 if (data && data->ptr == ptr) {
76 data->hit++;
77 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +080078 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +080079 } else {
80 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030081 if (!data) {
82 pr_err("%s: malloc failed\n", __func__);
83 return -1;
84 }
Li Zefanba77c9e2009-11-20 15:53:25 +080085 data->ptr = ptr;
Li Zefan079d3f62009-11-24 13:26:55 +080086 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +080087 data->hit = 1;
88 data->bytes_req = bytes_req;
89 data->bytes_alloc = bytes_alloc;
90
91 rb_link_node(&data->node, parent, node);
92 rb_insert_color(&data->node, &root_alloc_stat);
93 }
Li Zefan079d3f62009-11-24 13:26:55 +080094 data->call_site = call_site;
95 data->alloc_cpu = cpu;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030096 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +080097}
98
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030099static int insert_caller_stat(unsigned long call_site,
Li Zefanba77c9e2009-11-20 15:53:25 +0800100 int bytes_req, int bytes_alloc)
101{
102 struct rb_node **node = &root_caller_stat.rb_node;
103 struct rb_node *parent = NULL;
104 struct alloc_stat *data = NULL;
105
Li Zefanba77c9e2009-11-20 15:53:25 +0800106 while (*node) {
107 parent = *node;
108 data = rb_entry(*node, struct alloc_stat, node);
109
110 if (call_site > data->call_site)
111 node = &(*node)->rb_right;
112 else if (call_site < data->call_site)
113 node = &(*node)->rb_left;
114 else
115 break;
116 }
117
118 if (data && data->call_site == call_site) {
119 data->hit++;
120 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +0800121 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +0800122 } else {
123 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300124 if (!data) {
125 pr_err("%s: malloc failed\n", __func__);
126 return -1;
127 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800128 data->call_site = call_site;
Li Zefan079d3f62009-11-24 13:26:55 +0800129 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800130 data->hit = 1;
131 data->bytes_req = bytes_req;
132 data->bytes_alloc = bytes_alloc;
133
134 rb_link_node(&data->node, parent, node);
135 rb_insert_color(&data->node, &root_caller_stat);
136 }
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300137
138 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800139}
140
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300141static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300142 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800143{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300144 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
145 call_site = perf_evsel__intval(evsel, sample, "call_site");
146 int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
147 bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800148
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300149 if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300150 insert_caller_stat(call_site, bytes_req, bytes_alloc))
151 return -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800152
153 total_requested += bytes_req;
154 total_allocated += bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800155
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300156 nr_allocs++;
157 return 0;
158}
159
160static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
161 struct perf_sample *sample)
162{
163 int ret = perf_evsel__process_alloc_event(evsel, sample);
164
165 if (!ret) {
Don Zickus4b627952014-04-07 14:55:23 -0400166 int node1 = cpu__get_node(sample->cpu),
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300167 node2 = perf_evsel__intval(evsel, sample, "node");
168
Li Zefan7d0d3942009-11-24 13:26:31 +0800169 if (node1 != node2)
170 nr_cross_allocs++;
171 }
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300172
173 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +0800174}
175
Li Zefan079d3f62009-11-24 13:26:55 +0800176static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
177static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
178
179static struct alloc_stat *search_alloc_stat(unsigned long ptr,
180 unsigned long call_site,
181 struct rb_root *root,
182 sort_fn_t sort_fn)
183{
184 struct rb_node *node = root->rb_node;
185 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
186
187 while (node) {
188 struct alloc_stat *data;
189 int cmp;
190
191 data = rb_entry(node, struct alloc_stat, node);
192
193 cmp = sort_fn(&key, data);
194 if (cmp < 0)
195 node = node->rb_left;
196 else if (cmp > 0)
197 node = node->rb_right;
198 else
199 return data;
200 }
201 return NULL;
202}
203
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300204static int perf_evsel__process_free_event(struct perf_evsel *evsel,
205 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800206{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300207 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
Li Zefan079d3f62009-11-24 13:26:55 +0800208 struct alloc_stat *s_alloc, *s_caller;
209
Li Zefan079d3f62009-11-24 13:26:55 +0800210 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
211 if (!s_alloc)
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300212 return 0;
Li Zefan079d3f62009-11-24 13:26:55 +0800213
Arnaldo Carvalho de Melo22ad7982012-08-07 10:56:43 -0300214 if ((short)sample->cpu != s_alloc->alloc_cpu) {
Li Zefan079d3f62009-11-24 13:26:55 +0800215 s_alloc->pingpong++;
216
217 s_caller = search_alloc_stat(0, s_alloc->call_site,
218 &root_caller_stat, callsite_cmp);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300219 if (!s_caller)
220 return -1;
Li Zefan079d3f62009-11-24 13:26:55 +0800221 s_caller->pingpong++;
222 }
223 s_alloc->alloc_cpu = -1;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300224
225 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800226}
227
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300228typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
229 struct perf_sample *sample);
Li Zefanba77c9e2009-11-20 15:53:25 +0800230
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300231static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200232 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200233 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300234 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200235 struct machine *machine)
Li Zefanba77c9e2009-11-20 15:53:25 +0800236{
Adrian Hunteref893252013-08-27 11:23:06 +0300237 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900238 sample->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800239
Li Zefanba77c9e2009-11-20 15:53:25 +0800240 if (thread == NULL) {
241 pr_debug("problem processing %d event, skipping it.\n",
242 event->header.type);
243 return -1;
244 }
245
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200246 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800247
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300248 if (evsel->handler != NULL) {
249 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300250 return f(evsel, sample);
251 }
252
253 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800254}
255
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300256static struct perf_tool perf_kmem = {
257 .sample = process_sample_event,
258 .comm = perf_event__process_comm,
Namhyung Kim64c40902014-08-01 14:59:31 +0900259 .mmap = perf_event__process_mmap,
260 .mmap2 = perf_event__process_mmap2,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200261 .ordered_events = true,
Li Zefanba77c9e2009-11-20 15:53:25 +0800262};
263
Li Zefanba77c9e2009-11-20 15:53:25 +0800264static double fragmentation(unsigned long n_req, unsigned long n_alloc)
265{
266 if (n_alloc == 0)
267 return 0.0;
268 else
269 return 100.0 - (100.0 * n_req / n_alloc);
270}
271
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200272static void __print_result(struct rb_root *root, struct perf_session *session,
273 int n_lines, int is_caller)
Li Zefanba77c9e2009-11-20 15:53:25 +0800274{
275 struct rb_node *next;
Arnaldo Carvalho de Melo34ba5122012-12-19 09:04:24 -0300276 struct machine *machine = &session->machines.host;
Li Zefanba77c9e2009-11-20 15:53:25 +0800277
Li Zefan079d3f62009-11-24 13:26:55 +0800278 printf("%.102s\n", graph_dotted_line);
279 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
Pekka Enberg47103272010-01-19 19:23:23 +0200280 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
Li Zefan079d3f62009-11-24 13:26:55 +0800281 printf("%.102s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800282
283 next = rb_first(root);
284
285 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200286 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
287 node);
288 struct symbol *sym = NULL;
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300289 struct map *map;
Li Zefan079d3f62009-11-24 13:26:55 +0800290 char buf[BUFSIZ];
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200291 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800292
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200293 if (is_caller) {
294 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +0800295 if (!raw_ip)
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -0300296 sym = machine__find_kernel_function(machine, addr, &map, NULL);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200297 } else
298 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800299
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200300 if (sym != NULL)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200301 snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300302 addr - map->unmap_ip(map, sym->start));
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200303 else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200304 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
Li Zefan079d3f62009-11-24 13:26:55 +0800305 printf(" %-34s |", buf);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200306
Pekka Enberg47103272010-01-19 19:23:23 +0200307 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n",
Li Zefan079d3f62009-11-24 13:26:55 +0800308 (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +0800309 (unsigned long)data->bytes_alloc / data->hit,
310 (unsigned long long)data->bytes_req,
311 (unsigned long)data->bytes_req / data->hit,
312 (unsigned long)data->hit,
Li Zefan079d3f62009-11-24 13:26:55 +0800313 (unsigned long)data->pingpong,
Li Zefanba77c9e2009-11-20 15:53:25 +0800314 fragmentation(data->bytes_req, data->bytes_alloc));
315
316 next = rb_next(next);
317 }
318
319 if (n_lines == -1)
Li Zefan079d3f62009-11-24 13:26:55 +0800320 printf(" ... | ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +0800321
Li Zefan079d3f62009-11-24 13:26:55 +0800322 printf("%.102s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800323}
324
325static void print_summary(void)
326{
327 printf("\nSUMMARY\n=======\n");
328 printf("Total bytes requested: %lu\n", total_requested);
329 printf("Total bytes allocated: %lu\n", total_allocated);
330 printf("Total bytes wasted on internal fragmentation: %lu\n",
331 total_allocated - total_requested);
332 printf("Internal fragmentation: %f%%\n",
333 fragmentation(total_requested, total_allocated));
Li Zefan7d0d3942009-11-24 13:26:31 +0800334 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
Li Zefanba77c9e2009-11-20 15:53:25 +0800335}
336
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200337static void print_result(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +0800338{
339 if (caller_flag)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200340 __print_result(&root_caller_sorted, session, caller_lines, 1);
Li Zefanba77c9e2009-11-20 15:53:25 +0800341 if (alloc_flag)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200342 __print_result(&root_alloc_sorted, session, alloc_lines, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +0800343 print_summary();
344}
345
Li Zefan29b3e152009-11-24 13:26:10 +0800346struct sort_dimension {
347 const char name[20];
348 sort_fn_t cmp;
349 struct list_head list;
350};
351
352static LIST_HEAD(caller_sort);
353static LIST_HEAD(alloc_sort);
354
Li Zefanba77c9e2009-11-20 15:53:25 +0800355static void sort_insert(struct rb_root *root, struct alloc_stat *data,
Li Zefan29b3e152009-11-24 13:26:10 +0800356 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800357{
358 struct rb_node **new = &(root->rb_node);
359 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +0800360 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +0800361
362 while (*new) {
363 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +0800364 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800365
366 this = rb_entry(*new, struct alloc_stat, node);
367 parent = *new;
368
Li Zefan29b3e152009-11-24 13:26:10 +0800369 list_for_each_entry(sort, sort_list, list) {
370 cmp = sort->cmp(data, this);
371 if (cmp)
372 break;
373 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800374
375 if (cmp > 0)
376 new = &((*new)->rb_left);
377 else
378 new = &((*new)->rb_right);
379 }
380
381 rb_link_node(&data->node, parent, new);
382 rb_insert_color(&data->node, root);
383}
384
385static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
Li Zefan29b3e152009-11-24 13:26:10 +0800386 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800387{
388 struct rb_node *node;
389 struct alloc_stat *data;
390
391 for (;;) {
392 node = rb_first(root);
393 if (!node)
394 break;
395
396 rb_erase(node, root);
397 data = rb_entry(node, struct alloc_stat, node);
Li Zefan29b3e152009-11-24 13:26:10 +0800398 sort_insert(root_sorted, data, sort_list);
Li Zefanba77c9e2009-11-20 15:53:25 +0800399 }
400}
401
402static void sort_result(void)
403{
Li Zefan29b3e152009-11-24 13:26:10 +0800404 __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
405 __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
Li Zefanba77c9e2009-11-20 15:53:25 +0800406}
407
Feng Tang70cb4e92012-10-30 11:56:02 +0800408static int __cmd_kmem(void)
Li Zefanba77c9e2009-11-20 15:53:25 +0800409{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200410 int err = -EINVAL;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300411 struct perf_session *session;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300412 const struct perf_evsel_str_handler kmem_tracepoints[] = {
413 { "kmem:kmalloc", perf_evsel__process_alloc_event, },
414 { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
415 { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
416 { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
417 { "kmem:kfree", perf_evsel__process_free_event, },
418 { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
419 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200420 struct perf_data_file file = {
421 .path = input_name,
422 .mode = PERF_DATA_MODE_READ,
423 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300424
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200425 session = perf_session__new(&file, false, &perf_kmem);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200426 if (session == NULL)
427 return -ENOMEM;
Li Zefanba77c9e2009-11-20 15:53:25 +0800428
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200429 if (!perf_session__has_traces(session, "kmem record"))
430 goto out_delete;
431
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300432 if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
433 pr_err("Initializing perf session tracepoint handlers failed\n");
434 return -1;
435 }
436
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200437 setup_pager();
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300438 err = perf_session__process_events(session, &perf_kmem);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200439 if (err != 0)
440 goto out_delete;
441 sort_result();
442 print_result(session);
443out_delete:
444 perf_session__delete(session);
445 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +0800446}
447
Li Zefanba77c9e2009-11-20 15:53:25 +0800448static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
449{
450 if (l->ptr < r->ptr)
451 return -1;
452 else if (l->ptr > r->ptr)
453 return 1;
454 return 0;
455}
456
Li Zefan29b3e152009-11-24 13:26:10 +0800457static struct sort_dimension ptr_sort_dimension = {
458 .name = "ptr",
459 .cmp = ptr_cmp,
460};
461
Li Zefanba77c9e2009-11-20 15:53:25 +0800462static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
463{
464 if (l->call_site < r->call_site)
465 return -1;
466 else if (l->call_site > r->call_site)
467 return 1;
468 return 0;
469}
470
Li Zefan29b3e152009-11-24 13:26:10 +0800471static struct sort_dimension callsite_sort_dimension = {
472 .name = "callsite",
473 .cmp = callsite_cmp,
474};
475
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200476static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
477{
478 if (l->hit < r->hit)
479 return -1;
480 else if (l->hit > r->hit)
481 return 1;
482 return 0;
483}
484
Li Zefan29b3e152009-11-24 13:26:10 +0800485static struct sort_dimension hit_sort_dimension = {
486 .name = "hit",
487 .cmp = hit_cmp,
488};
489
Li Zefanba77c9e2009-11-20 15:53:25 +0800490static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
491{
492 if (l->bytes_alloc < r->bytes_alloc)
493 return -1;
494 else if (l->bytes_alloc > r->bytes_alloc)
495 return 1;
496 return 0;
497}
498
Li Zefan29b3e152009-11-24 13:26:10 +0800499static struct sort_dimension bytes_sort_dimension = {
500 .name = "bytes",
501 .cmp = bytes_cmp,
502};
503
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200504static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
505{
506 double x, y;
507
508 x = fragmentation(l->bytes_req, l->bytes_alloc);
509 y = fragmentation(r->bytes_req, r->bytes_alloc);
510
511 if (x < y)
512 return -1;
513 else if (x > y)
514 return 1;
515 return 0;
516}
517
Li Zefan29b3e152009-11-24 13:26:10 +0800518static struct sort_dimension frag_sort_dimension = {
519 .name = "frag",
520 .cmp = frag_cmp,
521};
522
Li Zefan079d3f62009-11-24 13:26:55 +0800523static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
524{
525 if (l->pingpong < r->pingpong)
526 return -1;
527 else if (l->pingpong > r->pingpong)
528 return 1;
529 return 0;
530}
531
532static struct sort_dimension pingpong_sort_dimension = {
533 .name = "pingpong",
534 .cmp = pingpong_cmp,
535};
536
Li Zefan29b3e152009-11-24 13:26:10 +0800537static struct sort_dimension *avail_sorts[] = {
538 &ptr_sort_dimension,
539 &callsite_sort_dimension,
540 &hit_sort_dimension,
541 &bytes_sort_dimension,
542 &frag_sort_dimension,
Li Zefan079d3f62009-11-24 13:26:55 +0800543 &pingpong_sort_dimension,
Li Zefan29b3e152009-11-24 13:26:10 +0800544};
545
Sasha Levin49e4ba52012-12-20 14:11:16 -0500546#define NUM_AVAIL_SORTS ((int)ARRAY_SIZE(avail_sorts))
Li Zefan29b3e152009-11-24 13:26:10 +0800547
548static int sort_dimension__add(const char *tok, struct list_head *list)
549{
550 struct sort_dimension *sort;
551 int i;
552
553 for (i = 0; i < NUM_AVAIL_SORTS; i++) {
554 if (!strcmp(avail_sorts[i]->name, tok)) {
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -0300555 sort = memdup(avail_sorts[i], sizeof(*avail_sorts[i]));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300556 if (!sort) {
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -0300557 pr_err("%s: memdup failed\n", __func__);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300558 return -1;
559 }
Li Zefan29b3e152009-11-24 13:26:10 +0800560 list_add_tail(&sort->list, list);
561 return 0;
562 }
563 }
564
565 return -1;
566}
567
568static int setup_sorting(struct list_head *sort_list, const char *arg)
569{
570 char *tok;
571 char *str = strdup(arg);
572
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300573 if (!str) {
574 pr_err("%s: strdup failed\n", __func__);
575 return -1;
576 }
Li Zefan29b3e152009-11-24 13:26:10 +0800577
578 while (true) {
579 tok = strsep(&str, ",");
580 if (!tok)
581 break;
582 if (sort_dimension__add(tok, sort_list) < 0) {
583 error("Unknown --sort key: '%s'", tok);
Namhyung Kim1b228592012-01-08 02:25:29 +0900584 free(str);
Li Zefan29b3e152009-11-24 13:26:10 +0800585 return -1;
586 }
587 }
588
589 free(str);
590 return 0;
591}
592
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300593static int parse_sort_opt(const struct option *opt __maybe_unused,
594 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800595{
Li Zefanba77c9e2009-11-20 15:53:25 +0800596 if (!arg)
597 return -1;
598
Li Zefanba77c9e2009-11-20 15:53:25 +0800599 if (caller_flag > alloc_flag)
Li Zefan29b3e152009-11-24 13:26:10 +0800600 return setup_sorting(&caller_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800601 else
Li Zefan29b3e152009-11-24 13:26:10 +0800602 return setup_sorting(&alloc_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800603
604 return 0;
605}
606
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300607static int parse_caller_opt(const struct option *opt __maybe_unused,
608 const char *arg __maybe_unused,
609 int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800610{
Li Zefan90b86a92009-12-10 15:21:57 +0800611 caller_flag = (alloc_flag + 1);
612 return 0;
613}
Li Zefanba77c9e2009-11-20 15:53:25 +0800614
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300615static int parse_alloc_opt(const struct option *opt __maybe_unused,
616 const char *arg __maybe_unused,
617 int unset __maybe_unused)
Li Zefan90b86a92009-12-10 15:21:57 +0800618{
619 alloc_flag = (caller_flag + 1);
Li Zefanba77c9e2009-11-20 15:53:25 +0800620 return 0;
621}
622
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300623static int parse_line_opt(const struct option *opt __maybe_unused,
624 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800625{
626 int lines;
627
628 if (!arg)
629 return -1;
630
631 lines = strtoul(arg, NULL, 10);
632
633 if (caller_flag > alloc_flag)
634 caller_lines = lines;
635 else
636 alloc_lines = lines;
637
638 return 0;
639}
640
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300641static int __cmd_record(int argc, const char **argv)
642{
643 const char * const record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +0200644 "record", "-a", "-R", "-c", "1",
Li Zefanba77c9e2009-11-20 15:53:25 +0800645 "-e", "kmem:kmalloc",
646 "-e", "kmem:kmalloc_node",
647 "-e", "kmem:kfree",
648 "-e", "kmem:kmem_cache_alloc",
649 "-e", "kmem:kmem_cache_alloc_node",
650 "-e", "kmem:kmem_cache_free",
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300651 };
Li Zefanba77c9e2009-11-20 15:53:25 +0800652 unsigned int rec_argc, i, j;
653 const char **rec_argv;
654
655 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
656 rec_argv = calloc(rec_argc + 1, sizeof(char *));
657
Chris Samuelce47dc52010-11-13 13:35:06 +1100658 if (rec_argv == NULL)
659 return -ENOMEM;
660
Li Zefanba77c9e2009-11-20 15:53:25 +0800661 for (i = 0; i < ARRAY_SIZE(record_args); i++)
662 rec_argv[i] = strdup(record_args[i]);
663
664 for (j = 1; j < (unsigned int)argc; j++, i++)
665 rec_argv[i] = argv[j];
666
667 return cmd_record(i, rec_argv, NULL);
668}
669
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300670int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800671{
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300672 const char * const default_sort_order = "frag,hit,bytes";
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300673 const struct option kmem_options[] = {
674 OPT_STRING('i', "input", &input_name, "file", "input file name"),
675 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
676 "show per-callsite statistics", parse_caller_opt),
677 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
678 "show per-allocation statistics", parse_alloc_opt),
679 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
680 "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
681 parse_sort_opt),
682 OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
683 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
684 OPT_END()
685 };
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -0400686 const char *const kmem_subcommands[] = { "record", "stat", NULL };
687 const char *kmem_usage[] = {
688 NULL,
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300689 NULL
690 };
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -0400691 argc = parse_options_subcommand(argc, argv, kmem_options,
692 kmem_subcommands, kmem_usage, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +0800693
Li Zefan90b86a92009-12-10 15:21:57 +0800694 if (!argc)
Li Zefanba77c9e2009-11-20 15:53:25 +0800695 usage_with_options(kmem_usage, kmem_options);
696
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200697 symbol__init();
698
Li Zefan90b86a92009-12-10 15:21:57 +0800699 if (!strncmp(argv[0], "rec", 3)) {
700 return __cmd_record(argc, argv);
701 } else if (!strcmp(argv[0], "stat")) {
Don Zickus4b627952014-04-07 14:55:23 -0400702 if (cpu__setup_cpunode_map())
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300703 return -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800704
Li Zefan90b86a92009-12-10 15:21:57 +0800705 if (list_empty(&caller_sort))
706 setup_sorting(&caller_sort, default_sort_order);
707 if (list_empty(&alloc_sort))
708 setup_sorting(&alloc_sort, default_sort_order);
Li Zefan7d0d3942009-11-24 13:26:31 +0800709
Feng Tang70cb4e92012-10-30 11:56:02 +0800710 return __cmd_kmem();
Pekka Enbergb00eca82010-01-19 19:26:11 +0200711 } else
712 usage_with_options(kmem_usage, kmem_options);
Li Zefan90b86a92009-12-10 15:21:57 +0800713
714 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800715}
716