]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/perf/util/values.c
Merge tag 'drm-misc-fixes-2017-07-27' of git://anongit.freedesktop.org/git/drm-misc...
[mirror_ubuntu-bionic-kernel.git] / tools / perf / util / values.c
CommitLineData
9c0899f1
ACM
1#include <inttypes.h>
2#include <stdio.h>
8d513270 3#include <stdlib.h>
9c0899f1 4#include <errno.h>
8d513270
BG
5
6#include "util.h"
7#include "values.h"
89973506 8#include "debug.h"
8d513270 9
89973506 10int perf_read_values_init(struct perf_read_values *values)
8d513270
BG
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));
89973506
ACM
16 if (!values->pid || !values->tid || !values->value) {
17 pr_debug("failed to allocate read_values threads arrays");
18 goto out_free_pid;
19 }
8d513270
BG
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));
89973506
ACM
27 if (!values->counterrawid || !values->countername) {
28 pr_debug("failed to allocate read_values counters arrays");
29 goto out_free_counter;
30 }
8d513270 31 values->counters = 0;
89973506
ACM
32
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;
8d513270
BG
43}
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++)
74cf249d
ACM
53 zfree(&values->value[i]);
54 zfree(&values->value);
55 zfree(&values->pid);
56 zfree(&values->tid);
57 zfree(&values->counterrawid);
8d513270 58 for (i = 0; i < values->counters; i++)
74cf249d
ACM
59 zfree(&values->countername[i]);
60 zfree(&values->countername);
8d513270
BG
61}
62
89973506 63static int perf_read_values__enlarge_threads(struct perf_read_values *values)
8d513270 64{
89973506
ACM
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;
78out_err:
79 free(npid);
80 free(ntid);
81 free(nvalue);
82 pr_debug("failed to enlarge read_values threads arrays");
83 return -ENOMEM;
8d513270
BG
84}
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
89973506
ACM
95 if (values->threads == values->threads_max) {
96 i = perf_read_values__enlarge_threads(values);
97 if (i < 0)
98 return i;
99 }
8d513270 100
89973506
ACM
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 }
8d513270
BG
107 values->pid[i] = pid;
108 values->tid[i] = tid;
89973506 109 values->threads = i;
8d513270
BG
110
111 return i;
112}
113
f05082b5 114static int perf_read_values__enlarge_counters(struct perf_read_values *values)
8d513270 115{
f05082b5
ACM
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 }
8d513270 124
f05082b5
ACM
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 }
8d513270
BG
130
131 for (i = 0; i < values->threads; i++) {
f05082b5
ACM
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;
8d513270 140 }
f05082b5
ACM
141
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;
8d513270
BG
153}
154
155static int perf_read_values__findnew_counter(struct perf_read_values *values,
83a0944f 156 u64 rawid, const char *name)
8d513270
BG
157{
158 int i;
159
160 for (i = 0; i < values->counters; i++)
161 if (values->counterrawid[i] == rawid)
162 return i;
163
f05082b5
ACM
164 if (values->counters == values->counters_max) {
165 i = perf_read_values__enlarge_counters(values);
166 if (i)
167 return i;
168 }
8d513270
BG
169
170 i = values->counters++;
171 values->counterrawid[i] = rawid;
172 values->countername[i] = strdup(name);
173
174 return i;
175}
176
89973506 177int perf_read_values_add_value(struct perf_read_values *values,
8d513270 178 u32 pid, u32 tid,
83a0944f 179 u64 rawid, const char *name, u64 value)
8d513270
BG
180{
181 int tindex, cindex;
182
183 tindex = perf_read_values__findnew_thread(values, pid, tid);
89973506
ACM
184 if (tindex < 0)
185 return tindex;
8d513270 186 cindex = perf_read_values__findnew_counter(values, rawid, name);
89973506
ACM
187 if (cindex < 0)
188 return cindex;
8d513270
BG
189
190 values->value[tindex][cindex] = value;
89973506 191 return 0;
8d513270
BG
192}
193
9f866697
BG
194static void perf_read_values__display_pretty(FILE *fp,
195 struct perf_read_values *values)
8d513270
BG
196{
197 int i, j;
198 int pidwidth, tidwidth;
199 int *counterwidth;
200
201 counterwidth = malloc(values->counters * sizeof(*counterwidth));
9c0899f1
ACM
202 if (!counterwidth) {
203 fprintf(fp, "INTERNAL ERROR: Failed to allocate counterwidth array\n");
204 return;
205 }
8d513270
BG
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++) {
9486aa38 220 width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
8d513270
BG
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++)
9486aa38 235 fprintf(fp, " %*" PRIu64,
8d513270
BG
236 counterwidth[j], values->value[i][j]);
237 fprintf(fp, "\n");
238 }
8d9e5039 239 free(counterwidth);
8d513270 240}
9f866697
BG
241
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;
9486aa38 266 width = snprintf(NULL, 0, "%" PRIx64, values->counterrawid[j]);
9f866697
BG
267 if (width > rawwidth)
268 rawwidth = width;
269 }
270 for (i = 0; i < values->threads; i++) {
271 for (j = 0; j < values->counters; j++) {
9486aa38 272 width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
9f866697
BG
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++)
9486aa38 284 fprintf(fp, " %*d %*d %*s %*" PRIx64 " %*" PRIu64,
9f866697
BG
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
83a0944f 292void perf_read_values_display(FILE *fp, struct perf_read_values *values, int raw)
9f866697
BG
293{
294 if (raw)
295 perf_read_values__display_raw(fp, values);
296 else
297 perf_read_values__display_pretty(fp, values);
298}