sort.c 39 KB

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