sort.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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__branch_mode = -1; /* -1 = means not set */
  11. enum sort_type sort__first_dimension;
  12. LIST_HEAD(hist_entry__sort_list);
  13. static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
  14. {
  15. int n;
  16. va_list ap;
  17. va_start(ap, fmt);
  18. n = vsnprintf(bf, size, fmt, ap);
  19. if (symbol_conf.field_sep && n > 0) {
  20. char *sep = bf;
  21. while (1) {
  22. sep = strchr(sep, *symbol_conf.field_sep);
  23. if (sep == NULL)
  24. break;
  25. *sep = '.';
  26. }
  27. }
  28. va_end(ap);
  29. if (n >= (int)size)
  30. return size - 1;
  31. return n;
  32. }
  33. static int64_t cmp_null(void *l, void *r)
  34. {
  35. if (!l && !r)
  36. return 0;
  37. else if (!l)
  38. return -1;
  39. else
  40. return 1;
  41. }
  42. /* --sort pid */
  43. static int64_t
  44. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  45. {
  46. return right->thread->pid - left->thread->pid;
  47. }
  48. static int hist_entry__thread_snprintf(struct hist_entry *self, char *bf,
  49. size_t size, unsigned int width)
  50. {
  51. return repsep_snprintf(bf, size, "%*s:%5d", width,
  52. self->thread->comm ?: "", self->thread->pid);
  53. }
  54. struct sort_entry sort_thread = {
  55. .se_header = "Command: Pid",
  56. .se_cmp = sort__thread_cmp,
  57. .se_snprintf = hist_entry__thread_snprintf,
  58. .se_width_idx = HISTC_THREAD,
  59. };
  60. /* --sort comm */
  61. static int64_t
  62. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  63. {
  64. return right->thread->pid - left->thread->pid;
  65. }
  66. static int64_t
  67. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  68. {
  69. char *comm_l = left->thread->comm;
  70. char *comm_r = right->thread->comm;
  71. if (!comm_l || !comm_r)
  72. return cmp_null(comm_l, comm_r);
  73. return strcmp(comm_l, comm_r);
  74. }
  75. static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf,
  76. size_t size, unsigned int width)
  77. {
  78. return repsep_snprintf(bf, size, "%*s", width, self->thread->comm);
  79. }
  80. static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
  81. {
  82. struct dso *dso_l = map_l ? map_l->dso : NULL;
  83. struct dso *dso_r = map_r ? map_r->dso : NULL;
  84. const char *dso_name_l, *dso_name_r;
  85. if (!dso_l || !dso_r)
  86. return cmp_null(dso_l, dso_r);
  87. if (verbose) {
  88. dso_name_l = dso_l->long_name;
  89. dso_name_r = dso_r->long_name;
  90. } else {
  91. dso_name_l = dso_l->short_name;
  92. dso_name_r = dso_r->short_name;
  93. }
  94. return strcmp(dso_name_l, dso_name_r);
  95. }
  96. struct sort_entry sort_comm = {
  97. .se_header = "Command",
  98. .se_cmp = sort__comm_cmp,
  99. .se_collapse = sort__comm_collapse,
  100. .se_snprintf = hist_entry__comm_snprintf,
  101. .se_width_idx = HISTC_COMM,
  102. };
  103. /* --sort dso */
  104. static int64_t
  105. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  106. {
  107. return _sort__dso_cmp(left->ms.map, right->ms.map);
  108. }
  109. static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r,
  110. u64 ip_l, u64 ip_r)
  111. {
  112. if (!sym_l || !sym_r)
  113. return cmp_null(sym_l, sym_r);
  114. if (sym_l == sym_r)
  115. return 0;
  116. if (sym_l)
  117. ip_l = sym_l->start;
  118. if (sym_r)
  119. ip_r = sym_r->start;
  120. return (int64_t)(ip_r - ip_l);
  121. }
  122. static int _hist_entry__dso_snprintf(struct map *map, char *bf,
  123. size_t size, unsigned int width)
  124. {
  125. if (map && map->dso) {
  126. const char *dso_name = !verbose ? map->dso->short_name :
  127. map->dso->long_name;
  128. return repsep_snprintf(bf, size, "%-*s", width, dso_name);
  129. }
  130. return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
  131. }
  132. static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf,
  133. size_t size, unsigned int width)
  134. {
  135. return _hist_entry__dso_snprintf(self->ms.map, bf, size, width);
  136. }
  137. static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
  138. u64 ip, char level, char *bf, size_t size,
  139. unsigned int width __used)
  140. {
  141. size_t ret = 0;
  142. if (verbose) {
  143. char o = map ? dso__symtab_origin(map->dso) : '!';
  144. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  145. BITS_PER_LONG / 4, ip, o);
  146. }
  147. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
  148. if (sym)
  149. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  150. width - ret,
  151. sym->name);
  152. else {
  153. size_t len = BITS_PER_LONG / 4;
  154. ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
  155. len, ip);
  156. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  157. width - ret, "");
  158. }
  159. return ret;
  160. }
  161. struct sort_entry sort_dso = {
  162. .se_header = "Shared Object",
  163. .se_cmp = sort__dso_cmp,
  164. .se_snprintf = hist_entry__dso_snprintf,
  165. .se_width_idx = HISTC_DSO,
  166. };
  167. static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
  168. size_t size, unsigned int width __used)
  169. {
  170. return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
  171. self->level, bf, size, width);
  172. }
  173. /* --sort symbol */
  174. static int64_t
  175. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  176. {
  177. u64 ip_l, ip_r;
  178. if (!left->ms.sym && !right->ms.sym)
  179. return right->level - left->level;
  180. if (!left->ms.sym || !right->ms.sym)
  181. return cmp_null(left->ms.sym, right->ms.sym);
  182. if (left->ms.sym == right->ms.sym)
  183. return 0;
  184. ip_l = left->ms.sym->start;
  185. ip_r = right->ms.sym->start;
  186. return _sort__sym_cmp(left->ms.sym, right->ms.sym, ip_l, ip_r);
  187. }
  188. struct sort_entry sort_sym = {
  189. .se_header = "Symbol",
  190. .se_cmp = sort__sym_cmp,
  191. .se_snprintf = hist_entry__sym_snprintf,
  192. .se_width_idx = HISTC_SYMBOL,
  193. };
  194. /* --sort srcline */
  195. static int64_t
  196. sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
  197. {
  198. return (int64_t)(right->ip - left->ip);
  199. }
  200. static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
  201. size_t size, unsigned int width __used)
  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. snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
  209. self->ms.map->dso->long_name, self->ip);
  210. fp = popen(cmd, "r");
  211. if (!fp)
  212. goto out_ip;
  213. if (getline(&path, &line_len, fp) < 0 || !line_len)
  214. goto out_ip;
  215. fclose(fp);
  216. self->srcline = strdup(path);
  217. if (self->srcline == NULL)
  218. goto out_ip;
  219. nl = strchr(self->srcline, '\n');
  220. if (nl != NULL)
  221. *nl = '\0';
  222. path = self->srcline;
  223. out_path:
  224. return repsep_snprintf(bf, size, "%s", path);
  225. out_ip:
  226. return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
  227. }
  228. struct sort_entry sort_srcline = {
  229. .se_header = "Source:Line",
  230. .se_cmp = sort__srcline_cmp,
  231. .se_snprintf = hist_entry__srcline_snprintf,
  232. .se_width_idx = HISTC_SRCLINE,
  233. };
  234. /* --sort parent */
  235. static int64_t
  236. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  237. {
  238. struct symbol *sym_l = left->parent;
  239. struct symbol *sym_r = right->parent;
  240. if (!sym_l || !sym_r)
  241. return cmp_null(sym_l, sym_r);
  242. return strcmp(sym_l->name, sym_r->name);
  243. }
  244. static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
  245. size_t size, unsigned int width)
  246. {
  247. return repsep_snprintf(bf, size, "%-*s", width,
  248. self->parent ? self->parent->name : "[other]");
  249. }
  250. struct sort_entry sort_parent = {
  251. .se_header = "Parent symbol",
  252. .se_cmp = sort__parent_cmp,
  253. .se_snprintf = hist_entry__parent_snprintf,
  254. .se_width_idx = HISTC_PARENT,
  255. };
  256. /* --sort cpu */
  257. static int64_t
  258. sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
  259. {
  260. return right->cpu - left->cpu;
  261. }
  262. static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
  263. size_t size, unsigned int width)
  264. {
  265. return repsep_snprintf(bf, size, "%-*d", width, self->cpu);
  266. }
  267. struct sort_entry sort_cpu = {
  268. .se_header = "CPU",
  269. .se_cmp = sort__cpu_cmp,
  270. .se_snprintf = hist_entry__cpu_snprintf,
  271. .se_width_idx = HISTC_CPU,
  272. };
  273. static int64_t
  274. sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
  275. {
  276. return _sort__dso_cmp(left->branch_info->from.map,
  277. right->branch_info->from.map);
  278. }
  279. static int hist_entry__dso_from_snprintf(struct hist_entry *self, char *bf,
  280. size_t size, unsigned int width)
  281. {
  282. return _hist_entry__dso_snprintf(self->branch_info->from.map,
  283. bf, size, width);
  284. }
  285. struct sort_entry sort_dso_from = {
  286. .se_header = "Source Shared Object",
  287. .se_cmp = sort__dso_from_cmp,
  288. .se_snprintf = hist_entry__dso_from_snprintf,
  289. .se_width_idx = HISTC_DSO_FROM,
  290. };
  291. static int64_t
  292. sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
  293. {
  294. return _sort__dso_cmp(left->branch_info->to.map,
  295. right->branch_info->to.map);
  296. }
  297. static int hist_entry__dso_to_snprintf(struct hist_entry *self, char *bf,
  298. size_t size, unsigned int width)
  299. {
  300. return _hist_entry__dso_snprintf(self->branch_info->to.map,
  301. bf, size, width);
  302. }
  303. static int64_t
  304. sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
  305. {
  306. struct addr_map_symbol *from_l = &left->branch_info->from;
  307. struct addr_map_symbol *from_r = &right->branch_info->from;
  308. if (!from_l->sym && !from_r->sym)
  309. return right->level - left->level;
  310. return _sort__sym_cmp(from_l->sym, from_r->sym, from_l->addr,
  311. from_r->addr);
  312. }
  313. static int64_t
  314. sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
  315. {
  316. struct addr_map_symbol *to_l = &left->branch_info->to;
  317. struct addr_map_symbol *to_r = &right->branch_info->to;
  318. if (!to_l->sym && !to_r->sym)
  319. return right->level - left->level;
  320. return _sort__sym_cmp(to_l->sym, to_r->sym, to_l->addr, to_r->addr);
  321. }
  322. static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf,
  323. size_t size, unsigned int width __used)
  324. {
  325. struct addr_map_symbol *from = &self->branch_info->from;
  326. return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
  327. self->level, bf, size, width);
  328. }
  329. static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
  330. size_t size, unsigned int width __used)
  331. {
  332. struct addr_map_symbol *to = &self->branch_info->to;
  333. return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
  334. self->level, bf, size, width);
  335. }
  336. struct sort_entry sort_dso_to = {
  337. .se_header = "Target Shared Object",
  338. .se_cmp = sort__dso_to_cmp,
  339. .se_snprintf = hist_entry__dso_to_snprintf,
  340. .se_width_idx = HISTC_DSO_TO,
  341. };
  342. struct sort_entry sort_sym_from = {
  343. .se_header = "Source Symbol",
  344. .se_cmp = sort__sym_from_cmp,
  345. .se_snprintf = hist_entry__sym_from_snprintf,
  346. .se_width_idx = HISTC_SYMBOL_FROM,
  347. };
  348. struct sort_entry sort_sym_to = {
  349. .se_header = "Target Symbol",
  350. .se_cmp = sort__sym_to_cmp,
  351. .se_snprintf = hist_entry__sym_to_snprintf,
  352. .se_width_idx = HISTC_SYMBOL_TO,
  353. };
  354. static int64_t
  355. sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
  356. {
  357. const unsigned char mp = left->branch_info->flags.mispred !=
  358. right->branch_info->flags.mispred;
  359. const unsigned char p = left->branch_info->flags.predicted !=
  360. right->branch_info->flags.predicted;
  361. return mp || p;
  362. }
  363. static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf,
  364. size_t size, unsigned int width){
  365. static const char *out = "N/A";
  366. if (self->branch_info->flags.predicted)
  367. out = "N";
  368. else if (self->branch_info->flags.mispred)
  369. out = "Y";
  370. return repsep_snprintf(bf, size, "%-*s", width, out);
  371. }
  372. struct sort_entry sort_mispredict = {
  373. .se_header = "Branch Mispredicted",
  374. .se_cmp = sort__mispredict_cmp,
  375. .se_snprintf = hist_entry__mispredict_snprintf,
  376. .se_width_idx = HISTC_MISPREDICT,
  377. };
  378. struct sort_dimension {
  379. const char *name;
  380. struct sort_entry *entry;
  381. int taken;
  382. };
  383. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  384. static struct sort_dimension sort_dimensions[] = {
  385. DIM(SORT_PID, "pid", sort_thread),
  386. DIM(SORT_COMM, "comm", sort_comm),
  387. DIM(SORT_DSO, "dso", sort_dso),
  388. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  389. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  390. DIM(SORT_SYM, "symbol", sort_sym),
  391. DIM(SORT_SYM_TO, "symbol_from", sort_sym_from),
  392. DIM(SORT_SYM_FROM, "symbol_to", sort_sym_to),
  393. DIM(SORT_PARENT, "parent", sort_parent),
  394. DIM(SORT_CPU, "cpu", sort_cpu),
  395. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  396. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  397. };
  398. int sort_dimension__add(const char *tok)
  399. {
  400. unsigned int i;
  401. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  402. struct sort_dimension *sd = &sort_dimensions[i];
  403. if (strncasecmp(tok, sd->name, strlen(tok)))
  404. continue;
  405. if (sd->entry == &sort_parent) {
  406. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  407. if (ret) {
  408. char err[BUFSIZ];
  409. regerror(ret, &parent_regex, err, sizeof(err));
  410. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  411. return -EINVAL;
  412. }
  413. sort__has_parent = 1;
  414. }
  415. if (sd->taken)
  416. return 0;
  417. if (sd->entry->se_collapse)
  418. sort__need_collapse = 1;
  419. if (list_empty(&hist_entry__sort_list)) {
  420. if (!strcmp(sd->name, "pid"))
  421. sort__first_dimension = SORT_PID;
  422. else if (!strcmp(sd->name, "comm"))
  423. sort__first_dimension = SORT_COMM;
  424. else if (!strcmp(sd->name, "dso"))
  425. sort__first_dimension = SORT_DSO;
  426. else if (!strcmp(sd->name, "symbol"))
  427. sort__first_dimension = SORT_SYM;
  428. else if (!strcmp(sd->name, "parent"))
  429. sort__first_dimension = SORT_PARENT;
  430. else if (!strcmp(sd->name, "cpu"))
  431. sort__first_dimension = SORT_CPU;
  432. else if (!strcmp(sd->name, "symbol_from"))
  433. sort__first_dimension = SORT_SYM_FROM;
  434. else if (!strcmp(sd->name, "symbol_to"))
  435. sort__first_dimension = SORT_SYM_TO;
  436. else if (!strcmp(sd->name, "dso_from"))
  437. sort__first_dimension = SORT_DSO_FROM;
  438. else if (!strcmp(sd->name, "dso_to"))
  439. sort__first_dimension = SORT_DSO_TO;
  440. else if (!strcmp(sd->name, "mispredict"))
  441. sort__first_dimension = SORT_MISPREDICT;
  442. }
  443. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  444. sd->taken = 1;
  445. return 0;
  446. }
  447. return -ESRCH;
  448. }
  449. void setup_sorting(const char * const usagestr[], const struct option *opts)
  450. {
  451. char *tmp, *tok, *str = strdup(sort_order);
  452. for (tok = strtok_r(str, ", ", &tmp);
  453. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  454. if (sort_dimension__add(tok) < 0) {
  455. error("Unknown --sort key: `%s'", tok);
  456. usage_with_options(usagestr, opts);
  457. }
  458. }
  459. free(str);
  460. }
  461. void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
  462. const char *list_name, FILE *fp)
  463. {
  464. if (list && strlist__nr_entries(list) == 1) {
  465. if (fp != NULL)
  466. fprintf(fp, "# %s: %s\n", list_name,
  467. strlist__entry(list, 0)->s);
  468. self->elide = true;
  469. }
  470. }