sort.c 8.3 KB

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