5 #include "util/cache.h"
6 #include "util/symbol.h"
7 #include "util/thread.h"
8 #include "util/header.h"
10 #include "util/parse-options.h"
11 #include "util/trace-event.h"
13 #include "util/debug.h"
14 #include "util/session.h"
16 #include <sys/types.h>
17 #include <sys/prctl.h>
18 #include <semaphore.h>
23 #include <linux/list.h>
24 #include <linux/hash.h>
26 /* based on kernel/lockdep.c */
27 #define LOCKHASH_BITS 12
28 #define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
30 static struct list_head lockhash_table[LOCKHASH_SIZE];
32 #define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
33 #define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
35 #define LOCK_STATE_UNLOCKED 0 /* initial state */
36 #define LOCK_STATE_LOCKED 1
39 struct list_head hash_entry;
40 struct rb_node rb; /* used for sorting */
42 /* FIXME: raw_field_value() returns unsigned long long,
43 * so address of lockdep_map should be dealed as 64bit.
44 * Is there more better solution? */
45 void *addr; /* address of lockdep_map, used as ID */
46 char *name; /* for strcpy(), we cannot use const */
51 u64 prev_event_time; /* timestamp of previous event */
53 unsigned int nr_acquired;
54 unsigned int nr_acquire;
55 unsigned int nr_contended;
56 unsigned int nr_release;
58 /* these times are in nano sec. */
64 /* build simple key function one is bigger than two */
65 #define SINGLE_KEY(member) \
66 static int lock_stat_key_ ## member(struct lock_stat *one, \
67 struct lock_stat *two) \
69 return one->member > two->member; \
72 SINGLE_KEY(nr_acquired)
73 SINGLE_KEY(nr_contended)
74 SINGLE_KEY(wait_time_total)
75 SINGLE_KEY(wait_time_min)
76 SINGLE_KEY(wait_time_max)
80 * name: the value for specify by user
81 * this should be simpler than raw name of member
82 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
85 int (*key)(struct lock_stat*, struct lock_stat*);
88 static const char *sort_key = "acquired";
89 static int (*compare)(struct lock_stat *, struct lock_stat *);
91 #define DEF_KEY_LOCK(name, fn_suffix) \
92 { #name, lock_stat_key_ ## fn_suffix }
93 struct lock_key keys[] = {
94 DEF_KEY_LOCK(acquired, nr_acquired),
95 DEF_KEY_LOCK(contended, nr_contended),
96 DEF_KEY_LOCK(wait_total, wait_time_total),
97 DEF_KEY_LOCK(wait_min, wait_time_min),
98 DEF_KEY_LOCK(wait_max, wait_time_max),
100 /* extra comparisons much complicated should be here */
105 static void select_key(void)
109 for (i = 0; keys[i].name; i++) {
110 if (!strcmp(keys[i].name, sort_key)) {
111 compare = keys[i].key;
116 die("Unknown compare key:%s\n", sort_key);
119 static struct rb_root result; /* place to store sorted data */
121 static void insert_to_result(struct lock_stat *st,
122 int (*bigger)(struct lock_stat *,
125 struct rb_node **rb = &result.rb_node;
126 struct rb_node *parent = NULL;
130 p = container_of(*rb, struct lock_stat, rb);
134 rb = &(*rb)->rb_left;
136 rb = &(*rb)->rb_right;
139 rb_link_node(&st->rb, parent, rb);
140 rb_insert_color(&st->rb, &result);
143 /* returns left most element of result, and erase it */
144 static struct lock_stat *pop_from_result(void)
146 struct rb_node *node = result.rb_node;
151 while (node->rb_left)
152 node = node->rb_left;
154 rb_erase(node, &result);
155 return container_of(node, struct lock_stat, rb);
158 static struct lock_stat *lock_stat_findnew(void *addr, const char *name,
159 const char *file, unsigned int line)
161 struct list_head *entry = lockhashentry(addr);
162 struct lock_stat *ret, *new;
164 list_for_each_entry(ret, entry, hash_entry) {
165 if (ret->addr == addr)
169 new = zalloc(sizeof(struct lock_stat));
174 new->name = zalloc(sizeof(char) * strlen(name) + 1);
177 strcpy(new->name, name);
178 new->file = zalloc(sizeof(char) * strlen(file) + 1);
181 strcpy(new->file, file);
184 /* LOCK_STATE_UNLOCKED == 0 isn't guaranteed forever */
185 new->state = LOCK_STATE_UNLOCKED;
186 new->wait_time_min = ULLONG_MAX;
188 list_add(&new->hash_entry, entry);
192 die("memory allocation failed\n");
195 static char const *input_name = "perf.data";
197 static int profile_cpu = -1;
199 struct raw_event_sample {
204 struct trace_acquire_event {
211 struct trace_acquired_event {
218 struct trace_contended_event {
225 struct trace_release_event {
232 struct trace_lock_handler {
233 void (*acquire_event)(struct trace_acquire_event *,
237 struct thread *thread);
239 void (*acquired_event)(struct trace_acquired_event *,
243 struct thread *thread);
245 void (*contended_event)(struct trace_contended_event *,
249 struct thread *thread);
251 void (*release_event)(struct trace_release_event *,
255 struct thread *thread);
258 static void prof_lock_acquire_event(struct trace_acquire_event *acquire_event,
259 struct event *__event __used,
262 struct thread *thread __used)
264 struct lock_stat *st;
266 st = lock_stat_findnew(acquire_event->addr, acquire_event->name,
267 acquire_event->file, acquire_event->line);
270 case LOCK_STATE_UNLOCKED:
272 case LOCK_STATE_LOCKED:
279 st->prev_event_time = timestamp;
282 static void prof_lock_acquired_event(struct trace_acquired_event *acquired_event,
283 struct event *__event __used,
286 struct thread *thread __used)
288 struct lock_stat *st;
290 st = lock_stat_findnew(acquired_event->addr, acquired_event->name,
291 acquired_event->file, acquired_event->line);
294 case LOCK_STATE_UNLOCKED:
295 st->state = LOCK_STATE_LOCKED;
298 case LOCK_STATE_LOCKED:
305 st->prev_event_time = timestamp;
308 static void prof_lock_contended_event(struct trace_contended_event *contended_event,
309 struct event *__event __used,
312 struct thread *thread __used)
314 struct lock_stat *st;
316 st = lock_stat_findnew(contended_event->addr, contended_event->name,
317 contended_event->file, contended_event->line);
320 case LOCK_STATE_UNLOCKED:
322 case LOCK_STATE_LOCKED:
330 st->prev_event_time = timestamp;
333 static void prof_lock_release_event(struct trace_release_event *release_event,
334 struct event *__event __used,
337 struct thread *thread __used)
339 struct lock_stat *st;
342 st = lock_stat_findnew(release_event->addr, release_event->name,
343 release_event->file, release_event->line);
346 case LOCK_STATE_UNLOCKED:
348 case LOCK_STATE_LOCKED:
349 st->state = LOCK_STATE_UNLOCKED;
350 hold_time = timestamp - st->prev_event_time;
352 if (timestamp < st->prev_event_time) {
353 /* terribly, this can happen... */
357 if (st->wait_time_min > hold_time)
358 st->wait_time_min = hold_time;
359 if (st->wait_time_max < hold_time)
360 st->wait_time_max = hold_time;
361 st->wait_time_total += hold_time;
371 st->prev_event_time = timestamp;
374 /* lock oriented handlers */
375 /* TODO: handlers for CPU oriented, thread oriented */
376 static struct trace_lock_handler prof_lock_ops = {
377 .acquire_event = prof_lock_acquire_event,
378 .acquired_event = prof_lock_acquired_event,
379 .contended_event = prof_lock_contended_event,
380 .release_event = prof_lock_release_event,
383 static struct trace_lock_handler *trace_handler;
386 process_lock_acquire_event(void *data,
387 struct event *event __used,
389 u64 timestamp __used,
390 struct thread *thread __used)
392 struct trace_acquire_event acquire_event;
393 u64 tmp; /* this is required for casting... */
395 tmp = raw_field_value(event, "lockdep_addr", data);
396 memcpy(&acquire_event.addr, &tmp, sizeof(void *));
397 acquire_event.name = (char *)raw_field_ptr(event, "name", data);
398 acquire_event.file = (char *)raw_field_ptr(event, "file", data);
400 (unsigned int)raw_field_value(event, "line", data);
402 if (trace_handler->acquire_event) {
403 trace_handler->acquire_event(&acquire_event,
404 event, cpu, timestamp, thread);
409 process_lock_acquired_event(void *data,
410 struct event *event __used,
412 u64 timestamp __used,
413 struct thread *thread __used)
415 struct trace_acquired_event acquired_event;
416 u64 tmp; /* this is required for casting... */
418 tmp = raw_field_value(event, "lockdep_addr", data);
419 memcpy(&acquired_event.addr, &tmp, sizeof(void *));
420 acquired_event.name = (char *)raw_field_ptr(event, "name", data);
421 acquired_event.file = (char *)raw_field_ptr(event, "file", data);
422 acquired_event.line =
423 (unsigned int)raw_field_value(event, "line", data);
425 if (trace_handler->acquire_event) {
426 trace_handler->acquired_event(&acquired_event,
427 event, cpu, timestamp, thread);
432 process_lock_contended_event(void *data,
433 struct event *event __used,
435 u64 timestamp __used,
436 struct thread *thread __used)
438 struct trace_contended_event contended_event;
439 u64 tmp; /* this is required for casting... */
441 tmp = raw_field_value(event, "lockdep_addr", data);
442 memcpy(&contended_event.addr, &tmp, sizeof(void *));
443 contended_event.name = (char *)raw_field_ptr(event, "name", data);
444 contended_event.file = (char *)raw_field_ptr(event, "file", data);
445 contended_event.line =
446 (unsigned int)raw_field_value(event, "line", data);
448 if (trace_handler->acquire_event) {
449 trace_handler->contended_event(&contended_event,
450 event, cpu, timestamp, thread);
455 process_lock_release_event(void *data,
456 struct event *event __used,
458 u64 timestamp __used,
459 struct thread *thread __used)
461 struct trace_release_event release_event;
462 u64 tmp; /* this is required for casting... */
464 tmp = raw_field_value(event, "lockdep_addr", data);
465 memcpy(&release_event.addr, &tmp, sizeof(void *));
466 release_event.name = (char *)raw_field_ptr(event, "name", data);
467 release_event.file = (char *)raw_field_ptr(event, "file", data);
469 (unsigned int)raw_field_value(event, "line", data);
471 if (trace_handler->acquire_event) {
472 trace_handler->release_event(&release_event,
473 event, cpu, timestamp, thread);
478 process_raw_event(void *data, int cpu,
479 u64 timestamp, struct thread *thread)
484 type = trace_parse_common_type(data);
485 event = trace_find_event(type);
487 if (!strcmp(event->name, "lock_acquire"))
488 process_lock_acquire_event(data, event, cpu, timestamp, thread);
489 if (!strcmp(event->name, "lock_acquired"))
490 process_lock_acquired_event(data, event, cpu, timestamp, thread);
491 if (!strcmp(event->name, "lock_contended"))
492 process_lock_contended_event(data, event, cpu, timestamp, thread);
493 if (!strcmp(event->name, "lock_release"))
494 process_lock_release_event(data, event, cpu, timestamp, thread);
497 static int process_sample_event(event_t *event, struct perf_session *session)
499 struct thread *thread;
500 struct sample_data data;
502 bzero(&data, sizeof(struct sample_data));
503 event__parse_sample(event, session->sample_type, &data);
504 thread = perf_session__findnew(session, data.pid);
507 * FIXME: this causes warn on 32bit environment
508 * because of (void *)data.ip (type of data.ip is u64)
510 /* dump_printf("(IP, %d): %d/%d: %p period: %llu\n", */
511 /* event->header.misc, */
512 /* data.pid, data.tid, (void *)data.ip, data.period); */
514 if (thread == NULL) {
515 pr_debug("problem processing %d event, skipping it.\n",
520 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
522 if (profile_cpu != -1 && profile_cpu != (int) data.cpu)
525 process_raw_event(data.raw_data, data.cpu, data.time, thread);
530 /* TODO: various way to print, coloring, nano or milli sec */
531 static void print_result(void)
533 struct lock_stat *st;
536 printf("%18s ", "ID");
537 printf("%20s ", "Name");
538 printf("%10s ", "acquired");
539 printf("%10s ", "contended");
541 printf("%15s ", "total wait (ns)");
542 printf("%15s ", "max wait (ns)");
543 printf("%15s ", "min wait (ns)");
547 while ((st = pop_from_result())) {
550 printf("%p ", st->addr);
552 if (strlen(st->name) < 16) {
553 /* output raw name */
554 printf("%20s ", st->name);
556 strncpy(cut_name, st->name, 16);
561 /* cut off name for saving output style */
562 printf("%20s ", cut_name);
565 printf("%10u ", st->nr_acquired);
566 printf("%10u ", st->nr_contended);
568 printf("%15llu ", st->wait_time_total);
569 printf("%15llu ", st->wait_time_max);
570 printf("%15llu ", st->wait_time_min == ULLONG_MAX ?
571 0 : st->wait_time_min);
576 static void dump_map(void)
579 struct lock_stat *st;
581 for (i = 0; i < LOCKHASH_SIZE; i++) {
582 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
583 printf("%p: %s (src: %s, line: %u)\n",
584 st->addr, st->name, st->file, st->line);
589 static struct perf_event_ops eops = {
590 .sample = process_sample_event,
591 .comm = event__process_comm,
594 static struct perf_session *session;
596 static int read_events(void)
598 session = perf_session__new(input_name, O_RDONLY, 0);
600 die("Initializing perf session failed\n");
602 return perf_session__process_events(session, &eops);
605 static void sort_result(void)
608 struct lock_stat *st;
610 for (i = 0; i < LOCKHASH_SIZE; i++) {
611 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
612 insert_to_result(st, compare);
617 static void __cmd_prof(void)
626 static const char * const prof_usage[] = {
627 "perf sched prof [<options>]",
631 static const struct option prof_options[] = {
632 OPT_STRING('k', "key", &sort_key, "acquired",
638 static const char * const lock_usage[] = {
639 "perf lock [<options>] {record|trace|prof}",
643 static const struct option lock_options[] = {
644 OPT_STRING('i', "input", &input_name, "file",
646 OPT_BOOLEAN('v', "verbose", &verbose,
647 "be more verbose (show symbol address, etc)"),
648 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
649 "dump raw trace in ASCII"),
653 static const char *record_args[] = {
661 "-e", "lock:lock_acquire:r",
662 "-e", "lock:lock_acquired:r",
663 "-e", "lock:lock_contended:r",
664 "-e", "lock:lock_release:r",
667 static int __cmd_record(int argc, const char **argv)
669 unsigned int rec_argc, i, j;
670 const char **rec_argv;
672 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
673 rec_argv = calloc(rec_argc + 1, sizeof(char *));
675 for (i = 0; i < ARRAY_SIZE(record_args); i++)
676 rec_argv[i] = strdup(record_args[i]);
678 for (j = 1; j < (unsigned int)argc; j++, i++)
679 rec_argv[i] = argv[j];
681 BUG_ON(i != rec_argc);
683 return cmd_record(i, rec_argv, NULL);
686 int cmd_lock(int argc, const char **argv, const char *prefix __used)
691 for (i = 0; i < LOCKHASH_SIZE; i++)
692 INIT_LIST_HEAD(lockhash_table + i);
694 argc = parse_options(argc, argv, lock_options, lock_usage,
695 PARSE_OPT_STOP_AT_NON_OPTION);
697 usage_with_options(lock_usage, lock_options);
699 if (!strncmp(argv[0], "rec", 3)) {
700 return __cmd_record(argc, argv);
701 } else if (!strncmp(argv[0], "prof", 4)) {
702 trace_handler = &prof_lock_ops;
704 argc = parse_options(argc, argv,
705 prof_options, prof_usage, 0);
707 usage_with_options(prof_usage, prof_options);
710 } else if (!strcmp(argv[0], "trace")) {
711 /* Aliased to 'perf trace' */
712 return cmd_trace(argc, argv, prefix);
713 } else if (!strcmp(argv[0], "map")) {
714 /* recycling prof_lock_ops */
715 trace_handler = &prof_lock_ops;
720 usage_with_options(lock_usage, lock_options);