sort.c 7.7 KB

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