blob: f5dba560d918802919ce47160cecb3e44f0e9656 [file] [log] [blame]
John Kacurdd68ada2009-09-24 18:02:49 +02001#include "sort.h"
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03002#include "hist.h"
John Kacurdd68ada2009-09-24 18:02:49 +02003
4regex_t parent_regex;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -03005const char default_parent_pattern[] = "^sys_|^do_page_fault";
6const char *parent_pattern = default_parent_pattern;
7const char default_sort_order[] = "comm,dso,symbol";
8const char *sort_order = default_sort_order;
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +02009int sort__need_collapse = 0;
10int sort__has_parent = 0;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020011
12enum sort_type sort__first_dimension;
John Kacurdd68ada2009-09-24 18:02:49 +020013
John Kacurdd68ada2009-09-24 18:02:49 +020014char * field_sep;
15
16LIST_HEAD(hist_entry__sort_list);
17
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030018static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
John Kacurdd68ada2009-09-24 18:02:49 +020019{
20 int n;
21 va_list ap;
22
23 va_start(ap, fmt);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030024 n = vsnprintf(bf, size, fmt, ap);
25 if (field_sep && n > 0) {
26 char *sep = bf;
John Kacurdd68ada2009-09-24 18:02:49 +020027
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030028 while (1) {
29 sep = strchr(sep, *field_sep);
30 if (sep == NULL)
31 break;
32 *sep = '.';
John Kacurdd68ada2009-09-24 18:02:49 +020033 }
John Kacurdd68ada2009-09-24 18:02:49 +020034 }
35 va_end(ap);
36 return n;
37}
38
Frederic Weisbecker872a8782011-06-29 03:14:52 +020039static 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
51static int64_t
52sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
53{
54 return right->thread->pid - left->thread->pid;
55}
56
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030057static int hist_entry__thread_snprintf(struct hist_entry *self, char *bf,
58 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +020059{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030060 return repsep_snprintf(bf, size, "%*s:%5d", width,
John Kacurdd68ada2009-09-24 18:02:49 +020061 self->thread->comm ?: "", self->thread->pid);
62}
63
Frederic Weisbecker872a8782011-06-29 03:14:52 +020064struct 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
73static int64_t
74sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
75{
76 return right->thread->pid - left->thread->pid;
77}
78
79static int64_t
80sort__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 Meloa4e3b952010-03-31 11:33:40 -030091static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf,
92 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +020093{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030094 return repsep_snprintf(bf, size, "%*s", width, self->thread->comm);
John Kacurdd68ada2009-09-24 18:02:49 +020095}
96
Frederic Weisbecker872a8782011-06-29 03:14:52 +020097struct 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 Kacurdd68ada2009-09-24 18:02:49 +0200105/* --sort dso */
106
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200107static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200108sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
109{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300110 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 Melo439d4732009-10-02 03:29:58 -0300112 const char *dso_name_l, *dso_name_r;
John Kacurdd68ada2009-09-24 18:02:49 +0200113
114 if (!dso_l || !dso_r)
115 return cmp_null(dso_l, dso_r);
116
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300117 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 Kacurdd68ada2009-09-24 18:02:49 +0200126}
127
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300128static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf,
129 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200130{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300131 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 Meloa4e3b952010-03-31 11:33:40 -0300134 return repsep_snprintf(bf, size, "%-*s", width, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300135 }
John Kacurdd68ada2009-09-24 18:02:49 +0200136
Ian Munsie1437a302010-12-06 13:37:04 +1100137 return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
John Kacurdd68ada2009-09-24 18:02:49 +0200138}
139
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200140struct 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 Kacurdd68ada2009-09-24 18:02:49 +0200147/* --sort symbol */
148
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200149static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200150sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
151{
152 u64 ip_l, ip_r;
153
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300154 if (left->ms.sym == right->ms.sym)
John Kacurdd68ada2009-09-24 18:02:49 +0200155 return 0;
156
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300157 ip_l = left->ms.sym ? left->ms.sym->start : left->ip;
158 ip_r = right->ms.sym ? right->ms.sym->start : right->ip;
John Kacurdd68ada2009-09-24 18:02:49 +0200159
160 return (int64_t)(ip_r - ip_l);
161}
162
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300163static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
164 size_t size, unsigned int width __used)
John Kacurdd68ada2009-09-24 18:02:49 +0200165{
166 size_t ret = 0;
167
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300168 if (verbose) {
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300169 char o = self->ms.map ? dso__symtab_origin(self->ms.map->dso) : '!';
Ian Munsie1437a302010-12-06 13:37:04 +1100170 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
Arnaldo Carvalho de Melofb899412010-08-05 19:17:22 -0300171 BITS_PER_LONG / 4, self->ip, o);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300172 }
John Kacurdd68ada2009-09-24 18:02:49 +0200173
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300174 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", self->level);
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300175 if (self->ms.sym)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300176 ret += repsep_snprintf(bf + ret, size - ret, "%s",
177 self->ms.sym->name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300178 else
Ian Munsie1437a302010-12-06 13:37:04 +1100179 ret += repsep_snprintf(bf + ret, size - ret, "%-#*llx",
Arnaldo Carvalho de Melofb899412010-08-05 19:17:22 -0300180 BITS_PER_LONG / 4, self->ip);
John Kacurdd68ada2009-09-24 18:02:49 +0200181
182 return ret;
183}
184
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200185struct sort_entry sort_sym = {
186 .se_header = "Symbol",
187 .se_cmp = sort__sym_cmp,
188 .se_snprintf = hist_entry__sym_snprintf,
189 .se_width_idx = HISTC_SYMBOL,
190};
John Kacurdd68ada2009-09-24 18:02:49 +0200191
192/* --sort parent */
193
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200194static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200195sort__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
200 if (!sym_l || !sym_r)
201 return cmp_null(sym_l, sym_r);
202
203 return strcmp(sym_l->name, sym_r->name);
204}
205
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300206static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
207 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200208{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300209 return repsep_snprintf(bf, size, "%-*s", width,
John Kacurdd68ada2009-09-24 18:02:49 +0200210 self->parent ? self->parent->name : "[other]");
211}
212
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200213struct sort_entry sort_parent = {
214 .se_header = "Parent symbol",
215 .se_cmp = sort__parent_cmp,
216 .se_snprintf = hist_entry__parent_snprintf,
217 .se_width_idx = HISTC_PARENT,
218};
219
Arun Sharmaf60f3592010-06-04 11:27:10 -0300220/* --sort cpu */
221
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200222static int64_t
Arun Sharmaf60f3592010-06-04 11:27:10 -0300223sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
224{
225 return right->cpu - left->cpu;
226}
227
228static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
229 size_t size, unsigned int width)
230{
231 return repsep_snprintf(bf, size, "%-*d", width, self->cpu);
232}
233
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200234struct sort_entry sort_cpu = {
235 .se_header = "CPU",
236 .se_cmp = sort__cpu_cmp,
237 .se_snprintf = hist_entry__cpu_snprintf,
238 .se_width_idx = HISTC_CPU,
239};
240
241struct sort_dimension {
242 const char *name;
243 struct sort_entry *entry;
244 int taken;
245};
246
247static struct sort_dimension sort_dimensions[] = {
248 { .name = "pid", .entry = &sort_thread, },
249 { .name = "comm", .entry = &sort_comm, },
250 { .name = "dso", .entry = &sort_dso, },
251 { .name = "symbol", .entry = &sort_sym, },
252 { .name = "parent", .entry = &sort_parent, },
253 { .name = "cpu", .entry = &sort_cpu, },
254};
255
John Kacurdd68ada2009-09-24 18:02:49 +0200256int sort_dimension__add(const char *tok)
257{
258 unsigned int i;
259
260 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
261 struct sort_dimension *sd = &sort_dimensions[i];
262
263 if (sd->taken)
264 continue;
265
266 if (strncasecmp(tok, sd->name, strlen(tok)))
267 continue;
268
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200269 if (sd->entry->se_collapse)
John Kacurdd68ada2009-09-24 18:02:49 +0200270 sort__need_collapse = 1;
271
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 Melo2aefa4f2010-04-02 12:30:57 -0300278 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
279 return -EINVAL;
John Kacurdd68ada2009-09-24 18:02:49 +0200280 }
281 sort__has_parent = 1;
282 }
283
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200284 if (list_empty(&hist_entry__sort_list)) {
285 if (!strcmp(sd->name, "pid"))
286 sort__first_dimension = SORT_PID;
287 else if (!strcmp(sd->name, "comm"))
288 sort__first_dimension = SORT_COMM;
289 else if (!strcmp(sd->name, "dso"))
290 sort__first_dimension = SORT_DSO;
291 else if (!strcmp(sd->name, "symbol"))
292 sort__first_dimension = SORT_SYM;
293 else if (!strcmp(sd->name, "parent"))
294 sort__first_dimension = SORT_PARENT;
Arun Sharmaf60f3592010-06-04 11:27:10 -0300295 else if (!strcmp(sd->name, "cpu"))
296 sort__first_dimension = SORT_CPU;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200297 }
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200298
John Kacurdd68ada2009-09-24 18:02:49 +0200299 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
300 sd->taken = 1;
301
302 return 0;
303 }
304
305 return -ESRCH;
306}
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -0200307
308void setup_sorting(const char * const usagestr[], const struct option *opts)
309{
310 char *tmp, *tok, *str = strdup(sort_order);
311
312 for (tok = strtok_r(str, ", ", &tmp);
313 tok; tok = strtok_r(NULL, ", ", &tmp)) {
314 if (sort_dimension__add(tok) < 0) {
315 error("Unknown --sort key: `%s'", tok);
316 usage_with_options(usagestr, opts);
317 }
318 }
319
320 free(str);
321}
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200322
323void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
324 const char *list_name, FILE *fp)
325{
326 if (list && strlist__nr_entries(list) == 1) {
327 if (fp != NULL)
328 fprintf(fp, "# %s: %s\n", list_name,
329 strlist__entry(list, 0)->s);
330 self->elide = true;
331 }
332}