sort.c 28 KB

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