sort.c 42 KB

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