sort.c 24 KB

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