builtin-lock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/util.h"
  4. #include "util/cache.h"
  5. #include "util/symbol.h"
  6. #include "util/thread.h"
  7. #include "util/header.h"
  8. #include "util/parse-options.h"
  9. #include "util/trace-event.h"
  10. #include "util/debug.h"
  11. #include "util/session.h"
  12. #include <sys/types.h>
  13. #include <sys/prctl.h>
  14. #include <semaphore.h>
  15. #include <pthread.h>
  16. #include <math.h>
  17. #include <limits.h>
  18. #include <linux/list.h>
  19. #include <linux/hash.h>
  20. /* based on kernel/lockdep.c */
  21. #define LOCKHASH_BITS 12
  22. #define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
  23. static struct list_head lockhash_table[LOCKHASH_SIZE];
  24. #define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
  25. #define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
  26. #define LOCK_STATE_UNLOCKED 0 /* initial state */
  27. #define LOCK_STATE_LOCKED 1
  28. struct lock_stat {
  29. struct list_head hash_entry;
  30. struct rb_node rb; /* used for sorting */
  31. /*
  32. * FIXME: raw_field_value() returns unsigned long long,
  33. * so address of lockdep_map should be dealed as 64bit.
  34. * Is there more better solution?
  35. */
  36. void *addr; /* address of lockdep_map, used as ID */
  37. char *name; /* for strcpy(), we cannot use const */
  38. int state;
  39. u64 prev_event_time; /* timestamp of previous event */
  40. unsigned int nr_acquired;
  41. unsigned int nr_acquire;
  42. unsigned int nr_contended;
  43. unsigned int nr_release;
  44. /* these times are in nano sec. */
  45. u64 wait_time_total;
  46. u64 wait_time_min;
  47. u64 wait_time_max;
  48. };
  49. /* build simple key function one is bigger than two */
  50. #define SINGLE_KEY(member) \
  51. static int lock_stat_key_ ## member(struct lock_stat *one, \
  52. struct lock_stat *two) \
  53. { \
  54. return one->member > two->member; \
  55. }
  56. SINGLE_KEY(nr_acquired)
  57. SINGLE_KEY(nr_contended)
  58. SINGLE_KEY(wait_time_total)
  59. SINGLE_KEY(wait_time_min)
  60. SINGLE_KEY(wait_time_max)
  61. struct lock_key {
  62. /*
  63. * name: the value for specify by user
  64. * this should be simpler than raw name of member
  65. * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
  66. */
  67. const char *name;
  68. int (*key)(struct lock_stat*, struct lock_stat*);
  69. };
  70. static const char *sort_key = "acquired";
  71. static int (*compare)(struct lock_stat *, struct lock_stat *);
  72. static struct rb_root result; /* place to store sorted data */
  73. #define DEF_KEY_LOCK(name, fn_suffix) \
  74. { #name, lock_stat_key_ ## fn_suffix }
  75. struct lock_key keys[] = {
  76. DEF_KEY_LOCK(acquired, nr_acquired),
  77. DEF_KEY_LOCK(contended, nr_contended),
  78. DEF_KEY_LOCK(wait_total, wait_time_total),
  79. DEF_KEY_LOCK(wait_min, wait_time_min),
  80. DEF_KEY_LOCK(wait_max, wait_time_max),
  81. /* extra comparisons much complicated should be here */
  82. { NULL, NULL }
  83. };
  84. static void select_key(void)
  85. {
  86. int i;
  87. for (i = 0; keys[i].name; i++) {
  88. if (!strcmp(keys[i].name, sort_key)) {
  89. compare = keys[i].key;
  90. return;
  91. }
  92. }
  93. die("Unknown compare key:%s\n", sort_key);
  94. }
  95. static void insert_to_result(struct lock_stat *st,
  96. int (*bigger)(struct lock_stat *, struct lock_stat *))
  97. {
  98. struct rb_node **rb = &result.rb_node;
  99. struct rb_node *parent = NULL;
  100. struct lock_stat *p;
  101. while (*rb) {
  102. p = container_of(*rb, struct lock_stat, rb);
  103. parent = *rb;
  104. if (bigger(st, p))
  105. rb = &(*rb)->rb_left;
  106. else
  107. rb = &(*rb)->rb_right;
  108. }
  109. rb_link_node(&st->rb, parent, rb);
  110. rb_insert_color(&st->rb, &result);
  111. }
  112. /* returns left most element of result, and erase it */
  113. static struct lock_stat *pop_from_result(void)
  114. {
  115. struct rb_node *node = result.rb_node;
  116. if (!node)
  117. return NULL;
  118. while (node->rb_left)
  119. node = node->rb_left;
  120. rb_erase(node, &result);
  121. return container_of(node, struct lock_stat, rb);
  122. }
  123. static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
  124. {
  125. struct list_head *entry = lockhashentry(addr);
  126. struct lock_stat *ret, *new;
  127. list_for_each_entry(ret, entry, hash_entry) {
  128. if (ret->addr == addr)
  129. return ret;
  130. }
  131. new = zalloc(sizeof(struct lock_stat));
  132. if (!new)
  133. goto alloc_failed;
  134. new->addr = addr;
  135. new->name = zalloc(sizeof(char) * strlen(name) + 1);
  136. if (!new->name)
  137. goto alloc_failed;
  138. strcpy(new->name, name);
  139. /* LOCK_STATE_UNLOCKED == 0 isn't guaranteed forever */
  140. new->state = LOCK_STATE_UNLOCKED;
  141. new->wait_time_min = ULLONG_MAX;
  142. list_add(&new->hash_entry, entry);
  143. return new;
  144. alloc_failed:
  145. die("memory allocation failed\n");
  146. }
  147. static char const *input_name = "perf.data";
  148. static int profile_cpu = -1;
  149. struct raw_event_sample {
  150. u32 size;
  151. char data[0];
  152. };
  153. struct trace_acquire_event {
  154. void *addr;
  155. const char *name;
  156. };
  157. struct trace_acquired_event {
  158. void *addr;
  159. const char *name;
  160. };
  161. struct trace_contended_event {
  162. void *addr;
  163. const char *name;
  164. };
  165. struct trace_release_event {
  166. void *addr;
  167. const char *name;
  168. };
  169. struct trace_lock_handler {
  170. void (*acquire_event)(struct trace_acquire_event *,
  171. struct event *,
  172. int cpu,
  173. u64 timestamp,
  174. struct thread *thread);
  175. void (*acquired_event)(struct trace_acquired_event *,
  176. struct event *,
  177. int cpu,
  178. u64 timestamp,
  179. struct thread *thread);
  180. void (*contended_event)(struct trace_contended_event *,
  181. struct event *,
  182. int cpu,
  183. u64 timestamp,
  184. struct thread *thread);
  185. void (*release_event)(struct trace_release_event *,
  186. struct event *,
  187. int cpu,
  188. u64 timestamp,
  189. struct thread *thread);
  190. };
  191. static void
  192. report_lock_acquire_event(struct trace_acquire_event *acquire_event,
  193. struct event *__event __used,
  194. int cpu __used,
  195. u64 timestamp,
  196. struct thread *thread __used)
  197. {
  198. struct lock_stat *st;
  199. st = lock_stat_findnew(acquire_event->addr, acquire_event->name);
  200. switch (st->state) {
  201. case LOCK_STATE_UNLOCKED:
  202. break;
  203. case LOCK_STATE_LOCKED:
  204. break;
  205. default:
  206. BUG_ON(1);
  207. break;
  208. }
  209. st->prev_event_time = timestamp;
  210. }
  211. static void
  212. report_lock_acquired_event(struct trace_acquired_event *acquired_event,
  213. struct event *__event __used,
  214. int cpu __used,
  215. u64 timestamp,
  216. struct thread *thread __used)
  217. {
  218. struct lock_stat *st;
  219. st = lock_stat_findnew(acquired_event->addr, acquired_event->name);
  220. switch (st->state) {
  221. case LOCK_STATE_UNLOCKED:
  222. st->state = LOCK_STATE_LOCKED;
  223. st->nr_acquired++;
  224. break;
  225. case LOCK_STATE_LOCKED:
  226. break;
  227. default:
  228. BUG_ON(1);
  229. break;
  230. }
  231. st->prev_event_time = timestamp;
  232. }
  233. static void
  234. report_lock_contended_event(struct trace_contended_event *contended_event,
  235. struct event *__event __used,
  236. int cpu __used,
  237. u64 timestamp,
  238. struct thread *thread __used)
  239. {
  240. struct lock_stat *st;
  241. st = lock_stat_findnew(contended_event->addr, contended_event->name);
  242. switch (st->state) {
  243. case LOCK_STATE_UNLOCKED:
  244. break;
  245. case LOCK_STATE_LOCKED:
  246. st->nr_contended++;
  247. break;
  248. default:
  249. BUG_ON(1);
  250. break;
  251. }
  252. st->prev_event_time = timestamp;
  253. }
  254. static void
  255. report_lock_release_event(struct trace_release_event *release_event,
  256. struct event *__event __used,
  257. int cpu __used,
  258. u64 timestamp,
  259. struct thread *thread __used)
  260. {
  261. struct lock_stat *st;
  262. u64 hold_time;
  263. st = lock_stat_findnew(release_event->addr, release_event->name);
  264. switch (st->state) {
  265. case LOCK_STATE_UNLOCKED:
  266. break;
  267. case LOCK_STATE_LOCKED:
  268. st->state = LOCK_STATE_UNLOCKED;
  269. hold_time = timestamp - st->prev_event_time;
  270. if (timestamp < st->prev_event_time) {
  271. /* terribly, this can happen... */
  272. goto end;
  273. }
  274. if (st->wait_time_min > hold_time)
  275. st->wait_time_min = hold_time;
  276. if (st->wait_time_max < hold_time)
  277. st->wait_time_max = hold_time;
  278. st->wait_time_total += hold_time;
  279. st->nr_release++;
  280. break;
  281. default:
  282. BUG_ON(1);
  283. break;
  284. }
  285. end:
  286. st->prev_event_time = timestamp;
  287. }
  288. /* lock oriented handlers */
  289. /* TODO: handlers for CPU oriented, thread oriented */
  290. static struct trace_lock_handler report_lock_ops = {
  291. .acquire_event = report_lock_acquire_event,
  292. .acquired_event = report_lock_acquired_event,
  293. .contended_event = report_lock_contended_event,
  294. .release_event = report_lock_release_event,
  295. };
  296. static struct trace_lock_handler *trace_handler;
  297. static void
  298. process_lock_acquire_event(void *data,
  299. struct event *event __used,
  300. int cpu __used,
  301. u64 timestamp __used,
  302. struct thread *thread __used)
  303. {
  304. struct trace_acquire_event acquire_event;
  305. u64 tmp; /* this is required for casting... */
  306. tmp = raw_field_value(event, "lockdep_addr", data);
  307. memcpy(&acquire_event.addr, &tmp, sizeof(void *));
  308. acquire_event.name = (char *)raw_field_ptr(event, "name", data);
  309. if (trace_handler->acquire_event)
  310. trace_handler->acquire_event(&acquire_event, event, cpu, timestamp, thread);
  311. }
  312. static void
  313. process_lock_acquired_event(void *data,
  314. struct event *event __used,
  315. int cpu __used,
  316. u64 timestamp __used,
  317. struct thread *thread __used)
  318. {
  319. struct trace_acquired_event acquired_event;
  320. u64 tmp; /* this is required for casting... */
  321. tmp = raw_field_value(event, "lockdep_addr", data);
  322. memcpy(&acquired_event.addr, &tmp, sizeof(void *));
  323. acquired_event.name = (char *)raw_field_ptr(event, "name", data);
  324. if (trace_handler->acquire_event)
  325. trace_handler->acquired_event(&acquired_event, event, cpu, timestamp, thread);
  326. }
  327. static void
  328. process_lock_contended_event(void *data,
  329. struct event *event __used,
  330. int cpu __used,
  331. u64 timestamp __used,
  332. struct thread *thread __used)
  333. {
  334. struct trace_contended_event contended_event;
  335. u64 tmp; /* this is required for casting... */
  336. tmp = raw_field_value(event, "lockdep_addr", data);
  337. memcpy(&contended_event.addr, &tmp, sizeof(void *));
  338. contended_event.name = (char *)raw_field_ptr(event, "name", data);
  339. if (trace_handler->acquire_event)
  340. trace_handler->contended_event(&contended_event, event, cpu, timestamp, thread);
  341. }
  342. static void
  343. process_lock_release_event(void *data,
  344. struct event *event __used,
  345. int cpu __used,
  346. u64 timestamp __used,
  347. struct thread *thread __used)
  348. {
  349. struct trace_release_event release_event;
  350. u64 tmp; /* this is required for casting... */
  351. tmp = raw_field_value(event, "lockdep_addr", data);
  352. memcpy(&release_event.addr, &tmp, sizeof(void *));
  353. release_event.name = (char *)raw_field_ptr(event, "name", data);
  354. if (trace_handler->acquire_event)
  355. trace_handler->release_event(&release_event, event, cpu, timestamp, thread);
  356. }
  357. static void
  358. process_raw_event(void *data, int cpu,
  359. u64 timestamp, struct thread *thread)
  360. {
  361. struct event *event;
  362. int type;
  363. type = trace_parse_common_type(data);
  364. event = trace_find_event(type);
  365. if (!strcmp(event->name, "lock_acquire"))
  366. process_lock_acquire_event(data, event, cpu, timestamp, thread);
  367. if (!strcmp(event->name, "lock_acquired"))
  368. process_lock_acquired_event(data, event, cpu, timestamp, thread);
  369. if (!strcmp(event->name, "lock_contended"))
  370. process_lock_contended_event(data, event, cpu, timestamp, thread);
  371. if (!strcmp(event->name, "lock_release"))
  372. process_lock_release_event(data, event, cpu, timestamp, thread);
  373. }
  374. static int process_sample_event(event_t *event, struct perf_session *session)
  375. {
  376. struct thread *thread;
  377. struct sample_data data;
  378. bzero(&data, sizeof(struct sample_data));
  379. event__parse_sample(event, session->sample_type, &data);
  380. thread = perf_session__findnew(session, data.pid);
  381. if (thread == NULL) {
  382. pr_debug("problem processing %d event, skipping it.\n",
  383. event->header.type);
  384. return -1;
  385. }
  386. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  387. if (profile_cpu != -1 && profile_cpu != (int) data.cpu)
  388. return 0;
  389. process_raw_event(data.raw_data, data.cpu, data.time, thread);
  390. return 0;
  391. }
  392. /* TODO: various way to print, coloring, nano or milli sec */
  393. static void print_result(void)
  394. {
  395. struct lock_stat *st;
  396. char cut_name[20];
  397. printf("%18s ", "ID");
  398. printf("%20s ", "Name");
  399. printf("%10s ", "acquired");
  400. printf("%10s ", "contended");
  401. printf("%15s ", "total wait (ns)");
  402. printf("%15s ", "max wait (ns)");
  403. printf("%15s ", "min wait (ns)");
  404. printf("\n\n");
  405. while ((st = pop_from_result())) {
  406. bzero(cut_name, 20);
  407. printf("%p ", st->addr);
  408. if (strlen(st->name) < 16) {
  409. /* output raw name */
  410. printf("%20s ", st->name);
  411. } else {
  412. strncpy(cut_name, st->name, 16);
  413. cut_name[16] = '.';
  414. cut_name[17] = '.';
  415. cut_name[18] = '.';
  416. cut_name[19] = '\0';
  417. /* cut off name for saving output style */
  418. printf("%20s ", cut_name);
  419. }
  420. printf("%10u ", st->nr_acquired);
  421. printf("%10u ", st->nr_contended);
  422. printf("%15llu ", st->wait_time_total);
  423. printf("%15llu ", st->wait_time_max);
  424. printf("%15llu ", st->wait_time_min == ULLONG_MAX ?
  425. 0 : st->wait_time_min);
  426. printf("\n");
  427. }
  428. }
  429. static void dump_map(void)
  430. {
  431. unsigned int i;
  432. struct lock_stat *st;
  433. for (i = 0; i < LOCKHASH_SIZE; i++) {
  434. list_for_each_entry(st, &lockhash_table[i], hash_entry) {
  435. printf("%p: %s\n", st->addr, st->name);
  436. }
  437. }
  438. }
  439. static struct perf_event_ops eops = {
  440. .sample = process_sample_event,
  441. .comm = event__process_comm,
  442. };
  443. static struct perf_session *session;
  444. static int read_events(void)
  445. {
  446. session = perf_session__new(input_name, O_RDONLY, 0);
  447. if (!session)
  448. die("Initializing perf session failed\n");
  449. return perf_session__process_events(session, &eops);
  450. }
  451. static void sort_result(void)
  452. {
  453. unsigned int i;
  454. struct lock_stat *st;
  455. for (i = 0; i < LOCKHASH_SIZE; i++) {
  456. list_for_each_entry(st, &lockhash_table[i], hash_entry) {
  457. insert_to_result(st, compare);
  458. }
  459. }
  460. }
  461. static void __cmd_report(void)
  462. {
  463. setup_pager();
  464. select_key();
  465. read_events();
  466. sort_result();
  467. print_result();
  468. }
  469. static const char * const report_usage[] = {
  470. "perf lock report [<options>]",
  471. NULL
  472. };
  473. static const struct option report_options[] = {
  474. OPT_STRING('k', "key", &sort_key, "acquired",
  475. "key for sorting"),
  476. /* TODO: type */
  477. OPT_END()
  478. };
  479. static const char * const lock_usage[] = {
  480. "perf lock [<options>] {record|trace|report}",
  481. NULL
  482. };
  483. static const struct option lock_options[] = {
  484. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  485. OPT_BOOLEAN('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
  486. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
  487. OPT_END()
  488. };
  489. static const char *record_args[] = {
  490. "record",
  491. "-a",
  492. "-R",
  493. "-M",
  494. "-f",
  495. "-m", "1024",
  496. "-c", "1",
  497. "-e", "lock:lock_acquire:r",
  498. "-e", "lock:lock_acquired:r",
  499. "-e", "lock:lock_contended:r",
  500. "-e", "lock:lock_release:r",
  501. };
  502. static int __cmd_record(int argc, const char **argv)
  503. {
  504. unsigned int rec_argc, i, j;
  505. const char **rec_argv;
  506. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  507. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  508. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  509. rec_argv[i] = strdup(record_args[i]);
  510. for (j = 1; j < (unsigned int)argc; j++, i++)
  511. rec_argv[i] = argv[j];
  512. BUG_ON(i != rec_argc);
  513. return cmd_record(i, rec_argv, NULL);
  514. }
  515. int cmd_lock(int argc, const char **argv, const char *prefix __used)
  516. {
  517. unsigned int i;
  518. symbol__init();
  519. for (i = 0; i < LOCKHASH_SIZE; i++)
  520. INIT_LIST_HEAD(lockhash_table + i);
  521. argc = parse_options(argc, argv, lock_options, lock_usage,
  522. PARSE_OPT_STOP_AT_NON_OPTION);
  523. if (!argc)
  524. usage_with_options(lock_usage, lock_options);
  525. if (!strncmp(argv[0], "rec", 3)) {
  526. return __cmd_record(argc, argv);
  527. } else if (!strncmp(argv[0], "report", 6)) {
  528. trace_handler = &report_lock_ops;
  529. if (argc) {
  530. argc = parse_options(argc, argv,
  531. report_options, report_usage, 0);
  532. if (argc)
  533. usage_with_options(report_usage, report_options);
  534. }
  535. __cmd_report();
  536. } else if (!strcmp(argv[0], "trace")) {
  537. /* Aliased to 'perf trace' */
  538. return cmd_trace(argc, argv, prefix);
  539. } else if (!strcmp(argv[0], "map")) {
  540. /* recycling report_lock_ops */
  541. trace_handler = &report_lock_ops;
  542. setup_pager();
  543. read_events();
  544. dump_map();
  545. } else {
  546. usage_with_options(lock_usage, lock_options);
  547. }
  548. return 0;
  549. }