sort.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. enum sort_type sort__first_dimension;
  11. char * field_sep;
  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 (field_sep && n > 0) {
  20. char *sep = bf;
  21. while (1) {
  22. sep = strchr(sep, *field_sep);
  23. if (sep == NULL)
  24. break;
  25. *sep = '.';
  26. }
  27. }
  28. va_end(ap);
  29. return n;
  30. }
  31. static int64_t cmp_null(void *l, void *r)
  32. {
  33. if (!l && !r)
  34. return 0;
  35. else if (!l)
  36. return -1;
  37. else
  38. return 1;
  39. }
  40. /* --sort pid */
  41. static int64_t
  42. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  43. {
  44. return right->thread->pid - left->thread->pid;
  45. }
  46. static int hist_entry__thread_snprintf(struct hist_entry *self, char *bf,
  47. size_t size, unsigned int width)
  48. {
  49. return repsep_snprintf(bf, size, "%*s:%5d", width,
  50. self->thread->comm ?: "", self->thread->pid);
  51. }
  52. struct sort_entry sort_thread = {
  53. .se_header = "Command: Pid",
  54. .se_cmp = sort__thread_cmp,
  55. .se_snprintf = hist_entry__thread_snprintf,
  56. .se_width_idx = HISTC_THREAD,
  57. };
  58. /* --sort comm */
  59. static int64_t
  60. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  61. {
  62. return right->thread->pid - left->thread->pid;
  63. }
  64. static int64_t
  65. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  66. {
  67. char *comm_l = left->thread->comm;
  68. char *comm_r = right->thread->comm;
  69. if (!comm_l || !comm_r)
  70. return cmp_null(comm_l, comm_r);
  71. return strcmp(comm_l, comm_r);
  72. }
  73. static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf,
  74. size_t size, unsigned int width)
  75. {
  76. return repsep_snprintf(bf, size, "%*s", width, self->thread->comm);
  77. }
  78. struct sort_entry sort_comm = {
  79. .se_header = "Command",
  80. .se_cmp = sort__comm_cmp,
  81. .se_collapse = sort__comm_collapse,
  82. .se_snprintf = hist_entry__comm_snprintf,
  83. .se_width_idx = HISTC_COMM,
  84. };
  85. /* --sort dso */
  86. static int64_t
  87. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  88. {
  89. struct dso *dso_l = left->ms.map ? left->ms.map->dso : NULL;
  90. struct dso *dso_r = right->ms.map ? right->ms.map->dso : NULL;
  91. const char *dso_name_l, *dso_name_r;
  92. if (!dso_l || !dso_r)
  93. return cmp_null(dso_l, dso_r);
  94. if (verbose) {
  95. dso_name_l = dso_l->long_name;
  96. dso_name_r = dso_r->long_name;
  97. } else {
  98. dso_name_l = dso_l->short_name;
  99. dso_name_r = dso_r->short_name;
  100. }
  101. return strcmp(dso_name_l, dso_name_r);
  102. }
  103. static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf,
  104. size_t size, unsigned int width)
  105. {
  106. if (self->ms.map && self->ms.map->dso) {
  107. const char *dso_name = !verbose ? self->ms.map->dso->short_name :
  108. self->ms.map->dso->long_name;
  109. return repsep_snprintf(bf, size, "%-*s", width, dso_name);
  110. }
  111. return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
  112. }
  113. struct sort_entry sort_dso = {
  114. .se_header = "Shared Object",
  115. .se_cmp = sort__dso_cmp,
  116. .se_snprintf = hist_entry__dso_snprintf,
  117. .se_width_idx = HISTC_DSO,
  118. };
  119. /* --sort symbol */
  120. static int64_t
  121. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  122. {
  123. u64 ip_l, ip_r;
  124. if (!left->ms.sym && !right->ms.sym)
  125. return right->level - left->level;
  126. if (!left->ms.sym || !right->ms.sym)
  127. return cmp_null(left->ms.sym, right->ms.sym);
  128. if (left->ms.sym == right->ms.sym)
  129. return 0;
  130. ip_l = left->ms.sym->start;
  131. ip_r = right->ms.sym->start;
  132. return (int64_t)(ip_r - ip_l);
  133. }
  134. static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
  135. size_t size, unsigned int width __used)
  136. {
  137. size_t ret = 0;
  138. if (verbose) {
  139. char o = self->ms.map ? dso__symtab_origin(self->ms.map->dso) : '!';
  140. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  141. BITS_PER_LONG / 4, self->ip, o);
  142. }
  143. if (!sort_dso.elide)
  144. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", self->level);
  145. if (self->ms.sym)
  146. ret += repsep_snprintf(bf + ret, size - ret, "%s",
  147. self->ms.sym->name);
  148. else
  149. ret += repsep_snprintf(bf + ret, size - ret, "%-#*llx",
  150. BITS_PER_LONG / 4, self->ip);
  151. return ret;
  152. }
  153. struct sort_entry sort_sym = {
  154. .se_header = "Symbol",
  155. .se_cmp = sort__sym_cmp,
  156. .se_snprintf = hist_entry__sym_snprintf,
  157. .se_width_idx = HISTC_SYMBOL,
  158. };
  159. /* --sort parent */
  160. static int64_t
  161. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  162. {
  163. struct symbol *sym_l = left->parent;
  164. struct symbol *sym_r = right->parent;
  165. if (!sym_l || !sym_r)
  166. return cmp_null(sym_l, sym_r);
  167. return strcmp(sym_l->name, sym_r->name);
  168. }
  169. static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
  170. size_t size, unsigned int width)
  171. {
  172. return repsep_snprintf(bf, size, "%-*s", width,
  173. self->parent ? self->parent->name : "[other]");
  174. }
  175. struct sort_entry sort_parent = {
  176. .se_header = "Parent symbol",
  177. .se_cmp = sort__parent_cmp,
  178. .se_snprintf = hist_entry__parent_snprintf,
  179. .se_width_idx = HISTC_PARENT,
  180. };
  181. /* --sort cpu */
  182. static int64_t
  183. sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
  184. {
  185. return right->cpu - left->cpu;
  186. }
  187. static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
  188. size_t size, unsigned int width)
  189. {
  190. return repsep_snprintf(bf, size, "%-*d", width, self->cpu);
  191. }
  192. struct sort_entry sort_cpu = {
  193. .se_header = "CPU",
  194. .se_cmp = sort__cpu_cmp,
  195. .se_snprintf = hist_entry__cpu_snprintf,
  196. .se_width_idx = HISTC_CPU,
  197. };
  198. struct sort_dimension {
  199. const char *name;
  200. struct sort_entry *entry;
  201. int taken;
  202. };
  203. static struct sort_dimension sort_dimensions[] = {
  204. { .name = "pid", .entry = &sort_thread, },
  205. { .name = "comm", .entry = &sort_comm, },
  206. { .name = "dso", .entry = &sort_dso, },
  207. { .name = "symbol", .entry = &sort_sym, },
  208. { .name = "parent", .entry = &sort_parent, },
  209. { .name = "cpu", .entry = &sort_cpu, },
  210. };
  211. int sort_dimension__add(const char *tok)
  212. {
  213. unsigned int i;
  214. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  215. struct sort_dimension *sd = &sort_dimensions[i];
  216. if (strncasecmp(tok, sd->name, strlen(tok)))
  217. continue;
  218. if (sd->entry == &sort_parent) {
  219. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  220. if (ret) {
  221. char err[BUFSIZ];
  222. regerror(ret, &parent_regex, err, sizeof(err));
  223. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  224. return -EINVAL;
  225. }
  226. sort__has_parent = 1;
  227. }
  228. if (sd->taken)
  229. return 0;
  230. if (sd->entry->se_collapse)
  231. sort__need_collapse = 1;
  232. if (list_empty(&hist_entry__sort_list)) {
  233. if (!strcmp(sd->name, "pid"))
  234. sort__first_dimension = SORT_PID;
  235. else if (!strcmp(sd->name, "comm"))
  236. sort__first_dimension = SORT_COMM;
  237. else if (!strcmp(sd->name, "dso"))
  238. sort__first_dimension = SORT_DSO;
  239. else if (!strcmp(sd->name, "symbol"))
  240. sort__first_dimension = SORT_SYM;
  241. else if (!strcmp(sd->name, "parent"))
  242. sort__first_dimension = SORT_PARENT;
  243. else if (!strcmp(sd->name, "cpu"))
  244. sort__first_dimension = SORT_CPU;
  245. }
  246. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  247. sd->taken = 1;
  248. return 0;
  249. }
  250. return -ESRCH;
  251. }
  252. void setup_sorting(const char * const usagestr[], const struct option *opts)
  253. {
  254. char *tmp, *tok, *str = strdup(sort_order);
  255. for (tok = strtok_r(str, ", ", &tmp);
  256. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  257. if (sort_dimension__add(tok) < 0) {
  258. error("Unknown --sort key: `%s'", tok);
  259. usage_with_options(usagestr, opts);
  260. }
  261. }
  262. free(str);
  263. }
  264. void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
  265. const char *list_name, FILE *fp)
  266. {
  267. if (list && strlist__nr_entries(list) == 1) {
  268. if (fp != NULL)
  269. fprintf(fp, "# %s: %s\n", list_name,
  270. strlist__entry(list, 0)->s);
  271. self->elide = true;
  272. }
  273. }