builtin-lock.c 23 KB

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