sort.c 28 KB

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