Commit | Line | Data |
---|---|---|
ba77c9e1 LZ |
1 | #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" | |
94c744b6 | 9 | #include "util/session.h" |
ba77c9e1 LZ |
10 | |
11 | #include "util/parse-options.h" | |
12 | #include "util/trace-event.h" | |
13 | ||
14 | #include "util/debug.h" | |
ba77c9e1 LZ |
15 | |
16 | #include <linux/rbtree.h> | |
17 | ||
18 | struct alloc_stat; | |
19 | typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *); | |
20 | ||
21 | static char const *input_name = "perf.data"; | |
22 | ||
ba77c9e1 LZ |
23 | static u64 sample_type; |
24 | ||
25 | static int alloc_flag; | |
26 | static int caller_flag; | |
27 | ||
ba77c9e1 LZ |
28 | static int alloc_lines = -1; |
29 | static int caller_lines = -1; | |
30 | ||
7707b6b6 LZ |
31 | static bool raw_ip; |
32 | ||
29b3e152 LZ |
33 | static char default_sort_order[] = "frag,hit,bytes"; |
34 | ||
7d0d3945 LZ |
35 | static int *cpunode_map; |
36 | static int max_cpu_num; | |
37 | ||
ba77c9e1 | 38 | struct alloc_stat { |
079d3f65 LZ |
39 | u64 call_site; |
40 | u64 ptr; | |
ba77c9e1 LZ |
41 | u64 bytes_req; |
42 | u64 bytes_alloc; | |
43 | u32 hit; | |
079d3f65 LZ |
44 | u32 pingpong; |
45 | ||
46 | short alloc_cpu; | |
ba77c9e1 LZ |
47 | |
48 | struct rb_node node; | |
49 | }; | |
50 | ||
51 | static struct rb_root root_alloc_stat; | |
52 | static struct rb_root root_alloc_sorted; | |
53 | static struct rb_root root_caller_stat; | |
54 | static struct rb_root root_caller_sorted; | |
55 | ||
56 | static unsigned long total_requested, total_allocated; | |
7d0d3945 | 57 | static unsigned long nr_allocs, nr_cross_allocs; |
ba77c9e1 | 58 | |
7d0d3945 LZ |
59 | #define PATH_SYS_NODE "/sys/devices/system/node" |
60 | ||
61 | static 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 | ||
84 | static 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 | ||
079d3f65 LZ |
120 | static void insert_alloc_stat(unsigned long call_site, unsigned long ptr, |
121 | int bytes_req, int bytes_alloc, int cpu) | |
ba77c9e1 LZ |
122 | { |
123 | struct rb_node **node = &root_alloc_stat.rb_node; | |
124 | struct rb_node *parent = NULL; | |
125 | struct alloc_stat *data = NULL; | |
126 | ||
ba77c9e1 LZ |
127 | 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)); | |
079d3f65 LZ |
145 | if (!data) |
146 | die("malloc"); | |
ba77c9e1 | 147 | data->ptr = ptr; |
079d3f65 | 148 | data->pingpong = 0; |
ba77c9e1 LZ |
149 | 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 | } | |
079d3f65 LZ |
156 | data->call_site = call_site; |
157 | data->alloc_cpu = cpu; | |
ba77c9e1 LZ |
158 | } |
159 | ||
160 | static 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 | ||
ba77c9e1 LZ |
167 | 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)); | |
079d3f65 LZ |
185 | if (!data) |
186 | die("malloc"); | |
ba77c9e1 | 187 | data->call_site = call_site; |
079d3f65 | 188 | data->pingpong = 0; |
ba77c9e1 LZ |
189 | 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 | ||
f48f669d | 198 | static void process_alloc_event(void *data, |
ba77c9e1 | 199 | struct event *event, |
7d0d3945 | 200 | int cpu, |
ba77c9e1 LZ |
201 | u64 timestamp __used, |
202 | struct thread *thread __used, | |
7d0d3945 | 203 | int node) |
ba77c9e1 LZ |
204 | { |
205 | unsigned long call_site; | |
206 | unsigned long ptr; | |
207 | int bytes_req; | |
208 | int bytes_alloc; | |
7d0d3945 | 209 | int node1, node2; |
ba77c9e1 | 210 | |
f48f669d XG |
211 | 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); | |
ba77c9e1 | 215 | |
079d3f65 | 216 | insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, cpu); |
ba77c9e1 LZ |
217 | insert_caller_stat(call_site, bytes_req, bytes_alloc); |
218 | ||
219 | total_requested += bytes_req; | |
220 | total_allocated += bytes_alloc; | |
7d0d3945 LZ |
221 | |
222 | if (node) { | |
223 | node1 = cpunode_map[cpu]; | |
f48f669d | 224 | node2 = raw_field_value(event, "node", data); |
7d0d3945 LZ |
225 | if (node1 != node2) |
226 | nr_cross_allocs++; | |
227 | } | |
228 | nr_allocs++; | |
ba77c9e1 LZ |
229 | } |
230 | ||
079d3f65 LZ |
231 | static int ptr_cmp(struct alloc_stat *, struct alloc_stat *); |
232 | static int callsite_cmp(struct alloc_stat *, struct alloc_stat *); | |
233 | ||
234 | static 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 | ||
f48f669d | 259 | static void process_free_event(void *data, |
079d3f65 LZ |
260 | struct event *event, |
261 | int cpu, | |
ba77c9e1 LZ |
262 | u64 timestamp __used, |
263 | struct thread *thread __used) | |
264 | { | |
079d3f65 LZ |
265 | unsigned long ptr; |
266 | struct alloc_stat *s_alloc, *s_caller; | |
267 | ||
f48f669d | 268 | ptr = raw_field_value(event, "ptr", data); |
079d3f65 LZ |
269 | |
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; | |
ba77c9e1 LZ |
283 | } |
284 | ||
285 | static void | |
f48f669d | 286 | process_raw_event(event_t *raw_event __used, void *data, |
ba77c9e1 LZ |
287 | int cpu, u64 timestamp, struct thread *thread) |
288 | { | |
ba77c9e1 LZ |
289 | struct event *event; |
290 | int type; | |
291 | ||
f48f669d | 292 | type = trace_parse_common_type(data); |
ba77c9e1 LZ |
293 | event = trace_find_event(type); |
294 | ||
295 | if (!strcmp(event->name, "kmalloc") || | |
296 | !strcmp(event->name, "kmem_cache_alloc")) { | |
f48f669d | 297 | process_alloc_event(data, event, cpu, timestamp, thread, 0); |
ba77c9e1 LZ |
298 | return; |
299 | } | |
300 | ||
301 | if (!strcmp(event->name, "kmalloc_node") || | |
302 | !strcmp(event->name, "kmem_cache_alloc_node")) { | |
f48f669d | 303 | process_alloc_event(data, event, cpu, timestamp, thread, 1); |
ba77c9e1 LZ |
304 | return; |
305 | } | |
306 | ||
307 | if (!strcmp(event->name, "kfree") || | |
308 | !strcmp(event->name, "kmem_cache_free")) { | |
f48f669d | 309 | process_free_event(data, event, cpu, timestamp, thread); |
ba77c9e1 LZ |
310 | return; |
311 | } | |
312 | } | |
313 | ||
d8f66248 | 314 | static int process_sample_event(event_t *event, struct perf_session *session __used) |
ba77c9e1 | 315 | { |
180f95e2 OH |
316 | struct sample_data data; |
317 | struct thread *thread; | |
ba77c9e1 | 318 | |
180f95e2 OH |
319 | memset(&data, 0, sizeof(data)); |
320 | data.time = -1; | |
321 | data.cpu = -1; | |
322 | data.period = 1; | |
ba77c9e1 | 323 | |
180f95e2 | 324 | event__parse_sample(event, sample_type, &data); |
ba77c9e1 | 325 | |
62daacb5 | 326 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", |
ba77c9e1 | 327 | event->header.misc, |
180f95e2 OH |
328 | data.pid, data.tid, |
329 | (void *)(long)data.ip, | |
330 | (long long)data.period); | |
ba77c9e1 | 331 | |
180f95e2 | 332 | thread = threads__findnew(event->ip.pid); |
ba77c9e1 LZ |
333 | 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 | ||
f48f669d | 341 | process_raw_event(event, data.raw_data, data.cpu, |
d8bd9e0a | 342 | data.time, thread); |
ba77c9e1 LZ |
343 | |
344 | return 0; | |
345 | } | |
346 | ||
347 | static 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 | ||
301a0b02 | 361 | static struct perf_event_ops event_ops = { |
ba77c9e1 | 362 | .process_sample_event = process_sample_event, |
62daacb5 | 363 | .process_comm_event = event__process_comm, |
ba77c9e1 LZ |
364 | .sample_type_check = sample_type_check, |
365 | }; | |
366 | ||
367 | static int read_events(void) | |
368 | { | |
94c744b6 ACM |
369 | int err; |
370 | struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0); | |
371 | ||
372 | if (session == NULL) | |
373 | return -ENOMEM; | |
374 | ||
ba77c9e1 | 375 | register_idle_thread(); |
301a0b02 ACM |
376 | err = perf_session__process_events(session, &event_ops, 0, |
377 | &event__cwdlen, &event__cwd); | |
94c744b6 ACM |
378 | perf_session__delete(session); |
379 | return err; | |
ba77c9e1 LZ |
380 | } |
381 | ||
382 | static double fragmentation(unsigned long n_req, unsigned long n_alloc) | |
383 | { | |
384 | if (n_alloc == 0) | |
385 | return 0.0; | |
386 | else | |
387 | return 100.0 - (100.0 * n_req / n_alloc); | |
388 | } | |
389 | ||
390 | static void __print_result(struct rb_root *root, int n_lines, int is_caller) | |
391 | { | |
392 | struct rb_node *next; | |
393 | ||
079d3f65 LZ |
394 | printf("%.102s\n", graph_dotted_line); |
395 | printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr"); | |
396 | printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n"); | |
397 | printf("%.102s\n", graph_dotted_line); | |
ba77c9e1 LZ |
398 | |
399 | next = rb_first(root); | |
400 | ||
401 | while (next && n_lines--) { | |
1b145ae5 ACM |
402 | struct alloc_stat *data = rb_entry(next, struct alloc_stat, |
403 | node); | |
404 | struct symbol *sym = NULL; | |
079d3f65 | 405 | char buf[BUFSIZ]; |
1b145ae5 ACM |
406 | u64 addr; |
407 | ||
408 | if (is_caller) { | |
409 | addr = data->call_site; | |
7707b6b6 | 410 | if (!raw_ip) |
9958e1f0 | 411 | sym = map_groups__find_function(kmaps, addr, NULL); |
1b145ae5 ACM |
412 | } else |
413 | addr = data->ptr; | |
414 | ||
415 | if (sym != NULL) | |
079d3f65 | 416 | snprintf(buf, sizeof(buf), "%s+%Lx", sym->name, |
1b145ae5 ACM |
417 | addr - sym->start); |
418 | else | |
079d3f65 LZ |
419 | snprintf(buf, sizeof(buf), "%#Lx", addr); |
420 | printf(" %-34s |", buf); | |
ba77c9e1 | 421 | |
079d3f65 LZ |
422 | printf(" %9llu/%-5lu | %9llu/%-5lu | %6lu | %8lu | %6.3f%%\n", |
423 | (unsigned long long)data->bytes_alloc, | |
ba77c9e1 LZ |
424 | (unsigned long)data->bytes_alloc / data->hit, |
425 | (unsigned long long)data->bytes_req, | |
426 | (unsigned long)data->bytes_req / data->hit, | |
427 | (unsigned long)data->hit, | |
079d3f65 | 428 | (unsigned long)data->pingpong, |
ba77c9e1 LZ |
429 | fragmentation(data->bytes_req, data->bytes_alloc)); |
430 | ||
431 | next = rb_next(next); | |
432 | } | |
433 | ||
434 | if (n_lines == -1) | |
079d3f65 | 435 | printf(" ... | ... | ... | ... | ... | ... \n"); |
ba77c9e1 | 436 | |
079d3f65 | 437 | printf("%.102s\n", graph_dotted_line); |
ba77c9e1 LZ |
438 | } |
439 | ||
440 | static void print_summary(void) | |
441 | { | |
442 | printf("\nSUMMARY\n=======\n"); | |
443 | printf("Total bytes requested: %lu\n", total_requested); | |
444 | printf("Total bytes allocated: %lu\n", total_allocated); | |
445 | printf("Total bytes wasted on internal fragmentation: %lu\n", | |
446 | total_allocated - total_requested); | |
447 | printf("Internal fragmentation: %f%%\n", | |
448 | fragmentation(total_requested, total_allocated)); | |
7d0d3945 | 449 | printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs); |
ba77c9e1 LZ |
450 | } |
451 | ||
452 | static void print_result(void) | |
453 | { | |
454 | if (caller_flag) | |
455 | __print_result(&root_caller_sorted, caller_lines, 1); | |
456 | if (alloc_flag) | |
457 | __print_result(&root_alloc_sorted, alloc_lines, 0); | |
458 | print_summary(); | |
459 | } | |
460 | ||
29b3e152 LZ |
461 | struct sort_dimension { |
462 | const char name[20]; | |
463 | sort_fn_t cmp; | |
464 | struct list_head list; | |
465 | }; | |
466 | ||
467 | static LIST_HEAD(caller_sort); | |
468 | static LIST_HEAD(alloc_sort); | |
469 | ||
ba77c9e1 | 470 | static void sort_insert(struct rb_root *root, struct alloc_stat *data, |
29b3e152 | 471 | struct list_head *sort_list) |
ba77c9e1 LZ |
472 | { |
473 | struct rb_node **new = &(root->rb_node); | |
474 | struct rb_node *parent = NULL; | |
29b3e152 | 475 | struct sort_dimension *sort; |
ba77c9e1 LZ |
476 | |
477 | while (*new) { | |
478 | struct alloc_stat *this; | |
29b3e152 | 479 | int cmp = 0; |
ba77c9e1 LZ |
480 | |
481 | this = rb_entry(*new, struct alloc_stat, node); | |
482 | parent = *new; | |
483 | ||
29b3e152 LZ |
484 | list_for_each_entry(sort, sort_list, list) { |
485 | cmp = sort->cmp(data, this); | |
486 | if (cmp) | |
487 | break; | |
488 | } | |
ba77c9e1 LZ |
489 | |
490 | if (cmp > 0) | |
491 | new = &((*new)->rb_left); | |
492 | else | |
493 | new = &((*new)->rb_right); | |
494 | } | |
495 | ||
496 | rb_link_node(&data->node, parent, new); | |
497 | rb_insert_color(&data->node, root); | |
498 | } | |
499 | ||
500 | static void __sort_result(struct rb_root *root, struct rb_root *root_sorted, | |
29b3e152 | 501 | struct list_head *sort_list) |
ba77c9e1 LZ |
502 | { |
503 | struct rb_node *node; | |
504 | struct alloc_stat *data; | |
505 | ||
506 | for (;;) { | |
507 | node = rb_first(root); | |
508 | if (!node) | |
509 | break; | |
510 | ||
511 | rb_erase(node, root); | |
512 | data = rb_entry(node, struct alloc_stat, node); | |
29b3e152 | 513 | sort_insert(root_sorted, data, sort_list); |
ba77c9e1 LZ |
514 | } |
515 | } | |
516 | ||
517 | static void sort_result(void) | |
518 | { | |
29b3e152 LZ |
519 | __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort); |
520 | __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort); | |
ba77c9e1 LZ |
521 | } |
522 | ||
523 | static int __cmd_kmem(void) | |
524 | { | |
525 | setup_pager(); | |
526 | read_events(); | |
527 | sort_result(); | |
528 | print_result(); | |
529 | ||
530 | return 0; | |
531 | } | |
532 | ||
533 | static const char * const kmem_usage[] = { | |
90b86a9f | 534 | "perf kmem [<options>] {record|stat}", |
ba77c9e1 LZ |
535 | NULL |
536 | }; | |
537 | ||
ba77c9e1 LZ |
538 | static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r) |
539 | { | |
540 | if (l->ptr < r->ptr) | |
541 | return -1; | |
542 | else if (l->ptr > r->ptr) | |
543 | return 1; | |
544 | return 0; | |
545 | } | |
546 | ||
29b3e152 LZ |
547 | static struct sort_dimension ptr_sort_dimension = { |
548 | .name = "ptr", | |
549 | .cmp = ptr_cmp, | |
550 | }; | |
551 | ||
ba77c9e1 LZ |
552 | static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r) |
553 | { | |
554 | if (l->call_site < r->call_site) | |
555 | return -1; | |
556 | else if (l->call_site > r->call_site) | |
557 | return 1; | |
558 | return 0; | |
559 | } | |
560 | ||
29b3e152 LZ |
561 | static struct sort_dimension callsite_sort_dimension = { |
562 | .name = "callsite", | |
563 | .cmp = callsite_cmp, | |
564 | }; | |
565 | ||
f3ced7cd PE |
566 | static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r) |
567 | { | |
568 | if (l->hit < r->hit) | |
569 | return -1; | |
570 | else if (l->hit > r->hit) | |
571 | return 1; | |
572 | return 0; | |
573 | } | |
574 | ||
29b3e152 LZ |
575 | static struct sort_dimension hit_sort_dimension = { |
576 | .name = "hit", | |
577 | .cmp = hit_cmp, | |
578 | }; | |
579 | ||
ba77c9e1 LZ |
580 | static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r) |
581 | { | |
582 | if (l->bytes_alloc < r->bytes_alloc) | |
583 | return -1; | |
584 | else if (l->bytes_alloc > r->bytes_alloc) | |
585 | return 1; | |
586 | return 0; | |
587 | } | |
588 | ||
29b3e152 LZ |
589 | static struct sort_dimension bytes_sort_dimension = { |
590 | .name = "bytes", | |
591 | .cmp = bytes_cmp, | |
592 | }; | |
593 | ||
f3ced7cd PE |
594 | static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r) |
595 | { | |
596 | double x, y; | |
597 | ||
598 | x = fragmentation(l->bytes_req, l->bytes_alloc); | |
599 | y = fragmentation(r->bytes_req, r->bytes_alloc); | |
600 | ||
601 | if (x < y) | |
602 | return -1; | |
603 | else if (x > y) | |
604 | return 1; | |
605 | return 0; | |
606 | } | |
607 | ||
29b3e152 LZ |
608 | static struct sort_dimension frag_sort_dimension = { |
609 | .name = "frag", | |
610 | .cmp = frag_cmp, | |
611 | }; | |
612 | ||
079d3f65 LZ |
613 | static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r) |
614 | { | |
615 | if (l->pingpong < r->pingpong) | |
616 | return -1; | |
617 | else if (l->pingpong > r->pingpong) | |
618 | return 1; | |
619 | return 0; | |
620 | } | |
621 | ||
622 | static struct sort_dimension pingpong_sort_dimension = { | |
623 | .name = "pingpong", | |
624 | .cmp = pingpong_cmp, | |
625 | }; | |
626 | ||
29b3e152 LZ |
627 | static struct sort_dimension *avail_sorts[] = { |
628 | &ptr_sort_dimension, | |
629 | &callsite_sort_dimension, | |
630 | &hit_sort_dimension, | |
631 | &bytes_sort_dimension, | |
632 | &frag_sort_dimension, | |
079d3f65 | 633 | &pingpong_sort_dimension, |
29b3e152 LZ |
634 | }; |
635 | ||
636 | #define NUM_AVAIL_SORTS \ | |
637 | (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *)) | |
638 | ||
639 | static int sort_dimension__add(const char *tok, struct list_head *list) | |
640 | { | |
641 | struct sort_dimension *sort; | |
642 | int i; | |
643 | ||
644 | for (i = 0; i < NUM_AVAIL_SORTS; i++) { | |
645 | if (!strcmp(avail_sorts[i]->name, tok)) { | |
646 | sort = malloc(sizeof(*sort)); | |
647 | if (!sort) | |
648 | die("malloc"); | |
649 | memcpy(sort, avail_sorts[i], sizeof(*sort)); | |
650 | list_add_tail(&sort->list, list); | |
651 | return 0; | |
652 | } | |
653 | } | |
654 | ||
655 | return -1; | |
656 | } | |
657 | ||
658 | static int setup_sorting(struct list_head *sort_list, const char *arg) | |
659 | { | |
660 | char *tok; | |
661 | char *str = strdup(arg); | |
662 | ||
663 | if (!str) | |
664 | die("strdup"); | |
665 | ||
666 | while (true) { | |
667 | tok = strsep(&str, ","); | |
668 | if (!tok) | |
669 | break; | |
670 | if (sort_dimension__add(tok, sort_list) < 0) { | |
671 | error("Unknown --sort key: '%s'", tok); | |
672 | return -1; | |
673 | } | |
674 | } | |
675 | ||
676 | free(str); | |
677 | return 0; | |
678 | } | |
679 | ||
ba77c9e1 LZ |
680 | static int parse_sort_opt(const struct option *opt __used, |
681 | const char *arg, int unset __used) | |
682 | { | |
ba77c9e1 LZ |
683 | if (!arg) |
684 | return -1; | |
685 | ||
ba77c9e1 | 686 | if (caller_flag > alloc_flag) |
29b3e152 | 687 | return setup_sorting(&caller_sort, arg); |
ba77c9e1 | 688 | else |
29b3e152 | 689 | return setup_sorting(&alloc_sort, arg); |
ba77c9e1 LZ |
690 | |
691 | return 0; | |
692 | } | |
693 | ||
90b86a9f | 694 | static int parse_caller_opt(const struct option *opt __used, |
79312416 | 695 | const char *arg __used, int unset __used) |
ba77c9e1 | 696 | { |
90b86a9f LZ |
697 | caller_flag = (alloc_flag + 1); |
698 | return 0; | |
699 | } | |
ba77c9e1 | 700 | |
90b86a9f | 701 | static int parse_alloc_opt(const struct option *opt __used, |
79312416 | 702 | const char *arg __used, int unset __used) |
90b86a9f LZ |
703 | { |
704 | alloc_flag = (caller_flag + 1); | |
ba77c9e1 LZ |
705 | return 0; |
706 | } | |
707 | ||
708 | static int parse_line_opt(const struct option *opt __used, | |
709 | const char *arg, int unset __used) | |
710 | { | |
711 | int lines; | |
712 | ||
713 | if (!arg) | |
714 | return -1; | |
715 | ||
716 | lines = strtoul(arg, NULL, 10); | |
717 | ||
718 | if (caller_flag > alloc_flag) | |
719 | caller_lines = lines; | |
720 | else | |
721 | alloc_lines = lines; | |
722 | ||
723 | return 0; | |
724 | } | |
725 | ||
726 | static const struct option kmem_options[] = { | |
727 | OPT_STRING('i', "input", &input_name, "file", | |
728 | "input file name"), | |
90b86a9f LZ |
729 | OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL, |
730 | "show per-callsite statistics", | |
731 | parse_caller_opt), | |
732 | OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL, | |
733 | "show per-allocation statistics", | |
734 | parse_alloc_opt), | |
29b3e152 | 735 | OPT_CALLBACK('s', "sort", NULL, "key[,key2...]", |
079d3f65 | 736 | "sort by keys: ptr, call_site, bytes, hit, pingpong, frag", |
ba77c9e1 LZ |
737 | parse_sort_opt), |
738 | OPT_CALLBACK('l', "line", NULL, "num", | |
90b86a9f | 739 | "show n lines", |
ba77c9e1 | 740 | parse_line_opt), |
7707b6b6 | 741 | OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"), |
ba77c9e1 LZ |
742 | OPT_END() |
743 | }; | |
744 | ||
745 | static const char *record_args[] = { | |
746 | "record", | |
747 | "-a", | |
748 | "-R", | |
749 | "-M", | |
750 | "-f", | |
751 | "-c", "1", | |
752 | "-e", "kmem:kmalloc", | |
753 | "-e", "kmem:kmalloc_node", | |
754 | "-e", "kmem:kfree", | |
755 | "-e", "kmem:kmem_cache_alloc", | |
756 | "-e", "kmem:kmem_cache_alloc_node", | |
757 | "-e", "kmem:kmem_cache_free", | |
758 | }; | |
759 | ||
760 | static int __cmd_record(int argc, const char **argv) | |
761 | { | |
762 | unsigned int rec_argc, i, j; | |
763 | const char **rec_argv; | |
764 | ||
765 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | |
766 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); | |
767 | ||
768 | for (i = 0; i < ARRAY_SIZE(record_args); i++) | |
769 | rec_argv[i] = strdup(record_args[i]); | |
770 | ||
771 | for (j = 1; j < (unsigned int)argc; j++, i++) | |
772 | rec_argv[i] = argv[j]; | |
773 | ||
774 | return cmd_record(i, rec_argv, NULL); | |
775 | } | |
776 | ||
777 | int cmd_kmem(int argc, const char **argv, const char *prefix __used) | |
778 | { | |
779 | symbol__init(0); | |
780 | ||
781 | argc = parse_options(argc, argv, kmem_options, kmem_usage, 0); | |
782 | ||
90b86a9f | 783 | if (!argc) |
ba77c9e1 LZ |
784 | usage_with_options(kmem_usage, kmem_options); |
785 | ||
90b86a9f LZ |
786 | if (!strncmp(argv[0], "rec", 3)) { |
787 | return __cmd_record(argc, argv); | |
788 | } else if (!strcmp(argv[0], "stat")) { | |
789 | setup_cpunode_map(); | |
790 | ||
791 | 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); | |
ba77c9e1 | 795 | |
90b86a9f LZ |
796 | return __cmd_kmem(); |
797 | } | |
7d0d3945 | 798 | |
90b86a9f | 799 | return 0; |
ba77c9e1 LZ |
800 | } |
801 |