blob: 5de2e15e2eda9c343246431caf0ba68d9559e614 [file] [log] [blame]
Arnaldo Carvalho de Melo9c0899f2017-04-04 12:11:07 -03001#include <inttypes.h>
2#include <stdio.h>
Brice Goglin8d513272009-08-07 13:55:24 +02003#include <stdlib.h>
Arnaldo Carvalho de Melo9c0899f2017-04-04 12:11:07 -03004#include <errno.h>
Brice Goglin8d513272009-08-07 13:55:24 +02005
6#include "util.h"
7#include "values.h"
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -03008#include "debug.h"
Brice Goglin8d513272009-08-07 13:55:24 +02009
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -030010int perf_read_values_init(struct perf_read_values *values)
Brice Goglin8d513272009-08-07 13:55:24 +020011{
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));
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -030016 if (!values->pid || !values->tid || !values->value) {
17 pr_debug("failed to allocate read_values threads arrays");
18 goto out_free_pid;
19 }
Brice Goglin8d513272009-08-07 13:55:24 +020020 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));
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -030027 if (!values->counterrawid || !values->countername) {
28 pr_debug("failed to allocate read_values counters arrays");
29 goto out_free_counter;
30 }
Brice Goglin8d513272009-08-07 13:55:24 +020031 values->counters = 0;
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -030032
33 return 0;
34
35out_free_counter:
36 zfree(&values->counterrawid);
37 zfree(&values->countername);
38out_free_pid:
39 zfree(&values->pid);
40 zfree(&values->tid);
41 zfree(&values->value);
42 return -ENOMEM;
Brice Goglin8d513272009-08-07 13:55:24 +020043}
44
45void 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++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -030053 zfree(&values->value[i]);
54 zfree(&values->value);
55 zfree(&values->pid);
56 zfree(&values->tid);
57 zfree(&values->counterrawid);
Brice Goglin8d513272009-08-07 13:55:24 +020058 for (i = 0; i < values->counters; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -030059 zfree(&values->countername[i]);
60 zfree(&values->countername);
Brice Goglin8d513272009-08-07 13:55:24 +020061}
62
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -030063static int perf_read_values__enlarge_threads(struct perf_read_values *values)
Brice Goglin8d513272009-08-07 13:55:24 +020064{
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -030065 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;
78out_err:
79 free(npid);
80 free(ntid);
81 free(nvalue);
82 pr_debug("failed to enlarge read_values threads arrays");
83 return -ENOMEM;
Brice Goglin8d513272009-08-07 13:55:24 +020084}
85
86static 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
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -030095 if (values->threads == values->threads_max) {
96 i = perf_read_values__enlarge_threads(values);
97 if (i < 0)
98 return i;
99 }
Brice Goglin8d513272009-08-07 13:55:24 +0200100
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -0300101 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 }
Brice Goglin8d513272009-08-07 13:55:24 +0200107 values->pid[i] = pid;
108 values->tid[i] = tid;
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -0300109 values->threads = i;
Brice Goglin8d513272009-08-07 13:55:24 +0200110
111 return i;
112}
113
Arnaldo Carvalho de Melof05082b2017-04-04 12:05:37 -0300114static int perf_read_values__enlarge_counters(struct perf_read_values *values)
Brice Goglin8d513272009-08-07 13:55:24 +0200115{
Arnaldo Carvalho de Melof05082b2017-04-04 12:05:37 -0300116 char **countername;
117 int i, counters_max = values->counters_max * 2;
118 u64 *counterrawid = realloc(values->counterrawid, counters_max * sizeof(*values->counterrawid));
Brice Goglin8d513272009-08-07 13:55:24 +0200119
Arnaldo Carvalho de Melof05082b2017-04-04 12:05:37 -0300120 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 }
Brice Goglin8d513272009-08-07 13:55:24 +0200130
131 for (i = 0; i < values->threads; i++) {
Arnaldo Carvalho de Melof05082b2017-04-04 12:05:37 -0300132 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;
Brice Goglin8d513272009-08-07 13:55:24 +0200140 }
Arnaldo Carvalho de Melof05082b2017-04-04 12:05:37 -0300141
142 values->counters_max = counters_max;
143 values->counterrawid = counterrawid;
144 values->countername = countername;
145
146 return 0;
147out_free_name:
148 free(countername);
149out_free_rawid:
150 free(counterrawid);
151out_enomem:
152 return -ENOMEM;
Brice Goglin8d513272009-08-07 13:55:24 +0200153}
154
155static int perf_read_values__findnew_counter(struct perf_read_values *values,
Ingo Molnar83a09442009-08-15 12:26:57 +0200156 u64 rawid, const char *name)
Brice Goglin8d513272009-08-07 13:55:24 +0200157{
158 int i;
159
160 for (i = 0; i < values->counters; i++)
161 if (values->counterrawid[i] == rawid)
162 return i;
163
Arnaldo Carvalho de Melof05082b2017-04-04 12:05:37 -0300164 if (values->counters == values->counters_max) {
165 i = perf_read_values__enlarge_counters(values);
166 if (i)
167 return i;
168 }
Brice Goglin8d513272009-08-07 13:55:24 +0200169
170 i = values->counters++;
171 values->counterrawid[i] = rawid;
172 values->countername[i] = strdup(name);
173
174 return i;
175}
176
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -0300177int perf_read_values_add_value(struct perf_read_values *values,
Brice Goglin8d513272009-08-07 13:55:24 +0200178 u32 pid, u32 tid,
Ingo Molnar83a09442009-08-15 12:26:57 +0200179 u64 rawid, const char *name, u64 value)
Brice Goglin8d513272009-08-07 13:55:24 +0200180{
181 int tindex, cindex;
182
183 tindex = perf_read_values__findnew_thread(values, pid, tid);
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -0300184 if (tindex < 0)
185 return tindex;
Brice Goglin8d513272009-08-07 13:55:24 +0200186 cindex = perf_read_values__findnew_counter(values, rawid, name);
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -0300187 if (cindex < 0)
188 return cindex;
Brice Goglin8d513272009-08-07 13:55:24 +0200189
190 values->value[tindex][cindex] = value;
Arnaldo Carvalho de Melo89973502016-10-14 18:23:11 -0300191 return 0;
Brice Goglin8d513272009-08-07 13:55:24 +0200192}
193
Brice Goglin9f866692009-08-10 15:26:32 +0200194static void perf_read_values__display_pretty(FILE *fp,
195 struct perf_read_values *values)
Brice Goglin8d513272009-08-07 13:55:24 +0200196{
197 int i, j;
198 int pidwidth, tidwidth;
199 int *counterwidth;
200
201 counterwidth = malloc(values->counters * sizeof(*counterwidth));
Arnaldo Carvalho de Melo9c0899f2017-04-04 12:11:07 -0300202 if (!counterwidth) {
203 fprintf(fp, "INTERNAL ERROR: Failed to allocate counterwidth array\n");
204 return;
205 }
Brice Goglin8d513272009-08-07 13:55:24 +0200206 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++) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200220 width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
Brice Goglin8d513272009-08-07 13:55:24 +0200221 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++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200235 fprintf(fp, " %*" PRIu64,
Brice Goglin8d513272009-08-07 13:55:24 +0200236 counterwidth[j], values->value[i][j]);
237 fprintf(fp, "\n");
238 }
Alexander Beregalov8d9e5032010-01-07 19:40:47 +0300239 free(counterwidth);
Brice Goglin8d513272009-08-07 13:55:24 +0200240}
Brice Goglin9f866692009-08-10 15:26:32 +0200241
242static 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;
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200266 width = snprintf(NULL, 0, "%" PRIx64, values->counterrawid[j]);
Brice Goglin9f866692009-08-10 15:26:32 +0200267 if (width > rawwidth)
268 rawwidth = width;
269 }
270 for (i = 0; i < values->threads; i++) {
271 for (j = 0; j < values->counters; j++) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200272 width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
Brice Goglin9f866692009-08-10 15:26:32 +0200273 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++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200284 fprintf(fp, " %*d %*d %*s %*" PRIx64 " %*" PRIu64,
Brice Goglin9f866692009-08-10 15:26:32 +0200285 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
Ingo Molnar83a09442009-08-15 12:26:57 +0200292void perf_read_values_display(FILE *fp, struct perf_read_values *values, int raw)
Brice Goglin9f866692009-08-10 15:26:32 +0200293{
294 if (raw)
295 perf_read_values__display_raw(fp, values);
296 else
297 perf_read_values__display_pretty(fp, values);
298}