sort.c 43 KB

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