sort.c 43 KB

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