sort.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. 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. struct sort_entry sort_comm = {
  81. .se_header = "Command",
  82. .se_cmp = sort__comm_cmp,
  83. .se_collapse = sort__comm_collapse,
  84. .se_snprintf = hist_entry__comm_snprintf,
  85. .se_width_idx = HISTC_COMM,
  86. };
  87. /* --sort dso */
  88. static int64_t
  89. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  90. {
  91. struct dso *dso_l = left->ms.map ? left->ms.map->dso : NULL;
  92. struct dso *dso_r = right->ms.map ? right->ms.map->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 int hist_entry__dso_snprintf(struct hist_entry *self, char *bf,
  106. size_t size, unsigned int width)
  107. {
  108. if (self->ms.map && self->ms.map->dso) {
  109. const char *dso_name = !verbose ? self->ms.map->dso->short_name :
  110. self->ms.map->dso->long_name;
  111. return repsep_snprintf(bf, size, "%-*s", width, dso_name);
  112. }
  113. return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
  114. }
  115. struct sort_entry sort_dso = {
  116. .se_header = "Shared Object",
  117. .se_cmp = sort__dso_cmp,
  118. .se_snprintf = hist_entry__dso_snprintf,
  119. .se_width_idx = HISTC_DSO,
  120. };
  121. /* --sort symbol */
  122. static int64_t
  123. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  124. {
  125. u64 ip_l, ip_r;
  126. if (!left->ms.sym && !right->ms.sym)
  127. return right->level - left->level;
  128. if (!left->ms.sym || !right->ms.sym)
  129. return cmp_null(left->ms.sym, right->ms.sym);
  130. if (left->ms.sym == right->ms.sym)
  131. return 0;
  132. ip_l = left->ms.sym->start;
  133. ip_r = right->ms.sym->start;
  134. return (int64_t)(ip_r - ip_l);
  135. }
  136. static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
  137. size_t size, unsigned int width __used)
  138. {
  139. size_t ret = 0;
  140. if (verbose) {
  141. char o = self->ms.map ? dso__symtab_origin(self->ms.map->dso) : '!';
  142. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  143. BITS_PER_LONG / 4, self->ip, o);
  144. }
  145. if (!sort_dso.elide)
  146. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", self->level);
  147. if (self->ms.sym)
  148. ret += repsep_snprintf(bf + ret, size - ret, "%s",
  149. self->ms.sym->name);
  150. else
  151. ret += repsep_snprintf(bf + ret, size - ret, "%-#*llx",
  152. BITS_PER_LONG / 4, self->ip);
  153. return ret;
  154. }
  155. struct sort_entry sort_sym = {
  156. .se_header = "Symbol",
  157. .se_cmp = sort__sym_cmp,
  158. .se_snprintf = hist_entry__sym_snprintf,
  159. .se_width_idx = HISTC_SYMBOL,
  160. };
  161. /* --sort parent */
  162. static int64_t
  163. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  164. {
  165. struct symbol *sym_l = left->parent;
  166. struct symbol *sym_r = right->parent;
  167. if (!sym_l || !sym_r)
  168. return cmp_null(sym_l, sym_r);
  169. return strcmp(sym_l->name, sym_r->name);
  170. }
  171. static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
  172. size_t size, unsigned int width)
  173. {
  174. return repsep_snprintf(bf, size, "%-*s", width,
  175. self->parent ? self->parent->name : "[other]");
  176. }
  177. struct sort_entry sort_parent = {
  178. .se_header = "Parent symbol",
  179. .se_cmp = sort__parent_cmp,
  180. .se_snprintf = hist_entry__parent_snprintf,
  181. .se_width_idx = HISTC_PARENT,
  182. };
  183. /* --sort cpu */
  184. static int64_t
  185. sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
  186. {
  187. return right->cpu - left->cpu;
  188. }
  189. static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
  190. size_t size, unsigned int width)
  191. {
  192. return repsep_snprintf(bf, size, "%-*d", width, self->cpu);
  193. }
  194. struct sort_entry sort_cpu = {
  195. .se_header = "CPU",
  196. .se_cmp = sort__cpu_cmp,
  197. .se_snprintf = hist_entry__cpu_snprintf,
  198. .se_width_idx = HISTC_CPU,
  199. };
  200. struct sort_dimension {
  201. const char *name;
  202. struct sort_entry *entry;
  203. int taken;
  204. };
  205. static struct sort_dimension sort_dimensions[] = {
  206. { .name = "pid", .entry = &sort_thread, },
  207. { .name = "comm", .entry = &sort_comm, },
  208. { .name = "dso", .entry = &sort_dso, },
  209. { .name = "symbol", .entry = &sort_sym, },
  210. { .name = "parent", .entry = &sort_parent, },
  211. { .name = "cpu", .entry = &sort_cpu, },
  212. };
  213. int sort_dimension__add(const char *tok)
  214. {
  215. unsigned int i;
  216. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  217. struct sort_dimension *sd = &sort_dimensions[i];
  218. if (strncasecmp(tok, sd->name, strlen(tok)))
  219. continue;
  220. if (sd->entry == &sort_parent) {
  221. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  222. if (ret) {
  223. char err[BUFSIZ];
  224. regerror(ret, &parent_regex, err, sizeof(err));
  225. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  226. return -EINVAL;
  227. }
  228. sort__has_parent = 1;
  229. }
  230. if (sd->taken)
  231. return 0;
  232. if (sd->entry->se_collapse)
  233. sort__need_collapse = 1;
  234. if (list_empty(&hist_entry__sort_list)) {
  235. if (!strcmp(sd->name, "pid"))
  236. sort__first_dimension = SORT_PID;
  237. else if (!strcmp(sd->name, "comm"))
  238. sort__first_dimension = SORT_COMM;
  239. else if (!strcmp(sd->name, "dso"))
  240. sort__first_dimension = SORT_DSO;
  241. else if (!strcmp(sd->name, "symbol"))
  242. sort__first_dimension = SORT_SYM;
  243. else if (!strcmp(sd->name, "parent"))
  244. sort__first_dimension = SORT_PARENT;
  245. else if (!strcmp(sd->name, "cpu"))
  246. sort__first_dimension = SORT_CPU;
  247. }
  248. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  249. sd->taken = 1;
  250. return 0;
  251. }
  252. return -ESRCH;
  253. }
  254. void setup_sorting(const char * const usagestr[], const struct option *opts)
  255. {
  256. char *tmp, *tok, *str = strdup(sort_order);
  257. for (tok = strtok_r(str, ", ", &tmp);
  258. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  259. if (sort_dimension__add(tok) < 0) {
  260. error("Unknown --sort key: `%s'", tok);
  261. usage_with_options(usagestr, opts);
  262. }
  263. }
  264. free(str);
  265. }
  266. void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
  267. const char *list_name, FILE *fp)
  268. {
  269. if (list && strlist__nr_entries(list) == 1) {
  270. if (fp != NULL)
  271. fprintf(fp, "# %s: %s\n", list_name,
  272. strlist__entry(list, 0)->s);
  273. self->elide = true;
  274. }
  275. }