Arnaldo Carvalho de Melo | 9c0899f | 2017-04-04 12:11:07 -0300 | [diff] [blame] | 1 | #include <inttypes.h> |
| 2 | #include <stdio.h> |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 3 | #include <stdlib.h> |
Arnaldo Carvalho de Melo | 9c0899f | 2017-04-04 12:11:07 -0300 | [diff] [blame] | 4 | #include <errno.h> |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 5 | |
| 6 | #include "util.h" |
| 7 | #include "values.h" |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 8 | #include "debug.h" |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 9 | |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 10 | int perf_read_values_init(struct perf_read_values *values) |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 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)); |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 16 | if (!values->pid || !values->tid || !values->value) { |
| 17 | pr_debug("failed to allocate read_values threads arrays"); |
| 18 | goto out_free_pid; |
| 19 | } |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 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)); |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 27 | if (!values->counterrawid || !values->countername) { |
| 28 | pr_debug("failed to allocate read_values counters arrays"); |
| 29 | goto out_free_counter; |
| 30 | } |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 31 | values->counters = 0; |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 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; |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 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++) |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 53 | zfree(&values->value[i]); |
| 54 | zfree(&values->value); |
| 55 | zfree(&values->pid); |
| 56 | zfree(&values->tid); |
| 57 | zfree(&values->counterrawid); |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 58 | for (i = 0; i < values->counters; i++) |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 59 | zfree(&values->countername[i]); |
| 60 | zfree(&values->countername); |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 63 | static int perf_read_values__enlarge_threads(struct perf_read_values *values) |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 64 | { |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 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; |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 95 | if (values->threads == values->threads_max) { |
| 96 | i = perf_read_values__enlarge_threads(values); |
| 97 | if (i < 0) |
| 98 | return i; |
| 99 | } |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 100 | |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 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 | } |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 107 | values->pid[i] = pid; |
| 108 | values->tid[i] = tid; |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 109 | values->threads = i; |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 110 | |
| 111 | return i; |
| 112 | } |
| 113 | |
Arnaldo Carvalho de Melo | f05082b | 2017-04-04 12:05:37 -0300 | [diff] [blame] | 114 | static int perf_read_values__enlarge_counters(struct perf_read_values *values) |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 115 | { |
Arnaldo Carvalho de Melo | f05082b | 2017-04-04 12:05:37 -0300 | [diff] [blame] | 116 | char **countername; |
| 117 | int i, counters_max = values->counters_max * 2; |
| 118 | u64 *counterrawid = realloc(values->counterrawid, counters_max * sizeof(*values->counterrawid)); |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 119 | |
Arnaldo Carvalho de Melo | f05082b | 2017-04-04 12:05:37 -0300 | [diff] [blame] | 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 | } |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 130 | |
| 131 | for (i = 0; i < values->threads; i++) { |
Arnaldo Carvalho de Melo | f05082b | 2017-04-04 12:05:37 -0300 | [diff] [blame] | 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; |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 140 | } |
Arnaldo Carvalho de Melo | f05082b | 2017-04-04 12:05:37 -0300 | [diff] [blame] | 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; |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int perf_read_values__findnew_counter(struct perf_read_values *values, |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 156 | u64 rawid, const char *name) |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 157 | { |
| 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 Melo | f05082b | 2017-04-04 12:05:37 -0300 | [diff] [blame] | 164 | if (values->counters == values->counters_max) { |
| 165 | i = perf_read_values__enlarge_counters(values); |
| 166 | if (i) |
| 167 | return i; |
| 168 | } |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 169 | |
| 170 | i = values->counters++; |
| 171 | values->counterrawid[i] = rawid; |
| 172 | values->countername[i] = strdup(name); |
| 173 | |
| 174 | return i; |
| 175 | } |
| 176 | |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 177 | int perf_read_values_add_value(struct perf_read_values *values, |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 178 | u32 pid, u32 tid, |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 179 | u64 rawid, const char *name, u64 value) |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 180 | { |
| 181 | int tindex, cindex; |
| 182 | |
| 183 | tindex = perf_read_values__findnew_thread(values, pid, tid); |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 184 | if (tindex < 0) |
| 185 | return tindex; |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 186 | cindex = perf_read_values__findnew_counter(values, rawid, name); |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 187 | if (cindex < 0) |
| 188 | return cindex; |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 189 | |
| 190 | values->value[tindex][cindex] = value; |
Arnaldo Carvalho de Melo | 8997350 | 2016-10-14 18:23:11 -0300 | [diff] [blame] | 191 | return 0; |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Brice Goglin | 9f86669 | 2009-08-10 15:26:32 +0200 | [diff] [blame] | 194 | static void perf_read_values__display_pretty(FILE *fp, |
| 195 | struct perf_read_values *values) |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 196 | { |
| 197 | int i, j; |
| 198 | int pidwidth, tidwidth; |
| 199 | int *counterwidth; |
| 200 | |
| 201 | counterwidth = malloc(values->counters * sizeof(*counterwidth)); |
Arnaldo Carvalho de Melo | 9c0899f | 2017-04-04 12:11:07 -0300 | [diff] [blame] | 202 | if (!counterwidth) { |
| 203 | fprintf(fp, "INTERNAL ERROR: Failed to allocate counterwidth array\n"); |
| 204 | return; |
| 205 | } |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 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++) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 220 | width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]); |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 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++) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 235 | fprintf(fp, " %*" PRIu64, |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 236 | counterwidth[j], values->value[i][j]); |
| 237 | fprintf(fp, "\n"); |
| 238 | } |
Alexander Beregalov | 8d9e503 | 2010-01-07 19:40:47 +0300 | [diff] [blame] | 239 | free(counterwidth); |
Brice Goglin | 8d51327 | 2009-08-07 13:55:24 +0200 | [diff] [blame] | 240 | } |
Brice Goglin | 9f86669 | 2009-08-10 15:26:32 +0200 | [diff] [blame] | 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; |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 266 | width = snprintf(NULL, 0, "%" PRIx64, values->counterrawid[j]); |
Brice Goglin | 9f86669 | 2009-08-10 15:26:32 +0200 | [diff] [blame] | 267 | 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 Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 272 | width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]); |
Brice Goglin | 9f86669 | 2009-08-10 15:26:32 +0200 | [diff] [blame] | 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++) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 284 | fprintf(fp, " %*d %*d %*s %*" PRIx64 " %*" PRIu64, |
Brice Goglin | 9f86669 | 2009-08-10 15:26:32 +0200 | [diff] [blame] | 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 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 292 | void perf_read_values_display(FILE *fp, struct perf_read_values *values, int raw) |
Brice Goglin | 9f86669 | 2009-08-10 15:26:32 +0200 | [diff] [blame] | 293 | { |
| 294 | if (raw) |
| 295 | perf_read_values__display_raw(fp, values); |
| 296 | else |
| 297 | perf_read_values__display_pretty(fp, values); |
| 298 | } |