70a9c13f4ad5a3cbe8e9aecdfb61c701a400db9c
[linux-2.6.git] / tools / perf / util / top.c
1 /*
2  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3  *
4  * Refactored from builtin-top.c, see that files for further copyright notes.
5  *
6  * Released under the GPL v2. (and only v2, not any later version)
7  */
8
9 #include "cpumap.h"
10 #include "event.h"
11 #include "evlist.h"
12 #include "evsel.h"
13 #include "parse-events.h"
14 #include "symbol.h"
15 #include "top.h"
16 #include <inttypes.h>
17
18 /*
19  * Ordering weight: count-1 * count-2 * ... / count-n
20  */
21 static double sym_weight(const struct sym_entry *sym, struct perf_top *top)
22 {
23         double weight = sym->snap_count;
24         int counter;
25
26         if (!top->display_weighted)
27                 return weight;
28
29         for (counter = 1; counter < top->evlist->nr_entries - 1; counter++)
30                 weight *= sym->count[counter];
31
32         weight /= (sym->count[counter] + 1);
33
34         return weight;
35 }
36
37 static void perf_top__remove_active_sym(struct perf_top *top, struct sym_entry *syme)
38 {
39         pthread_mutex_lock(&top->active_symbols_lock);
40         list_del_init(&syme->node);
41         pthread_mutex_unlock(&top->active_symbols_lock);
42 }
43
44 static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
45 {
46         struct rb_node **p = &tree->rb_node;
47         struct rb_node *parent = NULL;
48         struct sym_entry *iter;
49
50         while (*p != NULL) {
51                 parent = *p;
52                 iter = rb_entry(parent, struct sym_entry, rb_node);
53
54                 if (se->weight > iter->weight)
55                         p = &(*p)->rb_left;
56                 else
57                         p = &(*p)->rb_right;
58         }
59
60         rb_link_node(&se->rb_node, parent, p);
61         rb_insert_color(&se->rb_node, tree);
62 }
63
64 size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
65 {
66         struct perf_evsel *counter;
67         float samples_per_sec = top->samples / top->delay_secs;
68         float ksamples_per_sec = top->kernel_samples / top->delay_secs;
69         float esamples_percent = (100.0 * top->exact_samples) / top->samples;
70         size_t ret = 0;
71
72         if (!perf_guest) {
73                 ret = snprintf(bf, size,
74                                "   PerfTop:%8.0f irqs/sec  kernel:%4.1f%%"
75                                "  exact: %4.1f%% [", samples_per_sec,
76                                100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
77                                         samples_per_sec)),
78                                 esamples_percent);
79         } else {
80                 float us_samples_per_sec = top->us_samples / top->delay_secs;
81                 float guest_kernel_samples_per_sec = top->guest_kernel_samples / top->delay_secs;
82                 float guest_us_samples_per_sec = top->guest_us_samples / top->delay_secs;
83
84                 ret = snprintf(bf, size,
85                                "   PerfTop:%8.0f irqs/sec  kernel:%4.1f%% us:%4.1f%%"
86                                " guest kernel:%4.1f%% guest us:%4.1f%%"
87                                " exact: %4.1f%% [", samples_per_sec,
88                                100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
89                                                  samples_per_sec)),
90                                100.0 - (100.0 * ((samples_per_sec - us_samples_per_sec) /
91                                                  samples_per_sec)),
92                                100.0 - (100.0 * ((samples_per_sec -
93                                                   guest_kernel_samples_per_sec) /
94                                                  samples_per_sec)),
95                                100.0 - (100.0 * ((samples_per_sec -
96                                                   guest_us_samples_per_sec) /
97                                                  samples_per_sec)),
98                                esamples_percent);
99         }
100
101         if (top->evlist->nr_entries == 1 || !top->display_weighted) {
102                 struct perf_evsel *first;
103                 first = list_entry(top->evlist->entries.next, struct perf_evsel, node);
104                 ret += snprintf(bf + ret, size - ret, "%" PRIu64 "%s ",
105                                 (uint64_t)first->attr.sample_period,
106                                 top->freq ? "Hz" : "");
107         }
108
109         if (!top->display_weighted) {
110                 ret += snprintf(bf + ret, size - ret, "%s",
111                                 event_name(top->sym_evsel));
112         } else list_for_each_entry(counter, &top->evlist->entries, node) {
113                 ret += snprintf(bf + ret, size - ret, "%s%s",
114                                 counter->idx ? "/" : "", event_name(counter));
115         }
116
117         ret += snprintf(bf + ret, size - ret, "], ");
118
119         if (top->target_pid != -1)
120                 ret += snprintf(bf + ret, size - ret, " (target_pid: %d",
121                                 top->target_pid);
122         else if (top->target_tid != -1)
123                 ret += snprintf(bf + ret, size - ret, " (target_tid: %d",
124                                 top->target_tid);
125         else
126                 ret += snprintf(bf + ret, size - ret, " (all");
127
128         if (top->cpu_list)
129                 ret += snprintf(bf + ret, size - ret, ", CPU%s: %s)",
130                                 top->evlist->cpus->nr > 1 ? "s" : "", top->cpu_list);
131         else {
132                 if (top->target_tid != -1)
133                         ret += snprintf(bf + ret, size - ret, ")");
134                 else
135                         ret += snprintf(bf + ret, size - ret, ", %d CPU%s)",
136                                         top->evlist->cpus->nr,
137                                         top->evlist->cpus->nr > 1 ? "s" : "");
138         }
139
140         return ret;
141 }
142
143 void perf_top__reset_sample_counters(struct perf_top *top)
144 {
145         top->samples = top->us_samples = top->kernel_samples =
146         top->exact_samples = top->guest_kernel_samples =
147         top->guest_us_samples = 0;
148 }
149
150 float perf_top__decay_samples(struct perf_top *top, struct rb_root *root)
151 {
152         struct sym_entry *syme, *n;
153         float sum_ksamples = 0.0;
154         int snap = !top->display_weighted ? top->sym_counter : 0, j;
155
156         /* Sort the active symbols */
157         pthread_mutex_lock(&top->active_symbols_lock);
158         syme = list_entry(top->active_symbols.next, struct sym_entry, node);
159         pthread_mutex_unlock(&top->active_symbols_lock);
160
161         top->rb_entries = 0;
162         list_for_each_entry_safe_from(syme, n, &top->active_symbols, node) {
163                 syme->snap_count = syme->count[snap];
164                 if (syme->snap_count != 0) {
165
166                         if ((top->hide_user_symbols &&
167                              syme->origin == PERF_RECORD_MISC_USER) ||
168                             (top->hide_kernel_symbols &&
169                              syme->origin == PERF_RECORD_MISC_KERNEL)) {
170                                 perf_top__remove_active_sym(top, syme);
171                                 continue;
172                         }
173                         syme->weight = sym_weight(syme, top);
174
175                         if ((int)syme->snap_count >= top->count_filter) {
176                                 rb_insert_active_sym(root, syme);
177                                 ++top->rb_entries;
178                         }
179                         sum_ksamples += syme->snap_count;
180
181                         for (j = 0; j < top->evlist->nr_entries; j++)
182                                 syme->count[j] = top->zero ? 0 : syme->count[j] * 7 / 8;
183                 } else
184                         perf_top__remove_active_sym(top, syme);
185         }
186
187         return sum_ksamples;
188 }
189
190 /*
191  * Find the longest symbol name that will be displayed
192  */
193 void perf_top__find_widths(struct perf_top *top, struct rb_root *root,
194                            int *dso_width, int *dso_short_width, int *sym_width)
195 {
196         struct rb_node *nd;
197         int printed = 0;
198
199         *sym_width = *dso_width = *dso_short_width = 0;
200
201         for (nd = rb_first(root); nd; nd = rb_next(nd)) {
202                 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node);
203                 struct symbol *sym = sym_entry__symbol(syme);
204
205                 if (++printed > top->print_entries ||
206                     (int)syme->snap_count < top->count_filter)
207                         continue;
208
209                 if (syme->map->dso->long_name_len > *dso_width)
210                         *dso_width = syme->map->dso->long_name_len;
211
212                 if (syme->map->dso->short_name_len > *dso_short_width)
213                         *dso_short_width = syme->map->dso->short_name_len;
214
215                 if (sym->namelen > *sym_width)
216                         *sym_width = sym->namelen;
217         }
218 }