sort.c 28 KB

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