sort.c 7.7 KB

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