builtin-lock.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include "builtin.h"
  5. #include "perf.h"
  6. #include "util/evlist.h" // for struct evsel_str_handler
  7. #include "util/evsel.h"
  8. #include "util/symbol.h"
  9. #include "util/thread.h"
  10. #include "util/header.h"
  11. #include "util/target.h"
  12. #include "util/cgroup.h"
  13. #include "util/callchain.h"
  14. #include "util/lock-contention.h"
  15. #include "util/bpf_skel/lock_data.h"
  16. #include <subcmd/pager.h>
  17. #include <subcmd/parse-options.h>
  18. #include "util/trace-event.h"
  19. #include "util/tracepoint.h"
  20. #include "util/debug.h"
  21. #include "util/session.h"
  22. #include "util/tool.h"
  23. #include "util/data.h"
  24. #include "util/string2.h"
  25. #include "util/map.h"
  26. #include "util/util.h"
  27. #include <stdio.h>
  28. #include <sys/types.h>
  29. #include <sys/prctl.h>
  30. #include <semaphore.h>
  31. #include <math.h>
  32. #include <limits.h>
  33. #include <ctype.h>
  34. #include <linux/list.h>
  35. #include <linux/hash.h>
  36. #include <linux/kernel.h>
  37. #include <linux/zalloc.h>
  38. #include <linux/err.h>
  39. #include <linux/stringify.h>
  40. static struct perf_session *session;
  41. static struct target target;
  42. static struct rb_root thread_stats;
  43. static bool combine_locks;
  44. static bool show_thread_stats;
  45. static bool show_lock_addrs;
  46. static bool show_lock_owner;
  47. static bool show_lock_cgroups;
  48. static bool use_bpf;
  49. static unsigned long bpf_map_entries = MAX_ENTRIES;
  50. static int max_stack_depth = CONTENTION_STACK_DEPTH;
  51. static int stack_skip = CONTENTION_STACK_SKIP;
  52. static int print_nr_entries = INT_MAX / 2;
  53. static const char *output_name = NULL;
  54. static FILE *lock_output;
  55. static struct lock_filter filters;
  56. static struct lock_delay *delays;
  57. static int nr_delays;
  58. static enum lock_aggr_mode aggr_mode = LOCK_AGGR_ADDR;
  59. static struct thread_stat *thread_stat_find(u32 tid)
  60. {
  61. struct rb_node *node;
  62. struct thread_stat *st;
  63. node = thread_stats.rb_node;
  64. while (node) {
  65. st = container_of(node, struct thread_stat, rb);
  66. if (st->tid == tid)
  67. return st;
  68. else if (tid < st->tid)
  69. node = node->rb_left;
  70. else
  71. node = node->rb_right;
  72. }
  73. return NULL;
  74. }
  75. static void thread_stat_insert(struct thread_stat *new)
  76. {
  77. struct rb_node **rb = &thread_stats.rb_node;
  78. struct rb_node *parent = NULL;
  79. struct thread_stat *p;
  80. while (*rb) {
  81. p = container_of(*rb, struct thread_stat, rb);
  82. parent = *rb;
  83. if (new->tid < p->tid)
  84. rb = &(*rb)->rb_left;
  85. else if (new->tid > p->tid)
  86. rb = &(*rb)->rb_right;
  87. else
  88. BUG_ON("inserting invalid thread_stat\n");
  89. }
  90. rb_link_node(&new->rb, parent, rb);
  91. rb_insert_color(&new->rb, &thread_stats);
  92. }
  93. static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
  94. {
  95. struct thread_stat *st;
  96. st = thread_stat_find(tid);
  97. if (st)
  98. return st;
  99. st = zalloc(sizeof(struct thread_stat));
  100. if (!st) {
  101. pr_err("memory allocation failed\n");
  102. return NULL;
  103. }
  104. st->tid = tid;
  105. INIT_LIST_HEAD(&st->seq_list);
  106. thread_stat_insert(st);
  107. return st;
  108. }
  109. static struct thread_stat *thread_stat_findnew_first(u32 tid);
  110. static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
  111. thread_stat_findnew_first;
  112. static struct thread_stat *thread_stat_findnew_first(u32 tid)
  113. {
  114. struct thread_stat *st;
  115. st = zalloc(sizeof(struct thread_stat));
  116. if (!st) {
  117. pr_err("memory allocation failed\n");
  118. return NULL;
  119. }
  120. st->tid = tid;
  121. INIT_LIST_HEAD(&st->seq_list);
  122. rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
  123. rb_insert_color(&st->rb, &thread_stats);
  124. thread_stat_findnew = thread_stat_findnew_after_first;
  125. return st;
  126. }
  127. /* build simple key function one is bigger than two */
  128. #define SINGLE_KEY(member) \
  129. static int lock_stat_key_ ## member(struct lock_stat *one, \
  130. struct lock_stat *two) \
  131. { \
  132. return one->member > two->member; \
  133. }
  134. SINGLE_KEY(nr_acquired)
  135. SINGLE_KEY(nr_contended)
  136. SINGLE_KEY(avg_wait_time)
  137. SINGLE_KEY(wait_time_total)
  138. SINGLE_KEY(wait_time_max)
  139. static int lock_stat_key_wait_time_min(struct lock_stat *one,
  140. struct lock_stat *two)
  141. {
  142. u64 s1 = one->wait_time_min;
  143. u64 s2 = two->wait_time_min;
  144. if (s1 == ULLONG_MAX)
  145. s1 = 0;
  146. if (s2 == ULLONG_MAX)
  147. s2 = 0;
  148. return s1 > s2;
  149. }
  150. struct lock_key {
  151. /*
  152. * name: the value for specify by user
  153. * this should be simpler than raw name of member
  154. * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
  155. */
  156. const char *name;
  157. /* header: the string printed on the header line */
  158. const char *header;
  159. /* len: the printing width of the field */
  160. int len;
  161. /* key: a pointer to function to compare two lock stats for sorting */
  162. int (*key)(struct lock_stat*, struct lock_stat*);
  163. /* print: a pointer to function to print a given lock stats */
  164. void (*print)(struct lock_key*, struct lock_stat*);
  165. /* list: list entry to link this */
  166. struct list_head list;
  167. };
  168. static void lock_stat_key_print_time(unsigned long long nsec, int len)
  169. {
  170. static const struct {
  171. float base;
  172. const char *unit;
  173. } table[] = {
  174. { 1e9 * 3600, "h " },
  175. { 1e9 * 60, "m " },
  176. { 1e9, "s " },
  177. { 1e6, "ms" },
  178. { 1e3, "us" },
  179. { 0, NULL },
  180. };
  181. /* for CSV output */
  182. if (len == 0) {
  183. fprintf(lock_output, "%llu", nsec);
  184. return;
  185. }
  186. for (int i = 0; table[i].unit; i++) {
  187. if (nsec < table[i].base)
  188. continue;
  189. fprintf(lock_output, "%*.2f %s", len - 3, nsec / table[i].base, table[i].unit);
  190. return;
  191. }
  192. fprintf(lock_output, "%*llu %s", len - 3, nsec, "ns");
  193. }
  194. #define PRINT_KEY(member) \
  195. static void lock_stat_key_print_ ## member(struct lock_key *key, \
  196. struct lock_stat *ls) \
  197. { \
  198. fprintf(lock_output, "%*llu", key->len, (unsigned long long)ls->member);\
  199. }
  200. #define PRINT_TIME(member) \
  201. static void lock_stat_key_print_ ## member(struct lock_key *key, \
  202. struct lock_stat *ls) \
  203. { \
  204. lock_stat_key_print_time((unsigned long long)ls->member, key->len); \
  205. }
  206. PRINT_KEY(nr_acquired)
  207. PRINT_KEY(nr_contended)
  208. PRINT_TIME(avg_wait_time)
  209. PRINT_TIME(wait_time_total)
  210. PRINT_TIME(wait_time_max)
  211. static void lock_stat_key_print_wait_time_min(struct lock_key *key,
  212. struct lock_stat *ls)
  213. {
  214. u64 wait_time = ls->wait_time_min;
  215. if (wait_time == ULLONG_MAX)
  216. wait_time = 0;
  217. lock_stat_key_print_time(wait_time, key->len);
  218. }
  219. static const char *sort_key = "acquired";
  220. static int (*compare)(struct lock_stat *, struct lock_stat *);
  221. static struct rb_root sorted; /* place to store intermediate data */
  222. static struct rb_root result; /* place to store sorted data */
  223. static LIST_HEAD(lock_keys);
  224. static const char *output_fields;
  225. #define DEF_KEY_LOCK(name, header, fn_suffix, len) \
  226. { #name, header, len, lock_stat_key_ ## fn_suffix, lock_stat_key_print_ ## fn_suffix, {} }
  227. static struct lock_key report_keys[] = {
  228. DEF_KEY_LOCK(acquired, "acquired", nr_acquired, 10),
  229. DEF_KEY_LOCK(contended, "contended", nr_contended, 10),
  230. DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12),
  231. DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12),
  232. DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12),
  233. DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12),
  234. /* extra comparisons much complicated should be here */
  235. { }
  236. };
  237. static struct lock_key contention_keys[] = {
  238. DEF_KEY_LOCK(contended, "contended", nr_contended, 10),
  239. DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12),
  240. DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12),
  241. DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12),
  242. DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12),
  243. /* extra comparisons much complicated should be here */
  244. { }
  245. };
  246. static int select_key(bool contention)
  247. {
  248. int i;
  249. struct lock_key *keys = report_keys;
  250. if (contention)
  251. keys = contention_keys;
  252. for (i = 0; keys[i].name; i++) {
  253. if (!strcmp(keys[i].name, sort_key)) {
  254. compare = keys[i].key;
  255. /* selected key should be in the output fields */
  256. if (list_empty(&keys[i].list))
  257. list_add_tail(&keys[i].list, &lock_keys);
  258. return 0;
  259. }
  260. }
  261. pr_err("Unknown compare key: %s\n", sort_key);
  262. return -1;
  263. }
  264. static int add_output_field(bool contention, char *name)
  265. {
  266. int i;
  267. struct lock_key *keys = report_keys;
  268. if (contention)
  269. keys = contention_keys;
  270. for (i = 0; keys[i].name; i++) {
  271. if (strcmp(keys[i].name, name))
  272. continue;
  273. /* prevent double link */
  274. if (list_empty(&keys[i].list))
  275. list_add_tail(&keys[i].list, &lock_keys);
  276. return 0;
  277. }
  278. pr_err("Unknown output field: %s\n", name);
  279. return -1;
  280. }
  281. static int setup_output_field(bool contention, const char *str)
  282. {
  283. char *tok, *tmp, *orig;
  284. int i, ret = 0;
  285. struct lock_key *keys = report_keys;
  286. if (contention)
  287. keys = contention_keys;
  288. /* no output field given: use all of them */
  289. if (str == NULL) {
  290. for (i = 0; keys[i].name; i++)
  291. list_add_tail(&keys[i].list, &lock_keys);
  292. return 0;
  293. }
  294. for (i = 0; keys[i].name; i++)
  295. INIT_LIST_HEAD(&keys[i].list);
  296. orig = tmp = strdup(str);
  297. if (orig == NULL)
  298. return -ENOMEM;
  299. while ((tok = strsep(&tmp, ",")) != NULL){
  300. ret = add_output_field(contention, tok);
  301. if (ret < 0)
  302. break;
  303. }
  304. free(orig);
  305. return ret;
  306. }
  307. static void combine_lock_stats(struct lock_stat *st)
  308. {
  309. struct rb_node **rb = &sorted.rb_node;
  310. struct rb_node *parent = NULL;
  311. struct lock_stat *p;
  312. int ret;
  313. while (*rb) {
  314. p = container_of(*rb, struct lock_stat, rb);
  315. parent = *rb;
  316. if (st->name && p->name)
  317. ret = strcmp(st->name, p->name);
  318. else
  319. ret = !!st->name - !!p->name;
  320. if (ret == 0) {
  321. p->nr_acquired += st->nr_acquired;
  322. p->nr_contended += st->nr_contended;
  323. p->wait_time_total += st->wait_time_total;
  324. if (p->nr_contended)
  325. p->avg_wait_time = p->wait_time_total / p->nr_contended;
  326. if (p->wait_time_min > st->wait_time_min)
  327. p->wait_time_min = st->wait_time_min;
  328. if (p->wait_time_max < st->wait_time_max)
  329. p->wait_time_max = st->wait_time_max;
  330. p->broken |= st->broken;
  331. st->combined = 1;
  332. return;
  333. }
  334. if (ret < 0)
  335. rb = &(*rb)->rb_left;
  336. else
  337. rb = &(*rb)->rb_right;
  338. }
  339. rb_link_node(&st->rb, parent, rb);
  340. rb_insert_color(&st->rb, &sorted);
  341. }
  342. static void insert_to(struct rb_root *rr, struct lock_stat *st,
  343. int (*bigger)(struct lock_stat *, struct lock_stat *))
  344. {
  345. struct rb_node **rb = &rr->rb_node;
  346. struct rb_node *parent = NULL;
  347. struct lock_stat *p;
  348. while (*rb) {
  349. p = container_of(*rb, struct lock_stat, rb);
  350. parent = *rb;
  351. if (bigger(st, p))
  352. rb = &(*rb)->rb_left;
  353. else
  354. rb = &(*rb)->rb_right;
  355. }
  356. rb_link_node(&st->rb, parent, rb);
  357. rb_insert_color(&st->rb, rr);
  358. }
  359. static inline void insert_to_result(struct lock_stat *st,
  360. int (*bigger)(struct lock_stat *,
  361. struct lock_stat *))
  362. {
  363. if (combine_locks && st->combined)
  364. return;
  365. insert_to(&result, st, bigger);
  366. }
  367. static inline struct lock_stat *pop_from(struct rb_root *rr)
  368. {
  369. struct rb_node *node = rr->rb_node;
  370. if (!node)
  371. return NULL;
  372. while (node->rb_left)
  373. node = node->rb_left;
  374. rb_erase(node, rr);
  375. return container_of(node, struct lock_stat, rb);
  376. }
  377. /* returns left most element of result, and erase it */
  378. static struct lock_stat *pop_from_result(void)
  379. {
  380. return pop_from(&result);
  381. }
  382. struct trace_lock_handler {
  383. /* it's used on CONFIG_LOCKDEP */
  384. int (*acquire_event)(struct evsel *evsel,
  385. struct perf_sample *sample);
  386. /* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */
  387. int (*acquired_event)(struct evsel *evsel,
  388. struct perf_sample *sample);
  389. /* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */
  390. int (*contended_event)(struct evsel *evsel,
  391. struct perf_sample *sample);
  392. /* it's used on CONFIG_LOCKDEP */
  393. int (*release_event)(struct evsel *evsel,
  394. struct perf_sample *sample);
  395. /* it's used when CONFIG_LOCKDEP is off */
  396. int (*contention_begin_event)(struct evsel *evsel,
  397. struct perf_sample *sample);
  398. /* it's used when CONFIG_LOCKDEP is off */
  399. int (*contention_end_event)(struct evsel *evsel,
  400. struct perf_sample *sample);
  401. };
  402. static struct lock_seq_stat *get_seq(struct thread_stat *ts, u64 addr)
  403. {
  404. struct lock_seq_stat *seq;
  405. list_for_each_entry(seq, &ts->seq_list, list) {
  406. if (seq->addr == addr)
  407. return seq;
  408. }
  409. seq = zalloc(sizeof(struct lock_seq_stat));
  410. if (!seq) {
  411. pr_err("memory allocation failed\n");
  412. return NULL;
  413. }
  414. seq->state = SEQ_STATE_UNINITIALIZED;
  415. seq->addr = addr;
  416. list_add(&seq->list, &ts->seq_list);
  417. return seq;
  418. }
  419. enum broken_state {
  420. BROKEN_ACQUIRE,
  421. BROKEN_ACQUIRED,
  422. BROKEN_CONTENDED,
  423. BROKEN_RELEASE,
  424. BROKEN_MAX,
  425. };
  426. static int bad_hist[BROKEN_MAX];
  427. enum acquire_flags {
  428. TRY_LOCK = 1,
  429. READ_LOCK = 2,
  430. };
  431. static int get_key_by_aggr_mode_simple(u64 *key, u64 addr, u32 tid)
  432. {
  433. switch (aggr_mode) {
  434. case LOCK_AGGR_ADDR:
  435. *key = addr;
  436. break;
  437. case LOCK_AGGR_TASK:
  438. *key = tid;
  439. break;
  440. case LOCK_AGGR_CALLER:
  441. case LOCK_AGGR_CGROUP:
  442. default:
  443. pr_err("Invalid aggregation mode: %d\n", aggr_mode);
  444. return -EINVAL;
  445. }
  446. return 0;
  447. }
  448. static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample);
  449. static int get_key_by_aggr_mode(u64 *key, u64 addr, struct evsel *evsel,
  450. struct perf_sample *sample)
  451. {
  452. if (aggr_mode == LOCK_AGGR_CALLER) {
  453. *key = callchain_id(evsel, sample);
  454. return 0;
  455. }
  456. return get_key_by_aggr_mode_simple(key, addr, sample->tid);
  457. }
  458. static int report_lock_acquire_event(struct evsel *evsel,
  459. struct perf_sample *sample)
  460. {
  461. struct lock_stat *ls;
  462. struct thread_stat *ts;
  463. struct lock_seq_stat *seq;
  464. const char *name = evsel__strval(evsel, sample, "name");
  465. u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
  466. int flag = evsel__intval(evsel, sample, "flags");
  467. u64 key;
  468. int ret;
  469. ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
  470. if (ret < 0)
  471. return ret;
  472. ls = lock_stat_findnew(key, name, 0);
  473. if (!ls)
  474. return -ENOMEM;
  475. ts = thread_stat_findnew(sample->tid);
  476. if (!ts)
  477. return -ENOMEM;
  478. seq = get_seq(ts, addr);
  479. if (!seq)
  480. return -ENOMEM;
  481. switch (seq->state) {
  482. case SEQ_STATE_UNINITIALIZED:
  483. case SEQ_STATE_RELEASED:
  484. if (!flag) {
  485. seq->state = SEQ_STATE_ACQUIRING;
  486. } else {
  487. if (flag & TRY_LOCK)
  488. ls->nr_trylock++;
  489. if (flag & READ_LOCK)
  490. ls->nr_readlock++;
  491. seq->state = SEQ_STATE_READ_ACQUIRED;
  492. seq->read_count = 1;
  493. ls->nr_acquired++;
  494. }
  495. break;
  496. case SEQ_STATE_READ_ACQUIRED:
  497. if (flag & READ_LOCK) {
  498. seq->read_count++;
  499. ls->nr_acquired++;
  500. goto end;
  501. } else {
  502. goto broken;
  503. }
  504. break;
  505. case SEQ_STATE_ACQUIRED:
  506. case SEQ_STATE_ACQUIRING:
  507. case SEQ_STATE_CONTENDED:
  508. broken:
  509. /* broken lock sequence */
  510. if (!ls->broken) {
  511. ls->broken = 1;
  512. bad_hist[BROKEN_ACQUIRE]++;
  513. }
  514. list_del_init(&seq->list);
  515. free(seq);
  516. goto end;
  517. default:
  518. BUG_ON("Unknown state of lock sequence found!\n");
  519. break;
  520. }
  521. ls->nr_acquire++;
  522. seq->prev_event_time = sample->time;
  523. end:
  524. return 0;
  525. }
  526. static int report_lock_acquired_event(struct evsel *evsel,
  527. struct perf_sample *sample)
  528. {
  529. struct lock_stat *ls;
  530. struct thread_stat *ts;
  531. struct lock_seq_stat *seq;
  532. u64 contended_term;
  533. const char *name = evsel__strval(evsel, sample, "name");
  534. u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
  535. u64 key;
  536. int ret;
  537. ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
  538. if (ret < 0)
  539. return ret;
  540. ls = lock_stat_findnew(key, name, 0);
  541. if (!ls)
  542. return -ENOMEM;
  543. ts = thread_stat_findnew(sample->tid);
  544. if (!ts)
  545. return -ENOMEM;
  546. seq = get_seq(ts, addr);
  547. if (!seq)
  548. return -ENOMEM;
  549. switch (seq->state) {
  550. case SEQ_STATE_UNINITIALIZED:
  551. /* orphan event, do nothing */
  552. return 0;
  553. case SEQ_STATE_ACQUIRING:
  554. break;
  555. case SEQ_STATE_CONTENDED:
  556. contended_term = sample->time - seq->prev_event_time;
  557. ls->wait_time_total += contended_term;
  558. if (contended_term < ls->wait_time_min)
  559. ls->wait_time_min = contended_term;
  560. if (ls->wait_time_max < contended_term)
  561. ls->wait_time_max = contended_term;
  562. break;
  563. case SEQ_STATE_RELEASED:
  564. case SEQ_STATE_ACQUIRED:
  565. case SEQ_STATE_READ_ACQUIRED:
  566. /* broken lock sequence */
  567. if (!ls->broken) {
  568. ls->broken = 1;
  569. bad_hist[BROKEN_ACQUIRED]++;
  570. }
  571. list_del_init(&seq->list);
  572. free(seq);
  573. goto end;
  574. default:
  575. BUG_ON("Unknown state of lock sequence found!\n");
  576. break;
  577. }
  578. seq->state = SEQ_STATE_ACQUIRED;
  579. ls->nr_acquired++;
  580. ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
  581. seq->prev_event_time = sample->time;
  582. end:
  583. return 0;
  584. }
  585. static int report_lock_contended_event(struct evsel *evsel,
  586. struct perf_sample *sample)
  587. {
  588. struct lock_stat *ls;
  589. struct thread_stat *ts;
  590. struct lock_seq_stat *seq;
  591. const char *name = evsel__strval(evsel, sample, "name");
  592. u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
  593. u64 key;
  594. int ret;
  595. ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
  596. if (ret < 0)
  597. return ret;
  598. ls = lock_stat_findnew(key, name, 0);
  599. if (!ls)
  600. return -ENOMEM;
  601. ts = thread_stat_findnew(sample->tid);
  602. if (!ts)
  603. return -ENOMEM;
  604. seq = get_seq(ts, addr);
  605. if (!seq)
  606. return -ENOMEM;
  607. switch (seq->state) {
  608. case SEQ_STATE_UNINITIALIZED:
  609. /* orphan event, do nothing */
  610. return 0;
  611. case SEQ_STATE_ACQUIRING:
  612. break;
  613. case SEQ_STATE_RELEASED:
  614. case SEQ_STATE_ACQUIRED:
  615. case SEQ_STATE_READ_ACQUIRED:
  616. case SEQ_STATE_CONTENDED:
  617. /* broken lock sequence */
  618. if (!ls->broken) {
  619. ls->broken = 1;
  620. bad_hist[BROKEN_CONTENDED]++;
  621. }
  622. list_del_init(&seq->list);
  623. free(seq);
  624. goto end;
  625. default:
  626. BUG_ON("Unknown state of lock sequence found!\n");
  627. break;
  628. }
  629. seq->state = SEQ_STATE_CONTENDED;
  630. ls->nr_contended++;
  631. ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
  632. seq->prev_event_time = sample->time;
  633. end:
  634. return 0;
  635. }
  636. static int report_lock_release_event(struct evsel *evsel,
  637. struct perf_sample *sample)
  638. {
  639. struct lock_stat *ls;
  640. struct thread_stat *ts;
  641. struct lock_seq_stat *seq;
  642. const char *name = evsel__strval(evsel, sample, "name");
  643. u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
  644. u64 key;
  645. int ret;
  646. ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
  647. if (ret < 0)
  648. return ret;
  649. ls = lock_stat_findnew(key, name, 0);
  650. if (!ls)
  651. return -ENOMEM;
  652. ts = thread_stat_findnew(sample->tid);
  653. if (!ts)
  654. return -ENOMEM;
  655. seq = get_seq(ts, addr);
  656. if (!seq)
  657. return -ENOMEM;
  658. switch (seq->state) {
  659. case SEQ_STATE_UNINITIALIZED:
  660. goto end;
  661. case SEQ_STATE_ACQUIRED:
  662. break;
  663. case SEQ_STATE_READ_ACQUIRED:
  664. seq->read_count--;
  665. BUG_ON(seq->read_count < 0);
  666. if (seq->read_count) {
  667. ls->nr_release++;
  668. goto end;
  669. }
  670. break;
  671. case SEQ_STATE_ACQUIRING:
  672. case SEQ_STATE_CONTENDED:
  673. case SEQ_STATE_RELEASED:
  674. /* broken lock sequence */
  675. if (!ls->broken) {
  676. ls->broken = 1;
  677. bad_hist[BROKEN_RELEASE]++;
  678. }
  679. goto free_seq;
  680. default:
  681. BUG_ON("Unknown state of lock sequence found!\n");
  682. break;
  683. }
  684. ls->nr_release++;
  685. free_seq:
  686. list_del_init(&seq->list);
  687. free(seq);
  688. end:
  689. return 0;
  690. }
  691. static int get_symbol_name_offset(struct map *map, struct symbol *sym, u64 ip,
  692. char *buf, int size)
  693. {
  694. u64 offset;
  695. if (map == NULL || sym == NULL) {
  696. buf[0] = '\0';
  697. return 0;
  698. }
  699. offset = map__map_ip(map, ip) - sym->start;
  700. if (offset)
  701. return scnprintf(buf, size, "%s+%#lx", sym->name, offset);
  702. else
  703. return strlcpy(buf, sym->name, size);
  704. }
  705. static int lock_contention_caller(struct evsel *evsel, struct perf_sample *sample,
  706. char *buf, int size)
  707. {
  708. struct thread *thread;
  709. struct callchain_cursor *cursor;
  710. struct machine *machine = &session->machines.host;
  711. struct symbol *sym;
  712. int skip = 0;
  713. int ret;
  714. /* lock names will be replaced to task name later */
  715. if (show_thread_stats)
  716. return -1;
  717. thread = machine__findnew_thread(machine, -1, sample->pid);
  718. if (thread == NULL)
  719. return -1;
  720. cursor = get_tls_callchain_cursor();
  721. /* use caller function name from the callchain */
  722. ret = thread__resolve_callchain(thread, cursor, evsel, sample,
  723. NULL, NULL, max_stack_depth);
  724. if (ret != 0) {
  725. thread__put(thread);
  726. return -1;
  727. }
  728. callchain_cursor_commit(cursor);
  729. thread__put(thread);
  730. while (true) {
  731. struct callchain_cursor_node *node;
  732. node = callchain_cursor_current(cursor);
  733. if (node == NULL)
  734. break;
  735. /* skip first few entries - for lock functions */
  736. if (++skip <= stack_skip)
  737. goto next;
  738. sym = node->ms.sym;
  739. if (sym && !machine__is_lock_function(machine, node->ip)) {
  740. get_symbol_name_offset(node->ms.map, sym, node->ip,
  741. buf, size);
  742. return 0;
  743. }
  744. next:
  745. callchain_cursor_advance(cursor);
  746. }
  747. return -1;
  748. }
  749. static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample)
  750. {
  751. struct callchain_cursor *cursor;
  752. struct machine *machine = &session->machines.host;
  753. struct thread *thread;
  754. u64 hash = 0;
  755. int skip = 0;
  756. int ret;
  757. thread = machine__findnew_thread(machine, -1, sample->pid);
  758. if (thread == NULL)
  759. return -1;
  760. cursor = get_tls_callchain_cursor();
  761. /* use caller function name from the callchain */
  762. ret = thread__resolve_callchain(thread, cursor, evsel, sample,
  763. NULL, NULL, max_stack_depth);
  764. thread__put(thread);
  765. if (ret != 0)
  766. return -1;
  767. callchain_cursor_commit(cursor);
  768. while (true) {
  769. struct callchain_cursor_node *node;
  770. node = callchain_cursor_current(cursor);
  771. if (node == NULL)
  772. break;
  773. /* skip first few entries - for lock functions */
  774. if (++skip <= stack_skip)
  775. goto next;
  776. if (node->ms.sym && machine__is_lock_function(machine, node->ip))
  777. goto next;
  778. hash ^= hash_long((unsigned long)node->ip, 64);
  779. next:
  780. callchain_cursor_advance(cursor);
  781. }
  782. return hash;
  783. }
  784. static u64 *get_callstack(struct perf_sample *sample, int max_stack)
  785. {
  786. u64 *callstack;
  787. u64 i;
  788. int c;
  789. callstack = calloc(max_stack, sizeof(*callstack));
  790. if (callstack == NULL)
  791. return NULL;
  792. for (i = 0, c = 0; i < sample->callchain->nr && c < max_stack; i++) {
  793. u64 ip = sample->callchain->ips[i];
  794. if (ip >= PERF_CONTEXT_MAX)
  795. continue;
  796. callstack[c++] = ip;
  797. }
  798. return callstack;
  799. }
  800. static int report_lock_contention_begin_event(struct evsel *evsel,
  801. struct perf_sample *sample)
  802. {
  803. struct lock_stat *ls;
  804. struct thread_stat *ts;
  805. struct lock_seq_stat *seq;
  806. u64 addr = evsel__intval(evsel, sample, "lock_addr");
  807. unsigned int flags = evsel__intval(evsel, sample, "flags");
  808. u64 key;
  809. int i, ret;
  810. static bool kmap_loaded;
  811. struct machine *machine = &session->machines.host;
  812. struct map *kmap;
  813. struct symbol *sym;
  814. ret = get_key_by_aggr_mode(&key, addr, evsel, sample);
  815. if (ret < 0)
  816. return ret;
  817. if (!kmap_loaded) {
  818. unsigned long *addrs;
  819. /* make sure it loads the kernel map to find lock symbols */
  820. map__load(machine__kernel_map(machine));
  821. kmap_loaded = true;
  822. /* convert (kernel) symbols to addresses */
  823. for (i = 0; i < filters.nr_syms; i++) {
  824. sym = machine__find_kernel_symbol_by_name(machine,
  825. filters.syms[i],
  826. &kmap);
  827. if (sym == NULL) {
  828. pr_warning("ignore unknown symbol: %s\n",
  829. filters.syms[i]);
  830. continue;
  831. }
  832. addrs = realloc(filters.addrs,
  833. (filters.nr_addrs + 1) * sizeof(*addrs));
  834. if (addrs == NULL) {
  835. pr_warning("memory allocation failure\n");
  836. return -ENOMEM;
  837. }
  838. addrs[filters.nr_addrs++] = map__unmap_ip(kmap, sym->start);
  839. filters.addrs = addrs;
  840. }
  841. }
  842. ls = lock_stat_find(key);
  843. if (!ls) {
  844. char buf[128];
  845. const char *name = "";
  846. switch (aggr_mode) {
  847. case LOCK_AGGR_ADDR:
  848. sym = machine__find_kernel_symbol(machine, key, &kmap);
  849. if (sym)
  850. name = sym->name;
  851. break;
  852. case LOCK_AGGR_CALLER:
  853. name = buf;
  854. if (lock_contention_caller(evsel, sample, buf, sizeof(buf)) < 0)
  855. name = "Unknown";
  856. break;
  857. case LOCK_AGGR_CGROUP:
  858. case LOCK_AGGR_TASK:
  859. default:
  860. break;
  861. }
  862. ls = lock_stat_findnew(key, name, flags);
  863. if (!ls)
  864. return -ENOMEM;
  865. }
  866. if (filters.nr_types) {
  867. bool found = false;
  868. for (i = 0; i < filters.nr_types; i++) {
  869. if (flags == filters.types[i]) {
  870. found = true;
  871. break;
  872. }
  873. }
  874. if (!found)
  875. return 0;
  876. }
  877. if (filters.nr_addrs) {
  878. bool found = false;
  879. for (i = 0; i < filters.nr_addrs; i++) {
  880. if (addr == filters.addrs[i]) {
  881. found = true;
  882. break;
  883. }
  884. }
  885. if (!found)
  886. return 0;
  887. }
  888. if (needs_callstack()) {
  889. u64 *callstack = get_callstack(sample, max_stack_depth);
  890. if (callstack == NULL)
  891. return -ENOMEM;
  892. if (!match_callstack_filter(machine, callstack, max_stack_depth)) {
  893. free(callstack);
  894. return 0;
  895. }
  896. if (ls->callstack == NULL)
  897. ls->callstack = callstack;
  898. else
  899. free(callstack);
  900. }
  901. ts = thread_stat_findnew(sample->tid);
  902. if (!ts)
  903. return -ENOMEM;
  904. seq = get_seq(ts, addr);
  905. if (!seq)
  906. return -ENOMEM;
  907. switch (seq->state) {
  908. case SEQ_STATE_UNINITIALIZED:
  909. case SEQ_STATE_ACQUIRED:
  910. break;
  911. case SEQ_STATE_CONTENDED:
  912. /*
  913. * It can have nested contention begin with mutex spinning,
  914. * then we would use the original contention begin event and
  915. * ignore the second one.
  916. */
  917. goto end;
  918. case SEQ_STATE_ACQUIRING:
  919. case SEQ_STATE_READ_ACQUIRED:
  920. case SEQ_STATE_RELEASED:
  921. /* broken lock sequence */
  922. if (!ls->broken) {
  923. ls->broken = 1;
  924. bad_hist[BROKEN_CONTENDED]++;
  925. }
  926. list_del_init(&seq->list);
  927. free(seq);
  928. goto end;
  929. default:
  930. BUG_ON("Unknown state of lock sequence found!\n");
  931. break;
  932. }
  933. if (seq->state != SEQ_STATE_CONTENDED) {
  934. seq->state = SEQ_STATE_CONTENDED;
  935. seq->prev_event_time = sample->time;
  936. ls->nr_contended++;
  937. }
  938. end:
  939. return 0;
  940. }
  941. static int report_lock_contention_end_event(struct evsel *evsel,
  942. struct perf_sample *sample)
  943. {
  944. struct lock_stat *ls;
  945. struct thread_stat *ts;
  946. struct lock_seq_stat *seq;
  947. u64 contended_term;
  948. u64 addr = evsel__intval(evsel, sample, "lock_addr");
  949. u64 key;
  950. int ret;
  951. ret = get_key_by_aggr_mode(&key, addr, evsel, sample);
  952. if (ret < 0)
  953. return ret;
  954. ls = lock_stat_find(key);
  955. if (!ls)
  956. return 0;
  957. ts = thread_stat_find(sample->tid);
  958. if (!ts)
  959. return 0;
  960. seq = get_seq(ts, addr);
  961. if (!seq)
  962. return -ENOMEM;
  963. switch (seq->state) {
  964. case SEQ_STATE_UNINITIALIZED:
  965. goto end;
  966. case SEQ_STATE_CONTENDED:
  967. contended_term = sample->time - seq->prev_event_time;
  968. ls->wait_time_total += contended_term;
  969. if (contended_term < ls->wait_time_min)
  970. ls->wait_time_min = contended_term;
  971. if (ls->wait_time_max < contended_term)
  972. ls->wait_time_max = contended_term;
  973. break;
  974. case SEQ_STATE_ACQUIRING:
  975. case SEQ_STATE_ACQUIRED:
  976. case SEQ_STATE_READ_ACQUIRED:
  977. case SEQ_STATE_RELEASED:
  978. /* broken lock sequence */
  979. if (!ls->broken) {
  980. ls->broken = 1;
  981. bad_hist[BROKEN_ACQUIRED]++;
  982. }
  983. list_del_init(&seq->list);
  984. free(seq);
  985. goto end;
  986. default:
  987. BUG_ON("Unknown state of lock sequence found!\n");
  988. break;
  989. }
  990. seq->state = SEQ_STATE_ACQUIRED;
  991. ls->nr_acquired++;
  992. ls->avg_wait_time = ls->wait_time_total/ls->nr_acquired;
  993. end:
  994. return 0;
  995. }
  996. /* lock oriented handlers */
  997. /* TODO: handlers for CPU oriented, thread oriented */
  998. static struct trace_lock_handler report_lock_ops = {
  999. .acquire_event = report_lock_acquire_event,
  1000. .acquired_event = report_lock_acquired_event,
  1001. .contended_event = report_lock_contended_event,
  1002. .release_event = report_lock_release_event,
  1003. .contention_begin_event = report_lock_contention_begin_event,
  1004. .contention_end_event = report_lock_contention_end_event,
  1005. };
  1006. static struct trace_lock_handler contention_lock_ops = {
  1007. .contention_begin_event = report_lock_contention_begin_event,
  1008. .contention_end_event = report_lock_contention_end_event,
  1009. };
  1010. static struct trace_lock_handler *trace_handler;
  1011. static int evsel__process_lock_acquire(struct evsel *evsel, struct perf_sample *sample)
  1012. {
  1013. if (trace_handler->acquire_event)
  1014. return trace_handler->acquire_event(evsel, sample);
  1015. return 0;
  1016. }
  1017. static int evsel__process_lock_acquired(struct evsel *evsel, struct perf_sample *sample)
  1018. {
  1019. if (trace_handler->acquired_event)
  1020. return trace_handler->acquired_event(evsel, sample);
  1021. return 0;
  1022. }
  1023. static int evsel__process_lock_contended(struct evsel *evsel, struct perf_sample *sample)
  1024. {
  1025. if (trace_handler->contended_event)
  1026. return trace_handler->contended_event(evsel, sample);
  1027. return 0;
  1028. }
  1029. static int evsel__process_lock_release(struct evsel *evsel, struct perf_sample *sample)
  1030. {
  1031. if (trace_handler->release_event)
  1032. return trace_handler->release_event(evsel, sample);
  1033. return 0;
  1034. }
  1035. static int evsel__process_contention_begin(struct evsel *evsel, struct perf_sample *sample)
  1036. {
  1037. if (trace_handler->contention_begin_event)
  1038. return trace_handler->contention_begin_event(evsel, sample);
  1039. return 0;
  1040. }
  1041. static int evsel__process_contention_end(struct evsel *evsel, struct perf_sample *sample)
  1042. {
  1043. if (trace_handler->contention_end_event)
  1044. return trace_handler->contention_end_event(evsel, sample);
  1045. return 0;
  1046. }
  1047. static void print_bad_events(int bad, int total)
  1048. {
  1049. /* Output for debug, this have to be removed */
  1050. int i;
  1051. int broken = 0;
  1052. const char *name[4] =
  1053. { "acquire", "acquired", "contended", "release" };
  1054. for (i = 0; i < BROKEN_MAX; i++)
  1055. broken += bad_hist[i];
  1056. if (quiet || total == 0 || (broken == 0 && verbose <= 0))
  1057. return;
  1058. fprintf(lock_output, "\n=== output for debug ===\n\n");
  1059. fprintf(lock_output, "bad: %d, total: %d\n", bad, total);
  1060. fprintf(lock_output, "bad rate: %.2f %%\n", (double)bad / (double)total * 100);
  1061. fprintf(lock_output, "histogram of events caused bad sequence\n");
  1062. for (i = 0; i < BROKEN_MAX; i++)
  1063. fprintf(lock_output, " %10s: %d\n", name[i], bad_hist[i]);
  1064. }
  1065. /* TODO: various way to print, coloring, nano or milli sec */
  1066. static void print_result(void)
  1067. {
  1068. struct lock_stat *st;
  1069. struct lock_key *key;
  1070. char cut_name[20];
  1071. int bad, total, printed;
  1072. if (!quiet) {
  1073. fprintf(lock_output, "%20s ", "Name");
  1074. list_for_each_entry(key, &lock_keys, list)
  1075. fprintf(lock_output, "%*s ", key->len, key->header);
  1076. fprintf(lock_output, "\n\n");
  1077. }
  1078. bad = total = printed = 0;
  1079. while ((st = pop_from_result())) {
  1080. total++;
  1081. if (st->broken)
  1082. bad++;
  1083. if (!st->nr_acquired)
  1084. continue;
  1085. bzero(cut_name, 20);
  1086. if (strlen(st->name) < 20) {
  1087. /* output raw name */
  1088. const char *name = st->name;
  1089. if (show_thread_stats) {
  1090. struct thread *t;
  1091. /* st->addr contains tid of thread */
  1092. t = perf_session__findnew(session, st->addr);
  1093. name = thread__comm_str(t);
  1094. }
  1095. fprintf(lock_output, "%20s ", name);
  1096. } else {
  1097. strncpy(cut_name, st->name, 16);
  1098. cut_name[16] = '.';
  1099. cut_name[17] = '.';
  1100. cut_name[18] = '.';
  1101. cut_name[19] = '\0';
  1102. /* cut off name for saving output style */
  1103. fprintf(lock_output, "%20s ", cut_name);
  1104. }
  1105. list_for_each_entry(key, &lock_keys, list) {
  1106. key->print(key, st);
  1107. fprintf(lock_output, " ");
  1108. }
  1109. fprintf(lock_output, "\n");
  1110. if (++printed >= print_nr_entries)
  1111. break;
  1112. }
  1113. print_bad_events(bad, total);
  1114. }
  1115. static bool info_threads, info_map;
  1116. static void dump_threads(void)
  1117. {
  1118. struct thread_stat *st;
  1119. struct rb_node *node;
  1120. struct thread *t;
  1121. fprintf(lock_output, "%10s: comm\n", "Thread ID");
  1122. node = rb_first(&thread_stats);
  1123. while (node) {
  1124. st = container_of(node, struct thread_stat, rb);
  1125. t = perf_session__findnew(session, st->tid);
  1126. fprintf(lock_output, "%10d: %s\n", st->tid, thread__comm_str(t));
  1127. node = rb_next(node);
  1128. thread__put(t);
  1129. }
  1130. }
  1131. static int compare_maps(struct lock_stat *a, struct lock_stat *b)
  1132. {
  1133. int ret;
  1134. if (a->name && b->name)
  1135. ret = strcmp(a->name, b->name);
  1136. else
  1137. ret = !!a->name - !!b->name;
  1138. if (!ret)
  1139. return a->addr < b->addr;
  1140. else
  1141. return ret < 0;
  1142. }
  1143. static void dump_map(void)
  1144. {
  1145. unsigned int i;
  1146. struct lock_stat *st;
  1147. fprintf(lock_output, "Address of instance: name of class\n");
  1148. for (i = 0; i < LOCKHASH_SIZE; i++) {
  1149. hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
  1150. insert_to_result(st, compare_maps);
  1151. }
  1152. }
  1153. while ((st = pop_from_result()))
  1154. fprintf(lock_output, " %#llx: %s\n", (unsigned long long)st->addr, st->name);
  1155. }
  1156. static void dump_info(void)
  1157. {
  1158. if (info_threads)
  1159. dump_threads();
  1160. if (info_map) {
  1161. if (info_threads)
  1162. fputc('\n', lock_output);
  1163. dump_map();
  1164. }
  1165. }
  1166. static const struct evsel_str_handler lock_tracepoints[] = {
  1167. { "lock:lock_acquire", evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
  1168. { "lock:lock_acquired", evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
  1169. { "lock:lock_contended", evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
  1170. { "lock:lock_release", evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
  1171. };
  1172. static const struct evsel_str_handler contention_tracepoints[] = {
  1173. { "lock:contention_begin", evsel__process_contention_begin, },
  1174. { "lock:contention_end", evsel__process_contention_end, },
  1175. };
  1176. static int process_event_update(const struct perf_tool *tool,
  1177. union perf_event *event,
  1178. struct evlist **pevlist)
  1179. {
  1180. int ret;
  1181. ret = perf_event__process_event_update(tool, event, pevlist);
  1182. if (ret < 0)
  1183. return ret;
  1184. /* this can return -EEXIST since we call it for each evsel */
  1185. perf_session__set_tracepoints_handlers(session, lock_tracepoints);
  1186. perf_session__set_tracepoints_handlers(session, contention_tracepoints);
  1187. return 0;
  1188. }
  1189. typedef int (*tracepoint_handler)(struct evsel *evsel,
  1190. struct perf_sample *sample);
  1191. static int process_sample_event(const struct perf_tool *tool __maybe_unused,
  1192. union perf_event *event,
  1193. struct perf_sample *sample,
  1194. struct evsel *evsel,
  1195. struct machine *machine)
  1196. {
  1197. int err = 0;
  1198. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  1199. sample->tid);
  1200. if (thread == NULL) {
  1201. pr_debug("problem processing %d event, skipping it.\n",
  1202. event->header.type);
  1203. return -1;
  1204. }
  1205. if (evsel->handler != NULL) {
  1206. tracepoint_handler f = evsel->handler;
  1207. err = f(evsel, sample);
  1208. }
  1209. thread__put(thread);
  1210. return err;
  1211. }
  1212. static void combine_result(void)
  1213. {
  1214. unsigned int i;
  1215. struct lock_stat *st;
  1216. if (!combine_locks)
  1217. return;
  1218. for (i = 0; i < LOCKHASH_SIZE; i++) {
  1219. hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
  1220. combine_lock_stats(st);
  1221. }
  1222. }
  1223. }
  1224. static void sort_result(void)
  1225. {
  1226. unsigned int i;
  1227. struct lock_stat *st;
  1228. for (i = 0; i < LOCKHASH_SIZE; i++) {
  1229. hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
  1230. insert_to_result(st, compare);
  1231. }
  1232. }
  1233. }
  1234. static const struct {
  1235. unsigned int flags;
  1236. /*
  1237. * Name of the lock flags (access), with delimeter ':'.
  1238. * For example, rwsem:R of rwsem:W.
  1239. */
  1240. const char *flags_name;
  1241. /* Name of the lock (type), for example, rwlock or rwsem. */
  1242. const char *lock_name;
  1243. } lock_type_table[] = {
  1244. { 0, "semaphore", "semaphore" },
  1245. { LCB_F_SPIN, "spinlock", "spinlock" },
  1246. { LCB_F_SPIN | LCB_F_READ, "rwlock:R", "rwlock" },
  1247. { LCB_F_SPIN | LCB_F_WRITE, "rwlock:W", "rwlock" },
  1248. { LCB_F_READ, "rwsem:R", "rwsem" },
  1249. { LCB_F_WRITE, "rwsem:W", "rwsem" },
  1250. { LCB_F_RT, "rt-mutex", "rt-mutex" },
  1251. { LCB_F_RT | LCB_F_READ, "rwlock-rt:R", "rwlock-rt" },
  1252. { LCB_F_RT | LCB_F_WRITE, "rwlock-rt:W", "rwlock-rt" },
  1253. { LCB_F_PERCPU | LCB_F_READ, "pcpu-sem:R", "percpu-rwsem" },
  1254. { LCB_F_PERCPU | LCB_F_WRITE, "pcpu-sem:W", "percpu-rwsem" },
  1255. { LCB_F_MUTEX, "mutex", "mutex" },
  1256. { LCB_F_MUTEX | LCB_F_SPIN, "mutex", "mutex" },
  1257. /* alias for optimistic spinning only */
  1258. { LCB_F_MUTEX | LCB_F_SPIN, "mutex:spin", "mutex-spin" },
  1259. };
  1260. static const char *get_type_flags_name(unsigned int flags)
  1261. {
  1262. flags &= LCB_F_TYPE_MASK;
  1263. for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
  1264. if (lock_type_table[i].flags == flags)
  1265. return lock_type_table[i].flags_name;
  1266. }
  1267. return "unknown";
  1268. }
  1269. static const char *get_type_lock_name(unsigned int flags)
  1270. {
  1271. flags &= LCB_F_TYPE_MASK;
  1272. for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
  1273. if (lock_type_table[i].flags == flags)
  1274. return lock_type_table[i].lock_name;
  1275. }
  1276. return "unknown";
  1277. }
  1278. static void lock_filter_finish(void)
  1279. {
  1280. zfree(&filters.types);
  1281. filters.nr_types = 0;
  1282. zfree(&filters.addrs);
  1283. filters.nr_addrs = 0;
  1284. for (int i = 0; i < filters.nr_syms; i++)
  1285. free(filters.syms[i]);
  1286. zfree(&filters.syms);
  1287. filters.nr_syms = 0;
  1288. zfree(&filters.cgrps);
  1289. filters.nr_cgrps = 0;
  1290. for (int i = 0; i < filters.nr_slabs; i++)
  1291. free(filters.slabs[i]);
  1292. zfree(&filters.slabs);
  1293. filters.nr_slabs = 0;
  1294. }
  1295. static void sort_contention_result(void)
  1296. {
  1297. sort_result();
  1298. }
  1299. static void print_header_stdio(void)
  1300. {
  1301. struct lock_key *key;
  1302. list_for_each_entry(key, &lock_keys, list)
  1303. fprintf(lock_output, "%*s ", key->len, key->header);
  1304. switch (aggr_mode) {
  1305. case LOCK_AGGR_TASK:
  1306. fprintf(lock_output, " %10s %s\n\n", "pid",
  1307. show_lock_owner ? "owner" : "comm");
  1308. break;
  1309. case LOCK_AGGR_CALLER:
  1310. fprintf(lock_output, " %10s %s\n\n", "type", "caller");
  1311. break;
  1312. case LOCK_AGGR_ADDR:
  1313. fprintf(lock_output, " %16s %s\n\n", "address", "symbol");
  1314. break;
  1315. case LOCK_AGGR_CGROUP:
  1316. fprintf(lock_output, " %s\n\n", "cgroup");
  1317. break;
  1318. default:
  1319. break;
  1320. }
  1321. }
  1322. static void print_header_csv(const char *sep)
  1323. {
  1324. struct lock_key *key;
  1325. fprintf(lock_output, "# output: ");
  1326. list_for_each_entry(key, &lock_keys, list)
  1327. fprintf(lock_output, "%s%s ", key->header, sep);
  1328. switch (aggr_mode) {
  1329. case LOCK_AGGR_TASK:
  1330. fprintf(lock_output, "%s%s %s\n", "pid", sep,
  1331. show_lock_owner ? "owner" : "comm");
  1332. break;
  1333. case LOCK_AGGR_CALLER:
  1334. fprintf(lock_output, "%s%s %s", "type", sep, "caller");
  1335. if (verbose > 0)
  1336. fprintf(lock_output, "%s %s", sep, "stacktrace");
  1337. fprintf(lock_output, "\n");
  1338. break;
  1339. case LOCK_AGGR_ADDR:
  1340. fprintf(lock_output, "%s%s %s%s %s\n", "address", sep, "symbol", sep, "type");
  1341. break;
  1342. case LOCK_AGGR_CGROUP:
  1343. fprintf(lock_output, "%s\n", "cgroup");
  1344. break;
  1345. default:
  1346. break;
  1347. }
  1348. }
  1349. static void print_header(void)
  1350. {
  1351. if (!quiet) {
  1352. if (symbol_conf.field_sep)
  1353. print_header_csv(symbol_conf.field_sep);
  1354. else
  1355. print_header_stdio();
  1356. }
  1357. }
  1358. static void print_lock_stat_stdio(struct lock_contention *con, struct lock_stat *st)
  1359. {
  1360. struct lock_key *key;
  1361. struct thread *t;
  1362. int pid;
  1363. list_for_each_entry(key, &lock_keys, list) {
  1364. key->print(key, st);
  1365. fprintf(lock_output, " ");
  1366. }
  1367. switch (aggr_mode) {
  1368. case LOCK_AGGR_CALLER:
  1369. fprintf(lock_output, " %10s %s\n", get_type_flags_name(st->flags), st->name);
  1370. break;
  1371. case LOCK_AGGR_TASK:
  1372. pid = st->addr;
  1373. t = perf_session__findnew(session, pid);
  1374. fprintf(lock_output, " %10d %s\n",
  1375. pid, pid == -1 ? "Unknown" : thread__comm_str(t));
  1376. break;
  1377. case LOCK_AGGR_ADDR:
  1378. fprintf(lock_output, " %016llx %s (%s)\n", (unsigned long long)st->addr,
  1379. st->name, get_type_lock_name(st->flags));
  1380. break;
  1381. case LOCK_AGGR_CGROUP:
  1382. fprintf(lock_output, " %s\n", st->name);
  1383. break;
  1384. default:
  1385. break;
  1386. }
  1387. if (aggr_mode == LOCK_AGGR_CALLER && verbose > 0) {
  1388. struct map *kmap;
  1389. struct symbol *sym;
  1390. char buf[128];
  1391. u64 ip;
  1392. for (int i = 0; i < max_stack_depth; i++) {
  1393. if (!st->callstack || !st->callstack[i])
  1394. break;
  1395. ip = st->callstack[i];
  1396. sym = machine__find_kernel_symbol(con->machine, ip, &kmap);
  1397. get_symbol_name_offset(kmap, sym, ip, buf, sizeof(buf));
  1398. fprintf(lock_output, "\t\t\t%#lx %s\n", (unsigned long)ip, buf);
  1399. }
  1400. }
  1401. }
  1402. static void print_lock_stat_csv(struct lock_contention *con, struct lock_stat *st,
  1403. const char *sep)
  1404. {
  1405. struct lock_key *key;
  1406. struct thread *t;
  1407. int pid;
  1408. list_for_each_entry(key, &lock_keys, list) {
  1409. key->print(key, st);
  1410. fprintf(lock_output, "%s ", sep);
  1411. }
  1412. switch (aggr_mode) {
  1413. case LOCK_AGGR_CALLER:
  1414. fprintf(lock_output, "%s%s %s", get_type_flags_name(st->flags), sep, st->name);
  1415. if (verbose <= 0)
  1416. fprintf(lock_output, "\n");
  1417. break;
  1418. case LOCK_AGGR_TASK:
  1419. pid = st->addr;
  1420. t = perf_session__findnew(session, pid);
  1421. fprintf(lock_output, "%d%s %s\n", pid, sep,
  1422. pid == -1 ? "Unknown" : thread__comm_str(t));
  1423. break;
  1424. case LOCK_AGGR_ADDR:
  1425. fprintf(lock_output, "%llx%s %s%s %s\n", (unsigned long long)st->addr, sep,
  1426. st->name, sep, get_type_lock_name(st->flags));
  1427. break;
  1428. case LOCK_AGGR_CGROUP:
  1429. fprintf(lock_output, "%s\n",st->name);
  1430. break;
  1431. default:
  1432. break;
  1433. }
  1434. if (aggr_mode == LOCK_AGGR_CALLER && verbose > 0) {
  1435. struct map *kmap;
  1436. struct symbol *sym;
  1437. char buf[128];
  1438. u64 ip;
  1439. for (int i = 0; i < max_stack_depth; i++) {
  1440. if (!st->callstack || !st->callstack[i])
  1441. break;
  1442. ip = st->callstack[i];
  1443. sym = machine__find_kernel_symbol(con->machine, ip, &kmap);
  1444. get_symbol_name_offset(kmap, sym, ip, buf, sizeof(buf));
  1445. fprintf(lock_output, "%s %#lx %s", i ? ":" : sep, (unsigned long) ip, buf);
  1446. }
  1447. fprintf(lock_output, "\n");
  1448. }
  1449. }
  1450. static void print_lock_stat(struct lock_contention *con, struct lock_stat *st)
  1451. {
  1452. if (symbol_conf.field_sep)
  1453. print_lock_stat_csv(con, st, symbol_conf.field_sep);
  1454. else
  1455. print_lock_stat_stdio(con, st);
  1456. }
  1457. static void print_footer_stdio(int total, int bad, struct lock_contention_fails *fails)
  1458. {
  1459. /* Output for debug, this have to be removed */
  1460. int broken = fails->task + fails->stack + fails->time + fails->data;
  1461. if (!use_bpf)
  1462. print_bad_events(bad, total);
  1463. if (quiet || total == 0 || (broken == 0 && verbose <= 0))
  1464. return;
  1465. total += broken;
  1466. fprintf(lock_output, "\n=== output for debug ===\n\n");
  1467. fprintf(lock_output, "bad: %d, total: %d\n", broken, total);
  1468. fprintf(lock_output, "bad rate: %.2f %%\n", 100.0 * broken / total);
  1469. fprintf(lock_output, "histogram of failure reasons\n");
  1470. fprintf(lock_output, " %10s: %d\n", "task", fails->task);
  1471. fprintf(lock_output, " %10s: %d\n", "stack", fails->stack);
  1472. fprintf(lock_output, " %10s: %d\n", "time", fails->time);
  1473. fprintf(lock_output, " %10s: %d\n", "data", fails->data);
  1474. }
  1475. static void print_footer_csv(int total, int bad, struct lock_contention_fails *fails,
  1476. const char *sep)
  1477. {
  1478. /* Output for debug, this have to be removed */
  1479. if (use_bpf)
  1480. bad = fails->task + fails->stack + fails->time + fails->data;
  1481. if (quiet || total == 0 || (bad == 0 && verbose <= 0))
  1482. return;
  1483. total += bad;
  1484. fprintf(lock_output, "# debug: total=%d%s bad=%d", total, sep, bad);
  1485. if (use_bpf) {
  1486. fprintf(lock_output, "%s bad_%s=%d", sep, "task", fails->task);
  1487. fprintf(lock_output, "%s bad_%s=%d", sep, "stack", fails->stack);
  1488. fprintf(lock_output, "%s bad_%s=%d", sep, "time", fails->time);
  1489. fprintf(lock_output, "%s bad_%s=%d", sep, "data", fails->data);
  1490. } else {
  1491. int i;
  1492. const char *name[4] = { "acquire", "acquired", "contended", "release" };
  1493. for (i = 0; i < BROKEN_MAX; i++)
  1494. fprintf(lock_output, "%s bad_%s=%d", sep, name[i], bad_hist[i]);
  1495. }
  1496. fprintf(lock_output, "\n");
  1497. }
  1498. static void print_footer(int total, int bad, struct lock_contention_fails *fails)
  1499. {
  1500. if (symbol_conf.field_sep)
  1501. print_footer_csv(total, bad, fails, symbol_conf.field_sep);
  1502. else
  1503. print_footer_stdio(total, bad, fails);
  1504. }
  1505. static void print_contention_result(struct lock_contention *con)
  1506. {
  1507. struct lock_stat *st;
  1508. int bad, total, printed;
  1509. if (!quiet)
  1510. print_header();
  1511. bad = total = printed = 0;
  1512. while ((st = pop_from_result())) {
  1513. total += use_bpf ? st->nr_contended : 1;
  1514. if (st->broken)
  1515. bad++;
  1516. if (!st->wait_time_total)
  1517. continue;
  1518. print_lock_stat(con, st);
  1519. if (++printed >= print_nr_entries)
  1520. break;
  1521. }
  1522. if (con->owner && con->save_callstack && verbose > 0) {
  1523. struct rb_root root = RB_ROOT;
  1524. if (symbol_conf.field_sep)
  1525. fprintf(lock_output, "# owner stack trace:\n");
  1526. else
  1527. fprintf(lock_output, "\n=== owner stack trace ===\n\n");
  1528. while ((st = pop_owner_stack_trace(con)))
  1529. insert_to(&root, st, compare);
  1530. while ((st = pop_from(&root))) {
  1531. print_lock_stat(con, st);
  1532. free(st);
  1533. }
  1534. }
  1535. if (print_nr_entries) {
  1536. /* update the total/bad stats */
  1537. while ((st = pop_from_result())) {
  1538. total += use_bpf ? st->nr_contended : 1;
  1539. if (st->broken)
  1540. bad++;
  1541. }
  1542. }
  1543. /* some entries are collected but hidden by the callstack filter */
  1544. total += con->nr_filtered;
  1545. print_footer(total, bad, &con->fails);
  1546. }
  1547. static bool force;
  1548. static int __cmd_report(bool display_info)
  1549. {
  1550. int err = -EINVAL;
  1551. struct perf_tool eops;
  1552. struct perf_data data = {
  1553. .path = input_name,
  1554. .mode = PERF_DATA_MODE_READ,
  1555. .force = force,
  1556. };
  1557. perf_tool__init(&eops, /*ordered_events=*/true);
  1558. eops.attr = perf_event__process_attr;
  1559. eops.event_update = process_event_update;
  1560. eops.sample = process_sample_event;
  1561. eops.comm = perf_event__process_comm;
  1562. eops.mmap = perf_event__process_mmap;
  1563. eops.mmap2 = perf_event__process_mmap2;
  1564. eops.namespaces = perf_event__process_namespaces;
  1565. eops.tracing_data = perf_event__process_tracing_data;
  1566. session = perf_session__new(&data, &eops);
  1567. if (IS_ERR(session)) {
  1568. pr_err("Initializing perf session failed\n");
  1569. return PTR_ERR(session);
  1570. }
  1571. symbol_conf.allow_aliases = true;
  1572. symbol__init(perf_session__env(session));
  1573. if (!data.is_pipe) {
  1574. if (!perf_session__has_traces(session, "lock record"))
  1575. goto out_delete;
  1576. if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
  1577. pr_err("Initializing perf session tracepoint handlers failed\n");
  1578. goto out_delete;
  1579. }
  1580. if (perf_session__set_tracepoints_handlers(session, contention_tracepoints)) {
  1581. pr_err("Initializing perf session tracepoint handlers failed\n");
  1582. goto out_delete;
  1583. }
  1584. }
  1585. if (setup_output_field(false, output_fields))
  1586. goto out_delete;
  1587. if (select_key(false))
  1588. goto out_delete;
  1589. if (show_thread_stats)
  1590. aggr_mode = LOCK_AGGR_TASK;
  1591. err = perf_session__process_events(session);
  1592. if (err)
  1593. goto out_delete;
  1594. setup_pager();
  1595. if (display_info) /* used for info subcommand */
  1596. dump_info();
  1597. else {
  1598. combine_result();
  1599. sort_result();
  1600. print_result();
  1601. }
  1602. out_delete:
  1603. perf_session__delete(session);
  1604. return err;
  1605. }
  1606. static void sighandler(int sig __maybe_unused)
  1607. {
  1608. }
  1609. static int check_lock_contention_options(const struct option *options,
  1610. const char * const *usage)
  1611. {
  1612. if (show_thread_stats && show_lock_addrs) {
  1613. pr_err("Cannot use thread and addr mode together\n");
  1614. parse_options_usage(usage, options, "threads", 0);
  1615. parse_options_usage(NULL, options, "lock-addr", 0);
  1616. return -1;
  1617. }
  1618. if (show_lock_owner && !use_bpf) {
  1619. pr_err("Lock owners are available only with BPF\n");
  1620. parse_options_usage(usage, options, "lock-owner", 0);
  1621. parse_options_usage(NULL, options, "use-bpf", 0);
  1622. return -1;
  1623. }
  1624. if (show_lock_owner && show_lock_addrs) {
  1625. pr_err("Cannot use owner and addr mode together\n");
  1626. parse_options_usage(usage, options, "lock-owner", 0);
  1627. parse_options_usage(NULL, options, "lock-addr", 0);
  1628. return -1;
  1629. }
  1630. if (show_lock_cgroups && !use_bpf) {
  1631. pr_err("Cgroups are available only with BPF\n");
  1632. parse_options_usage(usage, options, "lock-cgroup", 0);
  1633. parse_options_usage(NULL, options, "use-bpf", 0);
  1634. return -1;
  1635. }
  1636. if (show_lock_cgroups && show_lock_addrs) {
  1637. pr_err("Cannot use cgroup and addr mode together\n");
  1638. parse_options_usage(usage, options, "lock-cgroup", 0);
  1639. parse_options_usage(NULL, options, "lock-addr", 0);
  1640. return -1;
  1641. }
  1642. if (show_lock_cgroups && show_thread_stats) {
  1643. pr_err("Cannot use cgroup and thread mode together\n");
  1644. parse_options_usage(usage, options, "lock-cgroup", 0);
  1645. parse_options_usage(NULL, options, "threads", 0);
  1646. return -1;
  1647. }
  1648. if (symbol_conf.field_sep) {
  1649. if (strstr(symbol_conf.field_sep, ":") || /* part of type flags */
  1650. strstr(symbol_conf.field_sep, "+") || /* part of caller offset */
  1651. strstr(symbol_conf.field_sep, ".")) { /* can be in a symbol name */
  1652. pr_err("Cannot use the separator that is already used\n");
  1653. parse_options_usage(usage, options, "x", 1);
  1654. return -1;
  1655. }
  1656. }
  1657. if (show_lock_owner && !show_thread_stats) {
  1658. pr_warning("Now -o try to show owner's callstack instead of pid and comm.\n");
  1659. pr_warning("Please use -t option too to keep the old behavior.\n");
  1660. }
  1661. return 0;
  1662. }
  1663. static int __cmd_contention(int argc, const char **argv)
  1664. {
  1665. int err = -EINVAL;
  1666. struct perf_tool eops;
  1667. struct perf_data data = {
  1668. .path = input_name,
  1669. .mode = PERF_DATA_MODE_READ,
  1670. .force = force,
  1671. };
  1672. struct lock_contention con = {
  1673. .target = &target,
  1674. .map_nr_entries = bpf_map_entries,
  1675. .max_stack = max_stack_depth,
  1676. .stack_skip = stack_skip,
  1677. .filters = &filters,
  1678. .delays = delays,
  1679. .nr_delays = nr_delays,
  1680. .save_callstack = needs_callstack(),
  1681. .owner = show_lock_owner,
  1682. .cgroups = RB_ROOT,
  1683. };
  1684. struct perf_env host_env;
  1685. lockhash_table = calloc(LOCKHASH_SIZE, sizeof(*lockhash_table));
  1686. if (!lockhash_table)
  1687. return -ENOMEM;
  1688. con.result = &lockhash_table[0];
  1689. perf_tool__init(&eops, /*ordered_events=*/true);
  1690. eops.attr = perf_event__process_attr;
  1691. eops.event_update = process_event_update;
  1692. eops.sample = process_sample_event;
  1693. eops.comm = perf_event__process_comm;
  1694. eops.mmap = perf_event__process_mmap;
  1695. eops.mmap2 = perf_event__process_mmap2;
  1696. eops.tracing_data = perf_event__process_tracing_data;
  1697. perf_env__init(&host_env);
  1698. session = __perf_session__new(use_bpf ? NULL : &data, &eops,
  1699. /*trace_event_repipe=*/false, &host_env);
  1700. if (IS_ERR(session)) {
  1701. pr_err("Initializing perf session failed\n");
  1702. err = PTR_ERR(session);
  1703. session = NULL;
  1704. goto out_delete;
  1705. }
  1706. con.machine = &session->machines.host;
  1707. con.aggr_mode = aggr_mode = show_thread_stats ? LOCK_AGGR_TASK :
  1708. show_lock_addrs ? LOCK_AGGR_ADDR :
  1709. show_lock_cgroups ? LOCK_AGGR_CGROUP : LOCK_AGGR_CALLER;
  1710. if (con.aggr_mode == LOCK_AGGR_CALLER)
  1711. con.save_callstack = true;
  1712. symbol_conf.allow_aliases = true;
  1713. symbol__init(perf_session__env(session));
  1714. if (use_bpf) {
  1715. err = target__validate(&target);
  1716. if (err) {
  1717. char errbuf[512];
  1718. target__strerror(&target, err, errbuf, 512);
  1719. pr_err("%s\n", errbuf);
  1720. goto out_delete;
  1721. }
  1722. signal(SIGINT, sighandler);
  1723. signal(SIGCHLD, sighandler);
  1724. signal(SIGTERM, sighandler);
  1725. con.evlist = evlist__new();
  1726. if (con.evlist == NULL) {
  1727. err = -ENOMEM;
  1728. goto out_delete;
  1729. }
  1730. err = evlist__create_maps(con.evlist, &target);
  1731. if (err < 0)
  1732. goto out_delete;
  1733. if (argc) {
  1734. err = evlist__prepare_workload(con.evlist, &target,
  1735. argv, false, NULL);
  1736. if (err < 0)
  1737. goto out_delete;
  1738. }
  1739. err = lock_contention_prepare(&con);
  1740. if (err < 0) {
  1741. pr_err("lock contention BPF setup failed\n");
  1742. goto out_delete;
  1743. }
  1744. } else if (!data.is_pipe) {
  1745. if (!perf_session__has_traces(session, "lock record"))
  1746. goto out_delete;
  1747. if (!evlist__find_evsel_by_str(session->evlist,
  1748. "lock:contention_begin")) {
  1749. pr_err("lock contention evsel not found\n");
  1750. goto out_delete;
  1751. }
  1752. if (perf_session__set_tracepoints_handlers(session,
  1753. contention_tracepoints)) {
  1754. pr_err("Initializing perf session tracepoint handlers failed\n");
  1755. goto out_delete;
  1756. }
  1757. }
  1758. err = setup_output_field(true, output_fields);
  1759. if (err) {
  1760. pr_err("Failed to setup output field\n");
  1761. goto out_delete;
  1762. }
  1763. err = select_key(true);
  1764. if (err)
  1765. goto out_delete;
  1766. if (symbol_conf.field_sep) {
  1767. int i;
  1768. struct lock_key *keys = contention_keys;
  1769. /* do not align output in CSV format */
  1770. for (i = 0; keys[i].name; i++)
  1771. keys[i].len = 0;
  1772. }
  1773. if (use_bpf) {
  1774. lock_contention_start();
  1775. if (argc)
  1776. evlist__start_workload(con.evlist);
  1777. /* wait for signal */
  1778. pause();
  1779. lock_contention_stop();
  1780. lock_contention_read(&con);
  1781. } else {
  1782. err = perf_session__process_events(session);
  1783. if (err)
  1784. goto out_delete;
  1785. }
  1786. setup_pager();
  1787. sort_contention_result();
  1788. print_contention_result(&con);
  1789. out_delete:
  1790. lock_filter_finish();
  1791. evlist__delete(con.evlist);
  1792. lock_contention_finish(&con);
  1793. perf_session__delete(session);
  1794. perf_env__exit(&host_env);
  1795. zfree(&lockhash_table);
  1796. return err;
  1797. }
  1798. static int __cmd_record(int argc, const char **argv)
  1799. {
  1800. const char *record_args[] = {
  1801. "record", "-R", "-m", "1024", "-c", "1", "--synth", "task",
  1802. };
  1803. const char *callgraph_args[] = {
  1804. "--call-graph", "fp," __stringify(CONTENTION_STACK_DEPTH),
  1805. };
  1806. unsigned int rec_argc, i, j, ret;
  1807. unsigned int nr_tracepoints;
  1808. unsigned int nr_callgraph_args = 0;
  1809. const char **rec_argv;
  1810. bool has_lock_stat = true;
  1811. for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
  1812. if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
  1813. pr_debug("tracepoint %s is not enabled. "
  1814. "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
  1815. lock_tracepoints[i].name);
  1816. has_lock_stat = false;
  1817. break;
  1818. }
  1819. }
  1820. if (has_lock_stat)
  1821. goto setup_args;
  1822. for (i = 0; i < ARRAY_SIZE(contention_tracepoints); i++) {
  1823. if (!is_valid_tracepoint(contention_tracepoints[i].name)) {
  1824. pr_err("tracepoint %s is not enabled.\n",
  1825. contention_tracepoints[i].name);
  1826. return 1;
  1827. }
  1828. }
  1829. nr_callgraph_args = ARRAY_SIZE(callgraph_args);
  1830. setup_args:
  1831. rec_argc = ARRAY_SIZE(record_args) + nr_callgraph_args + argc - 1;
  1832. if (has_lock_stat)
  1833. nr_tracepoints = ARRAY_SIZE(lock_tracepoints);
  1834. else
  1835. nr_tracepoints = ARRAY_SIZE(contention_tracepoints);
  1836. /* factor of 2 is for -e in front of each tracepoint */
  1837. rec_argc += 2 * nr_tracepoints;
  1838. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1839. if (!rec_argv)
  1840. return -ENOMEM;
  1841. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1842. rec_argv[i] = record_args[i];
  1843. for (j = 0; j < nr_tracepoints; j++) {
  1844. rec_argv[i++] = "-e";
  1845. rec_argv[i++] = has_lock_stat
  1846. ? lock_tracepoints[j].name
  1847. : contention_tracepoints[j].name;
  1848. }
  1849. for (j = 0; j < nr_callgraph_args; j++, i++)
  1850. rec_argv[i] = callgraph_args[j];
  1851. for (j = 1; j < (unsigned int)argc; j++, i++)
  1852. rec_argv[i] = argv[j];
  1853. BUG_ON(i != rec_argc);
  1854. ret = cmd_record(i, rec_argv);
  1855. free(rec_argv);
  1856. return ret;
  1857. }
  1858. static int parse_map_entry(const struct option *opt, const char *str,
  1859. int unset __maybe_unused)
  1860. {
  1861. unsigned long *len = (unsigned long *)opt->value;
  1862. unsigned long val;
  1863. char *endptr;
  1864. errno = 0;
  1865. val = strtoul(str, &endptr, 0);
  1866. if (*endptr != '\0' || errno != 0) {
  1867. pr_err("invalid BPF map length: %s\n", str);
  1868. return -1;
  1869. }
  1870. *len = val;
  1871. return 0;
  1872. }
  1873. static int parse_max_stack(const struct option *opt, const char *str,
  1874. int unset __maybe_unused)
  1875. {
  1876. unsigned long *len = (unsigned long *)opt->value;
  1877. long val;
  1878. char *endptr;
  1879. errno = 0;
  1880. val = strtol(str, &endptr, 0);
  1881. if (*endptr != '\0' || errno != 0) {
  1882. pr_err("invalid max stack depth: %s\n", str);
  1883. return -1;
  1884. }
  1885. if (val < 0 || val > sysctl__max_stack()) {
  1886. pr_err("invalid max stack depth: %ld\n", val);
  1887. return -1;
  1888. }
  1889. *len = val;
  1890. return 0;
  1891. }
  1892. static bool add_lock_type(unsigned int flags)
  1893. {
  1894. unsigned int *tmp;
  1895. tmp = realloc(filters.types, (filters.nr_types + 1) * sizeof(*filters.types));
  1896. if (tmp == NULL)
  1897. return false;
  1898. tmp[filters.nr_types++] = flags;
  1899. filters.types = tmp;
  1900. return true;
  1901. }
  1902. static int parse_lock_type(const struct option *opt __maybe_unused, const char *str,
  1903. int unset __maybe_unused)
  1904. {
  1905. char *s, *tmp, *tok;
  1906. s = strdup(str);
  1907. if (s == NULL)
  1908. return -1;
  1909. for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1910. bool found = false;
  1911. /* `tok` is a flags name if it contains ':'. */
  1912. if (strchr(tok, ':')) {
  1913. for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
  1914. if (!strcmp(lock_type_table[i].flags_name, tok) &&
  1915. add_lock_type(lock_type_table[i].flags)) {
  1916. found = true;
  1917. break;
  1918. }
  1919. }
  1920. if (!found) {
  1921. pr_err("Unknown lock flags name: %s\n", tok);
  1922. free(s);
  1923. return -1;
  1924. }
  1925. continue;
  1926. }
  1927. /*
  1928. * Otherwise `tok` is a lock name.
  1929. * Single lock name could contain multiple flags.
  1930. * Replace alias `pcpu-sem` with actual name `percpu-rwsem.
  1931. */
  1932. if (!strcmp(tok, "pcpu-sem"))
  1933. tok = (char *)"percpu-rwsem";
  1934. for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
  1935. if (!strcmp(lock_type_table[i].lock_name, tok)) {
  1936. if (add_lock_type(lock_type_table[i].flags)) {
  1937. found = true;
  1938. } else {
  1939. free(s);
  1940. return -1;
  1941. }
  1942. }
  1943. }
  1944. if (!found) {
  1945. pr_err("Unknown lock name: %s\n", tok);
  1946. free(s);
  1947. return -1;
  1948. }
  1949. }
  1950. free(s);
  1951. return 0;
  1952. }
  1953. static bool add_lock_addr(unsigned long addr)
  1954. {
  1955. unsigned long *tmp;
  1956. tmp = realloc(filters.addrs, (filters.nr_addrs + 1) * sizeof(*filters.addrs));
  1957. if (tmp == NULL) {
  1958. pr_err("Memory allocation failure\n");
  1959. return false;
  1960. }
  1961. tmp[filters.nr_addrs++] = addr;
  1962. filters.addrs = tmp;
  1963. return true;
  1964. }
  1965. static bool add_lock_sym(char *name)
  1966. {
  1967. char **tmp;
  1968. char *sym = strdup(name);
  1969. if (sym == NULL) {
  1970. pr_err("Memory allocation failure\n");
  1971. return false;
  1972. }
  1973. tmp = realloc(filters.syms, (filters.nr_syms + 1) * sizeof(*filters.syms));
  1974. if (tmp == NULL) {
  1975. pr_err("Memory allocation failure\n");
  1976. free(sym);
  1977. return false;
  1978. }
  1979. tmp[filters.nr_syms++] = sym;
  1980. filters.syms = tmp;
  1981. return true;
  1982. }
  1983. static bool add_lock_slab(char *name)
  1984. {
  1985. char **tmp;
  1986. char *sym = strdup(name);
  1987. if (sym == NULL) {
  1988. pr_err("Memory allocation failure\n");
  1989. return false;
  1990. }
  1991. tmp = realloc(filters.slabs, (filters.nr_slabs + 1) * sizeof(*filters.slabs));
  1992. if (tmp == NULL) {
  1993. pr_err("Memory allocation failure\n");
  1994. return false;
  1995. }
  1996. tmp[filters.nr_slabs++] = sym;
  1997. filters.slabs = tmp;
  1998. return true;
  1999. }
  2000. static int parse_lock_addr(const struct option *opt __maybe_unused, const char *str,
  2001. int unset __maybe_unused)
  2002. {
  2003. char *s, *tmp, *tok;
  2004. int ret = 0;
  2005. u64 addr;
  2006. s = strdup(str);
  2007. if (s == NULL)
  2008. return -1;
  2009. for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
  2010. char *end;
  2011. addr = strtoul(tok, &end, 16);
  2012. if (*end == '\0') {
  2013. if (!add_lock_addr(addr)) {
  2014. ret = -1;
  2015. break;
  2016. }
  2017. continue;
  2018. }
  2019. if (*tok == '&') {
  2020. if (!add_lock_slab(tok + 1)) {
  2021. ret = -1;
  2022. break;
  2023. }
  2024. continue;
  2025. }
  2026. /*
  2027. * At this moment, we don't have kernel symbols. Save the symbols
  2028. * in a separate list and resolve them to addresses later.
  2029. */
  2030. if (!add_lock_sym(tok)) {
  2031. ret = -1;
  2032. break;
  2033. }
  2034. }
  2035. free(s);
  2036. return ret;
  2037. }
  2038. static int parse_output(const struct option *opt __maybe_unused, const char *str,
  2039. int unset __maybe_unused)
  2040. {
  2041. const char **name = (const char **)opt->value;
  2042. if (str == NULL)
  2043. return -1;
  2044. lock_output = fopen(str, "w");
  2045. if (lock_output == NULL) {
  2046. pr_err("Cannot open %s\n", str);
  2047. return -1;
  2048. }
  2049. *name = str;
  2050. return 0;
  2051. }
  2052. static bool add_lock_cgroup(char *name)
  2053. {
  2054. u64 *tmp;
  2055. struct cgroup *cgrp;
  2056. cgrp = cgroup__new(name, /*do_open=*/false);
  2057. if (cgrp == NULL) {
  2058. pr_err("Failed to create cgroup: %s\n", name);
  2059. return false;
  2060. }
  2061. if (read_cgroup_id(cgrp) < 0) {
  2062. pr_err("Failed to read cgroup id for %s\n", name);
  2063. cgroup__put(cgrp);
  2064. return false;
  2065. }
  2066. tmp = realloc(filters.cgrps, (filters.nr_cgrps + 1) * sizeof(*filters.cgrps));
  2067. if (tmp == NULL) {
  2068. pr_err("Memory allocation failure\n");
  2069. return false;
  2070. }
  2071. tmp[filters.nr_cgrps++] = cgrp->id;
  2072. filters.cgrps = tmp;
  2073. cgroup__put(cgrp);
  2074. return true;
  2075. }
  2076. static int parse_cgroup_filter(const struct option *opt __maybe_unused, const char *str,
  2077. int unset __maybe_unused)
  2078. {
  2079. char *s, *tmp, *tok;
  2080. int ret = 0;
  2081. s = strdup(str);
  2082. if (s == NULL)
  2083. return -1;
  2084. for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
  2085. if (!add_lock_cgroup(tok)) {
  2086. ret = -1;
  2087. break;
  2088. }
  2089. }
  2090. free(s);
  2091. return ret;
  2092. }
  2093. static bool add_lock_delay(char *spec)
  2094. {
  2095. char *at, *pos;
  2096. struct lock_delay *tmp;
  2097. unsigned long duration;
  2098. at = strchr(spec, '@');
  2099. if (at == NULL) {
  2100. pr_err("lock delay should have '@' sign: %s\n", spec);
  2101. return false;
  2102. }
  2103. if (at == spec) {
  2104. pr_err("lock delay should have time before '@': %s\n", spec);
  2105. return false;
  2106. }
  2107. *at = '\0';
  2108. duration = strtoul(spec, &pos, 0);
  2109. if (!strcmp(pos, "ns"))
  2110. duration *= 1;
  2111. else if (!strcmp(pos, "us"))
  2112. duration *= 1000;
  2113. else if (!strcmp(pos, "ms"))
  2114. duration *= 1000 * 1000;
  2115. else if (*pos) {
  2116. pr_err("invalid delay time: %s@%s\n", spec, at + 1);
  2117. return false;
  2118. }
  2119. if (duration > 10 * 1000 * 1000) {
  2120. pr_err("lock delay is too long: %s (> 10ms)\n", spec);
  2121. return false;
  2122. }
  2123. tmp = realloc(delays, (nr_delays + 1) * sizeof(*delays));
  2124. if (tmp == NULL) {
  2125. pr_err("Memory allocation failure\n");
  2126. return false;
  2127. }
  2128. delays = tmp;
  2129. delays[nr_delays].sym = strdup(at + 1);
  2130. if (delays[nr_delays].sym == NULL) {
  2131. pr_err("Memory allocation failure\n");
  2132. return false;
  2133. }
  2134. delays[nr_delays].time = duration;
  2135. nr_delays++;
  2136. return true;
  2137. }
  2138. static int parse_lock_delay(const struct option *opt __maybe_unused, const char *str,
  2139. int unset __maybe_unused)
  2140. {
  2141. char *s, *tmp, *tok;
  2142. int ret = 0;
  2143. s = strdup(str);
  2144. if (s == NULL)
  2145. return -1;
  2146. for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
  2147. if (!add_lock_delay(tok)) {
  2148. ret = -1;
  2149. break;
  2150. }
  2151. }
  2152. free(s);
  2153. return ret;
  2154. }
  2155. int cmd_lock(int argc, const char **argv)
  2156. {
  2157. const struct option lock_options[] = {
  2158. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  2159. OPT_CALLBACK(0, "output", &output_name, "file", "output file name", parse_output),
  2160. OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
  2161. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
  2162. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  2163. OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
  2164. "file", "vmlinux pathname"),
  2165. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  2166. "file", "kallsyms pathname"),
  2167. OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"),
  2168. OPT_END()
  2169. };
  2170. const struct option info_options[] = {
  2171. OPT_BOOLEAN('t', "threads", &info_threads,
  2172. "dump the thread list in perf.data"),
  2173. OPT_BOOLEAN('m', "map", &info_map,
  2174. "dump the map of lock instances (address:name table)"),
  2175. OPT_PARENT(lock_options)
  2176. };
  2177. const struct option report_options[] = {
  2178. OPT_STRING('k', "key", &sort_key, "acquired",
  2179. "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
  2180. OPT_STRING('F', "field", &output_fields, NULL,
  2181. "output fields (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
  2182. /* TODO: type */
  2183. OPT_BOOLEAN('c', "combine-locks", &combine_locks,
  2184. "combine locks in the same class"),
  2185. OPT_BOOLEAN('t', "threads", &show_thread_stats,
  2186. "show per-thread lock stats"),
  2187. OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functions"),
  2188. OPT_PARENT(lock_options)
  2189. };
  2190. struct option contention_options[] = {
  2191. OPT_STRING('k', "key", &sort_key, "wait_total",
  2192. "key for sorting (contended / wait_total / wait_max / wait_min / avg_wait)"),
  2193. OPT_STRING('F', "field", &output_fields, "contended,wait_total,wait_max,avg_wait",
  2194. "output fields (contended / wait_total / wait_max / wait_min / avg_wait)"),
  2195. OPT_BOOLEAN('t', "threads", &show_thread_stats,
  2196. "show per-thread lock stats"),
  2197. OPT_BOOLEAN('b', "use-bpf", &use_bpf, "use BPF program to collect lock contention stats"),
  2198. OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
  2199. "System-wide collection from all CPUs"),
  2200. OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
  2201. "List of cpus to monitor"),
  2202. OPT_STRING('p', "pid", &target.pid, "pid",
  2203. "Trace on existing process id"),
  2204. OPT_STRING(0, "tid", &target.tid, "tid",
  2205. "Trace on existing thread id (exclusive to --pid)"),
  2206. OPT_CALLBACK('M', "map-nr-entries", &bpf_map_entries, "num",
  2207. "Max number of BPF map entries", parse_map_entry),
  2208. OPT_CALLBACK(0, "max-stack", &max_stack_depth, "num",
  2209. "Set the maximum stack depth when collecting lock contention, "
  2210. "Default: " __stringify(CONTENTION_STACK_DEPTH), parse_max_stack),
  2211. OPT_INTEGER(0, "stack-skip", &stack_skip,
  2212. "Set the number of stack depth to skip when finding a lock caller, "
  2213. "Default: " __stringify(CONTENTION_STACK_SKIP)),
  2214. OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functions"),
  2215. OPT_BOOLEAN('l', "lock-addr", &show_lock_addrs, "show lock stats by address"),
  2216. OPT_CALLBACK('Y', "type-filter", NULL, "FLAGS",
  2217. "Filter specific type of locks", parse_lock_type),
  2218. OPT_CALLBACK('L', "lock-filter", NULL, "ADDRS/NAMES",
  2219. "Filter specific address/symbol of locks", parse_lock_addr),
  2220. OPT_CALLBACK('S', "callstack-filter", NULL, "NAMES",
  2221. "Filter specific function in the callstack", parse_call_stack),
  2222. OPT_BOOLEAN('o', "lock-owner", &show_lock_owner, "show lock owners instead of waiters"),
  2223. OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep, "separator",
  2224. "print result in CSV format with custom separator"),
  2225. OPT_BOOLEAN(0, "lock-cgroup", &show_lock_cgroups, "show lock stats by cgroup"),
  2226. OPT_CALLBACK('G', "cgroup-filter", NULL, "CGROUPS",
  2227. "Filter specific cgroups", parse_cgroup_filter),
  2228. OPT_CALLBACK('J', "inject-delay", NULL, "TIME@FUNC",
  2229. "Inject delays to specific locks", parse_lock_delay),
  2230. OPT_PARENT(lock_options)
  2231. };
  2232. const char * const info_usage[] = {
  2233. "perf lock info [<options>]",
  2234. NULL
  2235. };
  2236. const char *const lock_subcommands[] = { "record", "report", "script",
  2237. "info", "contention", NULL };
  2238. const char *lock_usage[] = {
  2239. NULL,
  2240. NULL
  2241. };
  2242. const char * const report_usage[] = {
  2243. "perf lock report [<options>]",
  2244. NULL
  2245. };
  2246. const char * const contention_usage[] = {
  2247. "perf lock contention [<options>]",
  2248. NULL
  2249. };
  2250. unsigned int i;
  2251. int rc = 0;
  2252. lockhash_table = calloc(LOCKHASH_SIZE, sizeof(*lockhash_table));
  2253. if (!lockhash_table)
  2254. return -ENOMEM;
  2255. for (i = 0; i < LOCKHASH_SIZE; i++)
  2256. INIT_HLIST_HEAD(lockhash_table + i);
  2257. lock_output = stderr;
  2258. argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
  2259. lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  2260. if (!argc)
  2261. usage_with_options(lock_usage, lock_options);
  2262. if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
  2263. return __cmd_record(argc, argv);
  2264. } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
  2265. trace_handler = &report_lock_ops;
  2266. if (argc) {
  2267. argc = parse_options(argc, argv,
  2268. report_options, report_usage, 0);
  2269. if (argc)
  2270. usage_with_options(report_usage, report_options);
  2271. }
  2272. rc = __cmd_report(false);
  2273. } else if (!strcmp(argv[0], "script")) {
  2274. /* Aliased to 'perf script' */
  2275. rc = cmd_script(argc, argv);
  2276. } else if (!strcmp(argv[0], "info")) {
  2277. if (argc) {
  2278. argc = parse_options(argc, argv,
  2279. info_options, info_usage, 0);
  2280. if (argc)
  2281. usage_with_options(info_usage, info_options);
  2282. }
  2283. /* If neither threads nor map requested, display both */
  2284. if (!info_threads && !info_map) {
  2285. info_threads = true;
  2286. info_map = true;
  2287. }
  2288. /* recycling report_lock_ops */
  2289. trace_handler = &report_lock_ops;
  2290. rc = __cmd_report(true);
  2291. } else if (strlen(argv[0]) > 2 && strstarts("contention", argv[0])) {
  2292. trace_handler = &contention_lock_ops;
  2293. sort_key = "wait_total";
  2294. output_fields = "contended,wait_total,wait_max,avg_wait";
  2295. #ifndef HAVE_BPF_SKEL
  2296. set_option_nobuild(contention_options, 'b', "use-bpf",
  2297. "no BUILD_BPF_SKEL=1", false);
  2298. #endif
  2299. if (argc) {
  2300. argc = parse_options(argc, argv, contention_options,
  2301. contention_usage, 0);
  2302. }
  2303. if (check_lock_contention_options(contention_options,
  2304. contention_usage) < 0)
  2305. return -1;
  2306. rc = __cmd_contention(argc, argv);
  2307. } else {
  2308. usage_with_options(lock_usage, lock_options);
  2309. }
  2310. /* free usage string allocated by parse_options_subcommand */
  2311. free((void *)lock_usage[0]);
  2312. zfree(&lockhash_table);
  2313. return rc;
  2314. }