sort.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. #include <sys/mman.h>
  2. #include "sort.h"
  3. #include "hist.h"
  4. #include "comm.h"
  5. #include "symbol.h"
  6. #include "evsel.h"
  7. regex_t parent_regex;
  8. const char default_parent_pattern[] = "^sys_|^do_page_fault";
  9. const char *parent_pattern = default_parent_pattern;
  10. const char default_sort_order[] = "comm,dso,symbol";
  11. const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
  12. const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
  13. const char default_top_sort_order[] = "dso,symbol";
  14. const char default_diff_sort_order[] = "dso,symbol";
  15. const char *sort_order;
  16. const char *field_order;
  17. regex_t ignore_callees_regex;
  18. int have_ignore_callees = 0;
  19. int sort__need_collapse = 0;
  20. int sort__has_parent = 0;
  21. int sort__has_sym = 0;
  22. int sort__has_dso = 0;
  23. enum sort_mode sort__mode = SORT_MODE__NORMAL;
  24. static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
  25. {
  26. int n;
  27. va_list ap;
  28. va_start(ap, fmt);
  29. n = vsnprintf(bf, size, fmt, ap);
  30. if (symbol_conf.field_sep && n > 0) {
  31. char *sep = bf;
  32. while (1) {
  33. sep = strchr(sep, *symbol_conf.field_sep);
  34. if (sep == NULL)
  35. break;
  36. *sep = '.';
  37. }
  38. }
  39. va_end(ap);
  40. if (n >= (int)size)
  41. return size - 1;
  42. return n;
  43. }
  44. static int64_t cmp_null(const void *l, const void *r)
  45. {
  46. if (!l && !r)
  47. return 0;
  48. else if (!l)
  49. return -1;
  50. else
  51. return 1;
  52. }
  53. /* --sort pid */
  54. static int64_t
  55. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  56. {
  57. return right->thread->tid - left->thread->tid;
  58. }
  59. static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
  60. size_t size, unsigned int width)
  61. {
  62. const char *comm = thread__comm_str(he->thread);
  63. width = max(7U, width) - 6;
  64. return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid,
  65. width, width, comm ?: "");
  66. }
  67. struct sort_entry sort_thread = {
  68. .se_header = " Pid:Command",
  69. .se_cmp = sort__thread_cmp,
  70. .se_snprintf = hist_entry__thread_snprintf,
  71. .se_width_idx = HISTC_THREAD,
  72. };
  73. /* --sort comm */
  74. static int64_t
  75. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  76. {
  77. /* Compare the addr that should be unique among comm */
  78. return strcmp(comm__str(right->comm), comm__str(left->comm));
  79. }
  80. static int64_t
  81. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  82. {
  83. /* Compare the addr that should be unique among comm */
  84. return strcmp(comm__str(right->comm), comm__str(left->comm));
  85. }
  86. static int64_t
  87. sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
  88. {
  89. return strcmp(comm__str(right->comm), comm__str(left->comm));
  90. }
  91. static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
  92. size_t size, unsigned int width)
  93. {
  94. return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
  95. }
  96. struct sort_entry sort_comm = {
  97. .se_header = "Command",
  98. .se_cmp = sort__comm_cmp,
  99. .se_collapse = sort__comm_collapse,
  100. .se_sort = sort__comm_sort,
  101. .se_snprintf = hist_entry__comm_snprintf,
  102. .se_width_idx = HISTC_COMM,
  103. };
  104. /* --sort dso */
  105. static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
  106. {
  107. struct dso *dso_l = map_l ? map_l->dso : NULL;
  108. struct dso *dso_r = map_r ? map_r->dso : NULL;
  109. const char *dso_name_l, *dso_name_r;
  110. if (!dso_l || !dso_r)
  111. return cmp_null(dso_r, dso_l);
  112. if (verbose) {
  113. dso_name_l = dso_l->long_name;
  114. dso_name_r = dso_r->long_name;
  115. } else {
  116. dso_name_l = dso_l->short_name;
  117. dso_name_r = dso_r->short_name;
  118. }
  119. return strcmp(dso_name_l, dso_name_r);
  120. }
  121. static int64_t
  122. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  123. {
  124. return _sort__dso_cmp(right->ms.map, left->ms.map);
  125. }
  126. static int _hist_entry__dso_snprintf(struct map *map, char *bf,
  127. size_t size, unsigned int width)
  128. {
  129. if (map && map->dso) {
  130. const char *dso_name = !verbose ? map->dso->short_name :
  131. map->dso->long_name;
  132. return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
  133. }
  134. return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
  135. }
  136. static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
  137. size_t size, unsigned int width)
  138. {
  139. return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
  140. }
  141. struct sort_entry sort_dso = {
  142. .se_header = "Shared Object",
  143. .se_cmp = sort__dso_cmp,
  144. .se_snprintf = hist_entry__dso_snprintf,
  145. .se_width_idx = HISTC_DSO,
  146. };
  147. /* --sort symbol */
  148. static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
  149. {
  150. return (int64_t)(right_ip - left_ip);
  151. }
  152. static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
  153. {
  154. if (!sym_l || !sym_r)
  155. return cmp_null(sym_l, sym_r);
  156. if (sym_l == sym_r)
  157. return 0;
  158. if (sym_l->start != sym_r->start)
  159. return (int64_t)(sym_r->start - sym_l->start);
  160. return (int64_t)(sym_r->end - sym_l->end);
  161. }
  162. static int64_t
  163. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  164. {
  165. int64_t ret;
  166. if (!left->ms.sym && !right->ms.sym)
  167. return _sort__addr_cmp(left->ip, right->ip);
  168. /*
  169. * comparing symbol address alone is not enough since it's a
  170. * relative address within a dso.
  171. */
  172. if (!sort__has_dso) {
  173. ret = sort__dso_cmp(left, right);
  174. if (ret != 0)
  175. return ret;
  176. }
  177. return _sort__sym_cmp(left->ms.sym, right->ms.sym);
  178. }
  179. static int64_t
  180. sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
  181. {
  182. if (!left->ms.sym || !right->ms.sym)
  183. return cmp_null(left->ms.sym, right->ms.sym);
  184. return strcmp(right->ms.sym->name, left->ms.sym->name);
  185. }
  186. static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
  187. u64 ip, char level, char *bf, size_t size,
  188. unsigned int width)
  189. {
  190. size_t ret = 0;
  191. if (verbose) {
  192. char o = map ? dso__symtab_origin(map->dso) : '!';
  193. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  194. BITS_PER_LONG / 4 + 2, ip, o);
  195. }
  196. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
  197. if (sym && map) {
  198. if (map->type == MAP__VARIABLE) {
  199. ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
  200. ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
  201. ip - map->unmap_ip(map, sym->start));
  202. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  203. width - ret, "");
  204. } else {
  205. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  206. width - ret,
  207. sym->name);
  208. }
  209. } else {
  210. size_t len = BITS_PER_LONG / 4;
  211. ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
  212. len, ip);
  213. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  214. width - ret, "");
  215. }
  216. if (ret > width)
  217. bf[width] = '\0';
  218. return width;
  219. }
  220. static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
  221. size_t size, unsigned int width)
  222. {
  223. return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
  224. he->level, bf, size, width);
  225. }
  226. struct sort_entry sort_sym = {
  227. .se_header = "Symbol",
  228. .se_cmp = sort__sym_cmp,
  229. .se_sort = sort__sym_sort,
  230. .se_snprintf = hist_entry__sym_snprintf,
  231. .se_width_idx = HISTC_SYMBOL,
  232. };
  233. /* --sort srcline */
  234. static int64_t
  235. sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
  236. {
  237. if (!left->srcline) {
  238. if (!left->ms.map)
  239. left->srcline = SRCLINE_UNKNOWN;
  240. else {
  241. struct map *map = left->ms.map;
  242. left->srcline = get_srcline(map->dso,
  243. map__rip_2objdump(map, left->ip),
  244. left->ms.sym, true);
  245. }
  246. }
  247. if (!right->srcline) {
  248. if (!right->ms.map)
  249. right->srcline = SRCLINE_UNKNOWN;
  250. else {
  251. struct map *map = right->ms.map;
  252. right->srcline = get_srcline(map->dso,
  253. map__rip_2objdump(map, right->ip),
  254. right->ms.sym, true);
  255. }
  256. }
  257. return strcmp(right->srcline, left->srcline);
  258. }
  259. static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
  260. size_t size, unsigned int width)
  261. {
  262. return repsep_snprintf(bf, size, "%-*.*s", width, width, he->srcline);
  263. }
  264. struct sort_entry sort_srcline = {
  265. .se_header = "Source:Line",
  266. .se_cmp = sort__srcline_cmp,
  267. .se_snprintf = hist_entry__srcline_snprintf,
  268. .se_width_idx = HISTC_SRCLINE,
  269. };
  270. /* --sort srcfile */
  271. static char no_srcfile[1];
  272. static char *get_srcfile(struct hist_entry *e)
  273. {
  274. char *sf, *p;
  275. struct map *map = e->ms.map;
  276. sf = get_srcline(map->dso, map__rip_2objdump(map, e->ip),
  277. e->ms.sym, true);
  278. if (!strcmp(sf, SRCLINE_UNKNOWN))
  279. return no_srcfile;
  280. p = strchr(sf, ':');
  281. if (p && *sf) {
  282. *p = 0;
  283. return sf;
  284. }
  285. free(sf);
  286. return no_srcfile;
  287. }
  288. static int64_t
  289. sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
  290. {
  291. if (!left->srcfile) {
  292. if (!left->ms.map)
  293. left->srcfile = no_srcfile;
  294. else
  295. left->srcfile = get_srcfile(left);
  296. }
  297. if (!right->srcfile) {
  298. if (!right->ms.map)
  299. right->srcfile = no_srcfile;
  300. else
  301. right->srcfile = get_srcfile(right);
  302. }
  303. return strcmp(right->srcfile, left->srcfile);
  304. }
  305. static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
  306. size_t size, unsigned int width)
  307. {
  308. return repsep_snprintf(bf, size, "%-*.*s", width, width, he->srcfile);
  309. }
  310. struct sort_entry sort_srcfile = {
  311. .se_header = "Source File",
  312. .se_cmp = sort__srcfile_cmp,
  313. .se_snprintf = hist_entry__srcfile_snprintf,
  314. .se_width_idx = HISTC_SRCFILE,
  315. };
  316. /* --sort parent */
  317. static int64_t
  318. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  319. {
  320. struct symbol *sym_l = left->parent;
  321. struct symbol *sym_r = right->parent;
  322. if (!sym_l || !sym_r)
  323. return cmp_null(sym_l, sym_r);
  324. return strcmp(sym_r->name, sym_l->name);
  325. }
  326. static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
  327. size_t size, unsigned int width)
  328. {
  329. return repsep_snprintf(bf, size, "%-*.*s", width, width,
  330. he->parent ? he->parent->name : "[other]");
  331. }
  332. struct sort_entry sort_parent = {
  333. .se_header = "Parent symbol",
  334. .se_cmp = sort__parent_cmp,
  335. .se_snprintf = hist_entry__parent_snprintf,
  336. .se_width_idx = HISTC_PARENT,
  337. };
  338. /* --sort cpu */
  339. static int64_t
  340. sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
  341. {
  342. return right->cpu - left->cpu;
  343. }
  344. static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
  345. size_t size, unsigned int width)
  346. {
  347. return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
  348. }
  349. struct sort_entry sort_cpu = {
  350. .se_header = "CPU",
  351. .se_cmp = sort__cpu_cmp,
  352. .se_snprintf = hist_entry__cpu_snprintf,
  353. .se_width_idx = HISTC_CPU,
  354. };
  355. /* sort keys for branch stacks */
  356. static int64_t
  357. sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
  358. {
  359. if (!left->branch_info || !right->branch_info)
  360. return cmp_null(left->branch_info, right->branch_info);
  361. return _sort__dso_cmp(left->branch_info->from.map,
  362. right->branch_info->from.map);
  363. }
  364. static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
  365. size_t size, unsigned int width)
  366. {
  367. if (he->branch_info)
  368. return _hist_entry__dso_snprintf(he->branch_info->from.map,
  369. bf, size, width);
  370. else
  371. return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
  372. }
  373. static int64_t
  374. sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
  375. {
  376. if (!left->branch_info || !right->branch_info)
  377. return cmp_null(left->branch_info, right->branch_info);
  378. return _sort__dso_cmp(left->branch_info->to.map,
  379. right->branch_info->to.map);
  380. }
  381. static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
  382. size_t size, unsigned int width)
  383. {
  384. if (he->branch_info)
  385. return _hist_entry__dso_snprintf(he->branch_info->to.map,
  386. bf, size, width);
  387. else
  388. return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
  389. }
  390. static int64_t
  391. sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
  392. {
  393. struct addr_map_symbol *from_l = &left->branch_info->from;
  394. struct addr_map_symbol *from_r = &right->branch_info->from;
  395. if (!left->branch_info || !right->branch_info)
  396. return cmp_null(left->branch_info, right->branch_info);
  397. from_l = &left->branch_info->from;
  398. from_r = &right->branch_info->from;
  399. if (!from_l->sym && !from_r->sym)
  400. return _sort__addr_cmp(from_l->addr, from_r->addr);
  401. return _sort__sym_cmp(from_l->sym, from_r->sym);
  402. }
  403. static int64_t
  404. sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
  405. {
  406. struct addr_map_symbol *to_l, *to_r;
  407. if (!left->branch_info || !right->branch_info)
  408. return cmp_null(left->branch_info, right->branch_info);
  409. to_l = &left->branch_info->to;
  410. to_r = &right->branch_info->to;
  411. if (!to_l->sym && !to_r->sym)
  412. return _sort__addr_cmp(to_l->addr, to_r->addr);
  413. return _sort__sym_cmp(to_l->sym, to_r->sym);
  414. }
  415. static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
  416. size_t size, unsigned int width)
  417. {
  418. if (he->branch_info) {
  419. struct addr_map_symbol *from = &he->branch_info->from;
  420. return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
  421. he->level, bf, size, width);
  422. }
  423. return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
  424. }
  425. static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
  426. size_t size, unsigned int width)
  427. {
  428. if (he->branch_info) {
  429. struct addr_map_symbol *to = &he->branch_info->to;
  430. return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
  431. he->level, bf, size, width);
  432. }
  433. return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
  434. }
  435. struct sort_entry sort_dso_from = {
  436. .se_header = "Source Shared Object",
  437. .se_cmp = sort__dso_from_cmp,
  438. .se_snprintf = hist_entry__dso_from_snprintf,
  439. .se_width_idx = HISTC_DSO_FROM,
  440. };
  441. struct sort_entry sort_dso_to = {
  442. .se_header = "Target Shared Object",
  443. .se_cmp = sort__dso_to_cmp,
  444. .se_snprintf = hist_entry__dso_to_snprintf,
  445. .se_width_idx = HISTC_DSO_TO,
  446. };
  447. struct sort_entry sort_sym_from = {
  448. .se_header = "Source Symbol",
  449. .se_cmp = sort__sym_from_cmp,
  450. .se_snprintf = hist_entry__sym_from_snprintf,
  451. .se_width_idx = HISTC_SYMBOL_FROM,
  452. };
  453. struct sort_entry sort_sym_to = {
  454. .se_header = "Target Symbol",
  455. .se_cmp = sort__sym_to_cmp,
  456. .se_snprintf = hist_entry__sym_to_snprintf,
  457. .se_width_idx = HISTC_SYMBOL_TO,
  458. };
  459. static int64_t
  460. sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
  461. {
  462. unsigned char mp, p;
  463. if (!left->branch_info || !right->branch_info)
  464. return cmp_null(left->branch_info, right->branch_info);
  465. mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
  466. p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
  467. return mp || p;
  468. }
  469. static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
  470. size_t size, unsigned int width){
  471. static const char *out = "N/A";
  472. if (he->branch_info) {
  473. if (he->branch_info->flags.predicted)
  474. out = "N";
  475. else if (he->branch_info->flags.mispred)
  476. out = "Y";
  477. }
  478. return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
  479. }
  480. static int64_t
  481. sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
  482. {
  483. return left->branch_info->flags.cycles -
  484. right->branch_info->flags.cycles;
  485. }
  486. static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
  487. size_t size, unsigned int width)
  488. {
  489. if (he->branch_info->flags.cycles == 0)
  490. return repsep_snprintf(bf, size, "%-*s", width, "-");
  491. return repsep_snprintf(bf, size, "%-*hd", width,
  492. he->branch_info->flags.cycles);
  493. }
  494. struct sort_entry sort_cycles = {
  495. .se_header = "Basic Block Cycles",
  496. .se_cmp = sort__cycles_cmp,
  497. .se_snprintf = hist_entry__cycles_snprintf,
  498. .se_width_idx = HISTC_CYCLES,
  499. };
  500. /* --sort daddr_sym */
  501. static int64_t
  502. sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  503. {
  504. uint64_t l = 0, r = 0;
  505. if (left->mem_info)
  506. l = left->mem_info->daddr.addr;
  507. if (right->mem_info)
  508. r = right->mem_info->daddr.addr;
  509. return (int64_t)(r - l);
  510. }
  511. static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
  512. size_t size, unsigned int width)
  513. {
  514. uint64_t addr = 0;
  515. struct map *map = NULL;
  516. struct symbol *sym = NULL;
  517. if (he->mem_info) {
  518. addr = he->mem_info->daddr.addr;
  519. map = he->mem_info->daddr.map;
  520. sym = he->mem_info->daddr.sym;
  521. }
  522. return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
  523. width);
  524. }
  525. static int64_t
  526. sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  527. {
  528. struct map *map_l = NULL;
  529. struct map *map_r = NULL;
  530. if (left->mem_info)
  531. map_l = left->mem_info->daddr.map;
  532. if (right->mem_info)
  533. map_r = right->mem_info->daddr.map;
  534. return _sort__dso_cmp(map_l, map_r);
  535. }
  536. static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
  537. size_t size, unsigned int width)
  538. {
  539. struct map *map = NULL;
  540. if (he->mem_info)
  541. map = he->mem_info->daddr.map;
  542. return _hist_entry__dso_snprintf(map, bf, size, width);
  543. }
  544. static int64_t
  545. sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
  546. {
  547. union perf_mem_data_src data_src_l;
  548. union perf_mem_data_src data_src_r;
  549. if (left->mem_info)
  550. data_src_l = left->mem_info->data_src;
  551. else
  552. data_src_l.mem_lock = PERF_MEM_LOCK_NA;
  553. if (right->mem_info)
  554. data_src_r = right->mem_info->data_src;
  555. else
  556. data_src_r.mem_lock = PERF_MEM_LOCK_NA;
  557. return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
  558. }
  559. static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
  560. size_t size, unsigned int width)
  561. {
  562. const char *out;
  563. u64 mask = PERF_MEM_LOCK_NA;
  564. if (he->mem_info)
  565. mask = he->mem_info->data_src.mem_lock;
  566. if (mask & PERF_MEM_LOCK_NA)
  567. out = "N/A";
  568. else if (mask & PERF_MEM_LOCK_LOCKED)
  569. out = "Yes";
  570. else
  571. out = "No";
  572. return repsep_snprintf(bf, size, "%-*s", width, out);
  573. }
  574. static int64_t
  575. sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
  576. {
  577. union perf_mem_data_src data_src_l;
  578. union perf_mem_data_src data_src_r;
  579. if (left->mem_info)
  580. data_src_l = left->mem_info->data_src;
  581. else
  582. data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
  583. if (right->mem_info)
  584. data_src_r = right->mem_info->data_src;
  585. else
  586. data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
  587. return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
  588. }
  589. static const char * const tlb_access[] = {
  590. "N/A",
  591. "HIT",
  592. "MISS",
  593. "L1",
  594. "L2",
  595. "Walker",
  596. "Fault",
  597. };
  598. #define NUM_TLB_ACCESS (sizeof(tlb_access)/sizeof(const char *))
  599. static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
  600. size_t size, unsigned int width)
  601. {
  602. char out[64];
  603. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  604. size_t l = 0, i;
  605. u64 m = PERF_MEM_TLB_NA;
  606. u64 hit, miss;
  607. out[0] = '\0';
  608. if (he->mem_info)
  609. m = he->mem_info->data_src.mem_dtlb;
  610. hit = m & PERF_MEM_TLB_HIT;
  611. miss = m & PERF_MEM_TLB_MISS;
  612. /* already taken care of */
  613. m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS);
  614. for (i = 0; m && i < NUM_TLB_ACCESS; i++, m >>= 1) {
  615. if (!(m & 0x1))
  616. continue;
  617. if (l) {
  618. strcat(out, " or ");
  619. l += 4;
  620. }
  621. strncat(out, tlb_access[i], sz - l);
  622. l += strlen(tlb_access[i]);
  623. }
  624. if (*out == '\0')
  625. strcpy(out, "N/A");
  626. if (hit)
  627. strncat(out, " hit", sz - l);
  628. if (miss)
  629. strncat(out, " miss", sz - l);
  630. return repsep_snprintf(bf, size, "%-*s", width, out);
  631. }
  632. static int64_t
  633. sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
  634. {
  635. union perf_mem_data_src data_src_l;
  636. union perf_mem_data_src data_src_r;
  637. if (left->mem_info)
  638. data_src_l = left->mem_info->data_src;
  639. else
  640. data_src_l.mem_lvl = PERF_MEM_LVL_NA;
  641. if (right->mem_info)
  642. data_src_r = right->mem_info->data_src;
  643. else
  644. data_src_r.mem_lvl = PERF_MEM_LVL_NA;
  645. return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
  646. }
  647. static const char * const mem_lvl[] = {
  648. "N/A",
  649. "HIT",
  650. "MISS",
  651. "L1",
  652. "LFB",
  653. "L2",
  654. "L3",
  655. "Local RAM",
  656. "Remote RAM (1 hop)",
  657. "Remote RAM (2 hops)",
  658. "Remote Cache (1 hop)",
  659. "Remote Cache (2 hops)",
  660. "I/O",
  661. "Uncached",
  662. };
  663. #define NUM_MEM_LVL (sizeof(mem_lvl)/sizeof(const char *))
  664. static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
  665. size_t size, unsigned int width)
  666. {
  667. char out[64];
  668. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  669. size_t i, l = 0;
  670. u64 m = PERF_MEM_LVL_NA;
  671. u64 hit, miss;
  672. if (he->mem_info)
  673. m = he->mem_info->data_src.mem_lvl;
  674. out[0] = '\0';
  675. hit = m & PERF_MEM_LVL_HIT;
  676. miss = m & PERF_MEM_LVL_MISS;
  677. /* already taken care of */
  678. m &= ~(PERF_MEM_LVL_HIT|PERF_MEM_LVL_MISS);
  679. for (i = 0; m && i < NUM_MEM_LVL; i++, m >>= 1) {
  680. if (!(m & 0x1))
  681. continue;
  682. if (l) {
  683. strcat(out, " or ");
  684. l += 4;
  685. }
  686. strncat(out, mem_lvl[i], sz - l);
  687. l += strlen(mem_lvl[i]);
  688. }
  689. if (*out == '\0')
  690. strcpy(out, "N/A");
  691. if (hit)
  692. strncat(out, " hit", sz - l);
  693. if (miss)
  694. strncat(out, " miss", sz - l);
  695. return repsep_snprintf(bf, size, "%-*s", width, out);
  696. }
  697. static int64_t
  698. sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
  699. {
  700. union perf_mem_data_src data_src_l;
  701. union perf_mem_data_src data_src_r;
  702. if (left->mem_info)
  703. data_src_l = left->mem_info->data_src;
  704. else
  705. data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
  706. if (right->mem_info)
  707. data_src_r = right->mem_info->data_src;
  708. else
  709. data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
  710. return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
  711. }
  712. static const char * const snoop_access[] = {
  713. "N/A",
  714. "None",
  715. "Miss",
  716. "Hit",
  717. "HitM",
  718. };
  719. #define NUM_SNOOP_ACCESS (sizeof(snoop_access)/sizeof(const char *))
  720. static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
  721. size_t size, unsigned int width)
  722. {
  723. char out[64];
  724. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  725. size_t i, l = 0;
  726. u64 m = PERF_MEM_SNOOP_NA;
  727. out[0] = '\0';
  728. if (he->mem_info)
  729. m = he->mem_info->data_src.mem_snoop;
  730. for (i = 0; m && i < NUM_SNOOP_ACCESS; i++, m >>= 1) {
  731. if (!(m & 0x1))
  732. continue;
  733. if (l) {
  734. strcat(out, " or ");
  735. l += 4;
  736. }
  737. strncat(out, snoop_access[i], sz - l);
  738. l += strlen(snoop_access[i]);
  739. }
  740. if (*out == '\0')
  741. strcpy(out, "N/A");
  742. return repsep_snprintf(bf, size, "%-*s", width, out);
  743. }
  744. static inline u64 cl_address(u64 address)
  745. {
  746. /* return the cacheline of the address */
  747. return (address & ~(cacheline_size - 1));
  748. }
  749. static int64_t
  750. sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
  751. {
  752. u64 l, r;
  753. struct map *l_map, *r_map;
  754. if (!left->mem_info) return -1;
  755. if (!right->mem_info) return 1;
  756. /* group event types together */
  757. if (left->cpumode > right->cpumode) return -1;
  758. if (left->cpumode < right->cpumode) return 1;
  759. l_map = left->mem_info->daddr.map;
  760. r_map = right->mem_info->daddr.map;
  761. /* if both are NULL, jump to sort on al_addr instead */
  762. if (!l_map && !r_map)
  763. goto addr;
  764. if (!l_map) return -1;
  765. if (!r_map) return 1;
  766. if (l_map->maj > r_map->maj) return -1;
  767. if (l_map->maj < r_map->maj) return 1;
  768. if (l_map->min > r_map->min) return -1;
  769. if (l_map->min < r_map->min) return 1;
  770. if (l_map->ino > r_map->ino) return -1;
  771. if (l_map->ino < r_map->ino) return 1;
  772. if (l_map->ino_generation > r_map->ino_generation) return -1;
  773. if (l_map->ino_generation < r_map->ino_generation) return 1;
  774. /*
  775. * Addresses with no major/minor numbers are assumed to be
  776. * anonymous in userspace. Sort those on pid then address.
  777. *
  778. * The kernel and non-zero major/minor mapped areas are
  779. * assumed to be unity mapped. Sort those on address.
  780. */
  781. if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
  782. (!(l_map->flags & MAP_SHARED)) &&
  783. !l_map->maj && !l_map->min && !l_map->ino &&
  784. !l_map->ino_generation) {
  785. /* userspace anonymous */
  786. if (left->thread->pid_ > right->thread->pid_) return -1;
  787. if (left->thread->pid_ < right->thread->pid_) return 1;
  788. }
  789. addr:
  790. /* al_addr does all the right addr - start + offset calculations */
  791. l = cl_address(left->mem_info->daddr.al_addr);
  792. r = cl_address(right->mem_info->daddr.al_addr);
  793. if (l > r) return -1;
  794. if (l < r) return 1;
  795. return 0;
  796. }
  797. static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
  798. size_t size, unsigned int width)
  799. {
  800. uint64_t addr = 0;
  801. struct map *map = NULL;
  802. struct symbol *sym = NULL;
  803. char level = he->level;
  804. if (he->mem_info) {
  805. addr = cl_address(he->mem_info->daddr.al_addr);
  806. map = he->mem_info->daddr.map;
  807. sym = he->mem_info->daddr.sym;
  808. /* print [s] for shared data mmaps */
  809. if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
  810. map && (map->type == MAP__VARIABLE) &&
  811. (map->flags & MAP_SHARED) &&
  812. (map->maj || map->min || map->ino ||
  813. map->ino_generation))
  814. level = 's';
  815. else if (!map)
  816. level = 'X';
  817. }
  818. return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
  819. width);
  820. }
  821. struct sort_entry sort_mispredict = {
  822. .se_header = "Branch Mispredicted",
  823. .se_cmp = sort__mispredict_cmp,
  824. .se_snprintf = hist_entry__mispredict_snprintf,
  825. .se_width_idx = HISTC_MISPREDICT,
  826. };
  827. static u64 he_weight(struct hist_entry *he)
  828. {
  829. return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
  830. }
  831. static int64_t
  832. sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  833. {
  834. return he_weight(left) - he_weight(right);
  835. }
  836. static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
  837. size_t size, unsigned int width)
  838. {
  839. return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
  840. }
  841. struct sort_entry sort_local_weight = {
  842. .se_header = "Local Weight",
  843. .se_cmp = sort__local_weight_cmp,
  844. .se_snprintf = hist_entry__local_weight_snprintf,
  845. .se_width_idx = HISTC_LOCAL_WEIGHT,
  846. };
  847. static int64_t
  848. sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  849. {
  850. return left->stat.weight - right->stat.weight;
  851. }
  852. static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
  853. size_t size, unsigned int width)
  854. {
  855. return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
  856. }
  857. struct sort_entry sort_global_weight = {
  858. .se_header = "Weight",
  859. .se_cmp = sort__global_weight_cmp,
  860. .se_snprintf = hist_entry__global_weight_snprintf,
  861. .se_width_idx = HISTC_GLOBAL_WEIGHT,
  862. };
  863. struct sort_entry sort_mem_daddr_sym = {
  864. .se_header = "Data Symbol",
  865. .se_cmp = sort__daddr_cmp,
  866. .se_snprintf = hist_entry__daddr_snprintf,
  867. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  868. };
  869. struct sort_entry sort_mem_daddr_dso = {
  870. .se_header = "Data Object",
  871. .se_cmp = sort__dso_daddr_cmp,
  872. .se_snprintf = hist_entry__dso_daddr_snprintf,
  873. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  874. };
  875. struct sort_entry sort_mem_locked = {
  876. .se_header = "Locked",
  877. .se_cmp = sort__locked_cmp,
  878. .se_snprintf = hist_entry__locked_snprintf,
  879. .se_width_idx = HISTC_MEM_LOCKED,
  880. };
  881. struct sort_entry sort_mem_tlb = {
  882. .se_header = "TLB access",
  883. .se_cmp = sort__tlb_cmp,
  884. .se_snprintf = hist_entry__tlb_snprintf,
  885. .se_width_idx = HISTC_MEM_TLB,
  886. };
  887. struct sort_entry sort_mem_lvl = {
  888. .se_header = "Memory access",
  889. .se_cmp = sort__lvl_cmp,
  890. .se_snprintf = hist_entry__lvl_snprintf,
  891. .se_width_idx = HISTC_MEM_LVL,
  892. };
  893. struct sort_entry sort_mem_snoop = {
  894. .se_header = "Snoop",
  895. .se_cmp = sort__snoop_cmp,
  896. .se_snprintf = hist_entry__snoop_snprintf,
  897. .se_width_idx = HISTC_MEM_SNOOP,
  898. };
  899. struct sort_entry sort_mem_dcacheline = {
  900. .se_header = "Data Cacheline",
  901. .se_cmp = sort__dcacheline_cmp,
  902. .se_snprintf = hist_entry__dcacheline_snprintf,
  903. .se_width_idx = HISTC_MEM_DCACHELINE,
  904. };
  905. static int64_t
  906. sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
  907. {
  908. if (!left->branch_info || !right->branch_info)
  909. return cmp_null(left->branch_info, right->branch_info);
  910. return left->branch_info->flags.abort !=
  911. right->branch_info->flags.abort;
  912. }
  913. static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
  914. size_t size, unsigned int width)
  915. {
  916. static const char *out = "N/A";
  917. if (he->branch_info) {
  918. if (he->branch_info->flags.abort)
  919. out = "A";
  920. else
  921. out = ".";
  922. }
  923. return repsep_snprintf(bf, size, "%-*s", width, out);
  924. }
  925. struct sort_entry sort_abort = {
  926. .se_header = "Transaction abort",
  927. .se_cmp = sort__abort_cmp,
  928. .se_snprintf = hist_entry__abort_snprintf,
  929. .se_width_idx = HISTC_ABORT,
  930. };
  931. static int64_t
  932. sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
  933. {
  934. if (!left->branch_info || !right->branch_info)
  935. return cmp_null(left->branch_info, right->branch_info);
  936. return left->branch_info->flags.in_tx !=
  937. right->branch_info->flags.in_tx;
  938. }
  939. static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
  940. size_t size, unsigned int width)
  941. {
  942. static const char *out = "N/A";
  943. if (he->branch_info) {
  944. if (he->branch_info->flags.in_tx)
  945. out = "T";
  946. else
  947. out = ".";
  948. }
  949. return repsep_snprintf(bf, size, "%-*s", width, out);
  950. }
  951. struct sort_entry sort_in_tx = {
  952. .se_header = "Branch in transaction",
  953. .se_cmp = sort__in_tx_cmp,
  954. .se_snprintf = hist_entry__in_tx_snprintf,
  955. .se_width_idx = HISTC_IN_TX,
  956. };
  957. static int64_t
  958. sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
  959. {
  960. return left->transaction - right->transaction;
  961. }
  962. static inline char *add_str(char *p, const char *str)
  963. {
  964. strcpy(p, str);
  965. return p + strlen(str);
  966. }
  967. static struct txbit {
  968. unsigned flag;
  969. const char *name;
  970. int skip_for_len;
  971. } txbits[] = {
  972. { PERF_TXN_ELISION, "EL ", 0 },
  973. { PERF_TXN_TRANSACTION, "TX ", 1 },
  974. { PERF_TXN_SYNC, "SYNC ", 1 },
  975. { PERF_TXN_ASYNC, "ASYNC ", 0 },
  976. { PERF_TXN_RETRY, "RETRY ", 0 },
  977. { PERF_TXN_CONFLICT, "CON ", 0 },
  978. { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
  979. { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
  980. { 0, NULL, 0 }
  981. };
  982. int hist_entry__transaction_len(void)
  983. {
  984. int i;
  985. int len = 0;
  986. for (i = 0; txbits[i].name; i++) {
  987. if (!txbits[i].skip_for_len)
  988. len += strlen(txbits[i].name);
  989. }
  990. len += 4; /* :XX<space> */
  991. return len;
  992. }
  993. static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
  994. size_t size, unsigned int width)
  995. {
  996. u64 t = he->transaction;
  997. char buf[128];
  998. char *p = buf;
  999. int i;
  1000. buf[0] = 0;
  1001. for (i = 0; txbits[i].name; i++)
  1002. if (txbits[i].flag & t)
  1003. p = add_str(p, txbits[i].name);
  1004. if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
  1005. p = add_str(p, "NEITHER ");
  1006. if (t & PERF_TXN_ABORT_MASK) {
  1007. sprintf(p, ":%" PRIx64,
  1008. (t & PERF_TXN_ABORT_MASK) >>
  1009. PERF_TXN_ABORT_SHIFT);
  1010. p += strlen(p);
  1011. }
  1012. return repsep_snprintf(bf, size, "%-*s", width, buf);
  1013. }
  1014. struct sort_entry sort_transaction = {
  1015. .se_header = "Transaction ",
  1016. .se_cmp = sort__transaction_cmp,
  1017. .se_snprintf = hist_entry__transaction_snprintf,
  1018. .se_width_idx = HISTC_TRANSACTION,
  1019. };
  1020. struct sort_dimension {
  1021. const char *name;
  1022. struct sort_entry *entry;
  1023. int taken;
  1024. };
  1025. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  1026. static struct sort_dimension common_sort_dimensions[] = {
  1027. DIM(SORT_PID, "pid", sort_thread),
  1028. DIM(SORT_COMM, "comm", sort_comm),
  1029. DIM(SORT_DSO, "dso", sort_dso),
  1030. DIM(SORT_SYM, "symbol", sort_sym),
  1031. DIM(SORT_PARENT, "parent", sort_parent),
  1032. DIM(SORT_CPU, "cpu", sort_cpu),
  1033. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  1034. DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
  1035. DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
  1036. DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
  1037. DIM(SORT_TRANSACTION, "transaction", sort_transaction),
  1038. };
  1039. #undef DIM
  1040. #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
  1041. static struct sort_dimension bstack_sort_dimensions[] = {
  1042. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  1043. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  1044. DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
  1045. DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
  1046. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  1047. DIM(SORT_IN_TX, "in_tx", sort_in_tx),
  1048. DIM(SORT_ABORT, "abort", sort_abort),
  1049. DIM(SORT_CYCLES, "cycles", sort_cycles),
  1050. };
  1051. #undef DIM
  1052. #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
  1053. static struct sort_dimension memory_sort_dimensions[] = {
  1054. DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
  1055. DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
  1056. DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
  1057. DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
  1058. DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
  1059. DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
  1060. DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
  1061. };
  1062. #undef DIM
  1063. struct hpp_dimension {
  1064. const char *name;
  1065. struct perf_hpp_fmt *fmt;
  1066. int taken;
  1067. };
  1068. #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
  1069. static struct hpp_dimension hpp_sort_dimensions[] = {
  1070. DIM(PERF_HPP__OVERHEAD, "overhead"),
  1071. DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
  1072. DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
  1073. DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
  1074. DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
  1075. DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
  1076. DIM(PERF_HPP__SAMPLES, "sample"),
  1077. DIM(PERF_HPP__PERIOD, "period"),
  1078. };
  1079. #undef DIM
  1080. struct hpp_sort_entry {
  1081. struct perf_hpp_fmt hpp;
  1082. struct sort_entry *se;
  1083. };
  1084. bool perf_hpp__same_sort_entry(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
  1085. {
  1086. struct hpp_sort_entry *hse_a;
  1087. struct hpp_sort_entry *hse_b;
  1088. if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
  1089. return false;
  1090. hse_a = container_of(a, struct hpp_sort_entry, hpp);
  1091. hse_b = container_of(b, struct hpp_sort_entry, hpp);
  1092. return hse_a->se == hse_b->se;
  1093. }
  1094. void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
  1095. {
  1096. struct hpp_sort_entry *hse;
  1097. if (!perf_hpp__is_sort_entry(fmt))
  1098. return;
  1099. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1100. hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
  1101. }
  1102. static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1103. struct perf_evsel *evsel)
  1104. {
  1105. struct hpp_sort_entry *hse;
  1106. size_t len = fmt->user_len;
  1107. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1108. if (!len)
  1109. len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
  1110. return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
  1111. }
  1112. static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
  1113. struct perf_hpp *hpp __maybe_unused,
  1114. struct perf_evsel *evsel)
  1115. {
  1116. struct hpp_sort_entry *hse;
  1117. size_t len = fmt->user_len;
  1118. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1119. if (!len)
  1120. len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
  1121. return len;
  1122. }
  1123. static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1124. struct hist_entry *he)
  1125. {
  1126. struct hpp_sort_entry *hse;
  1127. size_t len = fmt->user_len;
  1128. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1129. if (!len)
  1130. len = hists__col_len(he->hists, hse->se->se_width_idx);
  1131. return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
  1132. }
  1133. static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
  1134. struct hist_entry *a, struct hist_entry *b)
  1135. {
  1136. struct hpp_sort_entry *hse;
  1137. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1138. return hse->se->se_cmp(a, b);
  1139. }
  1140. static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
  1141. struct hist_entry *a, struct hist_entry *b)
  1142. {
  1143. struct hpp_sort_entry *hse;
  1144. int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
  1145. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1146. collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
  1147. return collapse_fn(a, b);
  1148. }
  1149. static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
  1150. struct hist_entry *a, struct hist_entry *b)
  1151. {
  1152. struct hpp_sort_entry *hse;
  1153. int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
  1154. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1155. sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
  1156. return sort_fn(a, b);
  1157. }
  1158. static struct hpp_sort_entry *
  1159. __sort_dimension__alloc_hpp(struct sort_dimension *sd)
  1160. {
  1161. struct hpp_sort_entry *hse;
  1162. hse = malloc(sizeof(*hse));
  1163. if (hse == NULL) {
  1164. pr_err("Memory allocation failed\n");
  1165. return NULL;
  1166. }
  1167. hse->se = sd->entry;
  1168. hse->hpp.name = sd->entry->se_header;
  1169. hse->hpp.header = __sort__hpp_header;
  1170. hse->hpp.width = __sort__hpp_width;
  1171. hse->hpp.entry = __sort__hpp_entry;
  1172. hse->hpp.color = NULL;
  1173. hse->hpp.cmp = __sort__hpp_cmp;
  1174. hse->hpp.collapse = __sort__hpp_collapse;
  1175. hse->hpp.sort = __sort__hpp_sort;
  1176. INIT_LIST_HEAD(&hse->hpp.list);
  1177. INIT_LIST_HEAD(&hse->hpp.sort_list);
  1178. hse->hpp.elide = false;
  1179. hse->hpp.len = 0;
  1180. hse->hpp.user_len = 0;
  1181. return hse;
  1182. }
  1183. bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
  1184. {
  1185. return format->header == __sort__hpp_header;
  1186. }
  1187. static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd)
  1188. {
  1189. struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd);
  1190. if (hse == NULL)
  1191. return -1;
  1192. perf_hpp__register_sort_field(&hse->hpp);
  1193. return 0;
  1194. }
  1195. static int __sort_dimension__add_hpp_output(struct sort_dimension *sd)
  1196. {
  1197. struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd);
  1198. if (hse == NULL)
  1199. return -1;
  1200. perf_hpp__column_register(&hse->hpp);
  1201. return 0;
  1202. }
  1203. static int __sort_dimension__add(struct sort_dimension *sd)
  1204. {
  1205. if (sd->taken)
  1206. return 0;
  1207. if (__sort_dimension__add_hpp_sort(sd) < 0)
  1208. return -1;
  1209. if (sd->entry->se_collapse)
  1210. sort__need_collapse = 1;
  1211. sd->taken = 1;
  1212. return 0;
  1213. }
  1214. static int __hpp_dimension__add(struct hpp_dimension *hd)
  1215. {
  1216. if (!hd->taken) {
  1217. hd->taken = 1;
  1218. perf_hpp__register_sort_field(hd->fmt);
  1219. }
  1220. return 0;
  1221. }
  1222. static int __sort_dimension__add_output(struct sort_dimension *sd)
  1223. {
  1224. if (sd->taken)
  1225. return 0;
  1226. if (__sort_dimension__add_hpp_output(sd) < 0)
  1227. return -1;
  1228. sd->taken = 1;
  1229. return 0;
  1230. }
  1231. static int __hpp_dimension__add_output(struct hpp_dimension *hd)
  1232. {
  1233. if (!hd->taken) {
  1234. hd->taken = 1;
  1235. perf_hpp__column_register(hd->fmt);
  1236. }
  1237. return 0;
  1238. }
  1239. int sort_dimension__add(const char *tok)
  1240. {
  1241. unsigned int i;
  1242. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  1243. struct sort_dimension *sd = &common_sort_dimensions[i];
  1244. if (strncasecmp(tok, sd->name, strlen(tok)))
  1245. continue;
  1246. if (sd->entry == &sort_parent) {
  1247. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  1248. if (ret) {
  1249. char err[BUFSIZ];
  1250. regerror(ret, &parent_regex, err, sizeof(err));
  1251. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  1252. return -EINVAL;
  1253. }
  1254. sort__has_parent = 1;
  1255. } else if (sd->entry == &sort_sym) {
  1256. sort__has_sym = 1;
  1257. /*
  1258. * perf diff displays the performance difference amongst
  1259. * two or more perf.data files. Those files could come
  1260. * from different binaries. So we should not compare
  1261. * their ips, but the name of symbol.
  1262. */
  1263. if (sort__mode == SORT_MODE__DIFF)
  1264. sd->entry->se_collapse = sort__sym_sort;
  1265. } else if (sd->entry == &sort_dso) {
  1266. sort__has_dso = 1;
  1267. }
  1268. return __sort_dimension__add(sd);
  1269. }
  1270. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
  1271. struct hpp_dimension *hd = &hpp_sort_dimensions[i];
  1272. if (strncasecmp(tok, hd->name, strlen(tok)))
  1273. continue;
  1274. return __hpp_dimension__add(hd);
  1275. }
  1276. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  1277. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  1278. if (strncasecmp(tok, sd->name, strlen(tok)))
  1279. continue;
  1280. if (sort__mode != SORT_MODE__BRANCH)
  1281. return -EINVAL;
  1282. if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
  1283. sort__has_sym = 1;
  1284. __sort_dimension__add(sd);
  1285. return 0;
  1286. }
  1287. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  1288. struct sort_dimension *sd = &memory_sort_dimensions[i];
  1289. if (strncasecmp(tok, sd->name, strlen(tok)))
  1290. continue;
  1291. if (sort__mode != SORT_MODE__MEMORY)
  1292. return -EINVAL;
  1293. if (sd->entry == &sort_mem_daddr_sym)
  1294. sort__has_sym = 1;
  1295. __sort_dimension__add(sd);
  1296. return 0;
  1297. }
  1298. return -ESRCH;
  1299. }
  1300. static const char *get_default_sort_order(void)
  1301. {
  1302. const char *default_sort_orders[] = {
  1303. default_sort_order,
  1304. default_branch_sort_order,
  1305. default_mem_sort_order,
  1306. default_top_sort_order,
  1307. default_diff_sort_order,
  1308. };
  1309. BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
  1310. return default_sort_orders[sort__mode];
  1311. }
  1312. static int setup_sort_order(void)
  1313. {
  1314. char *new_sort_order;
  1315. /*
  1316. * Append '+'-prefixed sort order to the default sort
  1317. * order string.
  1318. */
  1319. if (!sort_order || is_strict_order(sort_order))
  1320. return 0;
  1321. if (sort_order[1] == '\0') {
  1322. error("Invalid --sort key: `+'");
  1323. return -EINVAL;
  1324. }
  1325. /*
  1326. * We allocate new sort_order string, but we never free it,
  1327. * because it's checked over the rest of the code.
  1328. */
  1329. if (asprintf(&new_sort_order, "%s,%s",
  1330. get_default_sort_order(), sort_order + 1) < 0) {
  1331. error("Not enough memory to set up --sort");
  1332. return -ENOMEM;
  1333. }
  1334. sort_order = new_sort_order;
  1335. return 0;
  1336. }
  1337. static int __setup_sorting(void)
  1338. {
  1339. char *tmp, *tok, *str;
  1340. const char *sort_keys;
  1341. int ret = 0;
  1342. ret = setup_sort_order();
  1343. if (ret)
  1344. return ret;
  1345. sort_keys = sort_order;
  1346. if (sort_keys == NULL) {
  1347. if (is_strict_order(field_order)) {
  1348. /*
  1349. * If user specified field order but no sort order,
  1350. * we'll honor it and not add default sort orders.
  1351. */
  1352. return 0;
  1353. }
  1354. sort_keys = get_default_sort_order();
  1355. }
  1356. str = strdup(sort_keys);
  1357. if (str == NULL) {
  1358. error("Not enough memory to setup sort keys");
  1359. return -ENOMEM;
  1360. }
  1361. for (tok = strtok_r(str, ", ", &tmp);
  1362. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1363. ret = sort_dimension__add(tok);
  1364. if (ret == -EINVAL) {
  1365. error("Invalid --sort key: `%s'", tok);
  1366. break;
  1367. } else if (ret == -ESRCH) {
  1368. error("Unknown --sort key: `%s'", tok);
  1369. break;
  1370. }
  1371. }
  1372. free(str);
  1373. return ret;
  1374. }
  1375. void perf_hpp__set_elide(int idx, bool elide)
  1376. {
  1377. struct perf_hpp_fmt *fmt;
  1378. struct hpp_sort_entry *hse;
  1379. perf_hpp__for_each_format(fmt) {
  1380. if (!perf_hpp__is_sort_entry(fmt))
  1381. continue;
  1382. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1383. if (hse->se->se_width_idx == idx) {
  1384. fmt->elide = elide;
  1385. break;
  1386. }
  1387. }
  1388. }
  1389. static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
  1390. {
  1391. if (list && strlist__nr_entries(list) == 1) {
  1392. if (fp != NULL)
  1393. fprintf(fp, "# %s: %s\n", list_name,
  1394. strlist__entry(list, 0)->s);
  1395. return true;
  1396. }
  1397. return false;
  1398. }
  1399. static bool get_elide(int idx, FILE *output)
  1400. {
  1401. switch (idx) {
  1402. case HISTC_SYMBOL:
  1403. return __get_elide(symbol_conf.sym_list, "symbol", output);
  1404. case HISTC_DSO:
  1405. return __get_elide(symbol_conf.dso_list, "dso", output);
  1406. case HISTC_COMM:
  1407. return __get_elide(symbol_conf.comm_list, "comm", output);
  1408. default:
  1409. break;
  1410. }
  1411. if (sort__mode != SORT_MODE__BRANCH)
  1412. return false;
  1413. switch (idx) {
  1414. case HISTC_SYMBOL_FROM:
  1415. return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
  1416. case HISTC_SYMBOL_TO:
  1417. return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
  1418. case HISTC_DSO_FROM:
  1419. return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
  1420. case HISTC_DSO_TO:
  1421. return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
  1422. default:
  1423. break;
  1424. }
  1425. return false;
  1426. }
  1427. void sort__setup_elide(FILE *output)
  1428. {
  1429. struct perf_hpp_fmt *fmt;
  1430. struct hpp_sort_entry *hse;
  1431. perf_hpp__for_each_format(fmt) {
  1432. if (!perf_hpp__is_sort_entry(fmt))
  1433. continue;
  1434. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1435. fmt->elide = get_elide(hse->se->se_width_idx, output);
  1436. }
  1437. /*
  1438. * It makes no sense to elide all of sort entries.
  1439. * Just revert them to show up again.
  1440. */
  1441. perf_hpp__for_each_format(fmt) {
  1442. if (!perf_hpp__is_sort_entry(fmt))
  1443. continue;
  1444. if (!fmt->elide)
  1445. return;
  1446. }
  1447. perf_hpp__for_each_format(fmt) {
  1448. if (!perf_hpp__is_sort_entry(fmt))
  1449. continue;
  1450. fmt->elide = false;
  1451. }
  1452. }
  1453. static int output_field_add(char *tok)
  1454. {
  1455. unsigned int i;
  1456. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  1457. struct sort_dimension *sd = &common_sort_dimensions[i];
  1458. if (strncasecmp(tok, sd->name, strlen(tok)))
  1459. continue;
  1460. return __sort_dimension__add_output(sd);
  1461. }
  1462. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
  1463. struct hpp_dimension *hd = &hpp_sort_dimensions[i];
  1464. if (strncasecmp(tok, hd->name, strlen(tok)))
  1465. continue;
  1466. return __hpp_dimension__add_output(hd);
  1467. }
  1468. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  1469. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  1470. if (strncasecmp(tok, sd->name, strlen(tok)))
  1471. continue;
  1472. return __sort_dimension__add_output(sd);
  1473. }
  1474. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  1475. struct sort_dimension *sd = &memory_sort_dimensions[i];
  1476. if (strncasecmp(tok, sd->name, strlen(tok)))
  1477. continue;
  1478. return __sort_dimension__add_output(sd);
  1479. }
  1480. return -ESRCH;
  1481. }
  1482. static void reset_dimensions(void)
  1483. {
  1484. unsigned int i;
  1485. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
  1486. common_sort_dimensions[i].taken = 0;
  1487. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
  1488. hpp_sort_dimensions[i].taken = 0;
  1489. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
  1490. bstack_sort_dimensions[i].taken = 0;
  1491. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
  1492. memory_sort_dimensions[i].taken = 0;
  1493. }
  1494. bool is_strict_order(const char *order)
  1495. {
  1496. return order && (*order != '+');
  1497. }
  1498. static int __setup_output_field(void)
  1499. {
  1500. char *tmp, *tok, *str, *strp;
  1501. int ret = -EINVAL;
  1502. if (field_order == NULL)
  1503. return 0;
  1504. reset_dimensions();
  1505. strp = str = strdup(field_order);
  1506. if (str == NULL) {
  1507. error("Not enough memory to setup output fields");
  1508. return -ENOMEM;
  1509. }
  1510. if (!is_strict_order(field_order))
  1511. strp++;
  1512. if (!strlen(strp)) {
  1513. error("Invalid --fields key: `+'");
  1514. goto out;
  1515. }
  1516. for (tok = strtok_r(strp, ", ", &tmp);
  1517. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1518. ret = output_field_add(tok);
  1519. if (ret == -EINVAL) {
  1520. error("Invalid --fields key: `%s'", tok);
  1521. break;
  1522. } else if (ret == -ESRCH) {
  1523. error("Unknown --fields key: `%s'", tok);
  1524. break;
  1525. }
  1526. }
  1527. out:
  1528. free(str);
  1529. return ret;
  1530. }
  1531. int setup_sorting(void)
  1532. {
  1533. int err;
  1534. err = __setup_sorting();
  1535. if (err < 0)
  1536. return err;
  1537. if (parent_pattern != default_parent_pattern) {
  1538. err = sort_dimension__add("parent");
  1539. if (err < 0)
  1540. return err;
  1541. }
  1542. reset_dimensions();
  1543. /*
  1544. * perf diff doesn't use default hpp output fields.
  1545. */
  1546. if (sort__mode != SORT_MODE__DIFF)
  1547. perf_hpp__init();
  1548. err = __setup_output_field();
  1549. if (err < 0)
  1550. return err;
  1551. /* copy sort keys to output fields */
  1552. perf_hpp__setup_output_field();
  1553. /* and then copy output fields to sort keys */
  1554. perf_hpp__append_sort_keys();
  1555. return 0;
  1556. }
  1557. void reset_output_field(void)
  1558. {
  1559. sort__need_collapse = 0;
  1560. sort__has_parent = 0;
  1561. sort__has_sym = 0;
  1562. sort__has_dso = 0;
  1563. field_order = NULL;
  1564. sort_order = NULL;
  1565. reset_dimensions();
  1566. perf_hpp__reset_output_field();
  1567. }