blob: de194958fe6e17f8b087a8719153ccfc0dca2630 [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"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02009#include "util/session.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080010
11#include "util/parse-options.h"
12#include "util/trace-event.h"
13
14#include "util/debug.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080015
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
Li Zefanba77c9e2009-11-20 15:53:25 +080023static u64 sample_type;
24
25static int alloc_flag;
26static int caller_flag;
27
Li Zefanba77c9e2009-11-20 15:53:25 +080028static int alloc_lines = -1;
29static int caller_lines = -1;
30
Li Zefan7707b6b2009-11-24 13:25:48 +080031static bool raw_ip;
32
Li Zefan29b3e152009-11-24 13:26:10 +080033static char default_sort_order[] = "frag,hit,bytes";
34
Li Zefan7d0d3942009-11-24 13:26:31 +080035static int *cpunode_map;
36static int max_cpu_num;
37
Li Zefanba77c9e2009-11-20 15:53:25 +080038struct alloc_stat {
Li Zefan079d3f62009-11-24 13:26:55 +080039 u64 call_site;
40 u64 ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +080041 u64 bytes_req;
42 u64 bytes_alloc;
43 u32 hit;
Li Zefan079d3f62009-11-24 13:26:55 +080044 u32 pingpong;
45
46 short alloc_cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +080047
48 struct rb_node node;
49};
50
51static struct rb_root root_alloc_stat;
52static struct rb_root root_alloc_sorted;
53static struct rb_root root_caller_stat;
54static struct rb_root root_caller_sorted;
55
56static unsigned long total_requested, total_allocated;
Li Zefan7d0d3942009-11-24 13:26:31 +080057static unsigned long nr_allocs, nr_cross_allocs;
Li Zefanba77c9e2009-11-20 15:53:25 +080058
Li Zefan7d0d3942009-11-24 13:26:31 +080059#define PATH_SYS_NODE "/sys/devices/system/node"
60
61static void init_cpunode_map(void)
62{
63 FILE *fp;
64 int i;
65
66 fp = fopen("/sys/devices/system/cpu/kernel_max", "r");
67 if (!fp) {
68 max_cpu_num = 4096;
69 return;
70 }
71
72 if (fscanf(fp, "%d", &max_cpu_num) < 1)
73 die("Failed to read 'kernel_max' from sysfs");
74 max_cpu_num++;
75
76 cpunode_map = calloc(max_cpu_num, sizeof(int));
77 if (!cpunode_map)
78 die("calloc");
79 for (i = 0; i < max_cpu_num; i++)
80 cpunode_map[i] = -1;
81 fclose(fp);
82}
83
84static void setup_cpunode_map(void)
85{
86 struct dirent *dent1, *dent2;
87 DIR *dir1, *dir2;
88 unsigned int cpu, mem;
89 char buf[PATH_MAX];
90
91 init_cpunode_map();
92
93 dir1 = opendir(PATH_SYS_NODE);
94 if (!dir1)
95 return;
96
97 while (true) {
98 dent1 = readdir(dir1);
99 if (!dent1)
100 break;
101
102 if (sscanf(dent1->d_name, "node%u", &mem) < 1)
103 continue;
104
105 snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name);
106 dir2 = opendir(buf);
107 if (!dir2)
108 continue;
109 while (true) {
110 dent2 = readdir(dir2);
111 if (!dent2)
112 break;
113 if (sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
114 continue;
115 cpunode_map[cpu] = mem;
116 }
117 }
118}
119
Li Zefan079d3f62009-11-24 13:26:55 +0800120static void insert_alloc_stat(unsigned long call_site, unsigned long ptr,
121 int bytes_req, int bytes_alloc, int cpu)
Li Zefanba77c9e2009-11-20 15:53:25 +0800122{
123 struct rb_node **node = &root_alloc_stat.rb_node;
124 struct rb_node *parent = NULL;
125 struct alloc_stat *data = NULL;
126
Li Zefanba77c9e2009-11-20 15:53:25 +0800127 while (*node) {
128 parent = *node;
129 data = rb_entry(*node, struct alloc_stat, node);
130
131 if (ptr > data->ptr)
132 node = &(*node)->rb_right;
133 else if (ptr < data->ptr)
134 node = &(*node)->rb_left;
135 else
136 break;
137 }
138
139 if (data && data->ptr == ptr) {
140 data->hit++;
141 data->bytes_req += bytes_req;
142 data->bytes_alloc += bytes_req;
143 } else {
144 data = malloc(sizeof(*data));
Li Zefan079d3f62009-11-24 13:26:55 +0800145 if (!data)
146 die("malloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800147 data->ptr = ptr;
Li Zefan079d3f62009-11-24 13:26:55 +0800148 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800149 data->hit = 1;
150 data->bytes_req = bytes_req;
151 data->bytes_alloc = bytes_alloc;
152
153 rb_link_node(&data->node, parent, node);
154 rb_insert_color(&data->node, &root_alloc_stat);
155 }
Li Zefan079d3f62009-11-24 13:26:55 +0800156 data->call_site = call_site;
157 data->alloc_cpu = cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +0800158}
159
160static void insert_caller_stat(unsigned long call_site,
161 int bytes_req, int bytes_alloc)
162{
163 struct rb_node **node = &root_caller_stat.rb_node;
164 struct rb_node *parent = NULL;
165 struct alloc_stat *data = NULL;
166
Li Zefanba77c9e2009-11-20 15:53:25 +0800167 while (*node) {
168 parent = *node;
169 data = rb_entry(*node, struct alloc_stat, node);
170
171 if (call_site > data->call_site)
172 node = &(*node)->rb_right;
173 else if (call_site < data->call_site)
174 node = &(*node)->rb_left;
175 else
176 break;
177 }
178
179 if (data && data->call_site == call_site) {
180 data->hit++;
181 data->bytes_req += bytes_req;
182 data->bytes_alloc += bytes_req;
183 } else {
184 data = malloc(sizeof(*data));
Li Zefan079d3f62009-11-24 13:26:55 +0800185 if (!data)
186 die("malloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800187 data->call_site = call_site;
Li Zefan079d3f62009-11-24 13:26:55 +0800188 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800189 data->hit = 1;
190 data->bytes_req = bytes_req;
191 data->bytes_alloc = bytes_alloc;
192
193 rb_link_node(&data->node, parent, node);
194 rb_insert_color(&data->node, &root_caller_stat);
195 }
196}
197
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800198static void process_alloc_event(void *data,
Li Zefanba77c9e2009-11-20 15:53:25 +0800199 struct event *event,
Li Zefan7d0d3942009-11-24 13:26:31 +0800200 int cpu,
Li Zefanba77c9e2009-11-20 15:53:25 +0800201 u64 timestamp __used,
202 struct thread *thread __used,
Li Zefan7d0d3942009-11-24 13:26:31 +0800203 int node)
Li Zefanba77c9e2009-11-20 15:53:25 +0800204{
205 unsigned long call_site;
206 unsigned long ptr;
207 int bytes_req;
208 int bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800209 int node1, node2;
Li Zefanba77c9e2009-11-20 15:53:25 +0800210
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800211 ptr = raw_field_value(event, "ptr", data);
212 call_site = raw_field_value(event, "call_site", data);
213 bytes_req = raw_field_value(event, "bytes_req", data);
214 bytes_alloc = raw_field_value(event, "bytes_alloc", data);
Li Zefanba77c9e2009-11-20 15:53:25 +0800215
Li Zefan079d3f62009-11-24 13:26:55 +0800216 insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, cpu);
Li Zefanba77c9e2009-11-20 15:53:25 +0800217 insert_caller_stat(call_site, bytes_req, bytes_alloc);
218
219 total_requested += bytes_req;
220 total_allocated += bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800221
222 if (node) {
223 node1 = cpunode_map[cpu];
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800224 node2 = raw_field_value(event, "node", data);
Li Zefan7d0d3942009-11-24 13:26:31 +0800225 if (node1 != node2)
226 nr_cross_allocs++;
227 }
228 nr_allocs++;
Li Zefanba77c9e2009-11-20 15:53:25 +0800229}
230
Li Zefan079d3f62009-11-24 13:26:55 +0800231static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
232static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
233
234static struct alloc_stat *search_alloc_stat(unsigned long ptr,
235 unsigned long call_site,
236 struct rb_root *root,
237 sort_fn_t sort_fn)
238{
239 struct rb_node *node = root->rb_node;
240 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
241
242 while (node) {
243 struct alloc_stat *data;
244 int cmp;
245
246 data = rb_entry(node, struct alloc_stat, node);
247
248 cmp = sort_fn(&key, data);
249 if (cmp < 0)
250 node = node->rb_left;
251 else if (cmp > 0)
252 node = node->rb_right;
253 else
254 return data;
255 }
256 return NULL;
257}
258
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800259static void process_free_event(void *data,
Li Zefan079d3f62009-11-24 13:26:55 +0800260 struct event *event,
261 int cpu,
Li Zefanba77c9e2009-11-20 15:53:25 +0800262 u64 timestamp __used,
263 struct thread *thread __used)
264{
Li Zefan079d3f62009-11-24 13:26:55 +0800265 unsigned long ptr;
266 struct alloc_stat *s_alloc, *s_caller;
267
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800268 ptr = raw_field_value(event, "ptr", data);
Li Zefan079d3f62009-11-24 13:26:55 +0800269
270 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
271 if (!s_alloc)
272 return;
273
274 if (cpu != s_alloc->alloc_cpu) {
275 s_alloc->pingpong++;
276
277 s_caller = search_alloc_stat(0, s_alloc->call_site,
278 &root_caller_stat, callsite_cmp);
279 assert(s_caller);
280 s_caller->pingpong++;
281 }
282 s_alloc->alloc_cpu = -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800283}
284
285static void
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800286process_raw_event(event_t *raw_event __used, void *data,
Li Zefanba77c9e2009-11-20 15:53:25 +0800287 int cpu, u64 timestamp, struct thread *thread)
288{
Li Zefanba77c9e2009-11-20 15:53:25 +0800289 struct event *event;
290 int type;
291
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800292 type = trace_parse_common_type(data);
Li Zefanba77c9e2009-11-20 15:53:25 +0800293 event = trace_find_event(type);
294
295 if (!strcmp(event->name, "kmalloc") ||
296 !strcmp(event->name, "kmem_cache_alloc")) {
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800297 process_alloc_event(data, event, cpu, timestamp, thread, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +0800298 return;
299 }
300
301 if (!strcmp(event->name, "kmalloc_node") ||
302 !strcmp(event->name, "kmem_cache_alloc_node")) {
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800303 process_alloc_event(data, event, cpu, timestamp, thread, 1);
Li Zefanba77c9e2009-11-20 15:53:25 +0800304 return;
305 }
306
307 if (!strcmp(event->name, "kfree") ||
308 !strcmp(event->name, "kmem_cache_free")) {
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800309 process_free_event(data, event, cpu, timestamp, thread);
Li Zefanba77c9e2009-11-20 15:53:25 +0800310 return;
311 }
312}
313
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200314static int process_sample_event(event_t *event, struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +0800315{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900316 struct sample_data data;
317 struct thread *thread;
Li Zefanba77c9e2009-11-20 15:53:25 +0800318
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900319 memset(&data, 0, sizeof(data));
320 data.time = -1;
321 data.cpu = -1;
322 data.period = 1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800323
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900324 event__parse_sample(event, sample_type, &data);
Li Zefanba77c9e2009-11-20 15:53:25 +0800325
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200326 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Li Zefanba77c9e2009-11-20 15:53:25 +0800327 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900328 data.pid, data.tid,
329 (void *)(long)data.ip,
330 (long long)data.period);
Li Zefanba77c9e2009-11-20 15:53:25 +0800331
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200332 thread = perf_session__findnew(session, event->ip.pid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800333 if (thread == NULL) {
334 pr_debug("problem processing %d event, skipping it.\n",
335 event->header.type);
336 return -1;
337 }
338
339 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
340
Xiao Guangrongf48f6692009-12-07 13:04:04 +0800341 process_raw_event(event, data.raw_data, data.cpu,
Xiao Guangrongd8bd9e02009-12-07 12:06:29 +0800342 data.time, thread);
Li Zefanba77c9e2009-11-20 15:53:25 +0800343
344 return 0;
345}
346
347static int sample_type_check(u64 type)
348{
349 sample_type = type;
350
351 if (!(sample_type & PERF_SAMPLE_RAW)) {
352 fprintf(stderr,
353 "No trace sample to read. Did you call perf record "
354 "without -R?");
355 return -1;
356 }
357
358 return 0;
359}
360
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200361static struct perf_event_ops event_ops = {
Li Zefanba77c9e2009-11-20 15:53:25 +0800362 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200363 .process_comm_event = event__process_comm,
Li Zefanba77c9e2009-11-20 15:53:25 +0800364 .sample_type_check = sample_type_check,
365};
366
367static int read_events(void)
368{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200369 int err;
370 struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0);
371
372 if (session == NULL)
373 return -ENOMEM;
374
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200375 err = perf_session__process_events(session, &event_ops);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200376 perf_session__delete(session);
377 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +0800378}
379
380static double fragmentation(unsigned long n_req, unsigned long n_alloc)
381{
382 if (n_alloc == 0)
383 return 0.0;
384 else
385 return 100.0 - (100.0 * n_req / n_alloc);
386}
387
388static void __print_result(struct rb_root *root, int n_lines, int is_caller)
389{
390 struct rb_node *next;
391
Li Zefan079d3f62009-11-24 13:26:55 +0800392 printf("%.102s\n", graph_dotted_line);
393 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
394 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
395 printf("%.102s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800396
397 next = rb_first(root);
398
399 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200400 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
401 node);
402 struct symbol *sym = NULL;
Li Zefan079d3f62009-11-24 13:26:55 +0800403 char buf[BUFSIZ];
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200404 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800405
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200406 if (is_caller) {
407 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +0800408 if (!raw_ip)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200409 sym = map_groups__find_function(kmaps, addr, NULL);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200410 } else
411 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800412
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200413 if (sym != NULL)
Li Zefan079d3f62009-11-24 13:26:55 +0800414 snprintf(buf, sizeof(buf), "%s+%Lx", sym->name,
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200415 addr - sym->start);
416 else
Li Zefan079d3f62009-11-24 13:26:55 +0800417 snprintf(buf, sizeof(buf), "%#Lx", addr);
418 printf(" %-34s |", buf);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200419
Li Zefan079d3f62009-11-24 13:26:55 +0800420 printf(" %9llu/%-5lu | %9llu/%-5lu | %6lu | %8lu | %6.3f%%\n",
421 (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +0800422 (unsigned long)data->bytes_alloc / data->hit,
423 (unsigned long long)data->bytes_req,
424 (unsigned long)data->bytes_req / data->hit,
425 (unsigned long)data->hit,
Li Zefan079d3f62009-11-24 13:26:55 +0800426 (unsigned long)data->pingpong,
Li Zefanba77c9e2009-11-20 15:53:25 +0800427 fragmentation(data->bytes_req, data->bytes_alloc));
428
429 next = rb_next(next);
430 }
431
432 if (n_lines == -1)
Li Zefan079d3f62009-11-24 13:26:55 +0800433 printf(" ... | ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +0800434
Li Zefan079d3f62009-11-24 13:26:55 +0800435 printf("%.102s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800436}
437
438static void print_summary(void)
439{
440 printf("\nSUMMARY\n=======\n");
441 printf("Total bytes requested: %lu\n", total_requested);
442 printf("Total bytes allocated: %lu\n", total_allocated);
443 printf("Total bytes wasted on internal fragmentation: %lu\n",
444 total_allocated - total_requested);
445 printf("Internal fragmentation: %f%%\n",
446 fragmentation(total_requested, total_allocated));
Li Zefan7d0d3942009-11-24 13:26:31 +0800447 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
Li Zefanba77c9e2009-11-20 15:53:25 +0800448}
449
450static void print_result(void)
451{
452 if (caller_flag)
453 __print_result(&root_caller_sorted, caller_lines, 1);
454 if (alloc_flag)
455 __print_result(&root_alloc_sorted, alloc_lines, 0);
456 print_summary();
457}
458
Li Zefan29b3e152009-11-24 13:26:10 +0800459struct sort_dimension {
460 const char name[20];
461 sort_fn_t cmp;
462 struct list_head list;
463};
464
465static LIST_HEAD(caller_sort);
466static LIST_HEAD(alloc_sort);
467
Li Zefanba77c9e2009-11-20 15:53:25 +0800468static void sort_insert(struct rb_root *root, struct alloc_stat *data,
Li Zefan29b3e152009-11-24 13:26:10 +0800469 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800470{
471 struct rb_node **new = &(root->rb_node);
472 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +0800473 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +0800474
475 while (*new) {
476 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +0800477 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800478
479 this = rb_entry(*new, struct alloc_stat, node);
480 parent = *new;
481
Li Zefan29b3e152009-11-24 13:26:10 +0800482 list_for_each_entry(sort, sort_list, list) {
483 cmp = sort->cmp(data, this);
484 if (cmp)
485 break;
486 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800487
488 if (cmp > 0)
489 new = &((*new)->rb_left);
490 else
491 new = &((*new)->rb_right);
492 }
493
494 rb_link_node(&data->node, parent, new);
495 rb_insert_color(&data->node, root);
496}
497
498static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
Li Zefan29b3e152009-11-24 13:26:10 +0800499 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800500{
501 struct rb_node *node;
502 struct alloc_stat *data;
503
504 for (;;) {
505 node = rb_first(root);
506 if (!node)
507 break;
508
509 rb_erase(node, root);
510 data = rb_entry(node, struct alloc_stat, node);
Li Zefan29b3e152009-11-24 13:26:10 +0800511 sort_insert(root_sorted, data, sort_list);
Li Zefanba77c9e2009-11-20 15:53:25 +0800512 }
513}
514
515static void sort_result(void)
516{
Li Zefan29b3e152009-11-24 13:26:10 +0800517 __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
518 __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
Li Zefanba77c9e2009-11-20 15:53:25 +0800519}
520
521static int __cmd_kmem(void)
522{
523 setup_pager();
524 read_events();
525 sort_result();
526 print_result();
527
528 return 0;
529}
530
531static const char * const kmem_usage[] = {
Li Zefan90b86a92009-12-10 15:21:57 +0800532 "perf kmem [<options>] {record|stat}",
Li Zefanba77c9e2009-11-20 15:53:25 +0800533 NULL
534};
535
Li Zefanba77c9e2009-11-20 15:53:25 +0800536static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
537{
538 if (l->ptr < r->ptr)
539 return -1;
540 else if (l->ptr > r->ptr)
541 return 1;
542 return 0;
543}
544
Li Zefan29b3e152009-11-24 13:26:10 +0800545static struct sort_dimension ptr_sort_dimension = {
546 .name = "ptr",
547 .cmp = ptr_cmp,
548};
549
Li Zefanba77c9e2009-11-20 15:53:25 +0800550static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
551{
552 if (l->call_site < r->call_site)
553 return -1;
554 else if (l->call_site > r->call_site)
555 return 1;
556 return 0;
557}
558
Li Zefan29b3e152009-11-24 13:26:10 +0800559static struct sort_dimension callsite_sort_dimension = {
560 .name = "callsite",
561 .cmp = callsite_cmp,
562};
563
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200564static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
565{
566 if (l->hit < r->hit)
567 return -1;
568 else if (l->hit > r->hit)
569 return 1;
570 return 0;
571}
572
Li Zefan29b3e152009-11-24 13:26:10 +0800573static struct sort_dimension hit_sort_dimension = {
574 .name = "hit",
575 .cmp = hit_cmp,
576};
577
Li Zefanba77c9e2009-11-20 15:53:25 +0800578static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
579{
580 if (l->bytes_alloc < r->bytes_alloc)
581 return -1;
582 else if (l->bytes_alloc > r->bytes_alloc)
583 return 1;
584 return 0;
585}
586
Li Zefan29b3e152009-11-24 13:26:10 +0800587static struct sort_dimension bytes_sort_dimension = {
588 .name = "bytes",
589 .cmp = bytes_cmp,
590};
591
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200592static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
593{
594 double x, y;
595
596 x = fragmentation(l->bytes_req, l->bytes_alloc);
597 y = fragmentation(r->bytes_req, r->bytes_alloc);
598
599 if (x < y)
600 return -1;
601 else if (x > y)
602 return 1;
603 return 0;
604}
605
Li Zefan29b3e152009-11-24 13:26:10 +0800606static struct sort_dimension frag_sort_dimension = {
607 .name = "frag",
608 .cmp = frag_cmp,
609};
610
Li Zefan079d3f62009-11-24 13:26:55 +0800611static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
612{
613 if (l->pingpong < r->pingpong)
614 return -1;
615 else if (l->pingpong > r->pingpong)
616 return 1;
617 return 0;
618}
619
620static struct sort_dimension pingpong_sort_dimension = {
621 .name = "pingpong",
622 .cmp = pingpong_cmp,
623};
624
Li Zefan29b3e152009-11-24 13:26:10 +0800625static struct sort_dimension *avail_sorts[] = {
626 &ptr_sort_dimension,
627 &callsite_sort_dimension,
628 &hit_sort_dimension,
629 &bytes_sort_dimension,
630 &frag_sort_dimension,
Li Zefan079d3f62009-11-24 13:26:55 +0800631 &pingpong_sort_dimension,
Li Zefan29b3e152009-11-24 13:26:10 +0800632};
633
634#define NUM_AVAIL_SORTS \
635 (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
636
637static int sort_dimension__add(const char *tok, struct list_head *list)
638{
639 struct sort_dimension *sort;
640 int i;
641
642 for (i = 0; i < NUM_AVAIL_SORTS; i++) {
643 if (!strcmp(avail_sorts[i]->name, tok)) {
644 sort = malloc(sizeof(*sort));
645 if (!sort)
646 die("malloc");
647 memcpy(sort, avail_sorts[i], sizeof(*sort));
648 list_add_tail(&sort->list, list);
649 return 0;
650 }
651 }
652
653 return -1;
654}
655
656static int setup_sorting(struct list_head *sort_list, const char *arg)
657{
658 char *tok;
659 char *str = strdup(arg);
660
661 if (!str)
662 die("strdup");
663
664 while (true) {
665 tok = strsep(&str, ",");
666 if (!tok)
667 break;
668 if (sort_dimension__add(tok, sort_list) < 0) {
669 error("Unknown --sort key: '%s'", tok);
670 return -1;
671 }
672 }
673
674 free(str);
675 return 0;
676}
677
Li Zefanba77c9e2009-11-20 15:53:25 +0800678static int parse_sort_opt(const struct option *opt __used,
679 const char *arg, int unset __used)
680{
Li Zefanba77c9e2009-11-20 15:53:25 +0800681 if (!arg)
682 return -1;
683
Li Zefanba77c9e2009-11-20 15:53:25 +0800684 if (caller_flag > alloc_flag)
Li Zefan29b3e152009-11-24 13:26:10 +0800685 return setup_sorting(&caller_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800686 else
Li Zefan29b3e152009-11-24 13:26:10 +0800687 return setup_sorting(&alloc_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800688
689 return 0;
690}
691
Li Zefan90b86a92009-12-10 15:21:57 +0800692static int parse_caller_opt(const struct option *opt __used,
Ingo Molnar79312412009-12-10 08:43:34 +0100693 const char *arg __used, int unset __used)
Li Zefanba77c9e2009-11-20 15:53:25 +0800694{
Li Zefan90b86a92009-12-10 15:21:57 +0800695 caller_flag = (alloc_flag + 1);
696 return 0;
697}
Li Zefanba77c9e2009-11-20 15:53:25 +0800698
Li Zefan90b86a92009-12-10 15:21:57 +0800699static int parse_alloc_opt(const struct option *opt __used,
Ingo Molnar79312412009-12-10 08:43:34 +0100700 const char *arg __used, int unset __used)
Li Zefan90b86a92009-12-10 15:21:57 +0800701{
702 alloc_flag = (caller_flag + 1);
Li Zefanba77c9e2009-11-20 15:53:25 +0800703 return 0;
704}
705
706static int parse_line_opt(const struct option *opt __used,
707 const char *arg, int unset __used)
708{
709 int lines;
710
711 if (!arg)
712 return -1;
713
714 lines = strtoul(arg, NULL, 10);
715
716 if (caller_flag > alloc_flag)
717 caller_lines = lines;
718 else
719 alloc_lines = lines;
720
721 return 0;
722}
723
724static const struct option kmem_options[] = {
725 OPT_STRING('i', "input", &input_name, "file",
726 "input file name"),
Li Zefan90b86a92009-12-10 15:21:57 +0800727 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
728 "show per-callsite statistics",
729 parse_caller_opt),
730 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
731 "show per-allocation statistics",
732 parse_alloc_opt),
Li Zefan29b3e152009-11-24 13:26:10 +0800733 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
Li Zefan079d3f62009-11-24 13:26:55 +0800734 "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
Li Zefanba77c9e2009-11-20 15:53:25 +0800735 parse_sort_opt),
736 OPT_CALLBACK('l', "line", NULL, "num",
Li Zefan90b86a92009-12-10 15:21:57 +0800737 "show n lines",
Li Zefanba77c9e2009-11-20 15:53:25 +0800738 parse_line_opt),
Li Zefan7707b6b2009-11-24 13:25:48 +0800739 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
Li Zefanba77c9e2009-11-20 15:53:25 +0800740 OPT_END()
741};
742
743static const char *record_args[] = {
744 "record",
745 "-a",
746 "-R",
747 "-M",
748 "-f",
749 "-c", "1",
750 "-e", "kmem:kmalloc",
751 "-e", "kmem:kmalloc_node",
752 "-e", "kmem:kfree",
753 "-e", "kmem:kmem_cache_alloc",
754 "-e", "kmem:kmem_cache_alloc_node",
755 "-e", "kmem:kmem_cache_free",
756};
757
758static int __cmd_record(int argc, const char **argv)
759{
760 unsigned int rec_argc, i, j;
761 const char **rec_argv;
762
763 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
764 rec_argv = calloc(rec_argc + 1, sizeof(char *));
765
766 for (i = 0; i < ARRAY_SIZE(record_args); i++)
767 rec_argv[i] = strdup(record_args[i]);
768
769 for (j = 1; j < (unsigned int)argc; j++, i++)
770 rec_argv[i] = argv[j];
771
772 return cmd_record(i, rec_argv, NULL);
773}
774
775int cmd_kmem(int argc, const char **argv, const char *prefix __used)
776{
777 symbol__init(0);
778
779 argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
780
Li Zefan90b86a92009-12-10 15:21:57 +0800781 if (!argc)
Li Zefanba77c9e2009-11-20 15:53:25 +0800782 usage_with_options(kmem_usage, kmem_options);
783
Li Zefan90b86a92009-12-10 15:21:57 +0800784 if (!strncmp(argv[0], "rec", 3)) {
785 return __cmd_record(argc, argv);
786 } else if (!strcmp(argv[0], "stat")) {
787 setup_cpunode_map();
Li Zefanba77c9e2009-11-20 15:53:25 +0800788
Li Zefan90b86a92009-12-10 15:21:57 +0800789 if (list_empty(&caller_sort))
790 setup_sorting(&caller_sort, default_sort_order);
791 if (list_empty(&alloc_sort))
792 setup_sorting(&alloc_sort, default_sort_order);
Li Zefan7d0d3942009-11-24 13:26:31 +0800793
Li Zefan90b86a92009-12-10 15:21:57 +0800794 return __cmd_kmem();
795 }
796
797 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800798}
799