sort.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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 cmd[PATH_MAX + 2], *path = self->srcline, *nl;
  209. size_t line_len;
  210. if (path != NULL)
  211. goto out_path;
  212. if (!self->ms.map)
  213. goto out_ip;
  214. if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
  215. goto out_ip;
  216. snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
  217. self->ms.map->dso->long_name, self->ip);
  218. fp = popen(cmd, "r");
  219. if (!fp)
  220. goto out_ip;
  221. if (getline(&self->srcline, &line_len, fp) < 0 || !line_len)
  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. static int64_t
  703. sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
  704. {
  705. return left->branch_info->flags.abort !=
  706. right->branch_info->flags.abort;
  707. }
  708. static int hist_entry__abort_snprintf(struct hist_entry *self, char *bf,
  709. size_t size, unsigned int width)
  710. {
  711. static const char *out = ".";
  712. if (self->branch_info->flags.abort)
  713. out = "A";
  714. return repsep_snprintf(bf, size, "%-*s", width, out);
  715. }
  716. struct sort_entry sort_abort = {
  717. .se_header = "Transaction abort",
  718. .se_cmp = sort__abort_cmp,
  719. .se_snprintf = hist_entry__abort_snprintf,
  720. .se_width_idx = HISTC_ABORT,
  721. };
  722. static int64_t
  723. sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
  724. {
  725. return left->branch_info->flags.in_tx !=
  726. right->branch_info->flags.in_tx;
  727. }
  728. static int hist_entry__in_tx_snprintf(struct hist_entry *self, char *bf,
  729. size_t size, unsigned int width)
  730. {
  731. static const char *out = ".";
  732. if (self->branch_info->flags.in_tx)
  733. out = "T";
  734. return repsep_snprintf(bf, size, "%-*s", width, out);
  735. }
  736. struct sort_entry sort_in_tx = {
  737. .se_header = "Branch in transaction",
  738. .se_cmp = sort__in_tx_cmp,
  739. .se_snprintf = hist_entry__in_tx_snprintf,
  740. .se_width_idx = HISTC_IN_TX,
  741. };
  742. static int64_t
  743. sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
  744. {
  745. return left->transaction - right->transaction;
  746. }
  747. static inline char *add_str(char *p, const char *str)
  748. {
  749. strcpy(p, str);
  750. return p + strlen(str);
  751. }
  752. static struct txbit {
  753. unsigned flag;
  754. const char *name;
  755. int skip_for_len;
  756. } txbits[] = {
  757. { PERF_TXN_ELISION, "EL ", 0 },
  758. { PERF_TXN_TRANSACTION, "TX ", 1 },
  759. { PERF_TXN_SYNC, "SYNC ", 1 },
  760. { PERF_TXN_ASYNC, "ASYNC ", 0 },
  761. { PERF_TXN_RETRY, "RETRY ", 0 },
  762. { PERF_TXN_CONFLICT, "CON ", 0 },
  763. { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
  764. { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
  765. { 0, NULL, 0 }
  766. };
  767. int hist_entry__transaction_len(void)
  768. {
  769. int i;
  770. int len = 0;
  771. for (i = 0; txbits[i].name; i++) {
  772. if (!txbits[i].skip_for_len)
  773. len += strlen(txbits[i].name);
  774. }
  775. len += 4; /* :XX<space> */
  776. return len;
  777. }
  778. static int hist_entry__transaction_snprintf(struct hist_entry *self, char *bf,
  779. size_t size, unsigned int width)
  780. {
  781. u64 t = self->transaction;
  782. char buf[128];
  783. char *p = buf;
  784. int i;
  785. buf[0] = 0;
  786. for (i = 0; txbits[i].name; i++)
  787. if (txbits[i].flag & t)
  788. p = add_str(p, txbits[i].name);
  789. if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
  790. p = add_str(p, "NEITHER ");
  791. if (t & PERF_TXN_ABORT_MASK) {
  792. sprintf(p, ":%" PRIx64,
  793. (t & PERF_TXN_ABORT_MASK) >>
  794. PERF_TXN_ABORT_SHIFT);
  795. p += strlen(p);
  796. }
  797. return repsep_snprintf(bf, size, "%-*s", width, buf);
  798. }
  799. struct sort_entry sort_transaction = {
  800. .se_header = "Transaction ",
  801. .se_cmp = sort__transaction_cmp,
  802. .se_snprintf = hist_entry__transaction_snprintf,
  803. .se_width_idx = HISTC_TRANSACTION,
  804. };
  805. struct sort_dimension {
  806. const char *name;
  807. struct sort_entry *entry;
  808. int taken;
  809. };
  810. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  811. static struct sort_dimension common_sort_dimensions[] = {
  812. DIM(SORT_PID, "pid", sort_thread),
  813. DIM(SORT_COMM, "comm", sort_comm),
  814. DIM(SORT_DSO, "dso", sort_dso),
  815. DIM(SORT_SYM, "symbol", sort_sym),
  816. DIM(SORT_PARENT, "parent", sort_parent),
  817. DIM(SORT_CPU, "cpu", sort_cpu),
  818. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  819. DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
  820. DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
  821. DIM(SORT_TRANSACTION, "transaction", sort_transaction),
  822. };
  823. #undef DIM
  824. #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
  825. static struct sort_dimension bstack_sort_dimensions[] = {
  826. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  827. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  828. DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
  829. DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
  830. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  831. DIM(SORT_IN_TX, "in_tx", sort_in_tx),
  832. DIM(SORT_ABORT, "abort", sort_abort),
  833. };
  834. #undef DIM
  835. #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
  836. static struct sort_dimension memory_sort_dimensions[] = {
  837. DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
  838. DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
  839. DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
  840. DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
  841. DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
  842. DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
  843. };
  844. #undef DIM
  845. static void __sort_dimension__add(struct sort_dimension *sd, enum sort_type idx)
  846. {
  847. if (sd->taken)
  848. return;
  849. if (sd->entry->se_collapse)
  850. sort__need_collapse = 1;
  851. if (list_empty(&hist_entry__sort_list))
  852. sort__first_dimension = idx;
  853. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  854. sd->taken = 1;
  855. }
  856. int sort_dimension__add(const char *tok)
  857. {
  858. unsigned int i;
  859. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  860. struct sort_dimension *sd = &common_sort_dimensions[i];
  861. if (strncasecmp(tok, sd->name, strlen(tok)))
  862. continue;
  863. if (sd->entry == &sort_parent) {
  864. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  865. if (ret) {
  866. char err[BUFSIZ];
  867. regerror(ret, &parent_regex, err, sizeof(err));
  868. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  869. return -EINVAL;
  870. }
  871. sort__has_parent = 1;
  872. } else if (sd->entry == &sort_sym) {
  873. sort__has_sym = 1;
  874. }
  875. __sort_dimension__add(sd, i);
  876. return 0;
  877. }
  878. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  879. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  880. if (strncasecmp(tok, sd->name, strlen(tok)))
  881. continue;
  882. if (sort__mode != SORT_MODE__BRANCH)
  883. return -EINVAL;
  884. if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
  885. sort__has_sym = 1;
  886. __sort_dimension__add(sd, i + __SORT_BRANCH_STACK);
  887. return 0;
  888. }
  889. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  890. struct sort_dimension *sd = &memory_sort_dimensions[i];
  891. if (strncasecmp(tok, sd->name, strlen(tok)))
  892. continue;
  893. if (sort__mode != SORT_MODE__MEMORY)
  894. return -EINVAL;
  895. if (sd->entry == &sort_mem_daddr_sym)
  896. sort__has_sym = 1;
  897. __sort_dimension__add(sd, i + __SORT_MEMORY_MODE);
  898. return 0;
  899. }
  900. return -ESRCH;
  901. }
  902. int setup_sorting(void)
  903. {
  904. char *tmp, *tok, *str = strdup(sort_order);
  905. int ret = 0;
  906. if (str == NULL) {
  907. error("Not enough memory to setup sort keys");
  908. return -ENOMEM;
  909. }
  910. for (tok = strtok_r(str, ", ", &tmp);
  911. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  912. ret = sort_dimension__add(tok);
  913. if (ret == -EINVAL) {
  914. error("Invalid --sort key: `%s'", tok);
  915. break;
  916. } else if (ret == -ESRCH) {
  917. error("Unknown --sort key: `%s'", tok);
  918. break;
  919. }
  920. }
  921. free(str);
  922. return ret;
  923. }
  924. static void sort_entry__setup_elide(struct sort_entry *self,
  925. struct strlist *list,
  926. const char *list_name, FILE *fp)
  927. {
  928. if (list && strlist__nr_entries(list) == 1) {
  929. if (fp != NULL)
  930. fprintf(fp, "# %s: %s\n", list_name,
  931. strlist__entry(list, 0)->s);
  932. self->elide = true;
  933. }
  934. }
  935. void sort__setup_elide(FILE *output)
  936. {
  937. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  938. "dso", output);
  939. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list,
  940. "comm", output);
  941. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list,
  942. "symbol", output);
  943. if (sort__mode == SORT_MODE__BRANCH) {
  944. sort_entry__setup_elide(&sort_dso_from,
  945. symbol_conf.dso_from_list,
  946. "dso_from", output);
  947. sort_entry__setup_elide(&sort_dso_to,
  948. symbol_conf.dso_to_list,
  949. "dso_to", output);
  950. sort_entry__setup_elide(&sort_sym_from,
  951. symbol_conf.sym_from_list,
  952. "sym_from", output);
  953. sort_entry__setup_elide(&sort_sym_to,
  954. symbol_conf.sym_to_list,
  955. "sym_to", output);
  956. } else if (sort__mode == SORT_MODE__MEMORY) {
  957. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  958. "symbol_daddr", output);
  959. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  960. "dso_daddr", output);
  961. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  962. "mem", output);
  963. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  964. "local_weight", output);
  965. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  966. "tlb", output);
  967. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  968. "snoop", output);
  969. }
  970. }