builtin-lock.c 23 KB

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