sort.c 14 KB

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