blob: 4ebf65c7943443291db80f3f3cd36aa9f17e85e6 [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>
Namhyung Kim77cfe382015-03-23 15:30:40 +090023#include <locale.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080024
25struct alloc_stat;
26typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
27
Li Zefanba77c9e2009-11-20 15:53:25 +080028static int alloc_flag;
29static int caller_flag;
30
Li Zefanba77c9e2009-11-20 15:53:25 +080031static int alloc_lines = -1;
32static int caller_lines = -1;
33
Li Zefan7707b6b2009-11-24 13:25:48 +080034static bool raw_ip;
35
Li Zefanba77c9e2009-11-20 15:53:25 +080036struct alloc_stat {
Li Zefan079d3f62009-11-24 13:26:55 +080037 u64 call_site;
38 u64 ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +080039 u64 bytes_req;
40 u64 bytes_alloc;
41 u32 hit;
Li Zefan079d3f62009-11-24 13:26:55 +080042 u32 pingpong;
43
44 short alloc_cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +080045
46 struct rb_node node;
47};
48
49static struct rb_root root_alloc_stat;
50static struct rb_root root_alloc_sorted;
51static struct rb_root root_caller_stat;
52static struct rb_root root_caller_sorted;
53
54static unsigned long total_requested, total_allocated;
Li Zefan7d0d3942009-11-24 13:26:31 +080055static unsigned long nr_allocs, nr_cross_allocs;
Li Zefanba77c9e2009-11-20 15:53:25 +080056
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030057static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
58 int bytes_req, int bytes_alloc, int cpu)
Li Zefanba77c9e2009-11-20 15:53:25 +080059{
60 struct rb_node **node = &root_alloc_stat.rb_node;
61 struct rb_node *parent = NULL;
62 struct alloc_stat *data = NULL;
63
Li Zefanba77c9e2009-11-20 15:53:25 +080064 while (*node) {
65 parent = *node;
66 data = rb_entry(*node, struct alloc_stat, node);
67
68 if (ptr > data->ptr)
69 node = &(*node)->rb_right;
70 else if (ptr < data->ptr)
71 node = &(*node)->rb_left;
72 else
73 break;
74 }
75
76 if (data && data->ptr == ptr) {
77 data->hit++;
78 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +080079 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +080080 } else {
81 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030082 if (!data) {
83 pr_err("%s: malloc failed\n", __func__);
84 return -1;
85 }
Li Zefanba77c9e2009-11-20 15:53:25 +080086 data->ptr = ptr;
Li Zefan079d3f62009-11-24 13:26:55 +080087 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +080088 data->hit = 1;
89 data->bytes_req = bytes_req;
90 data->bytes_alloc = bytes_alloc;
91
92 rb_link_node(&data->node, parent, node);
93 rb_insert_color(&data->node, &root_alloc_stat);
94 }
Li Zefan079d3f62009-11-24 13:26:55 +080095 data->call_site = call_site;
96 data->alloc_cpu = cpu;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030097 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +080098}
99
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300100static int insert_caller_stat(unsigned long call_site,
Li Zefanba77c9e2009-11-20 15:53:25 +0800101 int bytes_req, int bytes_alloc)
102{
103 struct rb_node **node = &root_caller_stat.rb_node;
104 struct rb_node *parent = NULL;
105 struct alloc_stat *data = NULL;
106
Li Zefanba77c9e2009-11-20 15:53:25 +0800107 while (*node) {
108 parent = *node;
109 data = rb_entry(*node, struct alloc_stat, node);
110
111 if (call_site > data->call_site)
112 node = &(*node)->rb_right;
113 else if (call_site < data->call_site)
114 node = &(*node)->rb_left;
115 else
116 break;
117 }
118
119 if (data && data->call_site == call_site) {
120 data->hit++;
121 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +0800122 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +0800123 } else {
124 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300125 if (!data) {
126 pr_err("%s: malloc failed\n", __func__);
127 return -1;
128 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800129 data->call_site = call_site;
Li Zefan079d3f62009-11-24 13:26:55 +0800130 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800131 data->hit = 1;
132 data->bytes_req = bytes_req;
133 data->bytes_alloc = bytes_alloc;
134
135 rb_link_node(&data->node, parent, node);
136 rb_insert_color(&data->node, &root_caller_stat);
137 }
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300138
139 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800140}
141
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300142static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300143 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800144{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300145 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
146 call_site = perf_evsel__intval(evsel, sample, "call_site");
147 int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
148 bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800149
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300150 if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300151 insert_caller_stat(call_site, bytes_req, bytes_alloc))
152 return -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800153
154 total_requested += bytes_req;
155 total_allocated += bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800156
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300157 nr_allocs++;
158 return 0;
159}
160
161static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
162 struct perf_sample *sample)
163{
164 int ret = perf_evsel__process_alloc_event(evsel, sample);
165
166 if (!ret) {
Don Zickus4b627952014-04-07 14:55:23 -0400167 int node1 = cpu__get_node(sample->cpu),
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300168 node2 = perf_evsel__intval(evsel, sample, "node");
169
Li Zefan7d0d3942009-11-24 13:26:31 +0800170 if (node1 != node2)
171 nr_cross_allocs++;
172 }
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300173
174 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +0800175}
176
Li Zefan079d3f62009-11-24 13:26:55 +0800177static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
178static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
179
180static struct alloc_stat *search_alloc_stat(unsigned long ptr,
181 unsigned long call_site,
182 struct rb_root *root,
183 sort_fn_t sort_fn)
184{
185 struct rb_node *node = root->rb_node;
186 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
187
188 while (node) {
189 struct alloc_stat *data;
190 int cmp;
191
192 data = rb_entry(node, struct alloc_stat, node);
193
194 cmp = sort_fn(&key, data);
195 if (cmp < 0)
196 node = node->rb_left;
197 else if (cmp > 0)
198 node = node->rb_right;
199 else
200 return data;
201 }
202 return NULL;
203}
204
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300205static int perf_evsel__process_free_event(struct perf_evsel *evsel,
206 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800207{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300208 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
Li Zefan079d3f62009-11-24 13:26:55 +0800209 struct alloc_stat *s_alloc, *s_caller;
210
Li Zefan079d3f62009-11-24 13:26:55 +0800211 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
212 if (!s_alloc)
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300213 return 0;
Li Zefan079d3f62009-11-24 13:26:55 +0800214
Arnaldo Carvalho de Melo22ad7982012-08-07 10:56:43 -0300215 if ((short)sample->cpu != s_alloc->alloc_cpu) {
Li Zefan079d3f62009-11-24 13:26:55 +0800216 s_alloc->pingpong++;
217
218 s_caller = search_alloc_stat(0, s_alloc->call_site,
219 &root_caller_stat, callsite_cmp);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300220 if (!s_caller)
221 return -1;
Li Zefan079d3f62009-11-24 13:26:55 +0800222 s_caller->pingpong++;
223 }
224 s_alloc->alloc_cpu = -1;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300225
226 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800227}
228
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300229typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
230 struct perf_sample *sample);
Li Zefanba77c9e2009-11-20 15:53:25 +0800231
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300232static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200233 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200234 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300235 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200236 struct machine *machine)
Li Zefanba77c9e2009-11-20 15:53:25 +0800237{
Adrian Hunteref893252013-08-27 11:23:06 +0300238 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900239 sample->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800240
Li Zefanba77c9e2009-11-20 15:53:25 +0800241 if (thread == NULL) {
242 pr_debug("problem processing %d event, skipping it.\n",
243 event->header.type);
244 return -1;
245 }
246
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200247 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800248
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300249 if (evsel->handler != NULL) {
250 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300251 return f(evsel, sample);
252 }
253
254 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800255}
256
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300257static struct perf_tool perf_kmem = {
258 .sample = process_sample_event,
259 .comm = perf_event__process_comm,
Namhyung Kim64c40902014-08-01 14:59:31 +0900260 .mmap = perf_event__process_mmap,
261 .mmap2 = perf_event__process_mmap2,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200262 .ordered_events = true,
Li Zefanba77c9e2009-11-20 15:53:25 +0800263};
264
Li Zefanba77c9e2009-11-20 15:53:25 +0800265static double fragmentation(unsigned long n_req, unsigned long n_alloc)
266{
267 if (n_alloc == 0)
268 return 0.0;
269 else
270 return 100.0 - (100.0 * n_req / n_alloc);
271}
272
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200273static void __print_result(struct rb_root *root, struct perf_session *session,
274 int n_lines, int is_caller)
Li Zefanba77c9e2009-11-20 15:53:25 +0800275{
276 struct rb_node *next;
Arnaldo Carvalho de Melo34ba5122012-12-19 09:04:24 -0300277 struct machine *machine = &session->machines.host;
Li Zefanba77c9e2009-11-20 15:53:25 +0800278
Namhyung Kim65f46e02015-03-12 16:32:48 +0900279 printf("%.105s\n", graph_dotted_line);
Li Zefan079d3f62009-11-24 13:26:55 +0800280 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
Pekka Enberg47103272010-01-19 19:23:23 +0200281 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
Namhyung Kim65f46e02015-03-12 16:32:48 +0900282 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800283
284 next = rb_first(root);
285
286 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200287 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
288 node);
289 struct symbol *sym = NULL;
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300290 struct map *map;
Li Zefan079d3f62009-11-24 13:26:55 +0800291 char buf[BUFSIZ];
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200292 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800293
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200294 if (is_caller) {
295 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +0800296 if (!raw_ip)
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -0300297 sym = machine__find_kernel_function(machine, addr, &map, NULL);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200298 } else
299 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800300
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200301 if (sym != NULL)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200302 snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300303 addr - map->unmap_ip(map, sym->start));
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200304 else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200305 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
Li Zefan079d3f62009-11-24 13:26:55 +0800306 printf(" %-34s |", buf);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200307
Namhyung Kim65f46e02015-03-12 16:32:48 +0900308 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n",
Li Zefan079d3f62009-11-24 13:26:55 +0800309 (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +0800310 (unsigned long)data->bytes_alloc / data->hit,
311 (unsigned long long)data->bytes_req,
312 (unsigned long)data->bytes_req / data->hit,
313 (unsigned long)data->hit,
Li Zefan079d3f62009-11-24 13:26:55 +0800314 (unsigned long)data->pingpong,
Li Zefanba77c9e2009-11-20 15:53:25 +0800315 fragmentation(data->bytes_req, data->bytes_alloc));
316
317 next = rb_next(next);
318 }
319
320 if (n_lines == -1)
Namhyung Kim65f46e02015-03-12 16:32:48 +0900321 printf(" ... | ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +0800322
Namhyung Kim65f46e02015-03-12 16:32:48 +0900323 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800324}
325
326static void print_summary(void)
327{
328 printf("\nSUMMARY\n=======\n");
Namhyung Kim77cfe382015-03-23 15:30:40 +0900329 printf("Total bytes requested: %'lu\n", total_requested);
330 printf("Total bytes allocated: %'lu\n", total_allocated);
331 printf("Total bytes wasted on internal fragmentation: %'lu\n",
Li Zefanba77c9e2009-11-20 15:53:25 +0800332 total_allocated - total_requested);
333 printf("Internal fragmentation: %f%%\n",
334 fragmentation(total_requested, total_allocated));
Namhyung Kim77cfe382015-03-23 15:30:40 +0900335 printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
Li Zefanba77c9e2009-11-20 15:53:25 +0800336}
337
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200338static void print_result(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +0800339{
340 if (caller_flag)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200341 __print_result(&root_caller_sorted, session, caller_lines, 1);
Li Zefanba77c9e2009-11-20 15:53:25 +0800342 if (alloc_flag)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200343 __print_result(&root_alloc_sorted, session, alloc_lines, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +0800344 print_summary();
345}
346
Li Zefan29b3e152009-11-24 13:26:10 +0800347struct sort_dimension {
348 const char name[20];
349 sort_fn_t cmp;
350 struct list_head list;
351};
352
353static LIST_HEAD(caller_sort);
354static LIST_HEAD(alloc_sort);
355
Li Zefanba77c9e2009-11-20 15:53:25 +0800356static void sort_insert(struct rb_root *root, struct alloc_stat *data,
Li Zefan29b3e152009-11-24 13:26:10 +0800357 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800358{
359 struct rb_node **new = &(root->rb_node);
360 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +0800361 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +0800362
363 while (*new) {
364 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +0800365 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800366
367 this = rb_entry(*new, struct alloc_stat, node);
368 parent = *new;
369
Li Zefan29b3e152009-11-24 13:26:10 +0800370 list_for_each_entry(sort, sort_list, list) {
371 cmp = sort->cmp(data, this);
372 if (cmp)
373 break;
374 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800375
376 if (cmp > 0)
377 new = &((*new)->rb_left);
378 else
379 new = &((*new)->rb_right);
380 }
381
382 rb_link_node(&data->node, parent, new);
383 rb_insert_color(&data->node, root);
384}
385
386static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
Li Zefan29b3e152009-11-24 13:26:10 +0800387 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800388{
389 struct rb_node *node;
390 struct alloc_stat *data;
391
392 for (;;) {
393 node = rb_first(root);
394 if (!node)
395 break;
396
397 rb_erase(node, root);
398 data = rb_entry(node, struct alloc_stat, node);
Li Zefan29b3e152009-11-24 13:26:10 +0800399 sort_insert(root_sorted, data, sort_list);
Li Zefanba77c9e2009-11-20 15:53:25 +0800400 }
401}
402
403static void sort_result(void)
404{
Li Zefan29b3e152009-11-24 13:26:10 +0800405 __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
406 __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
Li Zefanba77c9e2009-11-20 15:53:25 +0800407}
408
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900409static int __cmd_kmem(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +0800410{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200411 int err = -EINVAL;
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 };
Li Zefanba77c9e2009-11-20 15:53:25 +0800420
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200421 if (!perf_session__has_traces(session, "kmem record"))
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900422 goto out;
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200423
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300424 if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
425 pr_err("Initializing perf session tracepoint handlers failed\n");
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900426 goto out;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300427 }
428
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200429 setup_pager();
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300430 err = perf_session__process_events(session);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200431 if (err != 0)
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900432 goto out;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200433 sort_result();
434 print_result(session);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900435out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200436 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +0800437}
438
Li Zefanba77c9e2009-11-20 15:53:25 +0800439static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
440{
441 if (l->ptr < r->ptr)
442 return -1;
443 else if (l->ptr > r->ptr)
444 return 1;
445 return 0;
446}
447
Li Zefan29b3e152009-11-24 13:26:10 +0800448static struct sort_dimension ptr_sort_dimension = {
449 .name = "ptr",
450 .cmp = ptr_cmp,
451};
452
Li Zefanba77c9e2009-11-20 15:53:25 +0800453static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
454{
455 if (l->call_site < r->call_site)
456 return -1;
457 else if (l->call_site > r->call_site)
458 return 1;
459 return 0;
460}
461
Li Zefan29b3e152009-11-24 13:26:10 +0800462static struct sort_dimension callsite_sort_dimension = {
463 .name = "callsite",
464 .cmp = callsite_cmp,
465};
466
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200467static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
468{
469 if (l->hit < r->hit)
470 return -1;
471 else if (l->hit > r->hit)
472 return 1;
473 return 0;
474}
475
Li Zefan29b3e152009-11-24 13:26:10 +0800476static struct sort_dimension hit_sort_dimension = {
477 .name = "hit",
478 .cmp = hit_cmp,
479};
480
Li Zefanba77c9e2009-11-20 15:53:25 +0800481static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
482{
483 if (l->bytes_alloc < r->bytes_alloc)
484 return -1;
485 else if (l->bytes_alloc > r->bytes_alloc)
486 return 1;
487 return 0;
488}
489
Li Zefan29b3e152009-11-24 13:26:10 +0800490static struct sort_dimension bytes_sort_dimension = {
491 .name = "bytes",
492 .cmp = bytes_cmp,
493};
494
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200495static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
496{
497 double x, y;
498
499 x = fragmentation(l->bytes_req, l->bytes_alloc);
500 y = fragmentation(r->bytes_req, r->bytes_alloc);
501
502 if (x < y)
503 return -1;
504 else if (x > y)
505 return 1;
506 return 0;
507}
508
Li Zefan29b3e152009-11-24 13:26:10 +0800509static struct sort_dimension frag_sort_dimension = {
510 .name = "frag",
511 .cmp = frag_cmp,
512};
513
Li Zefan079d3f62009-11-24 13:26:55 +0800514static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
515{
516 if (l->pingpong < r->pingpong)
517 return -1;
518 else if (l->pingpong > r->pingpong)
519 return 1;
520 return 0;
521}
522
523static struct sort_dimension pingpong_sort_dimension = {
524 .name = "pingpong",
525 .cmp = pingpong_cmp,
526};
527
Li Zefan29b3e152009-11-24 13:26:10 +0800528static struct sort_dimension *avail_sorts[] = {
529 &ptr_sort_dimension,
530 &callsite_sort_dimension,
531 &hit_sort_dimension,
532 &bytes_sort_dimension,
533 &frag_sort_dimension,
Li Zefan079d3f62009-11-24 13:26:55 +0800534 &pingpong_sort_dimension,
Li Zefan29b3e152009-11-24 13:26:10 +0800535};
536
Sasha Levin49e4ba52012-12-20 14:11:16 -0500537#define NUM_AVAIL_SORTS ((int)ARRAY_SIZE(avail_sorts))
Li Zefan29b3e152009-11-24 13:26:10 +0800538
539static int sort_dimension__add(const char *tok, struct list_head *list)
540{
541 struct sort_dimension *sort;
542 int i;
543
544 for (i = 0; i < NUM_AVAIL_SORTS; i++) {
545 if (!strcmp(avail_sorts[i]->name, tok)) {
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -0300546 sort = memdup(avail_sorts[i], sizeof(*avail_sorts[i]));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300547 if (!sort) {
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -0300548 pr_err("%s: memdup failed\n", __func__);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300549 return -1;
550 }
Li Zefan29b3e152009-11-24 13:26:10 +0800551 list_add_tail(&sort->list, list);
552 return 0;
553 }
554 }
555
556 return -1;
557}
558
559static int setup_sorting(struct list_head *sort_list, const char *arg)
560{
561 char *tok;
562 char *str = strdup(arg);
Namhyung Kim405f8752015-03-12 16:32:46 +0900563 char *pos = str;
Li Zefan29b3e152009-11-24 13:26:10 +0800564
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300565 if (!str) {
566 pr_err("%s: strdup failed\n", __func__);
567 return -1;
568 }
Li Zefan29b3e152009-11-24 13:26:10 +0800569
570 while (true) {
Namhyung Kim405f8752015-03-12 16:32:46 +0900571 tok = strsep(&pos, ",");
Li Zefan29b3e152009-11-24 13:26:10 +0800572 if (!tok)
573 break;
574 if (sort_dimension__add(tok, sort_list) < 0) {
575 error("Unknown --sort key: '%s'", tok);
Namhyung Kim1b228592012-01-08 02:25:29 +0900576 free(str);
Li Zefan29b3e152009-11-24 13:26:10 +0800577 return -1;
578 }
579 }
580
581 free(str);
582 return 0;
583}
584
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300585static int parse_sort_opt(const struct option *opt __maybe_unused,
586 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800587{
Li Zefanba77c9e2009-11-20 15:53:25 +0800588 if (!arg)
589 return -1;
590
Li Zefanba77c9e2009-11-20 15:53:25 +0800591 if (caller_flag > alloc_flag)
Li Zefan29b3e152009-11-24 13:26:10 +0800592 return setup_sorting(&caller_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800593 else
Li Zefan29b3e152009-11-24 13:26:10 +0800594 return setup_sorting(&alloc_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800595
596 return 0;
597}
598
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300599static int parse_caller_opt(const struct option *opt __maybe_unused,
600 const char *arg __maybe_unused,
601 int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800602{
Li Zefan90b86a92009-12-10 15:21:57 +0800603 caller_flag = (alloc_flag + 1);
604 return 0;
605}
Li Zefanba77c9e2009-11-20 15:53:25 +0800606
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300607static int parse_alloc_opt(const struct option *opt __maybe_unused,
608 const char *arg __maybe_unused,
609 int unset __maybe_unused)
Li Zefan90b86a92009-12-10 15:21:57 +0800610{
611 alloc_flag = (caller_flag + 1);
Li Zefanba77c9e2009-11-20 15:53:25 +0800612 return 0;
613}
614
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300615static int parse_line_opt(const struct option *opt __maybe_unused,
616 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800617{
618 int lines;
619
620 if (!arg)
621 return -1;
622
623 lines = strtoul(arg, NULL, 10);
624
625 if (caller_flag > alloc_flag)
626 caller_lines = lines;
627 else
628 alloc_lines = lines;
629
630 return 0;
631}
632
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300633static int __cmd_record(int argc, const char **argv)
634{
635 const char * const record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +0200636 "record", "-a", "-R", "-c", "1",
Li Zefanba77c9e2009-11-20 15:53:25 +0800637 "-e", "kmem:kmalloc",
638 "-e", "kmem:kmalloc_node",
639 "-e", "kmem:kfree",
640 "-e", "kmem:kmem_cache_alloc",
641 "-e", "kmem:kmem_cache_alloc_node",
642 "-e", "kmem:kmem_cache_free",
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300643 };
Li Zefanba77c9e2009-11-20 15:53:25 +0800644 unsigned int rec_argc, i, j;
645 const char **rec_argv;
646
647 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
648 rec_argv = calloc(rec_argc + 1, sizeof(char *));
649
Chris Samuelce47dc52010-11-13 13:35:06 +1100650 if (rec_argv == NULL)
651 return -ENOMEM;
652
Li Zefanba77c9e2009-11-20 15:53:25 +0800653 for (i = 0; i < ARRAY_SIZE(record_args); i++)
654 rec_argv[i] = strdup(record_args[i]);
655
656 for (j = 1; j < (unsigned int)argc; j++, i++)
657 rec_argv[i] = argv[j];
658
659 return cmd_record(i, rec_argv, NULL);
660}
661
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300662int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +0800663{
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300664 const char * const default_sort_order = "frag,hit,bytes";
Yunlong Songd1eeb772015-04-02 21:47:12 +0800665 struct perf_data_file file = {
Yunlong Songd1eeb772015-04-02 21:47:12 +0800666 .mode = PERF_DATA_MODE_READ,
667 };
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300668 const struct option kmem_options[] = {
669 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Namhyung Kimbd72a332015-03-12 16:32:47 +0900670 OPT_INCR('v', "verbose", &verbose,
671 "be more verbose (show symbol address, etc)"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300672 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
673 "show per-callsite statistics", parse_caller_opt),
674 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
675 "show per-allocation statistics", parse_alloc_opt),
676 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
677 "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
678 parse_sort_opt),
679 OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
680 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
Yunlong Songd1eeb772015-04-02 21:47:12 +0800681 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300682 OPT_END()
683 };
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -0400684 const char *const kmem_subcommands[] = { "record", "stat", NULL };
685 const char *kmem_usage[] = {
686 NULL,
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -0300687 NULL
688 };
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900689 struct perf_session *session;
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900690 int ret = -1;
691
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -0400692 argc = parse_options_subcommand(argc, argv, kmem_options,
693 kmem_subcommands, kmem_usage, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +0800694
Li Zefan90b86a92009-12-10 15:21:57 +0800695 if (!argc)
Li Zefanba77c9e2009-11-20 15:53:25 +0800696 usage_with_options(kmem_usage, kmem_options);
697
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900698 if (!strncmp(argv[0], "rec", 3)) {
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900699 symbol__init(NULL);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900700 return __cmd_record(argc, argv);
701 }
702
Jiri Olsa28939e12015-04-06 14:36:08 +0900703 file.path = input_name;
704
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900705 session = perf_session__new(&file, false, &perf_kmem);
706 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900707 return -1;
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900708
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900709 symbol__init(&session->header.env);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200710
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900711 if (!strcmp(argv[0], "stat")) {
Namhyung Kim77cfe382015-03-23 15:30:40 +0900712 setlocale(LC_ALL, "");
713
Don Zickus4b627952014-04-07 14:55:23 -0400714 if (cpu__setup_cpunode_map())
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900715 goto out_delete;
Li Zefanba77c9e2009-11-20 15:53:25 +0800716
Li Zefan90b86a92009-12-10 15:21:57 +0800717 if (list_empty(&caller_sort))
718 setup_sorting(&caller_sort, default_sort_order);
719 if (list_empty(&alloc_sort))
720 setup_sorting(&alloc_sort, default_sort_order);
Li Zefan7d0d3942009-11-24 13:26:31 +0800721
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900722 ret = __cmd_kmem(session);
Pekka Enbergb00eca82010-01-19 19:26:11 +0200723 } else
724 usage_with_options(kmem_usage, kmem_options);
Li Zefan90b86a92009-12-10 15:21:57 +0800725
Namhyung Kim2b2b2c62014-08-12 15:40:38 +0900726out_delete:
727 perf_session__delete(session);
728
729 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +0800730}
731