aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/top.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/top.c')
-rw-r--r--tools/perf/util/top.c48
1 files changed, 34 insertions, 14 deletions
diff --git a/tools/perf/util/top.c b/tools/perf/util/top.c
index 70a9c13f4ad5..75cfe4d45119 100644
--- a/tools/perf/util/top.c
+++ b/tools/perf/util/top.c
@@ -61,6 +61,12 @@ static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
61 rb_insert_color(&se->rb_node, tree); 61 rb_insert_color(&se->rb_node, tree);
62} 62}
63 63
64#define SNPRINTF(buf, size, fmt, args...) \
65({ \
66 size_t r = snprintf(buf, size, fmt, ## args); \
67 r > size ? size : r; \
68})
69
64size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size) 70size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
65{ 71{
66 struct perf_evsel *counter; 72 struct perf_evsel *counter;
@@ -70,7 +76,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
70 size_t ret = 0; 76 size_t ret = 0;
71 77
72 if (!perf_guest) { 78 if (!perf_guest) {
73 ret = snprintf(bf, size, 79 ret = SNPRINTF(bf, size,
74 " PerfTop:%8.0f irqs/sec kernel:%4.1f%%" 80 " PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
75 " exact: %4.1f%% [", samples_per_sec, 81 " exact: %4.1f%% [", samples_per_sec,
76 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) / 82 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
@@ -81,7 +87,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
81 float guest_kernel_samples_per_sec = top->guest_kernel_samples / top->delay_secs; 87 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; 88 float guest_us_samples_per_sec = top->guest_us_samples / top->delay_secs;
83 89
84 ret = snprintf(bf, size, 90 ret = SNPRINTF(bf, size,
85 " PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%" 91 " PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%"
86 " guest kernel:%4.1f%% guest us:%4.1f%%" 92 " guest kernel:%4.1f%% guest us:%4.1f%%"
87 " exact: %4.1f%% [", samples_per_sec, 93 " exact: %4.1f%% [", samples_per_sec,
@@ -101,38 +107,52 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
101 if (top->evlist->nr_entries == 1 || !top->display_weighted) { 107 if (top->evlist->nr_entries == 1 || !top->display_weighted) {
102 struct perf_evsel *first; 108 struct perf_evsel *first;
103 first = list_entry(top->evlist->entries.next, struct perf_evsel, node); 109 first = list_entry(top->evlist->entries.next, struct perf_evsel, node);
104 ret += snprintf(bf + ret, size - ret, "%" PRIu64 "%s ", 110 ret += SNPRINTF(bf + ret, size - ret, "%" PRIu64 "%s ",
105 (uint64_t)first->attr.sample_period, 111 (uint64_t)first->attr.sample_period,
106 top->freq ? "Hz" : ""); 112 top->freq ? "Hz" : "");
107 } 113 }
108 114
109 if (!top->display_weighted) { 115 if (!top->display_weighted) {
110 ret += snprintf(bf + ret, size - ret, "%s", 116 ret += SNPRINTF(bf + ret, size - ret, "%s",
111 event_name(top->sym_evsel)); 117 event_name(top->sym_evsel));
112 } else list_for_each_entry(counter, &top->evlist->entries, node) { 118 } else {
113 ret += snprintf(bf + ret, size - ret, "%s%s", 119 /*
114 counter->idx ? "/" : "", event_name(counter)); 120 * Don't let events eat all the space. Leaving 30 bytes
121 * for the rest should be enough.
122 */
123 size_t last_pos = size - 30;
124
125 list_for_each_entry(counter, &top->evlist->entries, node) {
126 ret += SNPRINTF(bf + ret, size - ret, "%s%s",
127 counter->idx ? "/" : "",
128 event_name(counter));
129 if (ret > last_pos) {
130 sprintf(bf + last_pos - 3, "..");
131 ret = last_pos - 1;
132 break;
133 }
134 }
115 } 135 }
116 136
117 ret += snprintf(bf + ret, size - ret, "], "); 137 ret += SNPRINTF(bf + ret, size - ret, "], ");
118 138
119 if (top->target_pid != -1) 139 if (top->target_pid != -1)
120 ret += snprintf(bf + ret, size - ret, " (target_pid: %d", 140 ret += SNPRINTF(bf + ret, size - ret, " (target_pid: %d",
121 top->target_pid); 141 top->target_pid);
122 else if (top->target_tid != -1) 142 else if (top->target_tid != -1)
123 ret += snprintf(bf + ret, size - ret, " (target_tid: %d", 143 ret += SNPRINTF(bf + ret, size - ret, " (target_tid: %d",
124 top->target_tid); 144 top->target_tid);
125 else 145 else
126 ret += snprintf(bf + ret, size - ret, " (all"); 146 ret += SNPRINTF(bf + ret, size - ret, " (all");
127 147
128 if (top->cpu_list) 148 if (top->cpu_list)
129 ret += snprintf(bf + ret, size - ret, ", CPU%s: %s)", 149 ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
130 top->evlist->cpus->nr > 1 ? "s" : "", top->cpu_list); 150 top->evlist->cpus->nr > 1 ? "s" : "", top->cpu_list);
131 else { 151 else {
132 if (top->target_tid != -1) 152 if (top->target_tid != -1)
133 ret += snprintf(bf + ret, size - ret, ")"); 153 ret += SNPRINTF(bf + ret, size - ret, ")");
134 else 154 else
135 ret += snprintf(bf + ret, size - ret, ", %d CPU%s)", 155 ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
136 top->evlist->cpus->nr, 156 top->evlist->cpus->nr,
137 top->evlist->cpus->nr > 1 ? "s" : ""); 157 top->evlist->cpus->nr > 1 ? "s" : "");
138 } 158 }