linux/tools/perf/util/expr.c
Kajol Jain 1e1a873dc6 perf metricgroups: Enhance JSON/metric infrastructure to handle "?"
Patch enhances current metric infrastructure to handle "?" in the metric
expression. The "?" can be use for parameters whose value not known
while creating metric events and which can be replace later at runtime
to the proper value. It also add flexibility to create multiple events
out of single metric event added in JSON file.

Patch adds function 'arch_get_runtimeparam' which is a arch specific
function, returns the count of metric events need to be created.  By
default it return 1.

This infrastructure needed for hv_24x7 socket/chip level events.
"hv_24x7" chip level events needs specific chip-id to which the data is
requested. Function 'arch_get_runtimeparam' implemented in header.c
which extract number of sockets from sysfs file "sockets" under
"/sys/devices/hv_24x7/interface/".

With this patch basically we are trying to create as many metric events
as define by runtime_param.

For that one loop is added in function 'metricgroup__add_metric', which
create multiple events at run time depend on return value of
'arch_get_runtimeparam' and merge that event in 'group_list'.

To achieve that we are actually passing this parameter value as part of
`expr__find_other` function and changing "?" present in metric
expression with this value.

As in our JSON file, there gonna be single metric event, and out of
which we are creating multiple events.

To understand which data count belongs to which parameter value,
we also printing param value in generic_metric function.

For example,

  command:# ./perf stat  -M PowerBUS_Frequency -C 0 -I 1000
    1.000101867  9,356,933  hv_24x7/pm_pb_cyc,chip=0/ #  2.3 GHz  PowerBUS_Frequency_0
    1.000101867  9,366,134  hv_24x7/pm_pb_cyc,chip=1/ #  2.3 GHz  PowerBUS_Frequency_1
    2.000314878  9,365,868  hv_24x7/pm_pb_cyc,chip=0/ #  2.3 GHz  PowerBUS_Frequency_0
    2.000314878  9,366,092  hv_24x7/pm_pb_cyc,chip=1/ #  2.3 GHz  PowerBUS_Frequency_1

So, here _0 and _1 after PowerBUS_Frequency specify parameter value.

Signed-off-by: Kajol Jain <kjain@linux.ibm.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Anju T Sudhakar <anju@linux.vnet.ibm.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Joe Mario <jmario@redhat.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Madhavan Srinivasan <maddy@linux.vnet.ibm.com>
Cc: Mamatha Inamdar <mamatha4@linux.vnet.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paul Mackerras <paulus@ozlabs.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linuxppc-dev@lists.ozlabs.org
Link: http://lore.kernel.org/lkml/20200401203340.31402-5-kjain@linux.ibm.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-04-30 10:48:33 -03:00

116 lines
2.2 KiB
C

// SPDX-License-Identifier: GPL-2.0
#include <stdbool.h>
#include <assert.h>
#include "expr.h"
#include "expr-bison.h"
#include "expr-flex.h"
#ifdef PARSER_DEBUG
extern int expr_debug;
#endif
/* Caller must make sure id is allocated */
void expr__add_id(struct expr_parse_ctx *ctx, const char *name, double val)
{
int idx;
assert(ctx->num_ids < MAX_PARSE_ID);
idx = ctx->num_ids++;
ctx->ids[idx].name = name;
ctx->ids[idx].val = val;
}
void expr__ctx_init(struct expr_parse_ctx *ctx)
{
ctx->num_ids = 0;
}
static int
__expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
int start, int runtime)
{
struct expr_scanner_ctx scanner_ctx = {
.start_token = start,
.runtime = runtime,
};
YY_BUFFER_STATE buffer;
void *scanner;
int ret;
ret = expr_lex_init_extra(&scanner_ctx, &scanner);
if (ret)
return ret;
buffer = expr__scan_string(expr, scanner);
#ifdef PARSER_DEBUG
expr_debug = 1;
#endif
ret = expr_parse(val, ctx, scanner);
expr__flush_buffer(buffer, scanner);
expr__delete_buffer(buffer, scanner);
expr_lex_destroy(scanner);
return ret;
}
int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr, int runtime)
{
return __expr__parse(final_val, ctx, expr, EXPR_PARSE, runtime) ? -1 : 0;
}
static bool
already_seen(const char *val, const char *one, const char **other,
int num_other)
{
int i;
if (one && !strcasecmp(one, val))
return true;
for (i = 0; i < num_other; i++)
if (!strcasecmp(other[i], val))
return true;
return false;
}
int expr__find_other(const char *expr, const char *one, const char ***other,
int *num_other, int runtime)
{
int err, i = 0, j = 0;
struct expr_parse_ctx ctx;
expr__ctx_init(&ctx);
err = __expr__parse(NULL, &ctx, expr, EXPR_OTHER, runtime);
if (err)
return -1;
*other = malloc((ctx.num_ids + 1) * sizeof(char *));
if (!*other)
return -ENOMEM;
for (i = 0, j = 0; i < ctx.num_ids; i++) {
const char *str = ctx.ids[i].name;
if (already_seen(str, one, *other, j))
continue;
str = strdup(str);
if (!str)
goto out;
(*other)[j++] = str;
}
(*other)[j] = NULL;
out:
if (i != ctx.num_ids) {
while (--j)
free((char *) (*other)[i]);
free(*other);
err = -1;
}
*num_other = j;
return err;
}