sort.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. int sort__branch_mode = -1; /* -1 = means not set */
  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. u64 ip_l, u64 ip_r)
  134. {
  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. u64 ip_l, ip_r;
  147. if (!left->ms.sym && !right->ms.sym)
  148. return right->level - left->level;
  149. if (!left->ms.sym || !right->ms.sym)
  150. return cmp_null(left->ms.sym, right->ms.sym);
  151. if (left->ms.sym == right->ms.sym)
  152. return 0;
  153. ip_l = left->ms.sym->start;
  154. ip_r = right->ms.sym->start;
  155. return _sort__sym_cmp(left->ms.sym, right->ms.sym, ip_l, ip_r);
  156. }
  157. static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
  158. u64 ip, char level, char *bf, size_t size,
  159. unsigned int width)
  160. {
  161. size_t ret = 0;
  162. if (verbose) {
  163. char o = map ? dso__symtab_origin(map->dso) : '!';
  164. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  165. BITS_PER_LONG / 4, ip, o);
  166. }
  167. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
  168. if (sym)
  169. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  170. width - ret,
  171. sym->name);
  172. else {
  173. size_t len = BITS_PER_LONG / 4;
  174. ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
  175. len, ip);
  176. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  177. width - ret, "");
  178. }
  179. return ret;
  180. }
  181. static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
  182. size_t size, unsigned int width)
  183. {
  184. return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
  185. self->level, bf, size, width);
  186. }
  187. struct sort_entry sort_sym = {
  188. .se_header = "Symbol",
  189. .se_cmp = sort__sym_cmp,
  190. .se_snprintf = hist_entry__sym_snprintf,
  191. .se_width_idx = HISTC_SYMBOL,
  192. };
  193. /* --sort srcline */
  194. static int64_t
  195. sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
  196. {
  197. return (int64_t)(right->ip - left->ip);
  198. }
  199. static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
  200. size_t size,
  201. unsigned int width __maybe_unused)
  202. {
  203. FILE *fp;
  204. char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
  205. size_t line_len;
  206. if (path != NULL)
  207. goto out_path;
  208. if (!self->ms.map)
  209. goto out_ip;
  210. if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
  211. goto out_ip;
  212. snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
  213. self->ms.map->dso->long_name, self->ip);
  214. fp = popen(cmd, "r");
  215. if (!fp)
  216. goto out_ip;
  217. if (getline(&path, &line_len, fp) < 0 || !line_len)
  218. goto out_ip;
  219. fclose(fp);
  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. return repsep_snprintf(bf, size, "%s", path);
  229. out_ip:
  230. return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
  231. }
  232. struct sort_entry sort_srcline = {
  233. .se_header = "Source:Line",
  234. .se_cmp = sort__srcline_cmp,
  235. .se_snprintf = hist_entry__srcline_snprintf,
  236. .se_width_idx = HISTC_SRCLINE,
  237. };
  238. /* --sort parent */
  239. static int64_t
  240. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  241. {
  242. struct symbol *sym_l = left->parent;
  243. struct symbol *sym_r = right->parent;
  244. if (!sym_l || !sym_r)
  245. return cmp_null(sym_l, sym_r);
  246. return strcmp(sym_l->name, sym_r->name);
  247. }
  248. static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
  249. size_t size, unsigned int width)
  250. {
  251. return repsep_snprintf(bf, size, "%-*s", width,
  252. self->parent ? self->parent->name : "[other]");
  253. }
  254. struct sort_entry sort_parent = {
  255. .se_header = "Parent symbol",
  256. .se_cmp = sort__parent_cmp,
  257. .se_snprintf = hist_entry__parent_snprintf,
  258. .se_width_idx = HISTC_PARENT,
  259. };
  260. /* --sort cpu */
  261. static int64_t
  262. sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
  263. {
  264. return right->cpu - left->cpu;
  265. }
  266. static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
  267. size_t size, unsigned int width)
  268. {
  269. return repsep_snprintf(bf, size, "%*d", width, self->cpu);
  270. }
  271. struct sort_entry sort_cpu = {
  272. .se_header = "CPU",
  273. .se_cmp = sort__cpu_cmp,
  274. .se_snprintf = hist_entry__cpu_snprintf,
  275. .se_width_idx = HISTC_CPU,
  276. };
  277. /* sort keys for branch stacks */
  278. static int64_t
  279. sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
  280. {
  281. return _sort__dso_cmp(left->branch_info->from.map,
  282. right->branch_info->from.map);
  283. }
  284. static int hist_entry__dso_from_snprintf(struct hist_entry *self, char *bf,
  285. size_t size, unsigned int width)
  286. {
  287. return _hist_entry__dso_snprintf(self->branch_info->from.map,
  288. bf, size, width);
  289. }
  290. static int64_t
  291. sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
  292. {
  293. return _sort__dso_cmp(left->branch_info->to.map,
  294. right->branch_info->to.map);
  295. }
  296. static int hist_entry__dso_to_snprintf(struct hist_entry *self, char *bf,
  297. size_t size, unsigned int width)
  298. {
  299. return _hist_entry__dso_snprintf(self->branch_info->to.map,
  300. bf, size, width);
  301. }
  302. static int64_t
  303. sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
  304. {
  305. struct addr_map_symbol *from_l = &left->branch_info->from;
  306. struct addr_map_symbol *from_r = &right->branch_info->from;
  307. if (!from_l->sym && !from_r->sym)
  308. return right->level - left->level;
  309. return _sort__sym_cmp(from_l->sym, from_r->sym, from_l->addr,
  310. from_r->addr);
  311. }
  312. static int64_t
  313. sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
  314. {
  315. struct addr_map_symbol *to_l = &left->branch_info->to;
  316. struct addr_map_symbol *to_r = &right->branch_info->to;
  317. if (!to_l->sym && !to_r->sym)
  318. return right->level - left->level;
  319. return _sort__sym_cmp(to_l->sym, to_r->sym, to_l->addr, to_r->addr);
  320. }
  321. static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf,
  322. size_t size, unsigned int width)
  323. {
  324. struct addr_map_symbol *from = &self->branch_info->from;
  325. return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
  326. self->level, bf, size, width);
  327. }
  328. static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
  329. size_t size, unsigned int width)
  330. {
  331. struct addr_map_symbol *to = &self->branch_info->to;
  332. return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
  333. self->level, bf, size, width);
  334. }
  335. struct sort_entry sort_dso_from = {
  336. .se_header = "Source Shared Object",
  337. .se_cmp = sort__dso_from_cmp,
  338. .se_snprintf = hist_entry__dso_from_snprintf,
  339. .se_width_idx = HISTC_DSO_FROM,
  340. };
  341. struct sort_entry sort_dso_to = {
  342. .se_header = "Target Shared Object",
  343. .se_cmp = sort__dso_to_cmp,
  344. .se_snprintf = hist_entry__dso_to_snprintf,
  345. .se_width_idx = HISTC_DSO_TO,
  346. };
  347. struct sort_entry sort_sym_from = {
  348. .se_header = "Source Symbol",
  349. .se_cmp = sort__sym_from_cmp,
  350. .se_snprintf = hist_entry__sym_from_snprintf,
  351. .se_width_idx = HISTC_SYMBOL_FROM,
  352. };
  353. struct sort_entry sort_sym_to = {
  354. .se_header = "Target Symbol",
  355. .se_cmp = sort__sym_to_cmp,
  356. .se_snprintf = hist_entry__sym_to_snprintf,
  357. .se_width_idx = HISTC_SYMBOL_TO,
  358. };
  359. static int64_t
  360. sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
  361. {
  362. const unsigned char mp = left->branch_info->flags.mispred !=
  363. right->branch_info->flags.mispred;
  364. const unsigned char p = left->branch_info->flags.predicted !=
  365. right->branch_info->flags.predicted;
  366. return mp || p;
  367. }
  368. static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf,
  369. size_t size, unsigned int width){
  370. static const char *out = "N/A";
  371. if (self->branch_info->flags.predicted)
  372. out = "N";
  373. else if (self->branch_info->flags.mispred)
  374. out = "Y";
  375. return repsep_snprintf(bf, size, "%-*s", width, out);
  376. }
  377. struct sort_entry sort_mispredict = {
  378. .se_header = "Branch Mispredicted",
  379. .se_cmp = sort__mispredict_cmp,
  380. .se_snprintf = hist_entry__mispredict_snprintf,
  381. .se_width_idx = HISTC_MISPREDICT,
  382. };
  383. struct sort_dimension {
  384. const char *name;
  385. struct sort_entry *entry;
  386. int taken;
  387. };
  388. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  389. static struct sort_dimension common_sort_dimensions[] = {
  390. DIM(SORT_PID, "pid", sort_thread),
  391. DIM(SORT_COMM, "comm", sort_comm),
  392. DIM(SORT_DSO, "dso", sort_dso),
  393. DIM(SORT_SYM, "symbol", sort_sym),
  394. DIM(SORT_PARENT, "parent", sort_parent),
  395. DIM(SORT_CPU, "cpu", sort_cpu),
  396. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  397. };
  398. #undef DIM
  399. #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
  400. static struct sort_dimension bstack_sort_dimensions[] = {
  401. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  402. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  403. DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
  404. DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
  405. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  406. };
  407. #undef DIM
  408. int sort_dimension__add(const char *tok)
  409. {
  410. unsigned int i;
  411. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  412. struct sort_dimension *sd = &common_sort_dimensions[i];
  413. if (strncasecmp(tok, sd->name, strlen(tok)))
  414. continue;
  415. if (sd->entry == &sort_parent) {
  416. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  417. if (ret) {
  418. char err[BUFSIZ];
  419. regerror(ret, &parent_regex, err, sizeof(err));
  420. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  421. return -EINVAL;
  422. }
  423. sort__has_parent = 1;
  424. } else if (sd->entry == &sort_sym) {
  425. sort__has_sym = 1;
  426. }
  427. if (sd->taken)
  428. return 0;
  429. if (sd->entry->se_collapse)
  430. sort__need_collapse = 1;
  431. if (list_empty(&hist_entry__sort_list))
  432. sort__first_dimension = i;
  433. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  434. sd->taken = 1;
  435. return 0;
  436. }
  437. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  438. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  439. if (strncasecmp(tok, sd->name, strlen(tok)))
  440. continue;
  441. if (sort__branch_mode != 1)
  442. return -EINVAL;
  443. if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
  444. sort__has_sym = 1;
  445. if (sd->taken)
  446. return 0;
  447. if (sd->entry->se_collapse)
  448. sort__need_collapse = 1;
  449. if (list_empty(&hist_entry__sort_list))
  450. sort__first_dimension = i + __SORT_BRANCH_STACK;
  451. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  452. sd->taken = 1;
  453. return 0;
  454. }
  455. return -ESRCH;
  456. }
  457. void setup_sorting(const char * const usagestr[], const struct option *opts)
  458. {
  459. char *tmp, *tok, *str = strdup(sort_order);
  460. for (tok = strtok_r(str, ", ", &tmp);
  461. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  462. int ret = sort_dimension__add(tok);
  463. if (ret == -EINVAL) {
  464. error("Invalid --sort key: `%s'", tok);
  465. usage_with_options(usagestr, opts);
  466. } else if (ret == -ESRCH) {
  467. error("Unknown --sort key: `%s'", tok);
  468. usage_with_options(usagestr, opts);
  469. }
  470. }
  471. free(str);
  472. }
  473. void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
  474. const char *list_name, FILE *fp)
  475. {
  476. if (list && strlist__nr_entries(list) == 1) {
  477. if (fp != NULL)
  478. fprintf(fp, "# %s: %s\n", list_name,
  479. strlist__entry(list, 0)->s);
  480. self->elide = true;
  481. }
  482. }