builtin-lock.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include "builtin.h"
  5. #include "perf.h"
  6. #include "util/evlist.h"
  7. #include "util/evsel.h"
  8. #include "util/util.h"
  9. #include "util/cache.h"
  10. #include "util/symbol.h"
  11. #include "util/thread.h"
  12. #include "util/header.h"
  13. #include <subcmd/parse-options.h>
  14. #include "util/trace-event.h"
  15. #include "util/debug.h"
  16. #include "util/session.h"
  17. #include "util/tool.h"
  18. #include "util/data.h"
  19. #include <sys/types.h>
  20. #include <sys/prctl.h>
  21. #include <semaphore.h>
  22. #include <pthread.h>
  23. #include <math.h>
  24. #include <limits.h>
  25. #include <linux/list.h>
  26. #include <linux/hash.h>
  27. #include <linux/kernel.h>
  28. static struct perf_session *session;
  29. /* based on kernel/lockdep.c */
  30. #define LOCKHASH_BITS 12
  31. #define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
  32. static struct list_head lockhash_table[LOCKHASH_SIZE];
  33. #define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
  34. #define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
  35. struct lock_stat {
  36. struct list_head hash_entry;
  37. struct rb_node rb; /* used for sorting */
  38. /*
  39. * FIXME: perf_evsel__intval() returns u64,
  40. * so address of lockdep_map should be dealed as 64bit.
  41. * Is there more better solution?
  42. */
  43. void *addr; /* address of lockdep_map, used as ID */
  44. char *name; /* for strcpy(), we cannot use const */
  45. unsigned int nr_acquire;
  46. unsigned int nr_acquired;
  47. unsigned int nr_contended;
  48. unsigned int nr_release;
  49. unsigned int nr_readlock;
  50. unsigned int nr_trylock;
  51. /* these times are in nano sec. */
  52. u64 avg_wait_time;
  53. u64 wait_time_total;
  54. u64 wait_time_min;
  55. u64 wait_time_max;
  56. int discard; /* flag of blacklist */
  57. };
  58. /*
  59. * States of lock_seq_stat
  60. *
  61. * UNINITIALIZED is required for detecting first event of acquire.
  62. * As the nature of lock events, there is no guarantee
  63. * that the first event for the locks are acquire,
  64. * it can be acquired, contended or release.
  65. */
  66. #define SEQ_STATE_UNINITIALIZED 0 /* initial state */
  67. #define SEQ_STATE_RELEASED 1
  68. #define SEQ_STATE_ACQUIRING 2
  69. #define SEQ_STATE_ACQUIRED 3
  70. #define SEQ_STATE_READ_ACQUIRED 4
  71. #define SEQ_STATE_CONTENDED 5
  72. /*
  73. * MAX_LOCK_DEPTH
  74. * Imported from include/linux/sched.h.
  75. * Should this be synchronized?
  76. */
  77. #define MAX_LOCK_DEPTH 48
  78. /*
  79. * struct lock_seq_stat:
  80. * Place to put on state of one lock sequence
  81. * 1) acquire -> acquired -> release
  82. * 2) acquire -> contended -> acquired -> release
  83. * 3) acquire (with read or try) -> release
  84. * 4) Are there other patterns?
  85. */
  86. struct lock_seq_stat {
  87. struct list_head list;
  88. int state;
  89. u64 prev_event_time;
  90. void *addr;
  91. int read_count;
  92. };
  93. struct thread_stat {
  94. struct rb_node rb;
  95. u32 tid;
  96. struct list_head seq_list;
  97. };
  98. static struct rb_root thread_stats;
  99. static struct thread_stat *thread_stat_find(u32 tid)
  100. {
  101. struct rb_node *node;
  102. struct thread_stat *st;
  103. node = thread_stats.rb_node;
  104. while (node) {
  105. st = container_of(node, struct thread_stat, rb);
  106. if (st->tid == tid)
  107. return st;
  108. else if (tid < st->tid)
  109. node = node->rb_left;
  110. else
  111. node = node->rb_right;
  112. }
  113. return NULL;
  114. }
  115. static void thread_stat_insert(struct thread_stat *new)
  116. {
  117. struct rb_node **rb = &thread_stats.rb_node;
  118. struct rb_node *parent = NULL;
  119. struct thread_stat *p;
  120. while (*rb) {
  121. p = container_of(*rb, struct thread_stat, rb);
  122. parent = *rb;
  123. if (new->tid < p->tid)
  124. rb = &(*rb)->rb_left;
  125. else if (new->tid > p->tid)
  126. rb = &(*rb)->rb_right;
  127. else
  128. BUG_ON("inserting invalid thread_stat\n");
  129. }
  130. rb_link_node(&new->rb, parent, rb);
  131. rb_insert_color(&new->rb, &thread_stats);
  132. }
  133. static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
  134. {
  135. struct thread_stat *st;
  136. st = thread_stat_find(tid);
  137. if (st)
  138. return st;
  139. st = zalloc(sizeof(struct thread_stat));
  140. if (!st) {
  141. pr_err("memory allocation failed\n");
  142. return NULL;
  143. }
  144. st->tid = tid;
  145. INIT_LIST_HEAD(&st->seq_list);
  146. thread_stat_insert(st);
  147. return st;
  148. }
  149. static struct thread_stat *thread_stat_findnew_first(u32 tid);
  150. static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
  151. thread_stat_findnew_first;
  152. static struct thread_stat *thread_stat_findnew_first(u32 tid)
  153. {
  154. struct thread_stat *st;
  155. st = zalloc(sizeof(struct thread_stat));
  156. if (!st) {
  157. pr_err("memory allocation failed\n");
  158. return NULL;
  159. }
  160. st->tid = tid;
  161. INIT_LIST_HEAD(&st->seq_list);
  162. rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
  163. rb_insert_color(&st->rb, &thread_stats);
  164. thread_stat_findnew = thread_stat_findnew_after_first;
  165. return st;
  166. }
  167. /* build simple key function one is bigger than two */
  168. #define SINGLE_KEY(member) \
  169. static int lock_stat_key_ ## member(struct lock_stat *one, \
  170. struct lock_stat *two) \
  171. { \
  172. return one->member > two->member; \
  173. }
  174. SINGLE_KEY(nr_acquired)
  175. SINGLE_KEY(nr_contended)
  176. SINGLE_KEY(avg_wait_time)
  177. SINGLE_KEY(wait_time_total)
  178. SINGLE_KEY(wait_time_max)
  179. static int lock_stat_key_wait_time_min(struct lock_stat *one,
  180. struct lock_stat *two)
  181. {
  182. u64 s1 = one->wait_time_min;
  183. u64 s2 = two->wait_time_min;
  184. if (s1 == ULLONG_MAX)
  185. s1 = 0;
  186. if (s2 == ULLONG_MAX)
  187. s2 = 0;
  188. return s1 > s2;
  189. }
  190. struct lock_key {
  191. /*
  192. * name: the value for specify by user
  193. * this should be simpler than raw name of member
  194. * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
  195. */
  196. const char *name;
  197. int (*key)(struct lock_stat*, struct lock_stat*);
  198. };
  199. static const char *sort_key = "acquired";
  200. static int (*compare)(struct lock_stat *, struct lock_stat *);
  201. static struct rb_root result; /* place to store sorted data */
  202. #define DEF_KEY_LOCK(name, fn_suffix) \
  203. { #name, lock_stat_key_ ## fn_suffix }
  204. struct lock_key keys[] = {
  205. DEF_KEY_LOCK(acquired, nr_acquired),
  206. DEF_KEY_LOCK(contended, nr_contended),
  207. DEF_KEY_LOCK(avg_wait, avg_wait_time),
  208. DEF_KEY_LOCK(wait_total, wait_time_total),
  209. DEF_KEY_LOCK(wait_min, wait_time_min),
  210. DEF_KEY_LOCK(wait_max, wait_time_max),
  211. /* extra comparisons much complicated should be here */
  212. { NULL, NULL }
  213. };
  214. static int select_key(void)
  215. {
  216. int i;
  217. for (i = 0; keys[i].name; i++) {
  218. if (!strcmp(keys[i].name, sort_key)) {
  219. compare = keys[i].key;
  220. return 0;
  221. }
  222. }
  223. pr_err("Unknown compare key: %s\n", sort_key);
  224. return -1;
  225. }
  226. static void insert_to_result(struct lock_stat *st,
  227. int (*bigger)(struct lock_stat *, struct lock_stat *))
  228. {
  229. struct rb_node **rb = &result.rb_node;
  230. struct rb_node *parent = NULL;
  231. struct lock_stat *p;
  232. while (*rb) {
  233. p = container_of(*rb, struct lock_stat, rb);
  234. parent = *rb;
  235. if (bigger(st, p))
  236. rb = &(*rb)->rb_left;
  237. else
  238. rb = &(*rb)->rb_right;
  239. }
  240. rb_link_node(&st->rb, parent, rb);
  241. rb_insert_color(&st->rb, &result);
  242. }
  243. /* returns left most element of result, and erase it */
  244. static struct lock_stat *pop_from_result(void)
  245. {
  246. struct rb_node *node = result.rb_node;
  247. if (!node)
  248. return NULL;
  249. while (node->rb_left)
  250. node = node->rb_left;
  251. rb_erase(node, &result);
  252. return container_of(node, struct lock_stat, rb);
  253. }
  254. static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
  255. {
  256. struct list_head *entry = lockhashentry(addr);
  257. struct lock_stat *ret, *new;
  258. list_for_each_entry(ret, entry, hash_entry) {
  259. if (ret->addr == addr)
  260. return ret;
  261. }
  262. new = zalloc(sizeof(struct lock_stat));
  263. if (!new)
  264. goto alloc_failed;
  265. new->addr = addr;
  266. new->name = zalloc(sizeof(char) * strlen(name) + 1);
  267. if (!new->name) {
  268. free(new);
  269. goto alloc_failed;
  270. }
  271. strcpy(new->name, name);
  272. new->wait_time_min = ULLONG_MAX;
  273. list_add(&new->hash_entry, entry);
  274. return new;
  275. alloc_failed:
  276. pr_err("memory allocation failed\n");
  277. return NULL;
  278. }
  279. struct trace_lock_handler {
  280. int (*acquire_event)(struct perf_evsel *evsel,
  281. struct perf_sample *sample);
  282. int (*acquired_event)(struct perf_evsel *evsel,
  283. struct perf_sample *sample);
  284. int (*contended_event)(struct perf_evsel *evsel,
  285. struct perf_sample *sample);
  286. int (*release_event)(struct perf_evsel *evsel,
  287. struct perf_sample *sample);
  288. };
  289. static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
  290. {
  291. struct lock_seq_stat *seq;
  292. list_for_each_entry(seq, &ts->seq_list, list) {
  293. if (seq->addr == addr)
  294. return seq;
  295. }
  296. seq = zalloc(sizeof(struct lock_seq_stat));
  297. if (!seq) {
  298. pr_err("memory allocation failed\n");
  299. return NULL;
  300. }
  301. seq->state = SEQ_STATE_UNINITIALIZED;
  302. seq->addr = addr;
  303. list_add(&seq->list, &ts->seq_list);
  304. return seq;
  305. }
  306. enum broken_state {
  307. BROKEN_ACQUIRE,
  308. BROKEN_ACQUIRED,
  309. BROKEN_CONTENDED,
  310. BROKEN_RELEASE,
  311. BROKEN_MAX,
  312. };
  313. static int bad_hist[BROKEN_MAX];
  314. enum acquire_flags {
  315. TRY_LOCK = 1,
  316. READ_LOCK = 2,
  317. };
  318. static int report_lock_acquire_event(struct perf_evsel *evsel,
  319. struct perf_sample *sample)
  320. {
  321. void *addr;
  322. struct lock_stat *ls;
  323. struct thread_stat *ts;
  324. struct lock_seq_stat *seq;
  325. const char *name = perf_evsel__strval(evsel, sample, "name");
  326. u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
  327. int flag = perf_evsel__intval(evsel, sample, "flag");
  328. memcpy(&addr, &tmp, sizeof(void *));
  329. ls = lock_stat_findnew(addr, name);
  330. if (!ls)
  331. return -ENOMEM;
  332. if (ls->discard)
  333. return 0;
  334. ts = thread_stat_findnew(sample->tid);
  335. if (!ts)
  336. return -ENOMEM;
  337. seq = get_seq(ts, addr);
  338. if (!seq)
  339. return -ENOMEM;
  340. switch (seq->state) {
  341. case SEQ_STATE_UNINITIALIZED:
  342. case SEQ_STATE_RELEASED:
  343. if (!flag) {
  344. seq->state = SEQ_STATE_ACQUIRING;
  345. } else {
  346. if (flag & TRY_LOCK)
  347. ls->nr_trylock++;
  348. if (flag & READ_LOCK)
  349. ls->nr_readlock++;
  350. seq->state = SEQ_STATE_READ_ACQUIRED;
  351. seq->read_count = 1;
  352. ls->nr_acquired++;
  353. }
  354. break;
  355. case SEQ_STATE_READ_ACQUIRED:
  356. if (flag & READ_LOCK) {
  357. seq->read_count++;
  358. ls->nr_acquired++;
  359. goto end;
  360. } else {
  361. goto broken;
  362. }
  363. break;
  364. case SEQ_STATE_ACQUIRED:
  365. case SEQ_STATE_ACQUIRING:
  366. case SEQ_STATE_CONTENDED:
  367. broken:
  368. /* broken lock sequence, discard it */
  369. ls->discard = 1;
  370. bad_hist[BROKEN_ACQUIRE]++;
  371. list_del(&seq->list);
  372. free(seq);
  373. goto end;
  374. default:
  375. BUG_ON("Unknown state of lock sequence found!\n");
  376. break;
  377. }
  378. ls->nr_acquire++;
  379. seq->prev_event_time = sample->time;
  380. end:
  381. return 0;
  382. }
  383. static int report_lock_acquired_event(struct perf_evsel *evsel,
  384. struct perf_sample *sample)
  385. {
  386. void *addr;
  387. struct lock_stat *ls;
  388. struct thread_stat *ts;
  389. struct lock_seq_stat *seq;
  390. u64 contended_term;
  391. const char *name = perf_evsel__strval(evsel, sample, "name");
  392. u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
  393. memcpy(&addr, &tmp, sizeof(void *));
  394. ls = lock_stat_findnew(addr, name);
  395. if (!ls)
  396. return -ENOMEM;
  397. if (ls->discard)
  398. return 0;
  399. ts = thread_stat_findnew(sample->tid);
  400. if (!ts)
  401. return -ENOMEM;
  402. seq = get_seq(ts, addr);
  403. if (!seq)
  404. return -ENOMEM;
  405. switch (seq->state) {
  406. case SEQ_STATE_UNINITIALIZED:
  407. /* orphan event, do nothing */
  408. return 0;
  409. case SEQ_STATE_ACQUIRING:
  410. break;
  411. case SEQ_STATE_CONTENDED:
  412. contended_term = sample->time - seq->prev_event_time;
  413. ls->wait_time_total += contended_term;
  414. if (contended_term < ls->wait_time_min)
  415. ls->wait_time_min = contended_term;
  416. if (ls->wait_time_max < contended_term)
  417. ls->wait_time_max = contended_term;
  418. break;
  419. case SEQ_STATE_RELEASED:
  420. case SEQ_STATE_ACQUIRED:
  421. case SEQ_STATE_READ_ACQUIRED:
  422. /* broken lock sequence, discard it */
  423. ls->discard = 1;
  424. bad_hist[BROKEN_ACQUIRED]++;
  425. list_del(&seq->list);
  426. free(seq);
  427. goto end;
  428. default:
  429. BUG_ON("Unknown state of lock sequence found!\n");
  430. break;
  431. }
  432. seq->state = SEQ_STATE_ACQUIRED;
  433. ls->nr_acquired++;
  434. ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
  435. seq->prev_event_time = sample->time;
  436. end:
  437. return 0;
  438. }
  439. static int report_lock_contended_event(struct perf_evsel *evsel,
  440. struct perf_sample *sample)
  441. {
  442. void *addr;
  443. struct lock_stat *ls;
  444. struct thread_stat *ts;
  445. struct lock_seq_stat *seq;
  446. const char *name = perf_evsel__strval(evsel, sample, "name");
  447. u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
  448. memcpy(&addr, &tmp, sizeof(void *));
  449. ls = lock_stat_findnew(addr, name);
  450. if (!ls)
  451. return -ENOMEM;
  452. if (ls->discard)
  453. return 0;
  454. ts = thread_stat_findnew(sample->tid);
  455. if (!ts)
  456. return -ENOMEM;
  457. seq = get_seq(ts, addr);
  458. if (!seq)
  459. return -ENOMEM;
  460. switch (seq->state) {
  461. case SEQ_STATE_UNINITIALIZED:
  462. /* orphan event, do nothing */
  463. return 0;
  464. case SEQ_STATE_ACQUIRING:
  465. break;
  466. case SEQ_STATE_RELEASED:
  467. case SEQ_STATE_ACQUIRED:
  468. case SEQ_STATE_READ_ACQUIRED:
  469. case SEQ_STATE_CONTENDED:
  470. /* broken lock sequence, discard it */
  471. ls->discard = 1;
  472. bad_hist[BROKEN_CONTENDED]++;
  473. list_del(&seq->list);
  474. free(seq);
  475. goto end;
  476. default:
  477. BUG_ON("Unknown state of lock sequence found!\n");
  478. break;
  479. }
  480. seq->state = SEQ_STATE_CONTENDED;
  481. ls->nr_contended++;
  482. ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
  483. seq->prev_event_time = sample->time;
  484. end:
  485. return 0;
  486. }
  487. static int report_lock_release_event(struct perf_evsel *evsel,
  488. struct perf_sample *sample)
  489. {
  490. void *addr;
  491. struct lock_stat *ls;
  492. struct thread_stat *ts;
  493. struct lock_seq_stat *seq;
  494. const char *name = perf_evsel__strval(evsel, sample, "name");
  495. u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
  496. memcpy(&addr, &tmp, sizeof(void *));
  497. ls = lock_stat_findnew(addr, name);
  498. if (!ls)
  499. return -ENOMEM;
  500. if (ls->discard)
  501. return 0;
  502. ts = thread_stat_findnew(sample->tid);
  503. if (!ts)
  504. return -ENOMEM;
  505. seq = get_seq(ts, addr);
  506. if (!seq)
  507. return -ENOMEM;
  508. switch (seq->state) {
  509. case SEQ_STATE_UNINITIALIZED:
  510. goto end;
  511. case SEQ_STATE_ACQUIRED:
  512. break;
  513. case SEQ_STATE_READ_ACQUIRED:
  514. seq->read_count--;
  515. BUG_ON(seq->read_count < 0);
  516. if (!seq->read_count) {
  517. ls->nr_release++;
  518. goto end;
  519. }
  520. break;
  521. case SEQ_STATE_ACQUIRING:
  522. case SEQ_STATE_CONTENDED:
  523. case SEQ_STATE_RELEASED:
  524. /* broken lock sequence, discard it */
  525. ls->discard = 1;
  526. bad_hist[BROKEN_RELEASE]++;
  527. goto free_seq;
  528. default:
  529. BUG_ON("Unknown state of lock sequence found!\n");
  530. break;
  531. }
  532. ls->nr_release++;
  533. free_seq:
  534. list_del(&seq->list);
  535. free(seq);
  536. end:
  537. return 0;
  538. }
  539. /* lock oriented handlers */
  540. /* TODO: handlers for CPU oriented, thread oriented */
  541. static struct trace_lock_handler report_lock_ops = {
  542. .acquire_event = report_lock_acquire_event,
  543. .acquired_event = report_lock_acquired_event,
  544. .contended_event = report_lock_contended_event,
  545. .release_event = report_lock_release_event,
  546. };
  547. static struct trace_lock_handler *trace_handler;
  548. static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
  549. struct perf_sample *sample)
  550. {
  551. if (trace_handler->acquire_event)
  552. return trace_handler->acquire_event(evsel, sample);
  553. return 0;
  554. }
  555. static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
  556. struct perf_sample *sample)
  557. {
  558. if (trace_handler->acquired_event)
  559. return trace_handler->acquired_event(evsel, sample);
  560. return 0;
  561. }
  562. static int perf_evsel__process_lock_contended(struct perf_evsel *evsel,
  563. struct perf_sample *sample)
  564. {
  565. if (trace_handler->contended_event)
  566. return trace_handler->contended_event(evsel, sample);
  567. return 0;
  568. }
  569. static int perf_evsel__process_lock_release(struct perf_evsel *evsel,
  570. struct perf_sample *sample)
  571. {
  572. if (trace_handler->release_event)
  573. return trace_handler->release_event(evsel, sample);
  574. return 0;
  575. }
  576. static void print_bad_events(int bad, int total)
  577. {
  578. /* Output for debug, this have to be removed */
  579. int i;
  580. const char *name[4] =
  581. { "acquire", "acquired", "contended", "release" };
  582. pr_info("\n=== output for debug===\n\n");
  583. pr_info("bad: %d, total: %d\n", bad, total);
  584. pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
  585. pr_info("histogram of events caused bad sequence\n");
  586. for (i = 0; i < BROKEN_MAX; i++)
  587. pr_info(" %10s: %d\n", name[i], bad_hist[i]);
  588. }
  589. /* TODO: various way to print, coloring, nano or milli sec */
  590. static void print_result(void)
  591. {
  592. struct lock_stat *st;
  593. char cut_name[20];
  594. int bad, total;
  595. pr_info("%20s ", "Name");
  596. pr_info("%10s ", "acquired");
  597. pr_info("%10s ", "contended");
  598. pr_info("%15s ", "avg wait (ns)");
  599. pr_info("%15s ", "total wait (ns)");
  600. pr_info("%15s ", "max wait (ns)");
  601. pr_info("%15s ", "min wait (ns)");
  602. pr_info("\n\n");
  603. bad = total = 0;
  604. while ((st = pop_from_result())) {
  605. total++;
  606. if (st->discard) {
  607. bad++;
  608. continue;
  609. }
  610. bzero(cut_name, 20);
  611. if (strlen(st->name) < 16) {
  612. /* output raw name */
  613. pr_info("%20s ", st->name);
  614. } else {
  615. strncpy(cut_name, st->name, 16);
  616. cut_name[16] = '.';
  617. cut_name[17] = '.';
  618. cut_name[18] = '.';
  619. cut_name[19] = '\0';
  620. /* cut off name for saving output style */
  621. pr_info("%20s ", cut_name);
  622. }
  623. pr_info("%10u ", st->nr_acquired);
  624. pr_info("%10u ", st->nr_contended);
  625. pr_info("%15" PRIu64 " ", st->avg_wait_time);
  626. pr_info("%15" PRIu64 " ", st->wait_time_total);
  627. pr_info("%15" PRIu64 " ", st->wait_time_max);
  628. pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
  629. 0 : st->wait_time_min);
  630. pr_info("\n");
  631. }
  632. print_bad_events(bad, total);
  633. }
  634. static bool info_threads, info_map;
  635. static void dump_threads(void)
  636. {
  637. struct thread_stat *st;
  638. struct rb_node *node;
  639. struct thread *t;
  640. pr_info("%10s: comm\n", "Thread ID");
  641. node = rb_first(&thread_stats);
  642. while (node) {
  643. st = container_of(node, struct thread_stat, rb);
  644. t = perf_session__findnew(session, st->tid);
  645. pr_info("%10d: %s\n", st->tid, thread__comm_str(t));
  646. node = rb_next(node);
  647. thread__put(t);
  648. };
  649. }
  650. static void dump_map(void)
  651. {
  652. unsigned int i;
  653. struct lock_stat *st;
  654. pr_info("Address of instance: name of class\n");
  655. for (i = 0; i < LOCKHASH_SIZE; i++) {
  656. list_for_each_entry(st, &lockhash_table[i], hash_entry) {
  657. pr_info(" %p: %s\n", st->addr, st->name);
  658. }
  659. }
  660. }
  661. static int dump_info(void)
  662. {
  663. int rc = 0;
  664. if (info_threads)
  665. dump_threads();
  666. else if (info_map)
  667. dump_map();
  668. else {
  669. rc = -1;
  670. pr_err("Unknown type of information\n");
  671. }
  672. return rc;
  673. }
  674. typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
  675. struct perf_sample *sample);
  676. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  677. union perf_event *event,
  678. struct perf_sample *sample,
  679. struct perf_evsel *evsel,
  680. struct machine *machine)
  681. {
  682. int err = 0;
  683. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  684. sample->tid);
  685. if (thread == NULL) {
  686. pr_debug("problem processing %d event, skipping it.\n",
  687. event->header.type);
  688. return -1;
  689. }
  690. if (evsel->handler != NULL) {
  691. tracepoint_handler f = evsel->handler;
  692. err = f(evsel, sample);
  693. }
  694. thread__put(thread);
  695. return err;
  696. }
  697. static void sort_result(void)
  698. {
  699. unsigned int i;
  700. struct lock_stat *st;
  701. for (i = 0; i < LOCKHASH_SIZE; i++) {
  702. list_for_each_entry(st, &lockhash_table[i], hash_entry) {
  703. insert_to_result(st, compare);
  704. }
  705. }
  706. }
  707. static const struct perf_evsel_str_handler lock_tracepoints[] = {
  708. { "lock:lock_acquire", perf_evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
  709. { "lock:lock_acquired", perf_evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
  710. { "lock:lock_contended", perf_evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
  711. { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
  712. };
  713. static bool force;
  714. static int __cmd_report(bool display_info)
  715. {
  716. int err = -EINVAL;
  717. struct perf_tool eops = {
  718. .sample = process_sample_event,
  719. .comm = perf_event__process_comm,
  720. .namespaces = perf_event__process_namespaces,
  721. .ordered_events = true,
  722. };
  723. struct perf_data data = {
  724. .file = {
  725. .path = input_name,
  726. },
  727. .mode = PERF_DATA_MODE_READ,
  728. .force = force,
  729. };
  730. session = perf_session__new(&data, false, &eops);
  731. if (!session) {
  732. pr_err("Initializing perf session failed\n");
  733. return -1;
  734. }
  735. symbol__init(&session->header.env);
  736. if (!perf_session__has_traces(session, "lock record"))
  737. goto out_delete;
  738. if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
  739. pr_err("Initializing perf session tracepoint handlers failed\n");
  740. goto out_delete;
  741. }
  742. if (select_key())
  743. goto out_delete;
  744. err = perf_session__process_events(session);
  745. if (err)
  746. goto out_delete;
  747. setup_pager();
  748. if (display_info) /* used for info subcommand */
  749. err = dump_info();
  750. else {
  751. sort_result();
  752. print_result();
  753. }
  754. out_delete:
  755. perf_session__delete(session);
  756. return err;
  757. }
  758. static int __cmd_record(int argc, const char **argv)
  759. {
  760. const char *record_args[] = {
  761. "record", "-R", "-m", "1024", "-c", "1",
  762. };
  763. unsigned int rec_argc, i, j, ret;
  764. const char **rec_argv;
  765. for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
  766. if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
  767. pr_err("tracepoint %s is not enabled. "
  768. "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
  769. lock_tracepoints[i].name);
  770. return 1;
  771. }
  772. }
  773. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  774. /* factor of 2 is for -e in front of each tracepoint */
  775. rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
  776. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  777. if (!rec_argv)
  778. return -ENOMEM;
  779. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  780. rec_argv[i] = strdup(record_args[i]);
  781. for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
  782. rec_argv[i++] = "-e";
  783. rec_argv[i++] = strdup(lock_tracepoints[j].name);
  784. }
  785. for (j = 1; j < (unsigned int)argc; j++, i++)
  786. rec_argv[i] = argv[j];
  787. BUG_ON(i != rec_argc);
  788. ret = cmd_record(i, rec_argv);
  789. free(rec_argv);
  790. return ret;
  791. }
  792. int cmd_lock(int argc, const char **argv)
  793. {
  794. const struct option lock_options[] = {
  795. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  796. OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
  797. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
  798. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  799. OPT_END()
  800. };
  801. const struct option info_options[] = {
  802. OPT_BOOLEAN('t', "threads", &info_threads,
  803. "dump thread list in perf.data"),
  804. OPT_BOOLEAN('m', "map", &info_map,
  805. "map of lock instances (address:name table)"),
  806. OPT_PARENT(lock_options)
  807. };
  808. const struct option report_options[] = {
  809. OPT_STRING('k', "key", &sort_key, "acquired",
  810. "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
  811. /* TODO: type */
  812. OPT_PARENT(lock_options)
  813. };
  814. const char * const info_usage[] = {
  815. "perf lock info [<options>]",
  816. NULL
  817. };
  818. const char *const lock_subcommands[] = { "record", "report", "script",
  819. "info", NULL };
  820. const char *lock_usage[] = {
  821. NULL,
  822. NULL
  823. };
  824. const char * const report_usage[] = {
  825. "perf lock report [<options>]",
  826. NULL
  827. };
  828. unsigned int i;
  829. int rc = 0;
  830. for (i = 0; i < LOCKHASH_SIZE; i++)
  831. INIT_LIST_HEAD(lockhash_table + i);
  832. argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
  833. lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  834. if (!argc)
  835. usage_with_options(lock_usage, lock_options);
  836. if (!strncmp(argv[0], "rec", 3)) {
  837. return __cmd_record(argc, argv);
  838. } else if (!strncmp(argv[0], "report", 6)) {
  839. trace_handler = &report_lock_ops;
  840. if (argc) {
  841. argc = parse_options(argc, argv,
  842. report_options, report_usage, 0);
  843. if (argc)
  844. usage_with_options(report_usage, report_options);
  845. }
  846. rc = __cmd_report(false);
  847. } else if (!strcmp(argv[0], "script")) {
  848. /* Aliased to 'perf script' */
  849. return cmd_script(argc, argv);
  850. } else if (!strcmp(argv[0], "info")) {
  851. if (argc) {
  852. argc = parse_options(argc, argv,
  853. info_options, info_usage, 0);
  854. if (argc)
  855. usage_with_options(info_usage, info_options);
  856. }
  857. /* recycling report_lock_ops */
  858. trace_handler = &report_lock_ops;
  859. rc = __cmd_report(true);
  860. } else {
  861. usage_with_options(lock_usage, lock_options);
  862. }
  863. return rc;
  864. }