sort.c 41 KB

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