2020-02-28 17:36:12 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2020-02-28 17:36:13 +08:00
|
|
|
#include <stdbool.h>
|
2020-02-28 17:36:12 +08:00
|
|
|
#include <assert.h>
|
2020-06-23 21:09:03 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-07-20 02:13:11 +08:00
|
|
|
#include "metricgroup.h"
|
|
|
|
#include "debug.h"
|
2020-02-28 17:36:12 +08:00
|
|
|
#include "expr.h"
|
2020-02-28 17:36:13 +08:00
|
|
|
#include "expr-bison.h"
|
|
|
|
#include "expr-flex.h"
|
2020-05-16 06:17:32 +08:00
|
|
|
#include <linux/kernel.h>
|
2020-07-20 02:13:11 +08:00
|
|
|
#include <linux/zalloc.h>
|
|
|
|
#include <ctype.h>
|
2020-02-28 17:36:13 +08:00
|
|
|
|
|
|
|
#ifdef PARSER_DEBUG
|
|
|
|
extern int expr_debug;
|
|
|
|
#endif
|
2020-02-28 17:36:12 +08:00
|
|
|
|
2020-05-16 06:17:32 +08:00
|
|
|
static size_t key_hash(const void *key, void *ctx __maybe_unused)
|
|
|
|
{
|
|
|
|
const char *str = (const char *)key;
|
|
|
|
size_t hash = 0;
|
|
|
|
|
|
|
|
while (*str != '\0') {
|
|
|
|
hash *= 31;
|
|
|
|
hash += *str;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool key_equal(const void *key1, const void *key2,
|
|
|
|
void *ctx __maybe_unused)
|
|
|
|
{
|
|
|
|
return !strcmp((const char *)key1, (const char *)key2);
|
|
|
|
}
|
|
|
|
|
2020-07-20 02:13:03 +08:00
|
|
|
/* Caller must make sure id is allocated */
|
|
|
|
int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
|
|
|
|
{
|
|
|
|
struct expr_id_data *data_ptr = NULL, *old_data = NULL;
|
|
|
|
char *old_key = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
data_ptr = malloc(sizeof(*data_ptr));
|
|
|
|
if (!data_ptr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-07-20 02:13:16 +08:00
|
|
|
data_ptr->parent = ctx->parent;
|
|
|
|
|
2020-07-20 02:13:03 +08:00
|
|
|
ret = hashmap__set(&ctx->ids, id, data_ptr,
|
|
|
|
(const void **)&old_key, (void **)&old_data);
|
|
|
|
if (ret)
|
|
|
|
free(data_ptr);
|
|
|
|
free(old_key);
|
|
|
|
free(old_data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-28 17:36:12 +08:00
|
|
|
/* Caller must make sure id is allocated */
|
2020-07-12 21:26:18 +08:00
|
|
|
int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
|
2020-02-28 17:36:12 +08:00
|
|
|
{
|
2020-07-12 21:26:18 +08:00
|
|
|
struct expr_id_data *data_ptr = NULL, *old_data = NULL;
|
2020-05-16 06:17:32 +08:00
|
|
|
char *old_key = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2020-07-20 02:13:03 +08:00
|
|
|
data_ptr = malloc(sizeof(*data_ptr));
|
|
|
|
if (!data_ptr)
|
|
|
|
return -ENOMEM;
|
|
|
|
data_ptr->val = val;
|
2020-07-20 02:13:11 +08:00
|
|
|
data_ptr->is_ref = false;
|
2020-07-20 02:13:03 +08:00
|
|
|
|
2020-07-12 21:26:18 +08:00
|
|
|
ret = hashmap__set(&ctx->ids, id, data_ptr,
|
|
|
|
(const void **)&old_key, (void **)&old_data);
|
2020-07-20 02:13:02 +08:00
|
|
|
if (ret)
|
|
|
|
free(data_ptr);
|
2020-05-16 06:17:32 +08:00
|
|
|
free(old_key);
|
2020-07-12 21:26:18 +08:00
|
|
|
free(old_data);
|
2020-05-16 06:17:32 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-20 02:13:11 +08:00
|
|
|
int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
|
|
|
|
{
|
|
|
|
struct expr_id_data *data_ptr = NULL, *old_data = NULL;
|
|
|
|
char *old_key = NULL;
|
|
|
|
char *name, *p;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
data_ptr = zalloc(sizeof(*data_ptr));
|
|
|
|
if (!data_ptr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
name = strdup(ref->metric_name);
|
|
|
|
if (!name) {
|
|
|
|
free(data_ptr);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The jevents tool converts all metric expressions
|
|
|
|
* to lowercase, including metric references, hence
|
|
|
|
* we need to add lowercase name for metric, so it's
|
|
|
|
* properly found.
|
|
|
|
*/
|
|
|
|
for (p = name; *p; p++)
|
|
|
|
*p = tolower(*p);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Intentionally passing just const char pointers,
|
|
|
|
* originally from 'struct pmu_event' object.
|
|
|
|
* We don't need to change them, so there's no
|
|
|
|
* need to create our own copy.
|
|
|
|
*/
|
|
|
|
data_ptr->ref.metric_name = ref->metric_name;
|
|
|
|
data_ptr->ref.metric_expr = ref->metric_expr;
|
2020-07-20 02:13:12 +08:00
|
|
|
data_ptr->ref.counted = false;
|
2020-07-20 02:13:11 +08:00
|
|
|
data_ptr->is_ref = true;
|
|
|
|
|
|
|
|
ret = hashmap__set(&ctx->ids, name, data_ptr,
|
|
|
|
(const void **)&old_key, (void **)&old_data);
|
|
|
|
if (ret)
|
|
|
|
free(data_ptr);
|
|
|
|
|
|
|
|
pr_debug2("adding ref metric %s: %s\n",
|
|
|
|
ref->metric_name, ref->metric_expr);
|
|
|
|
|
|
|
|
free(old_key);
|
|
|
|
free(old_data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-20 02:13:04 +08:00
|
|
|
int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
|
|
|
|
struct expr_id_data **data)
|
2020-05-16 06:17:32 +08:00
|
|
|
{
|
2020-07-20 02:13:04 +08:00
|
|
|
return hashmap__find(&ctx->ids, id, (void **)data) ? 0 : -1;
|
2020-02-28 17:36:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-20 02:13:12 +08:00
|
|
|
int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
|
|
|
|
struct expr_id_data **datap)
|
|
|
|
{
|
|
|
|
struct expr_id_data *data;
|
|
|
|
|
|
|
|
if (expr__get_id(ctx, id, datap) || !*datap) {
|
|
|
|
pr_debug("%s not found\n", id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = *datap;
|
|
|
|
|
|
|
|
pr_debug2("lookup: is_ref %d, counted %d, val %f: %s\n",
|
|
|
|
data->is_ref, data->ref.counted, data->val, id);
|
|
|
|
|
|
|
|
if (data->is_ref && !data->ref.counted) {
|
|
|
|
data->ref.counted = true;
|
|
|
|
pr_debug("processing metric: %s ENTRY\n", id);
|
|
|
|
if (expr__parse(&data->val, ctx, data->ref.metric_expr, 1)) {
|
|
|
|
pr_debug("%s failed to count\n", id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pr_debug("processing metric: %s EXIT: %f\n", id, data->val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-20 02:13:05 +08:00
|
|
|
void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
|
|
|
|
{
|
|
|
|
struct expr_id_data *old_val = NULL;
|
|
|
|
char *old_key = NULL;
|
|
|
|
|
|
|
|
hashmap__delete(&ctx->ids, id,
|
|
|
|
(const void **)&old_key, (void **)&old_val);
|
|
|
|
free(old_key);
|
|
|
|
free(old_val);
|
|
|
|
}
|
|
|
|
|
2020-04-02 04:33:34 +08:00
|
|
|
void expr__ctx_init(struct expr_parse_ctx *ctx)
|
2020-02-28 17:36:12 +08:00
|
|
|
{
|
2020-05-16 06:17:32 +08:00
|
|
|
hashmap__init(&ctx->ids, key_hash, key_equal, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expr__ctx_clear(struct expr_parse_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct hashmap_entry *cur;
|
|
|
|
size_t bkt;
|
|
|
|
|
|
|
|
hashmap__for_each_entry((&ctx->ids), cur, bkt) {
|
|
|
|
free((char *)cur->key);
|
|
|
|
free(cur->value);
|
|
|
|
}
|
|
|
|
hashmap__clear(&ctx->ids);
|
2020-02-28 17:36:12 +08:00
|
|
|
}
|
2020-02-28 17:36:13 +08:00
|
|
|
|
|
|
|
static int
|
2020-04-02 04:33:34 +08:00
|
|
|
__expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
|
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-02 04:33:37 +08:00
|
|
|
int start, int runtime)
|
2020-02-28 17:36:13 +08:00
|
|
|
{
|
2020-04-02 04:33:35 +08:00
|
|
|
struct expr_scanner_ctx scanner_ctx = {
|
|
|
|
.start_token = start,
|
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-02 04:33:37 +08:00
|
|
|
.runtime = runtime,
|
2020-04-02 04:33:35 +08:00
|
|
|
};
|
2020-02-28 17:36:13 +08:00
|
|
|
YY_BUFFER_STATE buffer;
|
|
|
|
void *scanner;
|
|
|
|
int ret;
|
|
|
|
|
2020-07-20 02:13:12 +08:00
|
|
|
pr_debug2("parsing metric: %s\n", expr);
|
|
|
|
|
2020-04-02 04:33:35 +08:00
|
|
|
ret = expr_lex_init_extra(&scanner_ctx, &scanner);
|
2020-02-28 17:36:13 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
buffer = expr__scan_string(expr, scanner);
|
|
|
|
|
|
|
|
#ifdef PARSER_DEBUG
|
|
|
|
expr_debug = 1;
|
2020-05-02 01:33:28 +08:00
|
|
|
expr_set_debug(1, scanner);
|
2020-02-28 17:36:13 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = expr_parse(val, ctx, scanner);
|
|
|
|
|
|
|
|
expr__flush_buffer(buffer, scanner);
|
|
|
|
expr__delete_buffer(buffer, scanner);
|
|
|
|
expr_lex_destroy(scanner);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-16 06:17:32 +08:00
|
|
|
int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
|
|
|
|
const char *expr, int runtime)
|
2020-02-28 17:36:13 +08:00
|
|
|
{
|
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-02 04:33:37 +08:00
|
|
|
return __expr__parse(final_val, ctx, expr, EXPR_PARSE, runtime) ? -1 : 0;
|
2020-02-28 17:36:13 +08:00
|
|
|
}
|
|
|
|
|
2020-05-16 06:17:32 +08:00
|
|
|
int expr__find_other(const char *expr, const char *one,
|
|
|
|
struct expr_parse_ctx *ctx, int runtime)
|
2020-02-28 17:36:13 +08:00
|
|
|
{
|
2020-05-16 06:17:32 +08:00
|
|
|
int ret = __expr__parse(NULL, ctx, expr, EXPR_OTHER, runtime);
|
|
|
|
|
2020-07-20 02:13:05 +08:00
|
|
|
if (one)
|
|
|
|
expr__del_id(ctx, one);
|
2020-02-28 17:36:13 +08:00
|
|
|
|
2020-05-16 06:17:32 +08:00
|
|
|
return ret;
|
2020-02-28 17:36:13 +08:00
|
|
|
}
|