builtin-lock.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. pr_err("memory allocation failed\n");
  135. return NULL;
  136. }
  137. st->tid = tid;
  138. INIT_LIST_HEAD(&st->seq_list);
  139. thread_stat_insert(st);
  140. return st;
  141. }
  142. static struct thread_stat *thread_stat_findnew_first(u32 tid);
  143. static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
  144. thread_stat_findnew_first;
  145. static struct thread_stat *thread_stat_findnew_first(u32 tid)
  146. {
  147. struct thread_stat *st;
  148. st = zalloc(sizeof(struct thread_stat));
  149. if (!st) {
  150. pr_err("memory allocation failed\n");
  151. return NULL;
  152. }
  153. st->tid = tid;
  154. INIT_LIST_HEAD(&st->seq_list);
  155. rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
  156. rb_insert_color(&st->rb, &thread_stats);
  157. thread_stat_findnew = thread_stat_findnew_after_first;
  158. return st;
  159. }
  160. /* build simple key function one is bigger than two */
  161. #define SINGLE_KEY(member) \
  162. static int lock_stat_key_ ## member(struct lock_stat *one, \
  163. struct lock_stat *two) \
  164. { \
  165. return one->member > two->member; \
  166. }
  167. SINGLE_KEY(nr_acquired)
  168. SINGLE_KEY(nr_contended)
  169. SINGLE_KEY(wait_time_total)
  170. SINGLE_KEY(wait_time_max)
  171. static int lock_stat_key_wait_time_min(struct lock_stat *one,
  172. struct lock_stat *two)
  173. {
  174. u64 s1 = one->wait_time_min;
  175. u64 s2 = two->wait_time_min;
  176. if (s1 == ULLONG_MAX)
  177. s1 = 0;
  178. if (s2 == ULLONG_MAX)
  179. s2 = 0;
  180. return s1 > s2;
  181. }
  182. struct lock_key {
  183. /*
  184. * name: the value for specify by user
  185. * this should be simpler than raw name of member
  186. * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
  187. */
  188. const char *name;
  189. int (*key)(struct lock_stat*, struct lock_stat*);
  190. };
  191. static const char *sort_key = "acquired";
  192. static int (*compare)(struct lock_stat *, struct lock_stat *);
  193. static struct rb_root result; /* place to store sorted data */
  194. #define DEF_KEY_LOCK(name, fn_suffix) \
  195. { #name, lock_stat_key_ ## fn_suffix }
  196. struct lock_key keys[] = {
  197. DEF_KEY_LOCK(acquired, nr_acquired),
  198. DEF_KEY_LOCK(contended, nr_contended),
  199. DEF_KEY_LOCK(wait_total, wait_time_total),
  200. DEF_KEY_LOCK(wait_min, wait_time_min),
  201. DEF_KEY_LOCK(wait_max, wait_time_max),
  202. /* extra comparisons much complicated should be here */
  203. { NULL, NULL }
  204. };
  205. static int select_key(void)
  206. {
  207. int i;
  208. for (i = 0; keys[i].name; i++) {
  209. if (!strcmp(keys[i].name, sort_key)) {
  210. compare = keys[i].key;
  211. return 0;
  212. }
  213. }
  214. pr_err("Unknown compare key: %s\n", sort_key);
  215. return -1;
  216. }
  217. static void insert_to_result(struct lock_stat *st,
  218. int (*bigger)(struct lock_stat *, struct lock_stat *))
  219. {
  220. struct rb_node **rb = &result.rb_node;
  221. struct rb_node *parent = NULL;
  222. struct lock_stat *p;
  223. while (*rb) {
  224. p = container_of(*rb, struct lock_stat, rb);
  225. parent = *rb;
  226. if (bigger(st, p))
  227. rb = &(*rb)->rb_left;
  228. else
  229. rb = &(*rb)->rb_right;
  230. }
  231. rb_link_node(&st->rb, parent, rb);
  232. rb_insert_color(&st->rb, &result);
  233. }
  234. /* returns left most element of result, and erase it */
  235. static struct lock_stat *pop_from_result(void)
  236. {
  237. struct rb_node *node = result.rb_node;
  238. if (!node)
  239. return NULL;
  240. while (node->rb_left)
  241. node = node->rb_left;
  242. rb_erase(node, &result);
  243. return container_of(node, struct lock_stat, rb);
  244. }
  245. static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
  246. {
  247. struct list_head *entry = lockhashentry(addr);
  248. struct lock_stat *ret, *new;
  249. list_for_each_entry(ret, entry, hash_entry) {
  250. if (ret->addr == addr)
  251. return ret;
  252. }
  253. new = zalloc(sizeof(struct lock_stat));
  254. if (!new)
  255. goto alloc_failed;
  256. new->addr = addr;
  257. new->name = zalloc(sizeof(char) * strlen(name) + 1);
  258. if (!new->name)
  259. goto alloc_failed;
  260. strcpy(new->name, name);
  261. new->wait_time_min = ULLONG_MAX;
  262. list_add(&new->hash_entry, entry);
  263. return new;
  264. alloc_failed:
  265. pr_err("memory allocation failed\n");
  266. return NULL;
  267. }
  268. static const char *input_name;
  269. struct raw_event_sample {
  270. u32 size;
  271. char data[0];
  272. };
  273. struct trace_acquire_event {
  274. void *addr;
  275. const char *name;
  276. int flag;
  277. };
  278. struct trace_acquired_event {
  279. void *addr;
  280. const char *name;
  281. };
  282. struct trace_contended_event {
  283. void *addr;
  284. const char *name;
  285. };
  286. struct trace_release_event {
  287. void *addr;
  288. const char *name;
  289. };
  290. struct trace_lock_handler {
  291. int (*acquire_event)(struct trace_acquire_event *,
  292. const struct perf_sample *sample);
  293. int (*acquired_event)(struct trace_acquired_event *,
  294. const struct perf_sample *sample);
  295. int (*contended_event)(struct trace_contended_event *,
  296. const struct perf_sample *sample);
  297. int (*release_event)(struct trace_release_event *,
  298. const struct perf_sample *sample);
  299. };
  300. static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
  301. {
  302. struct lock_seq_stat *seq;
  303. list_for_each_entry(seq, &ts->seq_list, list) {
  304. if (seq->addr == addr)
  305. return seq;
  306. }
  307. seq = zalloc(sizeof(struct lock_seq_stat));
  308. if (!seq) {
  309. pr_err("memory allocation failed\n");
  310. return NULL;
  311. }
  312. seq->state = SEQ_STATE_UNINITIALIZED;
  313. seq->addr = addr;
  314. list_add(&seq->list, &ts->seq_list);
  315. return seq;
  316. }
  317. enum broken_state {
  318. BROKEN_ACQUIRE,
  319. BROKEN_ACQUIRED,
  320. BROKEN_CONTENDED,
  321. BROKEN_RELEASE,
  322. BROKEN_MAX,
  323. };
  324. static int bad_hist[BROKEN_MAX];
  325. enum acquire_flags {
  326. TRY_LOCK = 1,
  327. READ_LOCK = 2,
  328. };
  329. static int
  330. report_lock_acquire_event(struct trace_acquire_event *acquire_event,
  331. const struct perf_sample *sample)
  332. {
  333. struct lock_stat *ls;
  334. struct thread_stat *ts;
  335. struct lock_seq_stat *seq;
  336. ls = lock_stat_findnew(acquire_event->addr, acquire_event->name);
  337. if (!ls)
  338. return -1;
  339. if (ls->discard)
  340. return 0;
  341. ts = thread_stat_findnew(sample->tid);
  342. if (!ts)
  343. return -1;
  344. seq = get_seq(ts, acquire_event->addr);
  345. if (!seq)
  346. return -1;
  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 = sample->time;
  388. end:
  389. return 0;
  390. }
  391. static int
  392. report_lock_acquired_event(struct trace_acquired_event *acquired_event,
  393. const struct perf_sample *sample)
  394. {
  395. u64 timestamp = sample->time;
  396. struct lock_stat *ls;
  397. struct thread_stat *ts;
  398. struct lock_seq_stat *seq;
  399. u64 contended_term;
  400. ls = lock_stat_findnew(acquired_event->addr, acquired_event->name);
  401. if (!ls)
  402. return -1;
  403. if (ls->discard)
  404. return 0;
  405. ts = thread_stat_findnew(sample->tid);
  406. if (!ts)
  407. return -1;
  408. seq = get_seq(ts, acquired_event->addr);
  409. if (!seq)
  410. return -1;
  411. switch (seq->state) {
  412. case SEQ_STATE_UNINITIALIZED:
  413. /* orphan event, do nothing */
  414. return 0;
  415. case SEQ_STATE_ACQUIRING:
  416. break;
  417. case SEQ_STATE_CONTENDED:
  418. contended_term = timestamp - seq->prev_event_time;
  419. ls->wait_time_total += contended_term;
  420. if (contended_term < ls->wait_time_min)
  421. ls->wait_time_min = contended_term;
  422. if (ls->wait_time_max < contended_term)
  423. ls->wait_time_max = contended_term;
  424. break;
  425. case SEQ_STATE_RELEASED:
  426. case SEQ_STATE_ACQUIRED:
  427. case SEQ_STATE_READ_ACQUIRED:
  428. /* broken lock sequence, discard it */
  429. ls->discard = 1;
  430. bad_hist[BROKEN_ACQUIRED]++;
  431. list_del(&seq->list);
  432. free(seq);
  433. goto end;
  434. break;
  435. default:
  436. BUG_ON("Unknown state of lock sequence found!\n");
  437. break;
  438. }
  439. seq->state = SEQ_STATE_ACQUIRED;
  440. ls->nr_acquired++;
  441. seq->prev_event_time = timestamp;
  442. end:
  443. return 0;
  444. }
  445. static int
  446. report_lock_contended_event(struct trace_contended_event *contended_event,
  447. const struct perf_sample *sample)
  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)
  454. return -1;
  455. if (ls->discard)
  456. return 0;
  457. ts = thread_stat_findnew(sample->tid);
  458. if (!ts)
  459. return -1;
  460. seq = get_seq(ts, contended_event->addr);
  461. if (!seq)
  462. return -1;
  463. switch (seq->state) {
  464. case SEQ_STATE_UNINITIALIZED:
  465. /* orphan event, do nothing */
  466. return 0;
  467. case SEQ_STATE_ACQUIRING:
  468. break;
  469. case SEQ_STATE_RELEASED:
  470. case SEQ_STATE_ACQUIRED:
  471. case SEQ_STATE_READ_ACQUIRED:
  472. case SEQ_STATE_CONTENDED:
  473. /* broken lock sequence, discard it */
  474. ls->discard = 1;
  475. bad_hist[BROKEN_CONTENDED]++;
  476. list_del(&seq->list);
  477. free(seq);
  478. goto end;
  479. break;
  480. default:
  481. BUG_ON("Unknown state of lock sequence found!\n");
  482. break;
  483. }
  484. seq->state = SEQ_STATE_CONTENDED;
  485. ls->nr_contended++;
  486. seq->prev_event_time = sample->time;
  487. end:
  488. return 0;
  489. }
  490. static int
  491. report_lock_release_event(struct trace_release_event *release_event,
  492. const struct perf_sample *sample)
  493. {
  494. struct lock_stat *ls;
  495. struct thread_stat *ts;
  496. struct lock_seq_stat *seq;
  497. ls = lock_stat_findnew(release_event->addr, release_event->name);
  498. if (!ls)
  499. return -1;
  500. if (ls->discard)
  501. return 0;
  502. ts = thread_stat_findnew(sample->tid);
  503. if (!ts)
  504. return -1;
  505. seq = get_seq(ts, release_event->addr);
  506. if (!seq)
  507. return -1;
  508. switch (seq->state) {
  509. case SEQ_STATE_UNINITIALIZED:
  510. goto end;
  511. break;
  512. case SEQ_STATE_ACQUIRED:
  513. break;
  514. case SEQ_STATE_READ_ACQUIRED:
  515. seq->read_count--;
  516. BUG_ON(seq->read_count < 0);
  517. if (!seq->read_count) {
  518. ls->nr_release++;
  519. goto end;
  520. }
  521. break;
  522. case SEQ_STATE_ACQUIRING:
  523. case SEQ_STATE_CONTENDED:
  524. case SEQ_STATE_RELEASED:
  525. /* broken lock sequence, discard it */
  526. ls->discard = 1;
  527. bad_hist[BROKEN_RELEASE]++;
  528. goto free_seq;
  529. break;
  530. default:
  531. BUG_ON("Unknown state of lock sequence found!\n");
  532. break;
  533. }
  534. ls->nr_release++;
  535. free_seq:
  536. list_del(&seq->list);
  537. free(seq);
  538. end:
  539. return 0;
  540. }
  541. /* lock oriented handlers */
  542. /* TODO: handlers for CPU oriented, thread oriented */
  543. static struct trace_lock_handler report_lock_ops = {
  544. .acquire_event = report_lock_acquire_event,
  545. .acquired_event = report_lock_acquired_event,
  546. .contended_event = report_lock_contended_event,
  547. .release_event = report_lock_release_event,
  548. };
  549. static struct trace_lock_handler *trace_handler;
  550. static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
  551. struct perf_sample *sample)
  552. {
  553. struct trace_acquire_event acquire_event;
  554. struct event_format *event = evsel->tp_format;
  555. void *data = sample->raw_data;
  556. u64 tmp; /* this is required for casting... */
  557. int rc = 0;
  558. tmp = raw_field_value(event, "lockdep_addr", data);
  559. memcpy(&acquire_event.addr, &tmp, sizeof(void *));
  560. acquire_event.name = (char *)raw_field_ptr(event, "name", data);
  561. acquire_event.flag = (int)raw_field_value(event, "flag", data);
  562. if (trace_handler->acquire_event)
  563. rc = trace_handler->acquire_event(&acquire_event, sample);
  564. return rc;
  565. }
  566. static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
  567. struct perf_sample *sample)
  568. {
  569. struct trace_acquired_event acquired_event;
  570. struct event_format *event = evsel->tp_format;
  571. void *data = sample->raw_data;
  572. u64 tmp; /* this is required for casting... */
  573. int rc = 0;
  574. tmp = raw_field_value(event, "lockdep_addr", data);
  575. memcpy(&acquired_event.addr, &tmp, sizeof(void *));
  576. acquired_event.name = (char *)raw_field_ptr(event, "name", data);
  577. if (trace_handler->acquired_event)
  578. rc = trace_handler->acquired_event(&acquired_event, sample);
  579. return rc;
  580. }
  581. static int perf_evsel__process_lock_contended(struct perf_evsel *evsel,
  582. struct perf_sample *sample)
  583. {
  584. struct trace_contended_event contended_event;
  585. struct event_format *event = evsel->tp_format;
  586. void *data = sample->raw_data;
  587. u64 tmp; /* this is required for casting... */
  588. int rc = 0;
  589. tmp = raw_field_value(event, "lockdep_addr", data);
  590. memcpy(&contended_event.addr, &tmp, sizeof(void *));
  591. contended_event.name = (char *)raw_field_ptr(event, "name", data);
  592. if (trace_handler->contended_event)
  593. rc = trace_handler->contended_event(&contended_event, sample);
  594. return rc;
  595. }
  596. static int perf_evsel__process_lock_release(struct perf_evsel *evsel,
  597. struct perf_sample *sample)
  598. {
  599. struct trace_release_event release_event;
  600. struct event_format *event = evsel->tp_format;
  601. void *data = sample->raw_data;
  602. u64 tmp; /* this is required for casting... */
  603. int rc = 0;
  604. tmp = raw_field_value(event, "lockdep_addr", data);
  605. memcpy(&release_event.addr, &tmp, sizeof(void *));
  606. release_event.name = (char *)raw_field_ptr(event, "name", data);
  607. if (trace_handler->release_event)
  608. rc = trace_handler->release_event(&release_event, sample);
  609. return rc;
  610. }
  611. static int perf_evsel__process_lock_event(struct perf_evsel *evsel,
  612. struct perf_sample *sample)
  613. {
  614. struct event_format *event = evsel->tp_format;
  615. int rc = 0;
  616. if (!strcmp(event->name, "lock_acquire"))
  617. rc = perf_evsel__process_lock_acquire(evsel, sample);
  618. if (!strcmp(event->name, "lock_acquired"))
  619. rc = perf_evsel__process_lock_acquired(evsel, sample);
  620. if (!strcmp(event->name, "lock_contended"))
  621. rc = perf_evsel__process_lock_contended(evsel, sample);
  622. if (!strcmp(event->name, "lock_release"))
  623. rc = perf_evsel__process_lock_release(evsel, sample);
  624. return rc;
  625. }
  626. static void print_bad_events(int bad, int total)
  627. {
  628. /* Output for debug, this have to be removed */
  629. int i;
  630. const char *name[4] =
  631. { "acquire", "acquired", "contended", "release" };
  632. pr_info("\n=== output for debug===\n\n");
  633. pr_info("bad: %d, total: %d\n", bad, total);
  634. pr_info("bad rate: %f %%\n", (double)bad / (double)total * 100);
  635. pr_info("histogram of events caused bad sequence\n");
  636. for (i = 0; i < BROKEN_MAX; i++)
  637. pr_info(" %10s: %d\n", name[i], bad_hist[i]);
  638. }
  639. /* TODO: various way to print, coloring, nano or milli sec */
  640. static void print_result(void)
  641. {
  642. struct lock_stat *st;
  643. char cut_name[20];
  644. int bad, total;
  645. pr_info("%20s ", "Name");
  646. pr_info("%10s ", "acquired");
  647. pr_info("%10s ", "contended");
  648. pr_info("%15s ", "total wait (ns)");
  649. pr_info("%15s ", "max wait (ns)");
  650. pr_info("%15s ", "min wait (ns)");
  651. pr_info("\n\n");
  652. bad = total = 0;
  653. while ((st = pop_from_result())) {
  654. total++;
  655. if (st->discard) {
  656. bad++;
  657. continue;
  658. }
  659. bzero(cut_name, 20);
  660. if (strlen(st->name) < 16) {
  661. /* output raw name */
  662. pr_info("%20s ", st->name);
  663. } else {
  664. strncpy(cut_name, st->name, 16);
  665. cut_name[16] = '.';
  666. cut_name[17] = '.';
  667. cut_name[18] = '.';
  668. cut_name[19] = '\0';
  669. /* cut off name for saving output style */
  670. pr_info("%20s ", cut_name);
  671. }
  672. pr_info("%10u ", st->nr_acquired);
  673. pr_info("%10u ", st->nr_contended);
  674. pr_info("%15" PRIu64 " ", st->wait_time_total);
  675. pr_info("%15" PRIu64 " ", st->wait_time_max);
  676. pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
  677. 0 : st->wait_time_min);
  678. pr_info("\n");
  679. }
  680. print_bad_events(bad, total);
  681. }
  682. static bool info_threads, info_map;
  683. static void dump_threads(void)
  684. {
  685. struct thread_stat *st;
  686. struct rb_node *node;
  687. struct thread *t;
  688. pr_info("%10s: comm\n", "Thread ID");
  689. node = rb_first(&thread_stats);
  690. while (node) {
  691. st = container_of(node, struct thread_stat, rb);
  692. t = perf_session__findnew(session, st->tid);
  693. pr_info("%10d: %s\n", st->tid, t->comm);
  694. node = rb_next(node);
  695. };
  696. }
  697. static void dump_map(void)
  698. {
  699. unsigned int i;
  700. struct lock_stat *st;
  701. pr_info("Address of instance: name of class\n");
  702. for (i = 0; i < LOCKHASH_SIZE; i++) {
  703. list_for_each_entry(st, &lockhash_table[i], hash_entry) {
  704. pr_info(" %p: %s\n", st->addr, st->name);
  705. }
  706. }
  707. }
  708. static int dump_info(void)
  709. {
  710. int rc = 0;
  711. if (info_threads)
  712. dump_threads();
  713. else if (info_map)
  714. dump_map();
  715. else {
  716. rc = -1;
  717. pr_err("Unknown type of information\n");
  718. }
  719. return rc;
  720. }
  721. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  722. union perf_event *event,
  723. struct perf_sample *sample,
  724. struct perf_evsel *evsel,
  725. struct machine *machine)
  726. {
  727. struct thread *thread = machine__findnew_thread(machine, sample->tid);
  728. if (thread == NULL) {
  729. pr_debug("problem processing %d event, skipping it.\n",
  730. event->header.type);
  731. return -1;
  732. }
  733. return perf_evsel__process_lock_event(evsel, sample);
  734. }
  735. static struct perf_tool eops = {
  736. .sample = process_sample_event,
  737. .comm = perf_event__process_comm,
  738. .ordered_samples = true,
  739. };
  740. static int read_events(void)
  741. {
  742. session = perf_session__new(input_name, O_RDONLY, 0, false, &eops);
  743. if (!session) {
  744. pr_err("Initializing perf session failed\n");
  745. return -1;
  746. }
  747. return perf_session__process_events(session, &eops);
  748. }
  749. static void sort_result(void)
  750. {
  751. unsigned int i;
  752. struct lock_stat *st;
  753. for (i = 0; i < LOCKHASH_SIZE; i++) {
  754. list_for_each_entry(st, &lockhash_table[i], hash_entry) {
  755. insert_to_result(st, compare);
  756. }
  757. }
  758. }
  759. static int __cmd_report(void)
  760. {
  761. setup_pager();
  762. if ((select_key() != 0) ||
  763. (read_events() != 0))
  764. return -1;
  765. sort_result();
  766. print_result();
  767. return 0;
  768. }
  769. static const char * const report_usage[] = {
  770. "perf lock report [<options>]",
  771. NULL
  772. };
  773. static const struct option report_options[] = {
  774. OPT_STRING('k', "key", &sort_key, "acquired",
  775. "key for sorting (acquired / contended / wait_total / wait_max / wait_min)"),
  776. /* TODO: type */
  777. OPT_END()
  778. };
  779. static const char * const info_usage[] = {
  780. "perf lock info [<options>]",
  781. NULL
  782. };
  783. static const struct option info_options[] = {
  784. OPT_BOOLEAN('t', "threads", &info_threads,
  785. "dump thread list in perf.data"),
  786. OPT_BOOLEAN('m', "map", &info_map,
  787. "map of lock instances (address:name table)"),
  788. OPT_END()
  789. };
  790. static const char * const lock_usage[] = {
  791. "perf lock [<options>] {record|report|script|info}",
  792. NULL
  793. };
  794. static const struct option lock_options[] = {
  795. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  796. OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
  797. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
  798. OPT_END()
  799. };
  800. static const char * const lock_tracepoints[] = {
  801. "lock:lock_acquire", /* CONFIG_LOCKDEP */
  802. "lock:lock_acquired", /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
  803. "lock:lock_contended", /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
  804. "lock:lock_release", /* CONFIG_LOCKDEP */
  805. };
  806. static const char *record_args[] = {
  807. "record",
  808. "-R",
  809. "-f",
  810. "-m", "1024",
  811. "-c", "1",
  812. };
  813. static int __cmd_record(int argc, const char **argv)
  814. {
  815. unsigned int rec_argc, i, j;
  816. const char **rec_argv;
  817. for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
  818. if (!is_valid_tracepoint(lock_tracepoints[i])) {
  819. pr_err("tracepoint %s is not enabled. "
  820. "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
  821. lock_tracepoints[i]);
  822. return 1;
  823. }
  824. }
  825. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  826. /* factor of 2 is for -e in front of each tracepoint */
  827. rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
  828. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  829. if (rec_argv == NULL)
  830. return -ENOMEM;
  831. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  832. rec_argv[i] = strdup(record_args[i]);
  833. for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
  834. rec_argv[i++] = "-e";
  835. rec_argv[i++] = strdup(lock_tracepoints[j]);
  836. }
  837. for (j = 1; j < (unsigned int)argc; j++, i++)
  838. rec_argv[i] = argv[j];
  839. BUG_ON(i != rec_argc);
  840. return cmd_record(i, rec_argv, NULL);
  841. }
  842. int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
  843. {
  844. unsigned int i;
  845. int rc = 0;
  846. symbol__init();
  847. for (i = 0; i < LOCKHASH_SIZE; i++)
  848. INIT_LIST_HEAD(lockhash_table + i);
  849. argc = parse_options(argc, argv, lock_options, lock_usage,
  850. PARSE_OPT_STOP_AT_NON_OPTION);
  851. if (!argc)
  852. usage_with_options(lock_usage, lock_options);
  853. if (!strncmp(argv[0], "rec", 3)) {
  854. return __cmd_record(argc, argv);
  855. } else if (!strncmp(argv[0], "report", 6)) {
  856. trace_handler = &report_lock_ops;
  857. if (argc) {
  858. argc = parse_options(argc, argv,
  859. report_options, report_usage, 0);
  860. if (argc)
  861. usage_with_options(report_usage, report_options);
  862. }
  863. __cmd_report();
  864. } else if (!strcmp(argv[0], "script")) {
  865. /* Aliased to 'perf script' */
  866. return cmd_script(argc, argv, prefix);
  867. } else if (!strcmp(argv[0], "info")) {
  868. if (argc) {
  869. argc = parse_options(argc, argv,
  870. info_options, info_usage, 0);
  871. if (argc)
  872. usage_with_options(info_usage, info_options);
  873. }
  874. /* recycling report_lock_ops */
  875. trace_handler = &report_lock_ops;
  876. setup_pager();
  877. if (read_events() != 0)
  878. rc = -1;
  879. else
  880. rc = dump_info();
  881. } else {
  882. usage_with_options(lock_usage, lock_options);
  883. }
  884. return rc;
  885. }