sort.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. #include "sort.h"
  2. #include "hist.h"
  3. #include "comm.h"
  4. #include "symbol.h"
  5. regex_t parent_regex;
  6. const char default_parent_pattern[] = "^sys_|^do_page_fault";
  7. const char *parent_pattern = default_parent_pattern;
  8. const char default_sort_order[] = "comm,dso,symbol";
  9. const char *sort_order = default_sort_order;
  10. regex_t ignore_callees_regex;
  11. int have_ignore_callees = 0;
  12. int sort__need_collapse = 0;
  13. int sort__has_parent = 0;
  14. int sort__has_sym = 0;
  15. int sort__has_dso = 0;
  16. enum sort_mode sort__mode = SORT_MODE__NORMAL;
  17. enum sort_type sort__first_dimension;
  18. LIST_HEAD(hist_entry__sort_list);
  19. static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
  20. {
  21. int n;
  22. va_list ap;
  23. va_start(ap, fmt);
  24. n = vsnprintf(bf, size, fmt, ap);
  25. if (symbol_conf.field_sep && n > 0) {
  26. char *sep = bf;
  27. while (1) {
  28. sep = strchr(sep, *symbol_conf.field_sep);
  29. if (sep == NULL)
  30. break;
  31. *sep = '.';
  32. }
  33. }
  34. va_end(ap);
  35. if (n >= (int)size)
  36. return size - 1;
  37. return n;
  38. }
  39. static int64_t cmp_null(const void *l, const void *r)
  40. {
  41. if (!l && !r)
  42. return 0;
  43. else if (!l)
  44. return -1;
  45. else
  46. return 1;
  47. }
  48. /* --sort pid */
  49. static int64_t
  50. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  51. {
  52. return right->thread->tid - left->thread->tid;
  53. }
  54. static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
  55. size_t size, unsigned int width)
  56. {
  57. const char *comm = thread__comm_str(he->thread);
  58. return repsep_snprintf(bf, size, "%*s:%5d", width - 6,
  59. comm ?: "", he->thread->tid);
  60. }
  61. struct sort_entry sort_thread = {
  62. .se_header = "Command: Pid",
  63. .se_cmp = sort__thread_cmp,
  64. .se_snprintf = hist_entry__thread_snprintf,
  65. .se_width_idx = HISTC_THREAD,
  66. };
  67. /* --sort comm */
  68. static int64_t
  69. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  70. {
  71. /* Compare the addr that should be unique among comm */
  72. return comm__str(right->comm) - comm__str(left->comm);
  73. }
  74. static int64_t
  75. sort__comm_collapse(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 int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
  81. size_t size, unsigned int width)
  82. {
  83. return repsep_snprintf(bf, size, "%*s", width, comm__str(he->comm));
  84. }
  85. struct sort_entry sort_comm = {
  86. .se_header = "Command",
  87. .se_cmp = sort__comm_cmp,
  88. .se_collapse = sort__comm_collapse,
  89. .se_snprintf = hist_entry__comm_snprintf,
  90. .se_width_idx = HISTC_COMM,
  91. };
  92. /* --sort dso */
  93. static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
  94. {
  95. struct dso *dso_l = map_l ? map_l->dso : NULL;
  96. struct dso *dso_r = map_r ? map_r->dso : NULL;
  97. const char *dso_name_l, *dso_name_r;
  98. if (!dso_l || !dso_r)
  99. return cmp_null(dso_l, dso_r);
  100. if (verbose) {
  101. dso_name_l = dso_l->long_name;
  102. dso_name_r = dso_r->long_name;
  103. } else {
  104. dso_name_l = dso_l->short_name;
  105. dso_name_r = dso_r->short_name;
  106. }
  107. return strcmp(dso_name_l, dso_name_r);
  108. }
  109. static int64_t
  110. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  111. {
  112. return _sort__dso_cmp(left->ms.map, right->ms.map);
  113. }
  114. static int _hist_entry__dso_snprintf(struct map *map, char *bf,
  115. size_t size, unsigned int width)
  116. {
  117. if (map && map->dso) {
  118. const char *dso_name = !verbose ? map->dso->short_name :
  119. map->dso->long_name;
  120. return repsep_snprintf(bf, size, "%-*s", width, dso_name);
  121. }
  122. return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
  123. }
  124. static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
  125. size_t size, unsigned int width)
  126. {
  127. return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
  128. }
  129. struct sort_entry sort_dso = {
  130. .se_header = "Shared Object",
  131. .se_cmp = sort__dso_cmp,
  132. .se_snprintf = hist_entry__dso_snprintf,
  133. .se_width_idx = HISTC_DSO,
  134. };
  135. /* --sort symbol */
  136. static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
  137. {
  138. return (int64_t)(right_ip - left_ip);
  139. }
  140. static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
  141. {
  142. u64 ip_l, ip_r;
  143. if (!sym_l || !sym_r)
  144. return cmp_null(sym_l, sym_r);
  145. if (sym_l == sym_r)
  146. return 0;
  147. ip_l = sym_l->start;
  148. ip_r = sym_r->start;
  149. return (int64_t)(ip_r - ip_l);
  150. }
  151. static int64_t
  152. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  153. {
  154. int64_t ret;
  155. if (!left->ms.sym && !right->ms.sym)
  156. return _sort__addr_cmp(left->ip, right->ip);
  157. /*
  158. * comparing symbol address alone is not enough since it's a
  159. * relative address within a dso.
  160. */
  161. if (!sort__has_dso) {
  162. ret = sort__dso_cmp(left, right);
  163. if (ret != 0)
  164. return ret;
  165. }
  166. return _sort__sym_cmp(left->ms.sym, right->ms.sym);
  167. }
  168. static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
  169. u64 ip, char level, char *bf, size_t size,
  170. unsigned int width)
  171. {
  172. size_t ret = 0;
  173. if (verbose) {
  174. char o = map ? dso__symtab_origin(map->dso) : '!';
  175. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  176. BITS_PER_LONG / 4 + 2, ip, o);
  177. }
  178. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
  179. if (sym && map) {
  180. if (map->type == MAP__VARIABLE) {
  181. ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
  182. ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
  183. ip - map->unmap_ip(map, sym->start));
  184. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  185. width - ret, "");
  186. } else {
  187. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  188. width - ret,
  189. sym->name);
  190. }
  191. } else {
  192. size_t len = BITS_PER_LONG / 4;
  193. ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
  194. len, ip);
  195. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  196. width - ret, "");
  197. }
  198. return ret;
  199. }
  200. static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
  201. size_t size, unsigned int width)
  202. {
  203. return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
  204. he->level, bf, size, width);
  205. }
  206. struct sort_entry sort_sym = {
  207. .se_header = "Symbol",
  208. .se_cmp = sort__sym_cmp,
  209. .se_snprintf = hist_entry__sym_snprintf,
  210. .se_width_idx = HISTC_SYMBOL,
  211. };
  212. /* --sort srcline */
  213. static int64_t
  214. sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
  215. {
  216. if (!left->srcline) {
  217. if (!left->ms.map)
  218. left->srcline = SRCLINE_UNKNOWN;
  219. else {
  220. struct map *map = left->ms.map;
  221. left->srcline = get_srcline(map->dso,
  222. map__rip_2objdump(map, left->ip));
  223. }
  224. }
  225. if (!right->srcline) {
  226. if (!right->ms.map)
  227. right->srcline = SRCLINE_UNKNOWN;
  228. else {
  229. struct map *map = right->ms.map;
  230. right->srcline = get_srcline(map->dso,
  231. map__rip_2objdump(map, right->ip));
  232. }
  233. }
  234. return strcmp(left->srcline, right->srcline);
  235. }
  236. static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
  237. size_t size,
  238. unsigned int width __maybe_unused)
  239. {
  240. return repsep_snprintf(bf, size, "%s", he->srcline);
  241. }
  242. struct sort_entry sort_srcline = {
  243. .se_header = "Source:Line",
  244. .se_cmp = sort__srcline_cmp,
  245. .se_snprintf = hist_entry__srcline_snprintf,
  246. .se_width_idx = HISTC_SRCLINE,
  247. };
  248. /* --sort parent */
  249. static int64_t
  250. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  251. {
  252. struct symbol *sym_l = left->parent;
  253. struct symbol *sym_r = right->parent;
  254. if (!sym_l || !sym_r)
  255. return cmp_null(sym_l, sym_r);
  256. return strcmp(sym_l->name, sym_r->name);
  257. }
  258. static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
  259. size_t size, unsigned int width)
  260. {
  261. return repsep_snprintf(bf, size, "%-*s", width,
  262. he->parent ? he->parent->name : "[other]");
  263. }
  264. struct sort_entry sort_parent = {
  265. .se_header = "Parent symbol",
  266. .se_cmp = sort__parent_cmp,
  267. .se_snprintf = hist_entry__parent_snprintf,
  268. .se_width_idx = HISTC_PARENT,
  269. };
  270. /* --sort cpu */
  271. static int64_t
  272. sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
  273. {
  274. return right->cpu - left->cpu;
  275. }
  276. static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
  277. size_t size, unsigned int width)
  278. {
  279. return repsep_snprintf(bf, size, "%*d", width, he->cpu);
  280. }
  281. struct sort_entry sort_cpu = {
  282. .se_header = "CPU",
  283. .se_cmp = sort__cpu_cmp,
  284. .se_snprintf = hist_entry__cpu_snprintf,
  285. .se_width_idx = HISTC_CPU,
  286. };
  287. /* sort keys for branch stacks */
  288. static int64_t
  289. sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
  290. {
  291. return _sort__dso_cmp(left->branch_info->from.map,
  292. right->branch_info->from.map);
  293. }
  294. static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
  295. size_t size, unsigned int width)
  296. {
  297. return _hist_entry__dso_snprintf(he->branch_info->from.map,
  298. bf, size, width);
  299. }
  300. static int64_t
  301. sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
  302. {
  303. return _sort__dso_cmp(left->branch_info->to.map,
  304. right->branch_info->to.map);
  305. }
  306. static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
  307. size_t size, unsigned int width)
  308. {
  309. return _hist_entry__dso_snprintf(he->branch_info->to.map,
  310. bf, size, width);
  311. }
  312. static int64_t
  313. sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
  314. {
  315. struct addr_map_symbol *from_l = &left->branch_info->from;
  316. struct addr_map_symbol *from_r = &right->branch_info->from;
  317. if (!from_l->sym && !from_r->sym)
  318. return _sort__addr_cmp(from_l->addr, from_r->addr);
  319. return _sort__sym_cmp(from_l->sym, from_r->sym);
  320. }
  321. static int64_t
  322. sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
  323. {
  324. struct addr_map_symbol *to_l = &left->branch_info->to;
  325. struct addr_map_symbol *to_r = &right->branch_info->to;
  326. if (!to_l->sym && !to_r->sym)
  327. return _sort__addr_cmp(to_l->addr, to_r->addr);
  328. return _sort__sym_cmp(to_l->sym, to_r->sym);
  329. }
  330. static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
  331. size_t size, unsigned int width)
  332. {
  333. struct addr_map_symbol *from = &he->branch_info->from;
  334. return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
  335. he->level, bf, size, width);
  336. }
  337. static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
  338. size_t size, unsigned int width)
  339. {
  340. struct addr_map_symbol *to = &he->branch_info->to;
  341. return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
  342. he->level, bf, size, width);
  343. }
  344. struct sort_entry sort_dso_from = {
  345. .se_header = "Source Shared Object",
  346. .se_cmp = sort__dso_from_cmp,
  347. .se_snprintf = hist_entry__dso_from_snprintf,
  348. .se_width_idx = HISTC_DSO_FROM,
  349. };
  350. struct sort_entry sort_dso_to = {
  351. .se_header = "Target Shared Object",
  352. .se_cmp = sort__dso_to_cmp,
  353. .se_snprintf = hist_entry__dso_to_snprintf,
  354. .se_width_idx = HISTC_DSO_TO,
  355. };
  356. struct sort_entry sort_sym_from = {
  357. .se_header = "Source Symbol",
  358. .se_cmp = sort__sym_from_cmp,
  359. .se_snprintf = hist_entry__sym_from_snprintf,
  360. .se_width_idx = HISTC_SYMBOL_FROM,
  361. };
  362. struct sort_entry sort_sym_to = {
  363. .se_header = "Target Symbol",
  364. .se_cmp = sort__sym_to_cmp,
  365. .se_snprintf = hist_entry__sym_to_snprintf,
  366. .se_width_idx = HISTC_SYMBOL_TO,
  367. };
  368. static int64_t
  369. sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
  370. {
  371. const unsigned char mp = left->branch_info->flags.mispred !=
  372. right->branch_info->flags.mispred;
  373. const unsigned char p = left->branch_info->flags.predicted !=
  374. right->branch_info->flags.predicted;
  375. return mp || p;
  376. }
  377. static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
  378. size_t size, unsigned int width){
  379. static const char *out = "N/A";
  380. if (he->branch_info->flags.predicted)
  381. out = "N";
  382. else if (he->branch_info->flags.mispred)
  383. out = "Y";
  384. return repsep_snprintf(bf, size, "%-*s", width, out);
  385. }
  386. /* --sort daddr_sym */
  387. static int64_t
  388. sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  389. {
  390. uint64_t l = 0, r = 0;
  391. if (left->mem_info)
  392. l = left->mem_info->daddr.addr;
  393. if (right->mem_info)
  394. r = right->mem_info->daddr.addr;
  395. return (int64_t)(r - l);
  396. }
  397. static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
  398. size_t size, unsigned int width)
  399. {
  400. uint64_t addr = 0;
  401. struct map *map = NULL;
  402. struct symbol *sym = NULL;
  403. if (he->mem_info) {
  404. addr = he->mem_info->daddr.addr;
  405. map = he->mem_info->daddr.map;
  406. sym = he->mem_info->daddr.sym;
  407. }
  408. return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
  409. width);
  410. }
  411. static int64_t
  412. sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  413. {
  414. struct map *map_l = NULL;
  415. struct map *map_r = NULL;
  416. if (left->mem_info)
  417. map_l = left->mem_info->daddr.map;
  418. if (right->mem_info)
  419. map_r = right->mem_info->daddr.map;
  420. return _sort__dso_cmp(map_l, map_r);
  421. }
  422. static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
  423. size_t size, unsigned int width)
  424. {
  425. struct map *map = NULL;
  426. if (he->mem_info)
  427. map = he->mem_info->daddr.map;
  428. return _hist_entry__dso_snprintf(map, bf, size, width);
  429. }
  430. static int64_t
  431. sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
  432. {
  433. union perf_mem_data_src data_src_l;
  434. union perf_mem_data_src data_src_r;
  435. if (left->mem_info)
  436. data_src_l = left->mem_info->data_src;
  437. else
  438. data_src_l.mem_lock = PERF_MEM_LOCK_NA;
  439. if (right->mem_info)
  440. data_src_r = right->mem_info->data_src;
  441. else
  442. data_src_r.mem_lock = PERF_MEM_LOCK_NA;
  443. return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
  444. }
  445. static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
  446. size_t size, unsigned int width)
  447. {
  448. const char *out;
  449. u64 mask = PERF_MEM_LOCK_NA;
  450. if (he->mem_info)
  451. mask = he->mem_info->data_src.mem_lock;
  452. if (mask & PERF_MEM_LOCK_NA)
  453. out = "N/A";
  454. else if (mask & PERF_MEM_LOCK_LOCKED)
  455. out = "Yes";
  456. else
  457. out = "No";
  458. return repsep_snprintf(bf, size, "%-*s", width, out);
  459. }
  460. static int64_t
  461. sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
  462. {
  463. union perf_mem_data_src data_src_l;
  464. union perf_mem_data_src data_src_r;
  465. if (left->mem_info)
  466. data_src_l = left->mem_info->data_src;
  467. else
  468. data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
  469. if (right->mem_info)
  470. data_src_r = right->mem_info->data_src;
  471. else
  472. data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
  473. return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
  474. }
  475. static const char * const tlb_access[] = {
  476. "N/A",
  477. "HIT",
  478. "MISS",
  479. "L1",
  480. "L2",
  481. "Walker",
  482. "Fault",
  483. };
  484. #define NUM_TLB_ACCESS (sizeof(tlb_access)/sizeof(const char *))
  485. static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
  486. size_t size, unsigned int width)
  487. {
  488. char out[64];
  489. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  490. size_t l = 0, i;
  491. u64 m = PERF_MEM_TLB_NA;
  492. u64 hit, miss;
  493. out[0] = '\0';
  494. if (he->mem_info)
  495. m = he->mem_info->data_src.mem_dtlb;
  496. hit = m & PERF_MEM_TLB_HIT;
  497. miss = m & PERF_MEM_TLB_MISS;
  498. /* already taken care of */
  499. m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS);
  500. for (i = 0; m && i < NUM_TLB_ACCESS; i++, m >>= 1) {
  501. if (!(m & 0x1))
  502. continue;
  503. if (l) {
  504. strcat(out, " or ");
  505. l += 4;
  506. }
  507. strncat(out, tlb_access[i], sz - l);
  508. l += strlen(tlb_access[i]);
  509. }
  510. if (*out == '\0')
  511. strcpy(out, "N/A");
  512. if (hit)
  513. strncat(out, " hit", sz - l);
  514. if (miss)
  515. strncat(out, " miss", sz - l);
  516. return repsep_snprintf(bf, size, "%-*s", width, out);
  517. }
  518. static int64_t
  519. sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
  520. {
  521. union perf_mem_data_src data_src_l;
  522. union perf_mem_data_src data_src_r;
  523. if (left->mem_info)
  524. data_src_l = left->mem_info->data_src;
  525. else
  526. data_src_l.mem_lvl = PERF_MEM_LVL_NA;
  527. if (right->mem_info)
  528. data_src_r = right->mem_info->data_src;
  529. else
  530. data_src_r.mem_lvl = PERF_MEM_LVL_NA;
  531. return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
  532. }
  533. static const char * const mem_lvl[] = {
  534. "N/A",
  535. "HIT",
  536. "MISS",
  537. "L1",
  538. "LFB",
  539. "L2",
  540. "L3",
  541. "Local RAM",
  542. "Remote RAM (1 hop)",
  543. "Remote RAM (2 hops)",
  544. "Remote Cache (1 hop)",
  545. "Remote Cache (2 hops)",
  546. "I/O",
  547. "Uncached",
  548. };
  549. #define NUM_MEM_LVL (sizeof(mem_lvl)/sizeof(const char *))
  550. static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
  551. size_t size, unsigned int width)
  552. {
  553. char out[64];
  554. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  555. size_t i, l = 0;
  556. u64 m = PERF_MEM_LVL_NA;
  557. u64 hit, miss;
  558. if (he->mem_info)
  559. m = he->mem_info->data_src.mem_lvl;
  560. out[0] = '\0';
  561. hit = m & PERF_MEM_LVL_HIT;
  562. miss = m & PERF_MEM_LVL_MISS;
  563. /* already taken care of */
  564. m &= ~(PERF_MEM_LVL_HIT|PERF_MEM_LVL_MISS);
  565. for (i = 0; m && i < NUM_MEM_LVL; i++, m >>= 1) {
  566. if (!(m & 0x1))
  567. continue;
  568. if (l) {
  569. strcat(out, " or ");
  570. l += 4;
  571. }
  572. strncat(out, mem_lvl[i], sz - l);
  573. l += strlen(mem_lvl[i]);
  574. }
  575. if (*out == '\0')
  576. strcpy(out, "N/A");
  577. if (hit)
  578. strncat(out, " hit", sz - l);
  579. if (miss)
  580. strncat(out, " miss", sz - l);
  581. return repsep_snprintf(bf, size, "%-*s", width, out);
  582. }
  583. static int64_t
  584. sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
  585. {
  586. union perf_mem_data_src data_src_l;
  587. union perf_mem_data_src data_src_r;
  588. if (left->mem_info)
  589. data_src_l = left->mem_info->data_src;
  590. else
  591. data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
  592. if (right->mem_info)
  593. data_src_r = right->mem_info->data_src;
  594. else
  595. data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
  596. return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
  597. }
  598. static const char * const snoop_access[] = {
  599. "N/A",
  600. "None",
  601. "Miss",
  602. "Hit",
  603. "HitM",
  604. };
  605. #define NUM_SNOOP_ACCESS (sizeof(snoop_access)/sizeof(const char *))
  606. static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
  607. size_t size, unsigned int width)
  608. {
  609. char out[64];
  610. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  611. size_t i, l = 0;
  612. u64 m = PERF_MEM_SNOOP_NA;
  613. out[0] = '\0';
  614. if (he->mem_info)
  615. m = he->mem_info->data_src.mem_snoop;
  616. for (i = 0; m && i < NUM_SNOOP_ACCESS; i++, m >>= 1) {
  617. if (!(m & 0x1))
  618. continue;
  619. if (l) {
  620. strcat(out, " or ");
  621. l += 4;
  622. }
  623. strncat(out, snoop_access[i], sz - l);
  624. l += strlen(snoop_access[i]);
  625. }
  626. if (*out == '\0')
  627. strcpy(out, "N/A");
  628. return repsep_snprintf(bf, size, "%-*s", width, out);
  629. }
  630. struct sort_entry sort_mispredict = {
  631. .se_header = "Branch Mispredicted",
  632. .se_cmp = sort__mispredict_cmp,
  633. .se_snprintf = hist_entry__mispredict_snprintf,
  634. .se_width_idx = HISTC_MISPREDICT,
  635. };
  636. static u64 he_weight(struct hist_entry *he)
  637. {
  638. return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
  639. }
  640. static int64_t
  641. sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  642. {
  643. return he_weight(left) - he_weight(right);
  644. }
  645. static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
  646. size_t size, unsigned int width)
  647. {
  648. return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
  649. }
  650. struct sort_entry sort_local_weight = {
  651. .se_header = "Local Weight",
  652. .se_cmp = sort__local_weight_cmp,
  653. .se_snprintf = hist_entry__local_weight_snprintf,
  654. .se_width_idx = HISTC_LOCAL_WEIGHT,
  655. };
  656. static int64_t
  657. sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  658. {
  659. return left->stat.weight - right->stat.weight;
  660. }
  661. static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
  662. size_t size, unsigned int width)
  663. {
  664. return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
  665. }
  666. struct sort_entry sort_global_weight = {
  667. .se_header = "Weight",
  668. .se_cmp = sort__global_weight_cmp,
  669. .se_snprintf = hist_entry__global_weight_snprintf,
  670. .se_width_idx = HISTC_GLOBAL_WEIGHT,
  671. };
  672. struct sort_entry sort_mem_daddr_sym = {
  673. .se_header = "Data Symbol",
  674. .se_cmp = sort__daddr_cmp,
  675. .se_snprintf = hist_entry__daddr_snprintf,
  676. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  677. };
  678. struct sort_entry sort_mem_daddr_dso = {
  679. .se_header = "Data Object",
  680. .se_cmp = sort__dso_daddr_cmp,
  681. .se_snprintf = hist_entry__dso_daddr_snprintf,
  682. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  683. };
  684. struct sort_entry sort_mem_locked = {
  685. .se_header = "Locked",
  686. .se_cmp = sort__locked_cmp,
  687. .se_snprintf = hist_entry__locked_snprintf,
  688. .se_width_idx = HISTC_MEM_LOCKED,
  689. };
  690. struct sort_entry sort_mem_tlb = {
  691. .se_header = "TLB access",
  692. .se_cmp = sort__tlb_cmp,
  693. .se_snprintf = hist_entry__tlb_snprintf,
  694. .se_width_idx = HISTC_MEM_TLB,
  695. };
  696. struct sort_entry sort_mem_lvl = {
  697. .se_header = "Memory access",
  698. .se_cmp = sort__lvl_cmp,
  699. .se_snprintf = hist_entry__lvl_snprintf,
  700. .se_width_idx = HISTC_MEM_LVL,
  701. };
  702. struct sort_entry sort_mem_snoop = {
  703. .se_header = "Snoop",
  704. .se_cmp = sort__snoop_cmp,
  705. .se_snprintf = hist_entry__snoop_snprintf,
  706. .se_width_idx = HISTC_MEM_SNOOP,
  707. };
  708. static int64_t
  709. sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
  710. {
  711. return left->branch_info->flags.abort !=
  712. right->branch_info->flags.abort;
  713. }
  714. static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
  715. size_t size, unsigned int width)
  716. {
  717. static const char *out = ".";
  718. if (he->branch_info->flags.abort)
  719. out = "A";
  720. return repsep_snprintf(bf, size, "%-*s", width, out);
  721. }
  722. struct sort_entry sort_abort = {
  723. .se_header = "Transaction abort",
  724. .se_cmp = sort__abort_cmp,
  725. .se_snprintf = hist_entry__abort_snprintf,
  726. .se_width_idx = HISTC_ABORT,
  727. };
  728. static int64_t
  729. sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
  730. {
  731. return left->branch_info->flags.in_tx !=
  732. right->branch_info->flags.in_tx;
  733. }
  734. static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
  735. size_t size, unsigned int width)
  736. {
  737. static const char *out = ".";
  738. if (he->branch_info->flags.in_tx)
  739. out = "T";
  740. return repsep_snprintf(bf, size, "%-*s", width, out);
  741. }
  742. struct sort_entry sort_in_tx = {
  743. .se_header = "Branch in transaction",
  744. .se_cmp = sort__in_tx_cmp,
  745. .se_snprintf = hist_entry__in_tx_snprintf,
  746. .se_width_idx = HISTC_IN_TX,
  747. };
  748. static int64_t
  749. sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
  750. {
  751. return left->transaction - right->transaction;
  752. }
  753. static inline char *add_str(char *p, const char *str)
  754. {
  755. strcpy(p, str);
  756. return p + strlen(str);
  757. }
  758. static struct txbit {
  759. unsigned flag;
  760. const char *name;
  761. int skip_for_len;
  762. } txbits[] = {
  763. { PERF_TXN_ELISION, "EL ", 0 },
  764. { PERF_TXN_TRANSACTION, "TX ", 1 },
  765. { PERF_TXN_SYNC, "SYNC ", 1 },
  766. { PERF_TXN_ASYNC, "ASYNC ", 0 },
  767. { PERF_TXN_RETRY, "RETRY ", 0 },
  768. { PERF_TXN_CONFLICT, "CON ", 0 },
  769. { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
  770. { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
  771. { 0, NULL, 0 }
  772. };
  773. int hist_entry__transaction_len(void)
  774. {
  775. int i;
  776. int len = 0;
  777. for (i = 0; txbits[i].name; i++) {
  778. if (!txbits[i].skip_for_len)
  779. len += strlen(txbits[i].name);
  780. }
  781. len += 4; /* :XX<space> */
  782. return len;
  783. }
  784. static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
  785. size_t size, unsigned int width)
  786. {
  787. u64 t = he->transaction;
  788. char buf[128];
  789. char *p = buf;
  790. int i;
  791. buf[0] = 0;
  792. for (i = 0; txbits[i].name; i++)
  793. if (txbits[i].flag & t)
  794. p = add_str(p, txbits[i].name);
  795. if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
  796. p = add_str(p, "NEITHER ");
  797. if (t & PERF_TXN_ABORT_MASK) {
  798. sprintf(p, ":%" PRIx64,
  799. (t & PERF_TXN_ABORT_MASK) >>
  800. PERF_TXN_ABORT_SHIFT);
  801. p += strlen(p);
  802. }
  803. return repsep_snprintf(bf, size, "%-*s", width, buf);
  804. }
  805. struct sort_entry sort_transaction = {
  806. .se_header = "Transaction ",
  807. .se_cmp = sort__transaction_cmp,
  808. .se_snprintf = hist_entry__transaction_snprintf,
  809. .se_width_idx = HISTC_TRANSACTION,
  810. };
  811. struct sort_dimension {
  812. const char *name;
  813. struct sort_entry *entry;
  814. int taken;
  815. };
  816. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  817. static struct sort_dimension common_sort_dimensions[] = {
  818. DIM(SORT_PID, "pid", sort_thread),
  819. DIM(SORT_COMM, "comm", sort_comm),
  820. DIM(SORT_DSO, "dso", sort_dso),
  821. DIM(SORT_SYM, "symbol", sort_sym),
  822. DIM(SORT_PARENT, "parent", sort_parent),
  823. DIM(SORT_CPU, "cpu", sort_cpu),
  824. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  825. DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
  826. DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
  827. DIM(SORT_TRANSACTION, "transaction", sort_transaction),
  828. };
  829. #undef DIM
  830. #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
  831. static struct sort_dimension bstack_sort_dimensions[] = {
  832. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  833. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  834. DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
  835. DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
  836. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  837. DIM(SORT_IN_TX, "in_tx", sort_in_tx),
  838. DIM(SORT_ABORT, "abort", sort_abort),
  839. };
  840. #undef DIM
  841. #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
  842. static struct sort_dimension memory_sort_dimensions[] = {
  843. DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
  844. DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
  845. DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
  846. DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
  847. DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
  848. DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
  849. };
  850. #undef DIM
  851. static void __sort_dimension__add(struct sort_dimension *sd, enum sort_type idx)
  852. {
  853. if (sd->taken)
  854. return;
  855. if (sd->entry->se_collapse)
  856. sort__need_collapse = 1;
  857. if (list_empty(&hist_entry__sort_list))
  858. sort__first_dimension = idx;
  859. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  860. sd->taken = 1;
  861. }
  862. int sort_dimension__add(const char *tok)
  863. {
  864. unsigned int i;
  865. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  866. struct sort_dimension *sd = &common_sort_dimensions[i];
  867. if (strncasecmp(tok, sd->name, strlen(tok)))
  868. continue;
  869. if (sd->entry == &sort_parent) {
  870. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  871. if (ret) {
  872. char err[BUFSIZ];
  873. regerror(ret, &parent_regex, err, sizeof(err));
  874. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  875. return -EINVAL;
  876. }
  877. sort__has_parent = 1;
  878. } else if (sd->entry == &sort_sym) {
  879. sort__has_sym = 1;
  880. } else if (sd->entry == &sort_dso) {
  881. sort__has_dso = 1;
  882. }
  883. __sort_dimension__add(sd, i);
  884. return 0;
  885. }
  886. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  887. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  888. if (strncasecmp(tok, sd->name, strlen(tok)))
  889. continue;
  890. if (sort__mode != SORT_MODE__BRANCH)
  891. return -EINVAL;
  892. if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
  893. sort__has_sym = 1;
  894. __sort_dimension__add(sd, i + __SORT_BRANCH_STACK);
  895. return 0;
  896. }
  897. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  898. struct sort_dimension *sd = &memory_sort_dimensions[i];
  899. if (strncasecmp(tok, sd->name, strlen(tok)))
  900. continue;
  901. if (sort__mode != SORT_MODE__MEMORY)
  902. return -EINVAL;
  903. if (sd->entry == &sort_mem_daddr_sym)
  904. sort__has_sym = 1;
  905. __sort_dimension__add(sd, i + __SORT_MEMORY_MODE);
  906. return 0;
  907. }
  908. return -ESRCH;
  909. }
  910. int setup_sorting(void)
  911. {
  912. char *tmp, *tok, *str = strdup(sort_order);
  913. int ret = 0;
  914. if (str == NULL) {
  915. error("Not enough memory to setup sort keys");
  916. return -ENOMEM;
  917. }
  918. for (tok = strtok_r(str, ", ", &tmp);
  919. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  920. ret = sort_dimension__add(tok);
  921. if (ret == -EINVAL) {
  922. error("Invalid --sort key: `%s'", tok);
  923. break;
  924. } else if (ret == -ESRCH) {
  925. error("Unknown --sort key: `%s'", tok);
  926. break;
  927. }
  928. }
  929. free(str);
  930. return ret;
  931. }
  932. static void sort_entry__setup_elide(struct sort_entry *se,
  933. struct strlist *list,
  934. const char *list_name, FILE *fp)
  935. {
  936. if (list && strlist__nr_entries(list) == 1) {
  937. if (fp != NULL)
  938. fprintf(fp, "# %s: %s\n", list_name,
  939. strlist__entry(list, 0)->s);
  940. se->elide = true;
  941. }
  942. }
  943. void sort__setup_elide(FILE *output)
  944. {
  945. struct sort_entry *se;
  946. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  947. "dso", output);
  948. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list,
  949. "comm", output);
  950. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list,
  951. "symbol", output);
  952. if (sort__mode == SORT_MODE__BRANCH) {
  953. sort_entry__setup_elide(&sort_dso_from,
  954. symbol_conf.dso_from_list,
  955. "dso_from", output);
  956. sort_entry__setup_elide(&sort_dso_to,
  957. symbol_conf.dso_to_list,
  958. "dso_to", output);
  959. sort_entry__setup_elide(&sort_sym_from,
  960. symbol_conf.sym_from_list,
  961. "sym_from", output);
  962. sort_entry__setup_elide(&sort_sym_to,
  963. symbol_conf.sym_to_list,
  964. "sym_to", output);
  965. } else if (sort__mode == SORT_MODE__MEMORY) {
  966. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  967. "symbol_daddr", output);
  968. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  969. "dso_daddr", output);
  970. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  971. "mem", output);
  972. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  973. "local_weight", output);
  974. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  975. "tlb", output);
  976. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  977. "snoop", output);
  978. }
  979. /*
  980. * It makes no sense to elide all of sort entries.
  981. * Just revert them to show up again.
  982. */
  983. list_for_each_entry(se, &hist_entry__sort_list, list) {
  984. if (!se->elide)
  985. return;
  986. }
  987. list_for_each_entry(se, &hist_entry__sort_list, list)
  988. se->elide = false;
  989. }