sort.c 8.3 KB

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