builtin-lock.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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("%15" PRIu64 " ", st->wait_time_total);
  655. pr_info("%15" PRIu64 " ", st->wait_time_max);
  656. pr_info("%15" PRIu64 " ", 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(union perf_event *event, struct perf_sample *sample,
  698. struct perf_session *s)
  699. {
  700. struct thread *thread = perf_session__findnew(s, sample->tid);
  701. if (thread == NULL) {
  702. pr_debug("problem processing %d event, skipping it.\n",
  703. event->header.type);
  704. return -1;
  705. }
  706. process_raw_event(sample->raw_data, sample->cpu, sample->time, thread);
  707. return 0;
  708. }
  709. static struct perf_event_ops eops = {
  710. .sample = process_sample_event,
  711. .comm = perf_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, &eops);
  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 (acquired / contended / wait_total / wait_max / wait_min)"),
  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. "-R",
  773. "-f",
  774. "-m", "1024",
  775. "-c", "1",
  776. "-e", "lock:lock_acquire:r",
  777. "-e", "lock:lock_acquired:r",
  778. "-e", "lock:lock_contended:r",
  779. "-e", "lock:lock_release:r",
  780. };
  781. static int __cmd_record(int argc, const char **argv)
  782. {
  783. unsigned int rec_argc, i, j;
  784. const char **rec_argv;
  785. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  786. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  787. if (rec_argv == NULL)
  788. return -ENOMEM;
  789. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  790. rec_argv[i] = strdup(record_args[i]);
  791. for (j = 1; j < (unsigned int)argc; j++, i++)
  792. rec_argv[i] = argv[j];
  793. BUG_ON(i != rec_argc);
  794. return cmd_record(i, rec_argv, NULL);
  795. }
  796. int cmd_lock(int argc, const char **argv, const char *prefix __used)
  797. {
  798. unsigned int i;
  799. symbol__init();
  800. for (i = 0; i < LOCKHASH_SIZE; i++)
  801. INIT_LIST_HEAD(lockhash_table + i);
  802. argc = parse_options(argc, argv, lock_options, lock_usage,
  803. PARSE_OPT_STOP_AT_NON_OPTION);
  804. if (!argc)
  805. usage_with_options(lock_usage, lock_options);
  806. if (!strncmp(argv[0], "rec", 3)) {
  807. return __cmd_record(argc, argv);
  808. } else if (!strncmp(argv[0], "report", 6)) {
  809. trace_handler = &report_lock_ops;
  810. if (argc) {
  811. argc = parse_options(argc, argv,
  812. report_options, report_usage, 0);
  813. if (argc)
  814. usage_with_options(report_usage, report_options);
  815. }
  816. __cmd_report();
  817. } else if (!strcmp(argv[0], "script")) {
  818. /* Aliased to 'perf script' */
  819. return cmd_script(argc, argv, prefix);
  820. } else if (!strcmp(argv[0], "info")) {
  821. if (argc) {
  822. argc = parse_options(argc, argv,
  823. info_options, info_usage, 0);
  824. if (argc)
  825. usage_with_options(info_usage, info_options);
  826. }
  827. /* recycling report_lock_ops */
  828. trace_handler = &report_lock_ops;
  829. setup_pager();
  830. read_events();
  831. dump_info();
  832. } else {
  833. usage_with_options(lock_usage, lock_options);
  834. }
  835. return 0;
  836. }