]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/perf/util/values.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[mirror_ubuntu-bionic-kernel.git] / tools / perf / util / values.c
1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <errno.h>
5
6 #include "util.h"
7 #include "values.h"
8 #include "debug.h"
9
10 int perf_read_values_init(struct perf_read_values *values)
11 {
12 values->threads_max = 16;
13 values->pid = malloc(values->threads_max * sizeof(*values->pid));
14 values->tid = malloc(values->threads_max * sizeof(*values->tid));
15 values->value = malloc(values->threads_max * sizeof(*values->value));
16 if (!values->pid || !values->tid || !values->value) {
17 pr_debug("failed to allocate read_values threads arrays");
18 goto out_free_pid;
19 }
20 values->threads = 0;
21
22 values->counters_max = 16;
23 values->counterrawid = malloc(values->counters_max
24 * sizeof(*values->counterrawid));
25 values->countername = malloc(values->counters_max
26 * sizeof(*values->countername));
27 if (!values->counterrawid || !values->countername) {
28 pr_debug("failed to allocate read_values counters arrays");
29 goto out_free_counter;
30 }
31 values->counters = 0;
32
33 return 0;
34
35 out_free_counter:
36 zfree(&values->counterrawid);
37 zfree(&values->countername);
38 out_free_pid:
39 zfree(&values->pid);
40 zfree(&values->tid);
41 zfree(&values->value);
42 return -ENOMEM;
43 }
44
45 void perf_read_values_destroy(struct perf_read_values *values)
46 {
47 int i;
48
49 if (!values->threads_max || !values->counters_max)
50 return;
51
52 for (i = 0; i < values->threads; i++)
53 zfree(&values->value[i]);
54 zfree(&values->value);
55 zfree(&values->pid);
56 zfree(&values->tid);
57 zfree(&values->counterrawid);
58 for (i = 0; i < values->counters; i++)
59 zfree(&values->countername[i]);
60 zfree(&values->countername);
61 }
62
63 static int perf_read_values__enlarge_threads(struct perf_read_values *values)
64 {
65 int nthreads_max = values->threads_max * 2;
66 void *npid = realloc(values->pid, nthreads_max * sizeof(*values->pid)),
67 *ntid = realloc(values->tid, nthreads_max * sizeof(*values->tid)),
68 *nvalue = realloc(values->value, nthreads_max * sizeof(*values->value));
69
70 if (!npid || !ntid || !nvalue)
71 goto out_err;
72
73 values->threads_max = nthreads_max;
74 values->pid = npid;
75 values->tid = ntid;
76 values->value = nvalue;
77 return 0;
78 out_err:
79 free(npid);
80 free(ntid);
81 free(nvalue);
82 pr_debug("failed to enlarge read_values threads arrays");
83 return -ENOMEM;
84 }
85
86 static int perf_read_values__findnew_thread(struct perf_read_values *values,
87 u32 pid, u32 tid)
88 {
89 int i;
90
91 for (i = 0; i < values->threads; i++)
92 if (values->pid[i] == pid && values->tid[i] == tid)
93 return i;
94
95 if (values->threads == values->threads_max) {
96 i = perf_read_values__enlarge_threads(values);
97 if (i < 0)
98 return i;
99 }
100
101 i = values->threads + 1;
102 values->value[i] = malloc(values->counters_max * sizeof(**values->value));
103 if (!values->value[i]) {
104 pr_debug("failed to allocate read_values counters array");
105 return -ENOMEM;
106 }
107 values->pid[i] = pid;
108 values->tid[i] = tid;
109 values->threads = i;
110
111 return i;
112 }
113
114 static int perf_read_values__enlarge_counters(struct perf_read_values *values)
115 {
116 char **countername;
117 int i, counters_max = values->counters_max * 2;
118 u64 *counterrawid = realloc(values->counterrawid, counters_max * sizeof(*values->counterrawid));
119
120 if (!counterrawid) {
121 pr_debug("failed to enlarge read_values rawid array");
122 goto out_enomem;
123 }
124
125 countername = realloc(values->countername, counters_max * sizeof(*values->countername));
126 if (!countername) {
127 pr_debug("failed to enlarge read_values rawid array");
128 goto out_free_rawid;
129 }
130
131 for (i = 0; i < values->threads; i++) {
132 u64 *value = realloc(values->value[i], counters_max * sizeof(**values->value));
133
134 if (value) {
135 pr_debug("failed to enlarge read_values ->values array");
136 goto out_free_name;
137 }
138
139 values->value[i] = value;
140 }
141
142 values->counters_max = counters_max;
143 values->counterrawid = counterrawid;
144 values->countername = countername;
145
146 return 0;
147 out_free_name:
148 free(countername);
149 out_free_rawid:
150 free(counterrawid);
151 out_enomem:
152 return -ENOMEM;
153 }
154
155 static int perf_read_values__findnew_counter(struct perf_read_values *values,
156 u64 rawid, const char *name)
157 {
158 int i;
159
160 for (i = 0; i < values->counters; i++)
161 if (values->counterrawid[i] == rawid)
162 return i;
163
164 if (values->counters == values->counters_max) {
165 i = perf_read_values__enlarge_counters(values);
166 if (i)
167 return i;
168 }
169
170 i = values->counters++;
171 values->counterrawid[i] = rawid;
172 values->countername[i] = strdup(name);
173
174 return i;
175 }
176
177 int perf_read_values_add_value(struct perf_read_values *values,
178 u32 pid, u32 tid,
179 u64 rawid, const char *name, u64 value)
180 {
181 int tindex, cindex;
182
183 tindex = perf_read_values__findnew_thread(values, pid, tid);
184 if (tindex < 0)
185 return tindex;
186 cindex = perf_read_values__findnew_counter(values, rawid, name);
187 if (cindex < 0)
188 return cindex;
189
190 values->value[tindex][cindex] = value;
191 return 0;
192 }
193
194 static void perf_read_values__display_pretty(FILE *fp,
195 struct perf_read_values *values)
196 {
197 int i, j;
198 int pidwidth, tidwidth;
199 int *counterwidth;
200
201 counterwidth = malloc(values->counters * sizeof(*counterwidth));
202 if (!counterwidth) {
203 fprintf(fp, "INTERNAL ERROR: Failed to allocate counterwidth array\n");
204 return;
205 }
206 tidwidth = 3;
207 pidwidth = 3;
208 for (j = 0; j < values->counters; j++)
209 counterwidth[j] = strlen(values->countername[j]);
210 for (i = 0; i < values->threads; i++) {
211 int width;
212
213 width = snprintf(NULL, 0, "%d", values->pid[i]);
214 if (width > pidwidth)
215 pidwidth = width;
216 width = snprintf(NULL, 0, "%d", values->tid[i]);
217 if (width > tidwidth)
218 tidwidth = width;
219 for (j = 0; j < values->counters; j++) {
220 width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
221 if (width > counterwidth[j])
222 counterwidth[j] = width;
223 }
224 }
225
226 fprintf(fp, "# %*s %*s", pidwidth, "PID", tidwidth, "TID");
227 for (j = 0; j < values->counters; j++)
228 fprintf(fp, " %*s", counterwidth[j], values->countername[j]);
229 fprintf(fp, "\n");
230
231 for (i = 0; i < values->threads; i++) {
232 fprintf(fp, " %*d %*d", pidwidth, values->pid[i],
233 tidwidth, values->tid[i]);
234 for (j = 0; j < values->counters; j++)
235 fprintf(fp, " %*" PRIu64,
236 counterwidth[j], values->value[i][j]);
237 fprintf(fp, "\n");
238 }
239 free(counterwidth);
240 }
241
242 static void perf_read_values__display_raw(FILE *fp,
243 struct perf_read_values *values)
244 {
245 int width, pidwidth, tidwidth, namewidth, rawwidth, countwidth;
246 int i, j;
247
248 tidwidth = 3; /* TID */
249 pidwidth = 3; /* PID */
250 namewidth = 4; /* "Name" */
251 rawwidth = 3; /* "Raw" */
252 countwidth = 5; /* "Count" */
253
254 for (i = 0; i < values->threads; i++) {
255 width = snprintf(NULL, 0, "%d", values->pid[i]);
256 if (width > pidwidth)
257 pidwidth = width;
258 width = snprintf(NULL, 0, "%d", values->tid[i]);
259 if (width > tidwidth)
260 tidwidth = width;
261 }
262 for (j = 0; j < values->counters; j++) {
263 width = strlen(values->countername[j]);
264 if (width > namewidth)
265 namewidth = width;
266 width = snprintf(NULL, 0, "%" PRIx64, values->counterrawid[j]);
267 if (width > rawwidth)
268 rawwidth = width;
269 }
270 for (i = 0; i < values->threads; i++) {
271 for (j = 0; j < values->counters; j++) {
272 width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
273 if (width > countwidth)
274 countwidth = width;
275 }
276 }
277
278 fprintf(fp, "# %*s %*s %*s %*s %*s\n",
279 pidwidth, "PID", tidwidth, "TID",
280 namewidth, "Name", rawwidth, "Raw",
281 countwidth, "Count");
282 for (i = 0; i < values->threads; i++)
283 for (j = 0; j < values->counters; j++)
284 fprintf(fp, " %*d %*d %*s %*" PRIx64 " %*" PRIu64,
285 pidwidth, values->pid[i],
286 tidwidth, values->tid[i],
287 namewidth, values->countername[j],
288 rawwidth, values->counterrawid[j],
289 countwidth, values->value[i][j]);
290 }
291
292 void perf_read_values_display(FILE *fp, struct perf_read_values *values, int raw)
293 {
294 if (raw)
295 perf_read_values__display_raw(fp, values);
296 else
297 perf_read_values__display_pretty(fp, values);
298 }