sort.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "sort.h"
  2. regex_t parent_regex;
  3. char default_parent_pattern[] = "^sys_|^do_page_fault";
  4. char *parent_pattern = default_parent_pattern;
  5. char default_sort_order[] = "comm,dso,symbol";
  6. char *sort_order = default_sort_order;
  7. int sort__need_collapse = 0;
  8. int sort__has_parent = 0;
  9. unsigned int dsos__col_width;
  10. unsigned int comms__col_width;
  11. unsigned int threads__col_width;
  12. static unsigned int parent_symbol__col_width;
  13. char * field_sep;
  14. LIST_HEAD(hist_entry__sort_list);
  15. struct sort_entry sort_thread = {
  16. .header = "Command: Pid",
  17. .cmp = sort__thread_cmp,
  18. .print = sort__thread_print,
  19. .width = &threads__col_width,
  20. };
  21. struct sort_entry sort_comm = {
  22. .header = "Command",
  23. .cmp = sort__comm_cmp,
  24. .collapse = sort__comm_collapse,
  25. .print = sort__comm_print,
  26. .width = &comms__col_width,
  27. };
  28. struct sort_entry sort_dso = {
  29. .header = "Shared Object",
  30. .cmp = sort__dso_cmp,
  31. .print = sort__dso_print,
  32. .width = &dsos__col_width,
  33. };
  34. struct sort_entry sort_sym = {
  35. .header = "Symbol",
  36. .cmp = sort__sym_cmp,
  37. .print = sort__sym_print,
  38. };
  39. struct sort_entry sort_parent = {
  40. .header = "Parent symbol",
  41. .cmp = sort__parent_cmp,
  42. .print = sort__parent_print,
  43. .width = &parent_symbol__col_width,
  44. };
  45. struct sort_dimension {
  46. const char *name;
  47. struct sort_entry *entry;
  48. int taken;
  49. };
  50. static struct sort_dimension sort_dimensions[] = {
  51. { .name = "pid", .entry = &sort_thread, },
  52. { .name = "comm", .entry = &sort_comm, },
  53. { .name = "dso", .entry = &sort_dso, },
  54. { .name = "symbol", .entry = &sort_sym, },
  55. { .name = "parent", .entry = &sort_parent, },
  56. };
  57. int64_t cmp_null(void *l, void *r)
  58. {
  59. if (!l && !r)
  60. return 0;
  61. else if (!l)
  62. return -1;
  63. else
  64. return 1;
  65. }
  66. /* --sort pid */
  67. int64_t
  68. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  69. {
  70. return right->thread->pid - left->thread->pid;
  71. }
  72. int repsep_fprintf(FILE *fp, const char *fmt, ...)
  73. {
  74. int n;
  75. va_list ap;
  76. va_start(ap, fmt);
  77. if (!field_sep)
  78. n = vfprintf(fp, fmt, ap);
  79. else {
  80. char *bf = NULL;
  81. n = vasprintf(&bf, fmt, ap);
  82. if (n > 0) {
  83. char *sep = bf;
  84. while (1) {
  85. sep = strchr(sep, *field_sep);
  86. if (sep == NULL)
  87. break;
  88. *sep = '.';
  89. }
  90. }
  91. fputs(bf, fp);
  92. free(bf);
  93. }
  94. va_end(ap);
  95. return n;
  96. }
  97. size_t
  98. sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
  99. {
  100. return repsep_fprintf(fp, "%*s:%5d", width - 6,
  101. self->thread->comm ?: "", self->thread->pid);
  102. }
  103. size_t
  104. sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
  105. {
  106. return repsep_fprintf(fp, "%*s", width, self->thread->comm);
  107. }
  108. /* --sort dso */
  109. int64_t
  110. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  111. {
  112. struct dso *dso_l = left->map ? left->map->dso : NULL;
  113. struct dso *dso_r = right->map ? right->map->dso : NULL;
  114. const char *dso_name_l, *dso_name_r;
  115. if (!dso_l || !dso_r)
  116. return cmp_null(dso_l, dso_r);
  117. if (verbose) {
  118. dso_name_l = dso_l->long_name;
  119. dso_name_r = dso_r->long_name;
  120. } else {
  121. dso_name_l = dso_l->short_name;
  122. dso_name_r = dso_r->short_name;
  123. }
  124. return strcmp(dso_name_l, dso_name_r);
  125. }
  126. size_t
  127. sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
  128. {
  129. if (self->map && self->map->dso) {
  130. const char *dso_name = !verbose ? self->map->dso->short_name :
  131. self->map->dso->long_name;
  132. return repsep_fprintf(fp, "%-*s", width, dso_name);
  133. }
  134. return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
  135. }
  136. /* --sort symbol */
  137. int64_t
  138. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  139. {
  140. u64 ip_l, ip_r;
  141. if (left->sym == right->sym)
  142. return 0;
  143. ip_l = left->sym ? left->sym->start : left->ip;
  144. ip_r = right->sym ? right->sym->start : right->ip;
  145. return (int64_t)(ip_r - ip_l);
  146. }
  147. size_t
  148. sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
  149. {
  150. size_t ret = 0;
  151. if (verbose) {
  152. char o = self->map ? dso__symtab_origin(self->map->dso) : '!';
  153. ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip, o);
  154. }
  155. ret += repsep_fprintf(fp, "[%c] ", self->level);
  156. if (self->sym)
  157. ret += repsep_fprintf(fp, "%s", self->sym->name);
  158. else
  159. ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
  160. return ret;
  161. }
  162. /* --sort comm */
  163. int64_t
  164. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  165. {
  166. return right->thread->pid - left->thread->pid;
  167. }
  168. int64_t
  169. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  170. {
  171. char *comm_l = left->thread->comm;
  172. char *comm_r = right->thread->comm;
  173. if (!comm_l || !comm_r)
  174. return cmp_null(comm_l, comm_r);
  175. return strcmp(comm_l, comm_r);
  176. }
  177. /* --sort parent */
  178. int64_t
  179. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  180. {
  181. struct symbol *sym_l = left->parent;
  182. struct symbol *sym_r = right->parent;
  183. if (!sym_l || !sym_r)
  184. return cmp_null(sym_l, sym_r);
  185. return strcmp(sym_l->name, sym_r->name);
  186. }
  187. size_t
  188. sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
  189. {
  190. return repsep_fprintf(fp, "%-*s", width,
  191. self->parent ? self->parent->name : "[other]");
  192. }
  193. int sort_dimension__add(const char *tok)
  194. {
  195. unsigned int i;
  196. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  197. struct sort_dimension *sd = &sort_dimensions[i];
  198. if (sd->taken)
  199. continue;
  200. if (strncasecmp(tok, sd->name, strlen(tok)))
  201. continue;
  202. if (sd->entry->collapse)
  203. sort__need_collapse = 1;
  204. if (sd->entry == &sort_parent) {
  205. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  206. if (ret) {
  207. char err[BUFSIZ];
  208. regerror(ret, &parent_regex, err, sizeof(err));
  209. fprintf(stderr, "Invalid regex: %s\n%s",
  210. parent_pattern, err);
  211. exit(-1);
  212. }
  213. sort__has_parent = 1;
  214. }
  215. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  216. sd->taken = 1;
  217. return 0;
  218. }
  219. return -ESRCH;
  220. }