sort.c 8.4 KB

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