John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 1 | #include "sort.h" |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 2 | #include "hist.h" |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 3 | |
| 4 | regex_t parent_regex; |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 5 | const char default_parent_pattern[] = "^sys_|^do_page_fault"; |
| 6 | const char *parent_pattern = default_parent_pattern; |
| 7 | const char default_sort_order[] = "comm,dso,symbol"; |
| 8 | const char *sort_order = default_sort_order; |
Frederic Weisbecker | af0a6fa | 2009-10-22 23:23:22 +0200 | [diff] [blame] | 9 | int sort__need_collapse = 0; |
| 10 | int sort__has_parent = 0; |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 11 | |
| 12 | enum sort_type sort__first_dimension; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 13 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 14 | char * field_sep; |
| 15 | |
| 16 | LIST_HEAD(hist_entry__sort_list); |
| 17 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 18 | static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 19 | { |
| 20 | int n; |
| 21 | va_list ap; |
| 22 | |
| 23 | va_start(ap, fmt); |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 24 | n = vsnprintf(bf, size, fmt, ap); |
| 25 | if (field_sep && n > 0) { |
| 26 | char *sep = bf; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 27 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 28 | while (1) { |
| 29 | sep = strchr(sep, *field_sep); |
| 30 | if (sep == NULL) |
| 31 | break; |
| 32 | *sep = '.'; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 33 | } |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 34 | } |
| 35 | va_end(ap); |
| 36 | return n; |
| 37 | } |
| 38 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 39 | static int64_t cmp_null(void *l, void *r) |
| 40 | { |
| 41 | if (!l && !r) |
| 42 | return 0; |
| 43 | else if (!l) |
| 44 | return -1; |
| 45 | else |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | /* --sort pid */ |
| 50 | |
| 51 | static int64_t |
| 52 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
| 53 | { |
| 54 | return right->thread->pid - left->thread->pid; |
| 55 | } |
| 56 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 57 | static int hist_entry__thread_snprintf(struct hist_entry *self, char *bf, |
| 58 | size_t size, unsigned int width) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 59 | { |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 60 | return repsep_snprintf(bf, size, "%*s:%5d", width, |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 61 | self->thread->comm ?: "", self->thread->pid); |
| 62 | } |
| 63 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 64 | struct sort_entry sort_thread = { |
| 65 | .se_header = "Command: Pid", |
| 66 | .se_cmp = sort__thread_cmp, |
| 67 | .se_snprintf = hist_entry__thread_snprintf, |
| 68 | .se_width_idx = HISTC_THREAD, |
| 69 | }; |
| 70 | |
| 71 | /* --sort comm */ |
| 72 | |
| 73 | static int64_t |
| 74 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) |
| 75 | { |
| 76 | return right->thread->pid - left->thread->pid; |
| 77 | } |
| 78 | |
| 79 | static int64_t |
| 80 | sort__comm_collapse(struct hist_entry *left, struct hist_entry *right) |
| 81 | { |
| 82 | char *comm_l = left->thread->comm; |
| 83 | char *comm_r = right->thread->comm; |
| 84 | |
| 85 | if (!comm_l || !comm_r) |
| 86 | return cmp_null(comm_l, comm_r); |
| 87 | |
| 88 | return strcmp(comm_l, comm_r); |
| 89 | } |
| 90 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 91 | static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf, |
| 92 | size_t size, unsigned int width) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 93 | { |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 94 | return repsep_snprintf(bf, size, "%*s", width, self->thread->comm); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 97 | struct sort_entry sort_comm = { |
| 98 | .se_header = "Command", |
| 99 | .se_cmp = sort__comm_cmp, |
| 100 | .se_collapse = sort__comm_collapse, |
| 101 | .se_snprintf = hist_entry__comm_snprintf, |
| 102 | .se_width_idx = HISTC_COMM, |
| 103 | }; |
| 104 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 105 | /* --sort dso */ |
| 106 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 107 | static int64_t |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 108 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) |
| 109 | { |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 110 | struct dso *dso_l = left->ms.map ? left->ms.map->dso : NULL; |
| 111 | struct dso *dso_r = right->ms.map ? right->ms.map->dso : NULL; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 112 | const char *dso_name_l, *dso_name_r; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 113 | |
| 114 | if (!dso_l || !dso_r) |
| 115 | return cmp_null(dso_l, dso_r); |
| 116 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 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 | |
| 125 | return strcmp(dso_name_l, dso_name_r); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 128 | static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf, |
| 129 | size_t size, unsigned int width) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 130 | { |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 131 | if (self->ms.map && self->ms.map->dso) { |
| 132 | const char *dso_name = !verbose ? self->ms.map->dso->short_name : |
| 133 | self->ms.map->dso->long_name; |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 134 | return repsep_snprintf(bf, size, "%-*s", width, dso_name); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 135 | } |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 136 | |
Ian Munsie | 1437a30 | 2010-12-06 13:37:04 +1100 | [diff] [blame] | 137 | return repsep_snprintf(bf, size, "%-*s", width, "[unknown]"); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 140 | struct sort_entry sort_dso = { |
| 141 | .se_header = "Shared Object", |
| 142 | .se_cmp = sort__dso_cmp, |
| 143 | .se_snprintf = hist_entry__dso_snprintf, |
| 144 | .se_width_idx = HISTC_DSO, |
| 145 | }; |
| 146 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 147 | /* --sort symbol */ |
| 148 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 149 | static int64_t |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 150 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) |
| 151 | { |
| 152 | u64 ip_l, ip_r; |
| 153 | |
Anton Blanchard | 6bb8f31 | 2011-08-31 11:51:45 +1000 | [diff] [blame] | 154 | if (!left->ms.sym && !right->ms.sym) |
| 155 | return right->level - left->level; |
| 156 | |
| 157 | if (!left->ms.sym || !right->ms.sym) |
| 158 | return cmp_null(left->ms.sym, right->ms.sym); |
| 159 | |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 160 | if (left->ms.sym == right->ms.sym) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 161 | return 0; |
| 162 | |
Anton Blanchard | 6bb8f31 | 2011-08-31 11:51:45 +1000 | [diff] [blame] | 163 | ip_l = left->ms.sym->start; |
| 164 | ip_r = right->ms.sym->start; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 165 | |
| 166 | return (int64_t)(ip_r - ip_l); |
| 167 | } |
| 168 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 169 | static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf, |
| 170 | size_t size, unsigned int width __used) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 171 | { |
| 172 | size_t ret = 0; |
| 173 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 174 | if (verbose) { |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 175 | char o = self->ms.map ? dso__symtab_origin(self->ms.map->dso) : '!'; |
Ian Munsie | 1437a30 | 2010-12-06 13:37:04 +1100 | [diff] [blame] | 176 | ret += repsep_snprintf(bf, size, "%-#*llx %c ", |
Arnaldo Carvalho de Melo | fb89941 | 2010-08-05 19:17:22 -0300 | [diff] [blame] | 177 | BITS_PER_LONG / 4, self->ip, o); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 178 | } |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 179 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 180 | ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", self->level); |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 181 | if (self->ms.sym) |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 182 | ret += repsep_snprintf(bf + ret, size - ret, "%s", |
| 183 | self->ms.sym->name); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 184 | else |
Ian Munsie | 1437a30 | 2010-12-06 13:37:04 +1100 | [diff] [blame] | 185 | ret += repsep_snprintf(bf + ret, size - ret, "%-#*llx", |
Arnaldo Carvalho de Melo | fb89941 | 2010-08-05 19:17:22 -0300 | [diff] [blame] | 186 | BITS_PER_LONG / 4, self->ip); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 191 | struct sort_entry sort_sym = { |
| 192 | .se_header = "Symbol", |
| 193 | .se_cmp = sort__sym_cmp, |
| 194 | .se_snprintf = hist_entry__sym_snprintf, |
| 195 | .se_width_idx = HISTC_SYMBOL, |
| 196 | }; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 197 | |
| 198 | /* --sort parent */ |
| 199 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 200 | static int64_t |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 201 | sort__parent_cmp(struct hist_entry *left, struct hist_entry *right) |
| 202 | { |
| 203 | struct symbol *sym_l = left->parent; |
| 204 | struct symbol *sym_r = right->parent; |
| 205 | |
| 206 | if (!sym_l || !sym_r) |
| 207 | return cmp_null(sym_l, sym_r); |
| 208 | |
| 209 | return strcmp(sym_l->name, sym_r->name); |
| 210 | } |
| 211 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 212 | static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf, |
| 213 | size_t size, unsigned int width) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 214 | { |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 215 | return repsep_snprintf(bf, size, "%-*s", width, |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 216 | self->parent ? self->parent->name : "[other]"); |
| 217 | } |
| 218 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 219 | struct sort_entry sort_parent = { |
| 220 | .se_header = "Parent symbol", |
| 221 | .se_cmp = sort__parent_cmp, |
| 222 | .se_snprintf = hist_entry__parent_snprintf, |
| 223 | .se_width_idx = HISTC_PARENT, |
| 224 | }; |
| 225 | |
Arun Sharma | f60f359 | 2010-06-04 11:27:10 -0300 | [diff] [blame] | 226 | /* --sort cpu */ |
| 227 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 228 | static int64_t |
Arun Sharma | f60f359 | 2010-06-04 11:27:10 -0300 | [diff] [blame] | 229 | sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right) |
| 230 | { |
| 231 | return right->cpu - left->cpu; |
| 232 | } |
| 233 | |
| 234 | static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf, |
| 235 | size_t size, unsigned int width) |
| 236 | { |
| 237 | return repsep_snprintf(bf, size, "%-*d", width, self->cpu); |
| 238 | } |
| 239 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 240 | struct sort_entry sort_cpu = { |
| 241 | .se_header = "CPU", |
| 242 | .se_cmp = sort__cpu_cmp, |
| 243 | .se_snprintf = hist_entry__cpu_snprintf, |
| 244 | .se_width_idx = HISTC_CPU, |
| 245 | }; |
| 246 | |
| 247 | struct sort_dimension { |
| 248 | const char *name; |
| 249 | struct sort_entry *entry; |
| 250 | int taken; |
| 251 | }; |
| 252 | |
| 253 | static struct sort_dimension sort_dimensions[] = { |
| 254 | { .name = "pid", .entry = &sort_thread, }, |
| 255 | { .name = "comm", .entry = &sort_comm, }, |
| 256 | { .name = "dso", .entry = &sort_dso, }, |
| 257 | { .name = "symbol", .entry = &sort_sym, }, |
| 258 | { .name = "parent", .entry = &sort_parent, }, |
| 259 | { .name = "cpu", .entry = &sort_cpu, }, |
| 260 | }; |
| 261 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 262 | int sort_dimension__add(const char *tok) |
| 263 | { |
| 264 | unsigned int i; |
| 265 | |
| 266 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { |
| 267 | struct sort_dimension *sd = &sort_dimensions[i]; |
| 268 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 269 | if (strncasecmp(tok, sd->name, strlen(tok))) |
| 270 | continue; |
| 271 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 272 | if (sd->entry == &sort_parent) { |
| 273 | int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED); |
| 274 | if (ret) { |
| 275 | char err[BUFSIZ]; |
| 276 | |
| 277 | regerror(ret, &parent_regex, err, sizeof(err)); |
Arnaldo Carvalho de Melo | 2aefa4f | 2010-04-02 12:30:57 -0300 | [diff] [blame] | 278 | pr_err("Invalid regex: %s\n%s", parent_pattern, err); |
| 279 | return -EINVAL; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 280 | } |
| 281 | sort__has_parent = 1; |
| 282 | } |
| 283 | |
Frederic Weisbecker | fd8ea21 | 2011-06-29 23:08:14 +0200 | [diff] [blame] | 284 | if (sd->taken) |
| 285 | return 0; |
| 286 | |
| 287 | if (sd->entry->se_collapse) |
| 288 | sort__need_collapse = 1; |
| 289 | |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 290 | if (list_empty(&hist_entry__sort_list)) { |
| 291 | if (!strcmp(sd->name, "pid")) |
| 292 | sort__first_dimension = SORT_PID; |
| 293 | else if (!strcmp(sd->name, "comm")) |
| 294 | sort__first_dimension = SORT_COMM; |
| 295 | else if (!strcmp(sd->name, "dso")) |
| 296 | sort__first_dimension = SORT_DSO; |
| 297 | else if (!strcmp(sd->name, "symbol")) |
| 298 | sort__first_dimension = SORT_SYM; |
| 299 | else if (!strcmp(sd->name, "parent")) |
| 300 | sort__first_dimension = SORT_PARENT; |
Arun Sharma | f60f359 | 2010-06-04 11:27:10 -0300 | [diff] [blame] | 301 | else if (!strcmp(sd->name, "cpu")) |
| 302 | sort__first_dimension = SORT_CPU; |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 303 | } |
Frederic Weisbecker | af0a6fa | 2009-10-22 23:23:22 +0200 | [diff] [blame] | 304 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 305 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); |
| 306 | sd->taken = 1; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | return -ESRCH; |
| 312 | } |
Arnaldo Carvalho de Melo | c8829c7 | 2009-12-14 20:09:29 -0200 | [diff] [blame] | 313 | |
| 314 | void setup_sorting(const char * const usagestr[], const struct option *opts) |
| 315 | { |
| 316 | char *tmp, *tok, *str = strdup(sort_order); |
| 317 | |
| 318 | for (tok = strtok_r(str, ", ", &tmp); |
| 319 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
| 320 | if (sort_dimension__add(tok) < 0) { |
| 321 | error("Unknown --sort key: `%s'", tok); |
| 322 | usage_with_options(usagestr, opts); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | free(str); |
| 327 | } |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 328 | |
| 329 | void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list, |
| 330 | const char *list_name, FILE *fp) |
| 331 | { |
| 332 | if (list && strlist__nr_entries(list) == 1) { |
| 333 | if (fp != NULL) |
| 334 | fprintf(fp, "# %s: %s\n", list_name, |
| 335 | strlist__entry(list, 0)->s); |
| 336 | self->elide = true; |
| 337 | } |
| 338 | } |