2017-04-04 15:11:07 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdio.h>
|
2009-08-07 11:55:24 +00:00
|
|
|
#include <stdlib.h>
|
2017-04-04 15:11:07 +00:00
|
|
|
#include <errno.h>
|
2009-08-07 11:55:24 +00:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "values.h"
|
2016-10-14 21:23:11 +00:00
|
|
|
#include "debug.h"
|
2009-08-07 11:55:24 +00:00
|
|
|
|
2016-10-14 21:23:11 +00:00
|
|
|
int perf_read_values_init(struct perf_read_values *values)
|
2009-08-07 11:55:24 +00:00
|
|
|
{
|
|
|
|
values->threads_max = 16;
|
|
|
|
values->pid = malloc(values->threads_max * sizeof(*values->pid));
|
|
|
|
values->tid = malloc(values->threads_max * sizeof(*values->tid));
|
|
|
|
values->value = malloc(values->threads_max * sizeof(*values->value));
|
2016-10-14 21:23:11 +00:00
|
|
|
if (!values->pid || !values->tid || !values->value) {
|
|
|
|
pr_debug("failed to allocate read_values threads arrays");
|
|
|
|
goto out_free_pid;
|
|
|
|
}
|
2009-08-07 11:55:24 +00:00
|
|
|
values->threads = 0;
|
|
|
|
|
|
|
|
values->counters_max = 16;
|
|
|
|
values->counterrawid = malloc(values->counters_max
|
|
|
|
* sizeof(*values->counterrawid));
|
|
|
|
values->countername = malloc(values->counters_max
|
|
|
|
* sizeof(*values->countername));
|
2016-10-14 21:23:11 +00:00
|
|
|
if (!values->counterrawid || !values->countername) {
|
|
|
|
pr_debug("failed to allocate read_values counters arrays");
|
|
|
|
goto out_free_counter;
|
|
|
|
}
|
2009-08-07 11:55:24 +00:00
|
|
|
values->counters = 0;
|
2016-10-14 21:23:11 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_free_counter:
|
|
|
|
zfree(&values->counterrawid);
|
|
|
|
zfree(&values->countername);
|
|
|
|
out_free_pid:
|
|
|
|
zfree(&values->pid);
|
|
|
|
zfree(&values->tid);
|
|
|
|
zfree(&values->value);
|
|
|
|
return -ENOMEM;
|
2009-08-07 11:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void perf_read_values_destroy(struct perf_read_values *values)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!values->threads_max || !values->counters_max)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < values->threads; i++)
|
2013-12-27 19:55:14 +00:00
|
|
|
zfree(&values->value[i]);
|
|
|
|
zfree(&values->value);
|
|
|
|
zfree(&values->pid);
|
|
|
|
zfree(&values->tid);
|
|
|
|
zfree(&values->counterrawid);
|
2009-08-07 11:55:24 +00:00
|
|
|
for (i = 0; i < values->counters; i++)
|
2013-12-27 19:55:14 +00:00
|
|
|
zfree(&values->countername[i]);
|
|
|
|
zfree(&values->countername);
|
2009-08-07 11:55:24 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 21:23:11 +00:00
|
|
|
static int perf_read_values__enlarge_threads(struct perf_read_values *values)
|
2009-08-07 11:55:24 +00:00
|
|
|
{
|
2016-10-14 21:23:11 +00:00
|
|
|
int nthreads_max = values->threads_max * 2;
|
|
|
|
void *npid = realloc(values->pid, nthreads_max * sizeof(*values->pid)),
|
|
|
|
*ntid = realloc(values->tid, nthreads_max * sizeof(*values->tid)),
|
|
|
|
*nvalue = realloc(values->value, nthreads_max * sizeof(*values->value));
|
|
|
|
|
|
|
|
if (!npid || !ntid || !nvalue)
|
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
values->threads_max = nthreads_max;
|
|
|
|
values->pid = npid;
|
|
|
|
values->tid = ntid;
|
|
|
|
values->value = nvalue;
|
|
|
|
return 0;
|
|
|
|
out_err:
|
|
|
|
free(npid);
|
|
|
|
free(ntid);
|
|
|
|
free(nvalue);
|
|
|
|
pr_debug("failed to enlarge read_values threads arrays");
|
|
|
|
return -ENOMEM;
|
2009-08-07 11:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int perf_read_values__findnew_thread(struct perf_read_values *values,
|
|
|
|
u32 pid, u32 tid)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < values->threads; i++)
|
|
|
|
if (values->pid[i] == pid && values->tid[i] == tid)
|
|
|
|
return i;
|
|
|
|
|
2016-10-14 21:23:11 +00:00
|
|
|
if (values->threads == values->threads_max) {
|
|
|
|
i = perf_read_values__enlarge_threads(values);
|
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
}
|
2009-08-07 11:55:24 +00:00
|
|
|
|
2016-10-14 21:23:11 +00:00
|
|
|
i = values->threads + 1;
|
|
|
|
values->value[i] = malloc(values->counters_max * sizeof(**values->value));
|
|
|
|
if (!values->value[i]) {
|
|
|
|
pr_debug("failed to allocate read_values counters array");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2009-08-07 11:55:24 +00:00
|
|
|
values->pid[i] = pid;
|
|
|
|
values->tid[i] = tid;
|
2016-10-14 21:23:11 +00:00
|
|
|
values->threads = i;
|
2009-08-07 11:55:24 +00:00
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-04-04 15:05:37 +00:00
|
|
|
static int perf_read_values__enlarge_counters(struct perf_read_values *values)
|
2009-08-07 11:55:24 +00:00
|
|
|
{
|
2017-04-04 15:05:37 +00:00
|
|
|
char **countername;
|
|
|
|
int i, counters_max = values->counters_max * 2;
|
|
|
|
u64 *counterrawid = realloc(values->counterrawid, counters_max * sizeof(*values->counterrawid));
|
|
|
|
|
|
|
|
if (!counterrawid) {
|
|
|
|
pr_debug("failed to enlarge read_values rawid array");
|
|
|
|
goto out_enomem;
|
|
|
|
}
|
2009-08-07 11:55:24 +00:00
|
|
|
|
2017-04-04 15:05:37 +00:00
|
|
|
countername = realloc(values->countername, counters_max * sizeof(*values->countername));
|
|
|
|
if (!countername) {
|
|
|
|
pr_debug("failed to enlarge read_values rawid array");
|
|
|
|
goto out_free_rawid;
|
|
|
|
}
|
2009-08-07 11:55:24 +00:00
|
|
|
|
|
|
|
for (i = 0; i < values->threads; i++) {
|
2017-04-04 15:05:37 +00:00
|
|
|
u64 *value = realloc(values->value[i], counters_max * sizeof(**values->value));
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
pr_debug("failed to enlarge read_values ->values array");
|
|
|
|
goto out_free_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
values->value[i] = value;
|
2009-08-07 11:55:24 +00:00
|
|
|
}
|
2017-04-04 15:05:37 +00:00
|
|
|
|
|
|
|
values->counters_max = counters_max;
|
|
|
|
values->counterrawid = counterrawid;
|
|
|
|
values->countername = countername;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
out_free_name:
|
|
|
|
free(countername);
|
|
|
|
out_free_rawid:
|
|
|
|
free(counterrawid);
|
|
|
|
out_enomem:
|
|
|
|
return -ENOMEM;
|
2009-08-07 11:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int perf_read_values__findnew_counter(struct perf_read_values *values,
|
2009-08-15 10:26:57 +00:00
|
|
|
u64 rawid, const char *name)
|
2009-08-07 11:55:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < values->counters; i++)
|
|
|
|
if (values->counterrawid[i] == rawid)
|
|
|
|
return i;
|
|
|
|
|
2017-04-04 15:05:37 +00:00
|
|
|
if (values->counters == values->counters_max) {
|
|
|
|
i = perf_read_values__enlarge_counters(values);
|
|
|
|
if (i)
|
|
|
|
return i;
|
|
|
|
}
|
2009-08-07 11:55:24 +00:00
|
|
|
|
|
|
|
i = values->counters++;
|
|
|
|
values->counterrawid[i] = rawid;
|
|
|
|
values->countername[i] = strdup(name);
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2016-10-14 21:23:11 +00:00
|
|
|
int perf_read_values_add_value(struct perf_read_values *values,
|
2009-08-07 11:55:24 +00:00
|
|
|
u32 pid, u32 tid,
|
2009-08-15 10:26:57 +00:00
|
|
|
u64 rawid, const char *name, u64 value)
|
2009-08-07 11:55:24 +00:00
|
|
|
{
|
|
|
|
int tindex, cindex;
|
|
|
|
|
|
|
|
tindex = perf_read_values__findnew_thread(values, pid, tid);
|
2016-10-14 21:23:11 +00:00
|
|
|
if (tindex < 0)
|
|
|
|
return tindex;
|
2009-08-07 11:55:24 +00:00
|
|
|
cindex = perf_read_values__findnew_counter(values, rawid, name);
|
2016-10-14 21:23:11 +00:00
|
|
|
if (cindex < 0)
|
|
|
|
return cindex;
|
2009-08-07 11:55:24 +00:00
|
|
|
|
|
|
|
values->value[tindex][cindex] = value;
|
2016-10-14 21:23:11 +00:00
|
|
|
return 0;
|
2009-08-07 11:55:24 +00:00
|
|
|
}
|
|
|
|
|
2009-08-10 13:26:32 +00:00
|
|
|
static void perf_read_values__display_pretty(FILE *fp,
|
|
|
|
struct perf_read_values *values)
|
2009-08-07 11:55:24 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int pidwidth, tidwidth;
|
|
|
|
int *counterwidth;
|
|
|
|
|
|
|
|
counterwidth = malloc(values->counters * sizeof(*counterwidth));
|
2017-04-04 15:11:07 +00:00
|
|
|
if (!counterwidth) {
|
|
|
|
fprintf(fp, "INTERNAL ERROR: Failed to allocate counterwidth array\n");
|
|
|
|
return;
|
|
|
|
}
|
2009-08-07 11:55:24 +00:00
|
|
|
tidwidth = 3;
|
|
|
|
pidwidth = 3;
|
|
|
|
for (j = 0; j < values->counters; j++)
|
|
|
|
counterwidth[j] = strlen(values->countername[j]);
|
|
|
|
for (i = 0; i < values->threads; i++) {
|
|
|
|
int width;
|
|
|
|
|
|
|
|
width = snprintf(NULL, 0, "%d", values->pid[i]);
|
|
|
|
if (width > pidwidth)
|
|
|
|
pidwidth = width;
|
|
|
|
width = snprintf(NULL, 0, "%d", values->tid[i]);
|
|
|
|
if (width > tidwidth)
|
|
|
|
tidwidth = width;
|
|
|
|
for (j = 0; j < values->counters; j++) {
|
2011-01-22 22:37:02 +00:00
|
|
|
width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
|
2009-08-07 11:55:24 +00:00
|
|
|
if (width > counterwidth[j])
|
|
|
|
counterwidth[j] = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "# %*s %*s", pidwidth, "PID", tidwidth, "TID");
|
|
|
|
for (j = 0; j < values->counters; j++)
|
|
|
|
fprintf(fp, " %*s", counterwidth[j], values->countername[j]);
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
|
|
|
|
for (i = 0; i < values->threads; i++) {
|
|
|
|
fprintf(fp, " %*d %*d", pidwidth, values->pid[i],
|
|
|
|
tidwidth, values->tid[i]);
|
|
|
|
for (j = 0; j < values->counters; j++)
|
2011-01-22 22:37:02 +00:00
|
|
|
fprintf(fp, " %*" PRIu64,
|
2009-08-07 11:55:24 +00:00
|
|
|
counterwidth[j], values->value[i][j]);
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
}
|
2010-01-07 16:40:47 +00:00
|
|
|
free(counterwidth);
|
2009-08-07 11:55:24 +00:00
|
|
|
}
|
2009-08-10 13:26:32 +00:00
|
|
|
|
|
|
|
static void perf_read_values__display_raw(FILE *fp,
|
|
|
|
struct perf_read_values *values)
|
|
|
|
{
|
|
|
|
int width, pidwidth, tidwidth, namewidth, rawwidth, countwidth;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
tidwidth = 3; /* TID */
|
|
|
|
pidwidth = 3; /* PID */
|
|
|
|
namewidth = 4; /* "Name" */
|
|
|
|
rawwidth = 3; /* "Raw" */
|
|
|
|
countwidth = 5; /* "Count" */
|
|
|
|
|
|
|
|
for (i = 0; i < values->threads; i++) {
|
|
|
|
width = snprintf(NULL, 0, "%d", values->pid[i]);
|
|
|
|
if (width > pidwidth)
|
|
|
|
pidwidth = width;
|
|
|
|
width = snprintf(NULL, 0, "%d", values->tid[i]);
|
|
|
|
if (width > tidwidth)
|
|
|
|
tidwidth = width;
|
|
|
|
}
|
|
|
|
for (j = 0; j < values->counters; j++) {
|
|
|
|
width = strlen(values->countername[j]);
|
|
|
|
if (width > namewidth)
|
|
|
|
namewidth = width;
|
2011-01-22 22:37:02 +00:00
|
|
|
width = snprintf(NULL, 0, "%" PRIx64, values->counterrawid[j]);
|
2009-08-10 13:26:32 +00:00
|
|
|
if (width > rawwidth)
|
|
|
|
rawwidth = width;
|
|
|
|
}
|
|
|
|
for (i = 0; i < values->threads; i++) {
|
|
|
|
for (j = 0; j < values->counters; j++) {
|
2011-01-22 22:37:02 +00:00
|
|
|
width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
|
2009-08-10 13:26:32 +00:00
|
|
|
if (width > countwidth)
|
|
|
|
countwidth = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "# %*s %*s %*s %*s %*s\n",
|
|
|
|
pidwidth, "PID", tidwidth, "TID",
|
|
|
|
namewidth, "Name", rawwidth, "Raw",
|
|
|
|
countwidth, "Count");
|
|
|
|
for (i = 0; i < values->threads; i++)
|
|
|
|
for (j = 0; j < values->counters; j++)
|
2011-01-22 22:37:02 +00:00
|
|
|
fprintf(fp, " %*d %*d %*s %*" PRIx64 " %*" PRIu64,
|
2009-08-10 13:26:32 +00:00
|
|
|
pidwidth, values->pid[i],
|
|
|
|
tidwidth, values->tid[i],
|
|
|
|
namewidth, values->countername[j],
|
|
|
|
rawwidth, values->counterrawid[j],
|
|
|
|
countwidth, values->value[i][j]);
|
|
|
|
}
|
|
|
|
|
2009-08-15 10:26:57 +00:00
|
|
|
void perf_read_values_display(FILE *fp, struct perf_read_values *values, int raw)
|
2009-08-10 13:26:32 +00:00
|
|
|
{
|
|
|
|
if (raw)
|
|
|
|
perf_read_values__display_raw(fp, values);
|
|
|
|
else
|
|
|
|
perf_read_values__display_pretty(fp, values);
|
|
|
|
}
|