builtin-lock.c 23 KB

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