builtin-lock.c 23 KB

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