aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/builtin-kmem.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/builtin-kmem.c')
-rw-r--r--tools/perf/builtin-kmem.c64
1 files changed, 26 insertions, 38 deletions
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 2071d2485913..fc21ad79dd83 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -12,7 +12,6 @@
12#include "util/trace-event.h" 12#include "util/trace-event.h"
13 13
14#include "util/debug.h" 14#include "util/debug.h"
15#include "util/data_map.h"
16 15
17#include <linux/rbtree.h> 16#include <linux/rbtree.h>
18 17
@@ -21,8 +20,6 @@ typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
21 20
22static char const *input_name = "perf.data"; 21static char const *input_name = "perf.data";
23 22
24static u64 sample_type;
25
26static int alloc_flag; 23static int alloc_flag;
27static int caller_flag; 24static int caller_flag;
28 25
@@ -312,7 +309,7 @@ process_raw_event(event_t *raw_event __used, void *data,
312 } 309 }
313} 310}
314 311
315static int process_sample_event(event_t *event) 312static int process_sample_event(event_t *event, struct perf_session *session)
316{ 313{
317 struct sample_data data; 314 struct sample_data data;
318 struct thread *thread; 315 struct thread *thread;
@@ -322,7 +319,7 @@ static int process_sample_event(event_t *event)
322 data.cpu = -1; 319 data.cpu = -1;
323 data.period = 1; 320 data.period = 1;
324 321
325 event__parse_sample(event, sample_type, &data); 322 event__parse_sample(event, session->sample_type, &data);
326 323
327 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", 324 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
328 event->header.misc, 325 event->header.misc,
@@ -330,7 +327,7 @@ static int process_sample_event(event_t *event)
330 (void *)(long)data.ip, 327 (void *)(long)data.ip,
331 (long long)data.period); 328 (long long)data.period);
332 329
333 thread = threads__findnew(event->ip.pid); 330 thread = perf_session__findnew(session, event->ip.pid);
334 if (thread == NULL) { 331 if (thread == NULL) {
335 pr_debug("problem processing %d event, skipping it.\n", 332 pr_debug("problem processing %d event, skipping it.\n",
336 event->header.type); 333 event->header.type);
@@ -345,11 +342,9 @@ static int process_sample_event(event_t *event)
345 return 0; 342 return 0;
346} 343}
347 344
348static int sample_type_check(u64 type) 345static int sample_type_check(struct perf_session *session)
349{ 346{
350 sample_type = type; 347 if (!(session->sample_type & PERF_SAMPLE_RAW)) {
351
352 if (!(sample_type & PERF_SAMPLE_RAW)) {
353 fprintf(stderr, 348 fprintf(stderr,
354 "No trace sample to read. Did you call perf record " 349 "No trace sample to read. Did you call perf record "
355 "without -R?"); 350 "without -R?");
@@ -359,28 +354,12 @@ static int sample_type_check(u64 type)
359 return 0; 354 return 0;
360} 355}
361 356
362static struct perf_file_handler file_handler = { 357static struct perf_event_ops event_ops = {
363 .process_sample_event = process_sample_event, 358 .process_sample_event = process_sample_event,
364 .process_comm_event = event__process_comm, 359 .process_comm_event = event__process_comm,
365 .sample_type_check = sample_type_check, 360 .sample_type_check = sample_type_check,
366}; 361};
367 362
368static int read_events(void)
369{
370 int err;
371 struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0);
372
373 if (session == NULL)
374 return -ENOMEM;
375
376 register_idle_thread();
377 register_perf_file_handler(&file_handler);
378
379 err = perf_session__process_events(session, 0, &event__cwdlen, &event__cwd);
380 perf_session__delete(session);
381 return err;
382}
383
384static double fragmentation(unsigned long n_req, unsigned long n_alloc) 363static double fragmentation(unsigned long n_req, unsigned long n_alloc)
385{ 364{
386 if (n_alloc == 0) 365 if (n_alloc == 0)
@@ -389,7 +368,8 @@ static double fragmentation(unsigned long n_req, unsigned long n_alloc)
389 return 100.0 - (100.0 * n_req / n_alloc); 368 return 100.0 - (100.0 * n_req / n_alloc);
390} 369}
391 370
392static void __print_result(struct rb_root *root, int n_lines, int is_caller) 371static void __print_result(struct rb_root *root, struct perf_session *session,
372 int n_lines, int is_caller)
393{ 373{
394 struct rb_node *next; 374 struct rb_node *next;
395 375
@@ -410,7 +390,7 @@ static void __print_result(struct rb_root *root, int n_lines, int is_caller)
410 if (is_caller) { 390 if (is_caller) {
411 addr = data->call_site; 391 addr = data->call_site;
412 if (!raw_ip) 392 if (!raw_ip)
413 sym = map_groups__find_function(kmaps, addr, NULL); 393 sym = map_groups__find_function(&session->kmaps, session, addr, NULL);
414 } else 394 } else
415 addr = data->ptr; 395 addr = data->ptr;
416 396
@@ -451,12 +431,12 @@ static void print_summary(void)
451 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs); 431 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
452} 432}
453 433
454static void print_result(void) 434static void print_result(struct perf_session *session)
455{ 435{
456 if (caller_flag) 436 if (caller_flag)
457 __print_result(&root_caller_sorted, caller_lines, 1); 437 __print_result(&root_caller_sorted, session, caller_lines, 1);
458 if (alloc_flag) 438 if (alloc_flag)
459 __print_result(&root_alloc_sorted, alloc_lines, 0); 439 __print_result(&root_alloc_sorted, session, alloc_lines, 0);
460 print_summary(); 440 print_summary();
461} 441}
462 442
@@ -524,12 +504,20 @@ static void sort_result(void)
524 504
525static int __cmd_kmem(void) 505static int __cmd_kmem(void)
526{ 506{
507 int err;
508 struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0);
509 if (session == NULL)
510 return -ENOMEM;
511
527 setup_pager(); 512 setup_pager();
528 read_events(); 513 err = perf_session__process_events(session, &event_ops);
514 if (err != 0)
515 goto out_delete;
529 sort_result(); 516 sort_result();
530 print_result(); 517 print_result(session);
531 518out_delete:
532 return 0; 519 perf_session__delete(session);
520 return err;
533} 521}
534 522
535static const char * const kmem_usage[] = { 523static const char * const kmem_usage[] = {
@@ -778,13 +766,13 @@ static int __cmd_record(int argc, const char **argv)
778 766
779int cmd_kmem(int argc, const char **argv, const char *prefix __used) 767int cmd_kmem(int argc, const char **argv, const char *prefix __used)
780{ 768{
781 symbol__init(0);
782
783 argc = parse_options(argc, argv, kmem_options, kmem_usage, 0); 769 argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
784 770
785 if (!argc) 771 if (!argc)
786 usage_with_options(kmem_usage, kmem_options); 772 usage_with_options(kmem_usage, kmem_options);
787 773
774 symbol__init();
775
788 if (!strncmp(argv[0], "rec", 3)) { 776 if (!strncmp(argv[0], "rec", 3)) {
789 return __cmd_record(argc, argv); 777 return __cmd_record(argc, argv);
790 } else if (!strcmp(argv[0], "stat")) { 778 } else if (!strcmp(argv[0], "stat")) {