blob: f84d7a3db6815e59a14663458b3919b6e5953434 [file] [log] [blame]
Li Zefanba77c9e2009-11-20 15:53:25 +08001#include "builtin.h"
2#include "perf.h"
3
4#include "util/util.h"
5#include "util/cache.h"
6#include "util/symbol.h"
7#include "util/thread.h"
8#include "util/header.h"
9
10#include "util/parse-options.h"
11#include "util/trace-event.h"
12
13#include "util/debug.h"
14#include "util/data_map.h"
15
16#include <linux/rbtree.h>
17
18struct alloc_stat;
19typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
20
21static char const *input_name = "perf.data";
22
23static struct perf_header *header;
24static u64 sample_type;
25
26static 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
60struct raw_event_sample {
61 u32 size;
62 char data[0];
63};
64
Li Zefan7d0d3942009-11-24 13:26:31 +080065#define PATH_SYS_NODE "/sys/devices/system/node"
66
67static void init_cpunode_map(void)
68{
69 FILE *fp;
70 int i;
71
72 fp = fopen("/sys/devices/system/cpu/kernel_max", "r");
73 if (!fp) {
74 max_cpu_num = 4096;
75 return;
76 }
77
78 if (fscanf(fp, "%d", &max_cpu_num) < 1)
79 die("Failed to read 'kernel_max' from sysfs");
80 max_cpu_num++;
81
82 cpunode_map = calloc(max_cpu_num, sizeof(int));
83 if (!cpunode_map)
84 die("calloc");
85 for (i = 0; i < max_cpu_num; i++)
86 cpunode_map[i] = -1;
87 fclose(fp);
88}
89
90static void setup_cpunode_map(void)
91{
92 struct dirent *dent1, *dent2;
93 DIR *dir1, *dir2;
94 unsigned int cpu, mem;
95 char buf[PATH_MAX];
96
97 init_cpunode_map();
98
99 dir1 = opendir(PATH_SYS_NODE);
100 if (!dir1)
101 return;
102
103 while (true) {
104 dent1 = readdir(dir1);
105 if (!dent1)
106 break;
107
108 if (sscanf(dent1->d_name, "node%u", &mem) < 1)
109 continue;
110
111 snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name);
112 dir2 = opendir(buf);
113 if (!dir2)
114 continue;
115 while (true) {
116 dent2 = readdir(dir2);
117 if (!dent2)
118 break;
119 if (sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
120 continue;
121 cpunode_map[cpu] = mem;
122 }
123 }
124}
125
Li Zefan079d3f62009-11-24 13:26:55 +0800126static void insert_alloc_stat(unsigned long call_site, unsigned long ptr,
127 int bytes_req, int bytes_alloc, int cpu)
Li Zefanba77c9e2009-11-20 15:53:25 +0800128{
129 struct rb_node **node = &root_alloc_stat.rb_node;
130 struct rb_node *parent = NULL;
131 struct alloc_stat *data = NULL;
132
Li Zefanba77c9e2009-11-20 15:53:25 +0800133 while (*node) {
134 parent = *node;
135 data = rb_entry(*node, struct alloc_stat, node);
136
137 if (ptr > data->ptr)
138 node = &(*node)->rb_right;
139 else if (ptr < data->ptr)
140 node = &(*node)->rb_left;
141 else
142 break;
143 }
144
145 if (data && data->ptr == ptr) {
146 data->hit++;
147 data->bytes_req += bytes_req;
148 data->bytes_alloc += bytes_req;
149 } else {
150 data = malloc(sizeof(*data));
Li Zefan079d3f62009-11-24 13:26:55 +0800151 if (!data)
152 die("malloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800153 data->ptr = ptr;
Li Zefan079d3f62009-11-24 13:26:55 +0800154 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800155 data->hit = 1;
156 data->bytes_req = bytes_req;
157 data->bytes_alloc = bytes_alloc;
158
159 rb_link_node(&data->node, parent, node);
160 rb_insert_color(&data->node, &root_alloc_stat);
161 }
Li Zefan079d3f62009-11-24 13:26:55 +0800162 data->call_site = call_site;
163 data->alloc_cpu = cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +0800164}
165
166static void insert_caller_stat(unsigned long call_site,
167 int bytes_req, int bytes_alloc)
168{
169 struct rb_node **node = &root_caller_stat.rb_node;
170 struct rb_node *parent = NULL;
171 struct alloc_stat *data = NULL;
172
Li Zefanba77c9e2009-11-20 15:53:25 +0800173 while (*node) {
174 parent = *node;
175 data = rb_entry(*node, struct alloc_stat, node);
176
177 if (call_site > data->call_site)
178 node = &(*node)->rb_right;
179 else if (call_site < data->call_site)
180 node = &(*node)->rb_left;
181 else
182 break;
183 }
184
185 if (data && data->call_site == call_site) {
186 data->hit++;
187 data->bytes_req += bytes_req;
188 data->bytes_alloc += bytes_req;
189 } else {
190 data = malloc(sizeof(*data));
Li Zefan079d3f62009-11-24 13:26:55 +0800191 if (!data)
192 die("malloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800193 data->call_site = call_site;
Li Zefan079d3f62009-11-24 13:26:55 +0800194 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800195 data->hit = 1;
196 data->bytes_req = bytes_req;
197 data->bytes_alloc = bytes_alloc;
198
199 rb_link_node(&data->node, parent, node);
200 rb_insert_color(&data->node, &root_caller_stat);
201 }
202}
203
204static void process_alloc_event(struct raw_event_sample *raw,
205 struct event *event,
Li Zefan7d0d3942009-11-24 13:26:31 +0800206 int cpu,
Li Zefanba77c9e2009-11-20 15:53:25 +0800207 u64 timestamp __used,
208 struct thread *thread __used,
Li Zefan7d0d3942009-11-24 13:26:31 +0800209 int node)
Li Zefanba77c9e2009-11-20 15:53:25 +0800210{
211 unsigned long call_site;
212 unsigned long ptr;
213 int bytes_req;
214 int bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800215 int node1, node2;
Li Zefanba77c9e2009-11-20 15:53:25 +0800216
217 ptr = raw_field_value(event, "ptr", raw->data);
218 call_site = raw_field_value(event, "call_site", raw->data);
219 bytes_req = raw_field_value(event, "bytes_req", raw->data);
220 bytes_alloc = raw_field_value(event, "bytes_alloc", raw->data);
221
Li Zefan079d3f62009-11-24 13:26:55 +0800222 insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, cpu);
Li Zefanba77c9e2009-11-20 15:53:25 +0800223 insert_caller_stat(call_site, bytes_req, bytes_alloc);
224
225 total_requested += bytes_req;
226 total_allocated += bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800227
228 if (node) {
229 node1 = cpunode_map[cpu];
230 node2 = raw_field_value(event, "node", raw->data);
231 if (node1 != node2)
232 nr_cross_allocs++;
233 }
234 nr_allocs++;
Li Zefanba77c9e2009-11-20 15:53:25 +0800235}
236
Li Zefan079d3f62009-11-24 13:26:55 +0800237static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
238static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
239
240static struct alloc_stat *search_alloc_stat(unsigned long ptr,
241 unsigned long call_site,
242 struct rb_root *root,
243 sort_fn_t sort_fn)
244{
245 struct rb_node *node = root->rb_node;
246 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
247
248 while (node) {
249 struct alloc_stat *data;
250 int cmp;
251
252 data = rb_entry(node, struct alloc_stat, node);
253
254 cmp = sort_fn(&key, data);
255 if (cmp < 0)
256 node = node->rb_left;
257 else if (cmp > 0)
258 node = node->rb_right;
259 else
260 return data;
261 }
262 return NULL;
263}
264
265static void process_free_event(struct raw_event_sample *raw,
266 struct event *event,
267 int cpu,
Li Zefanba77c9e2009-11-20 15:53:25 +0800268 u64 timestamp __used,
269 struct thread *thread __used)
270{
Li Zefan079d3f62009-11-24 13:26:55 +0800271 unsigned long ptr;
272 struct alloc_stat *s_alloc, *s_caller;
273
274 ptr = raw_field_value(event, "ptr", raw->data);
275
276 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
277 if (!s_alloc)
278 return;
279
280 if (cpu != s_alloc->alloc_cpu) {
281 s_alloc->pingpong++;
282
283 s_caller = search_alloc_stat(0, s_alloc->call_site,
284 &root_caller_stat, callsite_cmp);
285 assert(s_caller);
286 s_caller->pingpong++;
287 }
288 s_alloc->alloc_cpu = -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800289}
290
291static void
Xiao Guangrongd8bd9e02009-12-07 12:06:29 +0800292process_raw_event(event_t *raw_event __used, u32 size, void *data,
Li Zefanba77c9e2009-11-20 15:53:25 +0800293 int cpu, u64 timestamp, struct thread *thread)
294{
Xiao Guangrongd8bd9e02009-12-07 12:06:29 +0800295 struct raw_event_sample *raw;
Li Zefanba77c9e2009-11-20 15:53:25 +0800296 struct event *event;
297 int type;
298
Xiao Guangrongd8bd9e02009-12-07 12:06:29 +0800299 raw = malloc_or_die(sizeof(*raw)+size);
300 raw->size = size;
301 memcpy(raw->data, data, size);
302
Li Zefanba77c9e2009-11-20 15:53:25 +0800303 type = trace_parse_common_type(raw->data);
304 event = trace_find_event(type);
305
306 if (!strcmp(event->name, "kmalloc") ||
307 !strcmp(event->name, "kmem_cache_alloc")) {
308 process_alloc_event(raw, event, cpu, timestamp, thread, 0);
309 return;
310 }
311
312 if (!strcmp(event->name, "kmalloc_node") ||
313 !strcmp(event->name, "kmem_cache_alloc_node")) {
314 process_alloc_event(raw, event, cpu, timestamp, thread, 1);
315 return;
316 }
317
318 if (!strcmp(event->name, "kfree") ||
319 !strcmp(event->name, "kmem_cache_free")) {
320 process_free_event(raw, event, cpu, timestamp, thread);
321 return;
322 }
323}
324
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200325static int process_sample_event(event_t *event)
Li Zefanba77c9e2009-11-20 15:53:25 +0800326{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900327 struct sample_data data;
328 struct thread *thread;
Li Zefanba77c9e2009-11-20 15:53:25 +0800329
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900330 memset(&data, 0, sizeof(data));
331 data.time = -1;
332 data.cpu = -1;
333 data.period = 1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800334
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900335 event__parse_sample(event, sample_type, &data);
Li Zefanba77c9e2009-11-20 15:53:25 +0800336
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200337 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Li Zefanba77c9e2009-11-20 15:53:25 +0800338 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900339 data.pid, data.tid,
340 (void *)(long)data.ip,
341 (long long)data.period);
Li Zefanba77c9e2009-11-20 15:53:25 +0800342
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900343 thread = threads__findnew(event->ip.pid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800344 if (thread == NULL) {
345 pr_debug("problem processing %d event, skipping it.\n",
346 event->header.type);
347 return -1;
348 }
349
350 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
351
Xiao Guangrongd8bd9e02009-12-07 12:06:29 +0800352 process_raw_event(event, data.raw_size, data.raw_data, data.cpu,
353 data.time, thread);
Li Zefanba77c9e2009-11-20 15:53:25 +0800354
355 return 0;
356}
357
358static int sample_type_check(u64 type)
359{
360 sample_type = type;
361
362 if (!(sample_type & PERF_SAMPLE_RAW)) {
363 fprintf(stderr,
364 "No trace sample to read. Did you call perf record "
365 "without -R?");
366 return -1;
367 }
368
369 return 0;
370}
371
372static struct perf_file_handler file_handler = {
373 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200374 .process_comm_event = event__process_comm,
Li Zefanba77c9e2009-11-20 15:53:25 +0800375 .sample_type_check = sample_type_check,
376};
377
378static int read_events(void)
379{
380 register_idle_thread();
381 register_perf_file_handler(&file_handler);
382
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200383 return mmap_dispatch_perf_file(&header, input_name, 0, 0,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200384 &event__cwdlen, &event__cwd);
Li Zefanba77c9e2009-11-20 15:53:25 +0800385}
386
387static double fragmentation(unsigned long n_req, unsigned long n_alloc)
388{
389 if (n_alloc == 0)
390 return 0.0;
391 else
392 return 100.0 - (100.0 * n_req / n_alloc);
393}
394
395static void __print_result(struct rb_root *root, int n_lines, int is_caller)
396{
397 struct rb_node *next;
398
Li Zefan079d3f62009-11-24 13:26:55 +0800399 printf("%.102s\n", graph_dotted_line);
400 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
401 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
402 printf("%.102s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800403
404 next = rb_first(root);
405
406 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200407 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
408 node);
409 struct symbol *sym = NULL;
Li Zefan079d3f62009-11-24 13:26:55 +0800410 char buf[BUFSIZ];
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200411 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800412
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200413 if (is_caller) {
414 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +0800415 if (!raw_ip)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200416 sym = thread__find_function(kthread, addr, NULL);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200417 } else
418 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800419
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200420 if (sym != NULL)
Li Zefan079d3f62009-11-24 13:26:55 +0800421 snprintf(buf, sizeof(buf), "%s+%Lx", sym->name,
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200422 addr - sym->start);
423 else
Li Zefan079d3f62009-11-24 13:26:55 +0800424 snprintf(buf, sizeof(buf), "%#Lx", addr);
425 printf(" %-34s |", buf);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200426
Li Zefan079d3f62009-11-24 13:26:55 +0800427 printf(" %9llu/%-5lu | %9llu/%-5lu | %6lu | %8lu | %6.3f%%\n",
428 (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +0800429 (unsigned long)data->bytes_alloc / data->hit,
430 (unsigned long long)data->bytes_req,
431 (unsigned long)data->bytes_req / data->hit,
432 (unsigned long)data->hit,
Li Zefan079d3f62009-11-24 13:26:55 +0800433 (unsigned long)data->pingpong,
Li Zefanba77c9e2009-11-20 15:53:25 +0800434 fragmentation(data->bytes_req, data->bytes_alloc));
435
436 next = rb_next(next);
437 }
438
439 if (n_lines == -1)
Li Zefan079d3f62009-11-24 13:26:55 +0800440 printf(" ... | ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +0800441
Li Zefan079d3f62009-11-24 13:26:55 +0800442 printf("%.102s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800443}
444
445static void print_summary(void)
446{
447 printf("\nSUMMARY\n=======\n");
448 printf("Total bytes requested: %lu\n", total_requested);
449 printf("Total bytes allocated: %lu\n", total_allocated);
450 printf("Total bytes wasted on internal fragmentation: %lu\n",
451 total_allocated - total_requested);
452 printf("Internal fragmentation: %f%%\n",
453 fragmentation(total_requested, total_allocated));
Li Zefan7d0d3942009-11-24 13:26:31 +0800454 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
Li Zefanba77c9e2009-11-20 15:53:25 +0800455}
456
457static void print_result(void)
458{
459 if (caller_flag)
460 __print_result(&root_caller_sorted, caller_lines, 1);
461 if (alloc_flag)
462 __print_result(&root_alloc_sorted, alloc_lines, 0);
463 print_summary();
464}
465
Li Zefan29b3e152009-11-24 13:26:10 +0800466struct sort_dimension {
467 const char name[20];
468 sort_fn_t cmp;
469 struct list_head list;
470};
471
472static LIST_HEAD(caller_sort);
473static LIST_HEAD(alloc_sort);
474
Li Zefanba77c9e2009-11-20 15:53:25 +0800475static void sort_insert(struct rb_root *root, struct alloc_stat *data,
Li Zefan29b3e152009-11-24 13:26:10 +0800476 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800477{
478 struct rb_node **new = &(root->rb_node);
479 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +0800480 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +0800481
482 while (*new) {
483 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +0800484 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800485
486 this = rb_entry(*new, struct alloc_stat, node);
487 parent = *new;
488
Li Zefan29b3e152009-11-24 13:26:10 +0800489 list_for_each_entry(sort, sort_list, list) {
490 cmp = sort->cmp(data, this);
491 if (cmp)
492 break;
493 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800494
495 if (cmp > 0)
496 new = &((*new)->rb_left);
497 else
498 new = &((*new)->rb_right);
499 }
500
501 rb_link_node(&data->node, parent, new);
502 rb_insert_color(&data->node, root);
503}
504
505static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
Li Zefan29b3e152009-11-24 13:26:10 +0800506 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800507{
508 struct rb_node *node;
509 struct alloc_stat *data;
510
511 for (;;) {
512 node = rb_first(root);
513 if (!node)
514 break;
515
516 rb_erase(node, root);
517 data = rb_entry(node, struct alloc_stat, node);
Li Zefan29b3e152009-11-24 13:26:10 +0800518 sort_insert(root_sorted, data, sort_list);
Li Zefanba77c9e2009-11-20 15:53:25 +0800519 }
520}
521
522static void sort_result(void)
523{
Li Zefan29b3e152009-11-24 13:26:10 +0800524 __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
525 __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
Li Zefanba77c9e2009-11-20 15:53:25 +0800526}
527
528static int __cmd_kmem(void)
529{
530 setup_pager();
531 read_events();
532 sort_result();
533 print_result();
534
535 return 0;
536}
537
538static const char * const kmem_usage[] = {
539 "perf kmem [<options>] {record}",
540 NULL
541};
542
Li Zefanba77c9e2009-11-20 15:53:25 +0800543static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
544{
545 if (l->ptr < r->ptr)
546 return -1;
547 else if (l->ptr > r->ptr)
548 return 1;
549 return 0;
550}
551
Li Zefan29b3e152009-11-24 13:26:10 +0800552static struct sort_dimension ptr_sort_dimension = {
553 .name = "ptr",
554 .cmp = ptr_cmp,
555};
556
Li Zefanba77c9e2009-11-20 15:53:25 +0800557static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
558{
559 if (l->call_site < r->call_site)
560 return -1;
561 else if (l->call_site > r->call_site)
562 return 1;
563 return 0;
564}
565
Li Zefan29b3e152009-11-24 13:26:10 +0800566static struct sort_dimension callsite_sort_dimension = {
567 .name = "callsite",
568 .cmp = callsite_cmp,
569};
570
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200571static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
572{
573 if (l->hit < r->hit)
574 return -1;
575 else if (l->hit > r->hit)
576 return 1;
577 return 0;
578}
579
Li Zefan29b3e152009-11-24 13:26:10 +0800580static struct sort_dimension hit_sort_dimension = {
581 .name = "hit",
582 .cmp = hit_cmp,
583};
584
Li Zefanba77c9e2009-11-20 15:53:25 +0800585static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
586{
587 if (l->bytes_alloc < r->bytes_alloc)
588 return -1;
589 else if (l->bytes_alloc > r->bytes_alloc)
590 return 1;
591 return 0;
592}
593
Li Zefan29b3e152009-11-24 13:26:10 +0800594static struct sort_dimension bytes_sort_dimension = {
595 .name = "bytes",
596 .cmp = bytes_cmp,
597};
598
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200599static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
600{
601 double x, y;
602
603 x = fragmentation(l->bytes_req, l->bytes_alloc);
604 y = fragmentation(r->bytes_req, r->bytes_alloc);
605
606 if (x < y)
607 return -1;
608 else if (x > y)
609 return 1;
610 return 0;
611}
612
Li Zefan29b3e152009-11-24 13:26:10 +0800613static struct sort_dimension frag_sort_dimension = {
614 .name = "frag",
615 .cmp = frag_cmp,
616};
617
Li Zefan079d3f62009-11-24 13:26:55 +0800618static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
619{
620 if (l->pingpong < r->pingpong)
621 return -1;
622 else if (l->pingpong > r->pingpong)
623 return 1;
624 return 0;
625}
626
627static struct sort_dimension pingpong_sort_dimension = {
628 .name = "pingpong",
629 .cmp = pingpong_cmp,
630};
631
Li Zefan29b3e152009-11-24 13:26:10 +0800632static struct sort_dimension *avail_sorts[] = {
633 &ptr_sort_dimension,
634 &callsite_sort_dimension,
635 &hit_sort_dimension,
636 &bytes_sort_dimension,
637 &frag_sort_dimension,
Li Zefan079d3f62009-11-24 13:26:55 +0800638 &pingpong_sort_dimension,
Li Zefan29b3e152009-11-24 13:26:10 +0800639};
640
641#define NUM_AVAIL_SORTS \
642 (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
643
644static int sort_dimension__add(const char *tok, struct list_head *list)
645{
646 struct sort_dimension *sort;
647 int i;
648
649 for (i = 0; i < NUM_AVAIL_SORTS; i++) {
650 if (!strcmp(avail_sorts[i]->name, tok)) {
651 sort = malloc(sizeof(*sort));
652 if (!sort)
653 die("malloc");
654 memcpy(sort, avail_sorts[i], sizeof(*sort));
655 list_add_tail(&sort->list, list);
656 return 0;
657 }
658 }
659
660 return -1;
661}
662
663static int setup_sorting(struct list_head *sort_list, const char *arg)
664{
665 char *tok;
666 char *str = strdup(arg);
667
668 if (!str)
669 die("strdup");
670
671 while (true) {
672 tok = strsep(&str, ",");
673 if (!tok)
674 break;
675 if (sort_dimension__add(tok, sort_list) < 0) {
676 error("Unknown --sort key: '%s'", tok);
677 return -1;
678 }
679 }
680
681 free(str);
682 return 0;
683}
684
Li Zefanba77c9e2009-11-20 15:53:25 +0800685static int parse_sort_opt(const struct option *opt __used,
686 const char *arg, int unset __used)
687{
Li Zefanba77c9e2009-11-20 15:53:25 +0800688 if (!arg)
689 return -1;
690
Li Zefanba77c9e2009-11-20 15:53:25 +0800691 if (caller_flag > alloc_flag)
Li Zefan29b3e152009-11-24 13:26:10 +0800692 return setup_sorting(&caller_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800693 else
Li Zefan29b3e152009-11-24 13:26:10 +0800694 return setup_sorting(&alloc_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800695
696 return 0;
697}
698
699static int parse_stat_opt(const struct option *opt __used,
700 const char *arg, int unset __used)
701{
702 if (!arg)
703 return -1;
704
705 if (strcmp(arg, "alloc") == 0)
706 alloc_flag = (caller_flag + 1);
707 else if (strcmp(arg, "caller") == 0)
708 caller_flag = (alloc_flag + 1);
709 else
710 return -1;
711 return 0;
712}
713
714static int parse_line_opt(const struct option *opt __used,
715 const char *arg, int unset __used)
716{
717 int lines;
718
719 if (!arg)
720 return -1;
721
722 lines = strtoul(arg, NULL, 10);
723
724 if (caller_flag > alloc_flag)
725 caller_lines = lines;
726 else
727 alloc_lines = lines;
728
729 return 0;
730}
731
732static const struct option kmem_options[] = {
733 OPT_STRING('i', "input", &input_name, "file",
734 "input file name"),
735 OPT_CALLBACK(0, "stat", NULL, "<alloc>|<caller>",
736 "stat selector, Pass 'alloc' or 'caller'.",
737 parse_stat_opt),
Li Zefan29b3e152009-11-24 13:26:10 +0800738 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
Li Zefan079d3f62009-11-24 13:26:55 +0800739 "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
Li Zefanba77c9e2009-11-20 15:53:25 +0800740 parse_sort_opt),
741 OPT_CALLBACK('l', "line", NULL, "num",
742 "show n lins",
743 parse_line_opt),
Li Zefan7707b6b2009-11-24 13:25:48 +0800744 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
Li Zefanba77c9e2009-11-20 15:53:25 +0800745 OPT_END()
746};
747
748static const char *record_args[] = {
749 "record",
750 "-a",
751 "-R",
752 "-M",
753 "-f",
754 "-c", "1",
755 "-e", "kmem:kmalloc",
756 "-e", "kmem:kmalloc_node",
757 "-e", "kmem:kfree",
758 "-e", "kmem:kmem_cache_alloc",
759 "-e", "kmem:kmem_cache_alloc_node",
760 "-e", "kmem:kmem_cache_free",
761};
762
763static int __cmd_record(int argc, const char **argv)
764{
765 unsigned int rec_argc, i, j;
766 const char **rec_argv;
767
768 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
769 rec_argv = calloc(rec_argc + 1, sizeof(char *));
770
771 for (i = 0; i < ARRAY_SIZE(record_args); i++)
772 rec_argv[i] = strdup(record_args[i]);
773
774 for (j = 1; j < (unsigned int)argc; j++, i++)
775 rec_argv[i] = argv[j];
776
777 return cmd_record(i, rec_argv, NULL);
778}
779
780int cmd_kmem(int argc, const char **argv, const char *prefix __used)
781{
782 symbol__init(0);
783
784 argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
785
786 if (argc && !strncmp(argv[0], "rec", 3))
787 return __cmd_record(argc, argv);
788 else if (argc)
789 usage_with_options(kmem_usage, kmem_options);
790
Li Zefan29b3e152009-11-24 13:26:10 +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 Zefanba77c9e2009-11-20 15:53:25 +0800795
Li Zefan7d0d3942009-11-24 13:26:31 +0800796 setup_cpunode_map();
797
Li Zefanba77c9e2009-11-20 15:53:25 +0800798 return __cmd_kmem();
799}
800