builtin-lock.c 23 KB

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