builtin-lock.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. enum acquire_flags {
  318. TRY_LOCK = 1,
  319. READ_LOCK = 2,
  320. };
  321. static void
  322. report_lock_acquire_event(struct trace_acquire_event *acquire_event,
  323. struct event *__event __used,
  324. int cpu __used,
  325. u64 timestamp __used,
  326. struct thread *thread __used)
  327. {
  328. struct lock_stat *ls;
  329. struct thread_stat *ts;
  330. struct lock_seq_stat *seq;
  331. ls = lock_stat_findnew(acquire_event->addr, acquire_event->name);
  332. if (ls->discard)
  333. return;
  334. ts = thread_stat_findnew(thread->pid);
  335. seq = get_seq(ts, acquire_event->addr);
  336. switch (seq->state) {
  337. case SEQ_STATE_UNINITIALIZED:
  338. case SEQ_STATE_RELEASED:
  339. if (!acquire_event->flag) {
  340. seq->state = SEQ_STATE_ACQUIRING;
  341. } else {
  342. if (acquire_event->flag & TRY_LOCK)
  343. ls->nr_trylock++;
  344. if (acquire_event->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 (acquire_event->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. break;
  371. default:
  372. BUG_ON("Unknown state of lock sequence found!\n");
  373. break;
  374. }
  375. ls->nr_acquire++;
  376. seq->prev_event_time = timestamp;
  377. end:
  378. return;
  379. }
  380. static void
  381. report_lock_acquired_event(struct trace_acquired_event *acquired_event,
  382. struct event *__event __used,
  383. int cpu __used,
  384. u64 timestamp __used,
  385. struct thread *thread __used)
  386. {
  387. struct lock_stat *ls;
  388. struct thread_stat *ts;
  389. struct lock_seq_stat *seq;
  390. u64 contended_term;
  391. ls = lock_stat_findnew(acquired_event->addr, acquired_event->name);
  392. if (ls->discard)
  393. return;
  394. ts = thread_stat_findnew(thread->pid);
  395. seq = get_seq(ts, acquired_event->addr);
  396. switch (seq->state) {
  397. case SEQ_STATE_UNINITIALIZED:
  398. /* orphan event, do nothing */
  399. return;
  400. case SEQ_STATE_ACQUIRING:
  401. break;
  402. case SEQ_STATE_CONTENDED:
  403. contended_term = timestamp - seq->prev_event_time;
  404. ls->wait_time_total += contended_term;
  405. if (contended_term < ls->wait_time_min)
  406. ls->wait_time_min = contended_term;
  407. if (ls->wait_time_max < contended_term)
  408. ls->wait_time_max = contended_term;
  409. break;
  410. case SEQ_STATE_RELEASED:
  411. case SEQ_STATE_ACQUIRED:
  412. case SEQ_STATE_READ_ACQUIRED:
  413. /* broken lock sequence, discard it */
  414. ls->discard = 1;
  415. bad_hist[BROKEN_ACQUIRED]++;
  416. list_del(&seq->list);
  417. free(seq);
  418. goto end;
  419. break;
  420. default:
  421. BUG_ON("Unknown state of lock sequence found!\n");
  422. break;
  423. }
  424. seq->state = SEQ_STATE_ACQUIRED;
  425. ls->nr_acquired++;
  426. seq->prev_event_time = timestamp;
  427. end:
  428. return;
  429. }
  430. static void
  431. report_lock_contended_event(struct trace_contended_event *contended_event,
  432. struct event *__event __used,
  433. int cpu __used,
  434. u64 timestamp __used,
  435. struct thread *thread __used)
  436. {
  437. struct lock_stat *ls;
  438. struct thread_stat *ts;
  439. struct lock_seq_stat *seq;
  440. ls = lock_stat_findnew(contended_event->addr, contended_event->name);
  441. if (ls->discard)
  442. return;
  443. ts = thread_stat_findnew(thread->pid);
  444. seq = get_seq(ts, contended_event->addr);
  445. switch (seq->state) {
  446. case SEQ_STATE_UNINITIALIZED:
  447. /* orphan event, do nothing */
  448. return;
  449. case SEQ_STATE_ACQUIRING:
  450. break;
  451. case SEQ_STATE_RELEASED:
  452. case SEQ_STATE_ACQUIRED:
  453. case SEQ_STATE_READ_ACQUIRED:
  454. case SEQ_STATE_CONTENDED:
  455. /* broken lock sequence, discard it */
  456. ls->discard = 1;
  457. bad_hist[BROKEN_CONTENDED]++;
  458. list_del(&seq->list);
  459. free(seq);
  460. goto end;
  461. break;
  462. default:
  463. BUG_ON("Unknown state of lock sequence found!\n");
  464. break;
  465. }
  466. seq->state = SEQ_STATE_CONTENDED;
  467. ls->nr_contended++;
  468. seq->prev_event_time = timestamp;
  469. end:
  470. return;
  471. }
  472. static void
  473. report_lock_release_event(struct trace_release_event *release_event,
  474. struct event *__event __used,
  475. int cpu __used,
  476. u64 timestamp __used,
  477. struct thread *thread __used)
  478. {
  479. struct lock_stat *ls;
  480. struct thread_stat *ts;
  481. struct lock_seq_stat *seq;
  482. ls = lock_stat_findnew(release_event->addr, release_event->name);
  483. if (ls->discard)
  484. return;
  485. ts = thread_stat_findnew(thread->pid);
  486. seq = get_seq(ts, release_event->addr);
  487. switch (seq->state) {
  488. case SEQ_STATE_UNINITIALIZED:
  489. goto end;
  490. break;
  491. case SEQ_STATE_ACQUIRED:
  492. break;
  493. case SEQ_STATE_READ_ACQUIRED:
  494. seq->read_count--;
  495. BUG_ON(seq->read_count < 0);
  496. if (!seq->read_count) {
  497. ls->nr_release++;
  498. goto end;
  499. }
  500. break;
  501. case SEQ_STATE_ACQUIRING:
  502. case SEQ_STATE_CONTENDED:
  503. case SEQ_STATE_RELEASED:
  504. /* broken lock sequence, discard it */
  505. ls->discard = 1;
  506. bad_hist[BROKEN_RELEASE]++;
  507. goto free_seq;
  508. break;
  509. default:
  510. BUG_ON("Unknown state of lock sequence found!\n");
  511. break;
  512. }
  513. ls->nr_release++;
  514. free_seq:
  515. list_del(&seq->list);
  516. free(seq);
  517. end:
  518. return;
  519. }
  520. /* lock oriented handlers */
  521. /* TODO: handlers for CPU oriented, thread oriented */
  522. static struct trace_lock_handler report_lock_ops = {
  523. .acquire_event = report_lock_acquire_event,
  524. .acquired_event = report_lock_acquired_event,
  525. .contended_event = report_lock_contended_event,
  526. .release_event = report_lock_release_event,
  527. };
  528. static struct trace_lock_handler *trace_handler;
  529. static void
  530. process_lock_acquire_event(void *data,
  531. struct event *event __used,
  532. int cpu __used,
  533. u64 timestamp __used,
  534. struct thread *thread __used)
  535. {
  536. struct trace_acquire_event acquire_event;
  537. u64 tmp; /* this is required for casting... */
  538. tmp = raw_field_value(event, "lockdep_addr", data);
  539. memcpy(&acquire_event.addr, &tmp, sizeof(void *));
  540. acquire_event.name = (char *)raw_field_ptr(event, "name", data);
  541. acquire_event.flag = (int)raw_field_value(event, "flag", data);
  542. if (trace_handler->acquire_event)
  543. trace_handler->acquire_event(&acquire_event, event, cpu, timestamp, thread);
  544. }
  545. static void
  546. process_lock_acquired_event(void *data,
  547. struct event *event __used,
  548. int cpu __used,
  549. u64 timestamp __used,
  550. struct thread *thread __used)
  551. {
  552. struct trace_acquired_event acquired_event;
  553. u64 tmp; /* this is required for casting... */
  554. tmp = raw_field_value(event, "lockdep_addr", data);
  555. memcpy(&acquired_event.addr, &tmp, sizeof(void *));
  556. acquired_event.name = (char *)raw_field_ptr(event, "name", data);
  557. if (trace_handler->acquire_event)
  558. trace_handler->acquired_event(&acquired_event, event, cpu, timestamp, thread);
  559. }
  560. static void
  561. process_lock_contended_event(void *data,
  562. struct event *event __used,
  563. int cpu __used,
  564. u64 timestamp __used,
  565. struct thread *thread __used)
  566. {
  567. struct trace_contended_event contended_event;
  568. u64 tmp; /* this is required for casting... */
  569. tmp = raw_field_value(event, "lockdep_addr", data);
  570. memcpy(&contended_event.addr, &tmp, sizeof(void *));
  571. contended_event.name = (char *)raw_field_ptr(event, "name", data);
  572. if (trace_handler->acquire_event)
  573. trace_handler->contended_event(&contended_event, event, cpu, timestamp, thread);
  574. }
  575. static void
  576. process_lock_release_event(void *data,
  577. struct event *event __used,
  578. int cpu __used,
  579. u64 timestamp __used,
  580. struct thread *thread __used)
  581. {
  582. struct trace_release_event release_event;
  583. u64 tmp; /* this is required for casting... */
  584. tmp = raw_field_value(event, "lockdep_addr", data);
  585. memcpy(&release_event.addr, &tmp, sizeof(void *));
  586. release_event.name = (char *)raw_field_ptr(event, "name", data);
  587. if (trace_handler->acquire_event)
  588. trace_handler->release_event(&release_event, event, cpu, timestamp, thread);
  589. }
  590. static void
  591. process_raw_event(void *data, int cpu, u64 timestamp, struct thread *thread)
  592. {
  593. struct event *event;
  594. int type;
  595. type = trace_parse_common_type(data);
  596. event = trace_find_event(type);
  597. if (!strcmp(event->name, "lock_acquire"))
  598. process_lock_acquire_event(data, event, cpu, timestamp, thread);
  599. if (!strcmp(event->name, "lock_acquired"))
  600. process_lock_acquired_event(data, event, cpu, timestamp, thread);
  601. if (!strcmp(event->name, "lock_contended"))
  602. process_lock_contended_event(data, event, cpu, timestamp, thread);
  603. if (!strcmp(event->name, "lock_release"))
  604. process_lock_release_event(data, event, cpu, timestamp, thread);
  605. }
  606. static void print_bad_events(int bad, int total)
  607. {
  608. /* Output for debug, this have to be removed */
  609. int i;
  610. const char *name[4] =
  611. { "acquire", "acquired", "contended", "release" };
  612. pr_info("\n=== output for debug===\n\n");
  613. pr_info("bad: %d, total: %d\n", bad, total);
  614. pr_info("bad rate: %f %%\n", (double)bad / (double)total * 100);
  615. pr_info("histogram of events caused bad sequence\n");
  616. for (i = 0; i < BROKEN_MAX; i++)
  617. pr_info(" %10s: %d\n", name[i], bad_hist[i]);
  618. }
  619. /* TODO: various way to print, coloring, nano or milli sec */
  620. static void print_result(void)
  621. {
  622. struct lock_stat *st;
  623. char cut_name[20];
  624. int bad, total;
  625. pr_info("%20s ", "Name");
  626. pr_info("%10s ", "acquired");
  627. pr_info("%10s ", "contended");
  628. pr_info("%15s ", "total wait (ns)");
  629. pr_info("%15s ", "max wait (ns)");
  630. pr_info("%15s ", "min wait (ns)");
  631. pr_info("\n\n");
  632. bad = total = 0;
  633. while ((st = pop_from_result())) {
  634. total++;
  635. if (st->discard) {
  636. bad++;
  637. continue;
  638. }
  639. bzero(cut_name, 20);
  640. if (strlen(st->name) < 16) {
  641. /* output raw name */
  642. pr_info("%20s ", st->name);
  643. } else {
  644. strncpy(cut_name, st->name, 16);
  645. cut_name[16] = '.';
  646. cut_name[17] = '.';
  647. cut_name[18] = '.';
  648. cut_name[19] = '\0';
  649. /* cut off name for saving output style */
  650. pr_info("%20s ", cut_name);
  651. }
  652. pr_info("%10u ", st->nr_acquired);
  653. pr_info("%10u ", st->nr_contended);
  654. pr_info("%15llu ", st->wait_time_total);
  655. pr_info("%15llu ", st->wait_time_max);
  656. pr_info("%15llu ", st->wait_time_min == ULLONG_MAX ?
  657. 0 : st->wait_time_min);
  658. pr_info("\n");
  659. }
  660. print_bad_events(bad, total);
  661. }
  662. static bool info_threads, info_map;
  663. static void dump_threads(void)
  664. {
  665. struct thread_stat *st;
  666. struct rb_node *node;
  667. struct thread *t;
  668. pr_info("%10s: comm\n", "Thread ID");
  669. node = rb_first(&thread_stats);
  670. while (node) {
  671. st = container_of(node, struct thread_stat, rb);
  672. t = perf_session__findnew(session, st->tid);
  673. pr_info("%10d: %s\n", st->tid, t->comm);
  674. node = rb_next(node);
  675. };
  676. }
  677. static void dump_map(void)
  678. {
  679. unsigned int i;
  680. struct lock_stat *st;
  681. pr_info("Address of instance: name of class\n");
  682. for (i = 0; i < LOCKHASH_SIZE; i++) {
  683. list_for_each_entry(st, &lockhash_table[i], hash_entry) {
  684. pr_info(" %p: %s\n", st->addr, st->name);
  685. }
  686. }
  687. }
  688. static void dump_info(void)
  689. {
  690. if (info_threads)
  691. dump_threads();
  692. else if (info_map)
  693. dump_map();
  694. else
  695. die("Unknown type of information\n");
  696. }
  697. static int process_sample_event(event_t *self, struct perf_session *s)
  698. {
  699. struct sample_data data;
  700. struct thread *thread;
  701. bzero(&data, sizeof(data));
  702. event__parse_sample(self, s->sample_type, &data);
  703. thread = perf_session__findnew(s, data.tid);
  704. if (thread == NULL) {
  705. pr_debug("problem processing %d event, skipping it.\n",
  706. self->header.type);
  707. return -1;
  708. }
  709. process_raw_event(data.raw_data, data.cpu, data.time, thread);
  710. return 0;
  711. }
  712. static struct perf_event_ops eops = {
  713. .sample = process_sample_event,
  714. .comm = event__process_comm,
  715. .ordered_samples = true,
  716. };
  717. static int read_events(void)
  718. {
  719. session = perf_session__new(input_name, O_RDONLY, 0, false);
  720. if (!session)
  721. die("Initializing perf session failed\n");
  722. return perf_session__process_events(session, &eops);
  723. }
  724. static void sort_result(void)
  725. {
  726. unsigned int i;
  727. struct lock_stat *st;
  728. for (i = 0; i < LOCKHASH_SIZE; i++) {
  729. list_for_each_entry(st, &lockhash_table[i], hash_entry) {
  730. insert_to_result(st, compare);
  731. }
  732. }
  733. }
  734. static void __cmd_report(void)
  735. {
  736. setup_pager();
  737. select_key();
  738. read_events();
  739. sort_result();
  740. print_result();
  741. }
  742. static const char * const report_usage[] = {
  743. "perf lock report [<options>]",
  744. NULL
  745. };
  746. static const struct option report_options[] = {
  747. OPT_STRING('k', "key", &sort_key, "acquired",
  748. "key for sorting"),
  749. /* TODO: type */
  750. OPT_END()
  751. };
  752. static const char * const info_usage[] = {
  753. "perf lock info [<options>]",
  754. NULL
  755. };
  756. static const struct option info_options[] = {
  757. OPT_BOOLEAN('t', "threads", &info_threads,
  758. "dump thread list in perf.data"),
  759. OPT_BOOLEAN('m', "map", &info_map,
  760. "map of lock instances (name:address table)"),
  761. OPT_END()
  762. };
  763. static const char * const lock_usage[] = {
  764. "perf lock [<options>] {record|trace|report}",
  765. NULL
  766. };
  767. static const struct option lock_options[] = {
  768. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  769. OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
  770. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
  771. OPT_END()
  772. };
  773. static const char *record_args[] = {
  774. "record",
  775. "-R",
  776. "-f",
  777. "-m", "1024",
  778. "-c", "1",
  779. "-e", "lock:lock_acquire:r",
  780. "-e", "lock:lock_acquired:r",
  781. "-e", "lock:lock_contended:r",
  782. "-e", "lock:lock_release:r",
  783. };
  784. static int __cmd_record(int argc, const char **argv)
  785. {
  786. unsigned int rec_argc, i, j;
  787. const char **rec_argv;
  788. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  789. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  790. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  791. rec_argv[i] = strdup(record_args[i]);
  792. for (j = 1; j < (unsigned int)argc; j++, i++)
  793. rec_argv[i] = argv[j];
  794. BUG_ON(i != rec_argc);
  795. return cmd_record(i, rec_argv, NULL);
  796. }
  797. int cmd_lock(int argc, const char **argv, const char *prefix __used)
  798. {
  799. unsigned int i;
  800. symbol__init();
  801. for (i = 0; i < LOCKHASH_SIZE; i++)
  802. INIT_LIST_HEAD(lockhash_table + i);
  803. argc = parse_options(argc, argv, lock_options, lock_usage,
  804. PARSE_OPT_STOP_AT_NON_OPTION);
  805. if (!argc)
  806. usage_with_options(lock_usage, lock_options);
  807. if (!strncmp(argv[0], "rec", 3)) {
  808. return __cmd_record(argc, argv);
  809. } else if (!strncmp(argv[0], "report", 6)) {
  810. trace_handler = &report_lock_ops;
  811. if (argc) {
  812. argc = parse_options(argc, argv,
  813. report_options, report_usage, 0);
  814. if (argc)
  815. usage_with_options(report_usage, report_options);
  816. }
  817. __cmd_report();
  818. } else if (!strcmp(argv[0], "trace")) {
  819. /* Aliased to 'perf trace' */
  820. return cmd_trace(argc, argv, prefix);
  821. } else if (!strcmp(argv[0], "info")) {
  822. if (argc) {
  823. argc = parse_options(argc, argv,
  824. info_options, info_usage, 0);
  825. if (argc)
  826. usage_with_options(info_usage, info_options);
  827. }
  828. /* recycling report_lock_ops */
  829. trace_handler = &report_lock_ops;
  830. setup_pager();
  831. read_events();
  832. dump_info();
  833. } else {
  834. usage_with_options(lock_usage, lock_options);
  835. }
  836. return 0;
  837. }