2020-05-07 08:16:51

by Ian Rogers

[permalink] [raw]
Subject: [RFC PATCH 0/7] Share events between metrics

Metric groups contain metrics. Metrics create groups of events to
ideally be scheduled together. Often metrics refer to the same events,
for example, a cache hit and cache miss rate. Using separate event
groups means these metrics are multiplexed at different times and the
counts don't sum to 100%. More multiplexing also decreases the
accuracy of the measurement.

This change orders metrics from groups or the command line, so that
the ones with the most events are set up first. Later metrics see if
groups already provide their events, and reuse them if
possible. Unnecessary events and groups are eliminated.

RFC because:
- without this change events within a metric may get scheduled
together, after they may appear as part of a larger group and be
multiplexed at different times, lowering accuracy - however, less
multiplexing may compensate for this.
- libbpf's hashmap is used, however, libbpf is an optional
requirement for building perf.
- other things I'm not thinking of.

Thanks!

Ian Rogers (7):
perf expr: migrate expr ids table to libbpf's hashmap
perf metricgroup: change evlist_used to a bitmap
perf metricgroup: free metric_events on error
perf metricgroup: always place duration_time last
perf metricgroup: delay events string creation
perf metricgroup: order event groups by size
perf metricgroup: remove duped metric group events

tools/perf/tests/expr.c | 32 ++---
tools/perf/tests/pmu-events.c | 22 ++--
tools/perf/util/expr.c | 125 ++++++++++--------
tools/perf/util/expr.h | 22 ++--
tools/perf/util/expr.y | 22 +---
tools/perf/util/metricgroup.c | 242 +++++++++++++++++++++-------------
tools/perf/util/stat-shadow.c | 46 ++++---
7 files changed, 280 insertions(+), 231 deletions(-)

--
2.26.2.526.g744177e7f7-goog


2020-05-07 08:16:54

by Ian Rogers

[permalink] [raw]
Subject: [RFC PATCH 3/7] perf metricgroup: free metric_events on error

Avoid a simple memory leak.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/util/metricgroup.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index dcd175c05872..2356dda92a07 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -185,6 +185,7 @@ static int metricgroup__setup_events(struct list_head *groups,
if (!evsel) {
pr_debug("Cannot resolve %s: %s\n",
eg->metric_name, eg->metric_expr);
+ free(metric_events);
continue;
}
for (i = 0; metric_events[i]; i++)
@@ -192,11 +193,13 @@ static int metricgroup__setup_events(struct list_head *groups,
me = metricgroup__lookup(metric_events_list, evsel, true);
if (!me) {
ret = -ENOMEM;
+ free(metric_events);
break;
}
expr = malloc(sizeof(struct metric_expr));
if (!expr) {
ret = -ENOMEM;
+ free(metric_events);
break;
}
expr->metric_expr = eg->metric_expr;
--
2.26.2.526.g744177e7f7-goog

2020-05-07 08:17:04

by Ian Rogers

[permalink] [raw]
Subject: [RFC PATCH 6/7] perf metricgroup: order event groups by size

When adding event groups to the group list, insert them in size order.
This performs an insertion sort on the group list. By placing the
largest groups at the front of the group list it is possible to see if a
larger group contains the same events as a later group. This can make
the later group redundant - it can reuse the events from the large group.
A later patch will add this sharing.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/util/metricgroup.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 0a00c0f87872..25e3e8df6b45 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -519,7 +519,21 @@ static int __metricgroup__add_metric(struct list_head *group_list,
return -EINVAL;
}

- list_add_tail(&eg->nd, group_list);
+ if (list_empty(group_list))
+ list_add(&eg->nd, group_list);
+ else {
+ struct list_head *pos;
+
+ /* Place the largest groups at the front. */
+ list_for_each_prev(pos, group_list) {
+ struct egroup *old = list_entry(pos, struct egroup, nd);
+
+ if (hashmap__size(&eg->pctx.ids) <=
+ hashmap__size(&old->pctx.ids))
+ break;
+ }
+ list_add(&eg->nd, pos);
+ }

return 0;
}
--
2.26.2.526.g744177e7f7-goog

2020-05-07 08:17:08

by Ian Rogers

[permalink] [raw]
Subject: [RFC PATCH 7/7] perf metricgroup: remove duped metric group events

A metric group contains multiple metrics. These metrics may use the same
events. If metrics use separate events then it leads to more
multiplexing and overall metric counts fail to sum to 100%.
Modify how metrics are associated with events so that if the events in
an earlier group satisfy the current metric, the same events are used.
A record of used events is kept and at the end of processing unnecessary
events are eliminated.

Before:
$ perf stat -a -M TopDownL1 sleep 1

Performance counter stats for 'system wide':

920,211,343 uops_issued.any # 0.5 Backend_Bound (16.56%)
1,977,733,128 idq_uops_not_delivered.core (16.56%)
51,668,510 int_misc.recovery_cycles (16.56%)
732,305,692 uops_retired.retire_slots (16.56%)
1,497,621,849 cycles (16.56%)
721,098,274 uops_issued.any # 0.1 Bad_Speculation (16.79%)
1,332,681,791 cycles (16.79%)
552,475,482 uops_retired.retire_slots (16.79%)
47,708,340 int_misc.recovery_cycles (16.79%)
1,383,713,292 cycles
# 0.4 Frontend_Bound (16.76%)
2,013,757,701 idq_uops_not_delivered.core (16.76%)
1,373,363,790 cycles
# 0.1 Retiring (33.54%)
577,302,589 uops_retired.retire_slots (33.54%)
392,766,987 inst_retired.any # 0.3 IPC (50.24%)
1,351,873,350 cpu_clk_unhalted.thread (50.24%)
1,332,510,318 cycles
# 5330041272.0 SLOTS (49.90%)

1.006336145 seconds time elapsed

After:
$ perf stat -a -M TopDownL1 sleep 1

Performance counter stats for 'system wide':

765,949,145 uops_issued.any # 0.1 Bad_Speculation
# 0.5 Backend_Bound (50.09%)
1,883,830,591 idq_uops_not_delivered.core # 0.3 Frontend_Bound (50.09%)
48,237,080 int_misc.recovery_cycles (50.09%)
581,798,385 uops_retired.retire_slots # 0.1 Retiring (50.09%)
1,361,628,527 cycles
# 5446514108.0 SLOTS (50.09%)
391,415,714 inst_retired.any # 0.3 IPC (49.91%)
1,336,486,781 cpu_clk_unhalted.thread (49.91%)

1.005469298 seconds time elapsed

Note: Bad_Speculation + Backend_Bound + Frontend_Bound + Retiring = 100%
after, where as before it is 110%. After there are 2 groups, whereas
before there are 6. After the cycles event appears once, before it
appeared 5 times.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/util/metricgroup.c | 97 ++++++++++++++++++++++-------------
1 file changed, 61 insertions(+), 36 deletions(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 25e3e8df6b45..8bb2aeeb70ad 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -93,44 +93,72 @@ struct egroup {
bool has_constraint;
};

+/**
+ * Find a group of events in perf_evlist that correpond to those from a parsed
+ * metric expression.
+ * @perf_evlist: a list of events something like: {metric1 leader, metric1
+ * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling,
+ * metric2 sibling}:W,duration_time
+ * @pctx: the parse context for the metric expression.
+ * @has_constraint: is there a contraint on the group of events? In which case
+ * the events won't be grouped.
+ * @metric_events: out argument, null terminated array of evsel's associated
+ * with the metric.
+ * @evlist_used: in/out argument, bitmap tracking which evlist events are used.
+ * @return the first metric event or NULL on failure.
+ */
static struct evsel *find_evsel_group(struct evlist *perf_evlist,
struct expr_parse_ctx *pctx,
+ bool has_constraint,
struct evsel **metric_events,
unsigned long *evlist_used)
{
- struct evsel *ev;
- bool leader_found;
- const size_t idnum = hashmap__size(&pctx->ids);
- size_t i = 0;
- int j = 0;
- double *val_ptr;
+ struct evsel *ev, *current_leader = NULL;
+ double *val_ptr;
+ int i = 0, matched_events = 0, events_to_match;
+ const int idnum = (int)hashmap__size(&pctx->ids);
+
+ /* duration_time is grouped separately. */
+ if (!has_constraint &&
+ hashmap__find(&pctx->ids, "duration_time", (void**)&val_ptr))
+ events_to_match = idnum - 1;
+ else
+ events_to_match = idnum;

evlist__for_each_entry (perf_evlist, ev) {
- if (test_bit(j++, evlist_used))
+ /*
+ * Events with a constraint aren't grouped and match the first
+ * events available.
+ */
+ if (has_constraint && ev->weak_group)
continue;
- if (hashmap__find(&pctx->ids, ev->name, (void**)&val_ptr)) {
- if (!metric_events[i])
- metric_events[i] = ev;
- i++;
- if (i == idnum)
- break;
- } else {
- /* Discard the whole match and start again */
- i = 0;
+ if (!has_constraint && ev->leader != current_leader) {
+ /*
+ * Start of a new group, discard the whole match and
+ * start again.
+ */
+ matched_events = 0;
memset(metric_events, 0,
sizeof(struct evsel *) * idnum);
+ current_leader = ev->leader;
+ }
+ if (hashmap__find(&pctx->ids, ev->name, (void**)&val_ptr))
+ metric_events[matched_events++] = ev;
+ if (matched_events == events_to_match)
+ break;
+ }

- if (hashmap__find(&pctx->ids, ev->name, (void**)&val_ptr)) {
- if (!metric_events[i])
- metric_events[i] = ev;
- i++;
- if (i == idnum)
- break;
+ if (events_to_match != idnum) {
+ /* Add the first duration_time. */
+ evlist__for_each_entry (perf_evlist, ev) {
+ if (!strcmp(ev->name, "duration_time")) {
+ metric_events[matched_events++] = ev;
+ break;
}
}
}

- if (i != idnum) {
+ if (matched_events != idnum) {
/* Not whole match */
return NULL;
}
@@ -138,18 +166,8 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,
metric_events[idnum] = NULL;

for (i = 0; i < idnum; i++) {
- leader_found = false;
- evlist__for_each_entry(perf_evlist, ev) {
- if (!leader_found && (ev == metric_events[i]))
- leader_found = true;
-
- if (leader_found &&
- !strcmp(ev->name, metric_events[i]->name)) {
- ev->metric_leader = metric_events[i];
- }
- j++;
- }
ev = metric_events[i];
+ ev->metric_leader = ev;
set_bit(ev->idx, evlist_used);
}

@@ -165,7 +183,7 @@ static int metricgroup__setup_events(struct list_head *groups,
int i = 0;
int ret = 0;
struct egroup *eg;
- struct evsel *evsel;
+ struct evsel *evsel, *tmp;
unsigned long *evlist_used;

evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
@@ -181,7 +199,8 @@ static int metricgroup__setup_events(struct list_head *groups,
ret = -ENOMEM;
break;
}
- evsel = find_evsel_group(perf_evlist, &eg->pctx, metric_events,
+ evsel = find_evsel_group(perf_evlist, &eg->pctx,
+ eg->has_constraint, metric_events,
evlist_used);
if (!evsel) {
pr_debug("Cannot resolve %s: %s\n",
@@ -211,6 +230,12 @@ static int metricgroup__setup_events(struct list_head *groups,
list_add(&expr->nd, &me->head);
}

+ evlist__for_each_entry_safe (perf_evlist, tmp, evsel) {
+ if (!test_bit(evsel->idx, evlist_used)) {
+ evlist__remove(perf_evlist, evsel);
+ evsel__delete(evsel);
+ }
+ }
bitmap_free(evlist_used);

return ret;
--
2.26.2.526.g744177e7f7-goog

2020-05-07 08:17:22

by Ian Rogers

[permalink] [raw]
Subject: [RFC PATCH 5/7] perf metricgroup: delay events string creation

Currently event groups are placed into groups_list at the same time as
the events string containing the events is built. Separate these two
operations and build the groups_list first, then the event string from
the groups_list. This adds an ability to reorder the groups_list that
will be used in a later patch.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/util/metricgroup.c | 38 +++++++++++++++++++++++------------
1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 48d0143b4b0c..0a00c0f87872 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -90,6 +90,7 @@ struct egroup {
const char *metric_expr;
const char *metric_unit;
int runtime;
+ bool has_constraint;
};

static struct evsel *find_evsel_group(struct evlist *perf_evlist,
@@ -496,8 +497,8 @@ int __weak arch_get_runtimeparam(void)
return 1;
}

-static int __metricgroup__add_metric(struct strbuf *events,
- struct list_head *group_list, struct pmu_event *pe, int runtime)
+static int __metricgroup__add_metric(struct list_head *group_list,
+ struct pmu_event *pe, int runtime)
{
struct egroup *eg;

@@ -510,6 +511,7 @@ static int __metricgroup__add_metric(struct strbuf *events,
eg->metric_expr = pe->metric_expr;
eg->metric_unit = pe->unit;
eg->runtime = runtime;
+ eg->has_constraint = metricgroup__has_constraint(pe);

if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) {
expr__ctx_clear(&eg->pctx);
@@ -517,14 +519,6 @@ static int __metricgroup__add_metric(struct strbuf *events,
return -EINVAL;
}

- if (events->len > 0)
- strbuf_addf(events, ",");
-
- if (metricgroup__has_constraint(pe))
- metricgroup__add_metric_non_group(events, &eg->pctx);
- else
- metricgroup__add_metric_weak_group(events, &eg->pctx);
-
list_add_tail(&eg->nd, group_list);

return 0;
@@ -535,6 +529,7 @@ static int metricgroup__add_metric(const char *metric, struct strbuf *events,
{
struct pmu_events_map *map = perf_pmu__find_map(NULL);
struct pmu_event *pe;
+ struct egroup *eg;
int i, ret = -EINVAL;

if (!map)
@@ -553,7 +548,8 @@ static int metricgroup__add_metric(const char *metric, struct strbuf *events,
pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);

if (!strstr(pe->metric_expr, "?")) {
- ret = __metricgroup__add_metric(events, group_list, pe, 1);
+ ret = __metricgroup__add_metric(group_list,
+ pe, 1);
} else {
int j, count;

@@ -564,13 +560,29 @@ static int metricgroup__add_metric(const char *metric, struct strbuf *events,
* those events to group_list.
*/

- for (j = 0; j < count; j++)
- ret = __metricgroup__add_metric(events, group_list, pe, j);
+ for (j = 0; j < count; j++) {
+ ret = __metricgroup__add_metric(
+ group_list, pe, j);
+ }
}
if (ret == -ENOMEM)
break;
}
}
+ if (!ret) {
+ list_for_each_entry (eg, group_list, nd) {
+ if (events->len > 0)
+ strbuf_addf(events, ",");
+
+ if (eg->has_constraint) {
+ metricgroup__add_metric_non_group(events,
+ &eg->pctx);
+ } else {
+ metricgroup__add_metric_weak_group(events,
+ &eg->pctx);
+ }
+ }
+ }
return ret;
}

--
2.26.2.526.g744177e7f7-goog

2020-05-07 08:17:48

by Ian Rogers

[permalink] [raw]
Subject: [RFC PATCH 4/7] perf metricgroup: always place duration_time last

If a metric contains the duration_time event then the event is placed
outside of the metric's group of events. Rather than split the group,
make it so the duration_time is immediately after the group.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/util/metricgroup.c | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 2356dda92a07..48d0143b4b0c 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -421,8 +421,8 @@ static void metricgroup__add_metric_weak_group(struct strbuf *events,
struct expr_parse_ctx *ctx)
{
struct hashmap_entry *cur;
- size_t bkt, i = 0;
- bool no_group = false;
+ size_t bkt;
+ bool no_group = true, has_duration = false;

hashmap__for_each_entry((&ctx->ids), cur, bkt) {
pr_debug("found event %s\n", (const char*)cur->key);
@@ -432,20 +432,20 @@ static void metricgroup__add_metric_weak_group(struct strbuf *events,
* group.
*/
if (!strcmp(cur->key, "duration_time")) {
- if (i > 0)
- strbuf_addf(events, "}:W,");
- strbuf_addf(events, "duration_time");
- no_group = true;
+ has_duration = true;
continue;
}
strbuf_addf(events, "%s%s",
- i == 0 || no_group ? "{" : ",",
+ no_group ? "{" : ",",
(const char*)cur->key);
no_group = false;
- i++;
}
- if (!no_group)
- strbuf_addf(events, "}:W");
+ if (!no_group) {
+ strbuf_addf(events, "}:W");
+ if (has_duration)
+ strbuf_addf(events, ",duration_time");
+ } else if (has_duration)
+ strbuf_addf(events, "duration_time");
}

static void metricgroup__add_metric_non_group(struct strbuf *events,
--
2.26.2.526.g744177e7f7-goog

2020-05-07 08:18:46

by Ian Rogers

[permalink] [raw]
Subject: [RFC PATCH 1/7] perf expr: migrate expr ids table to libbpf's hashmap

Use a hashmap between a char* string and a double* value. While bpf's
hashmap entries are size_t in size, we can't guarantee sizeof(size_t) >=
sizeof(double). Avoid a memory allocation when gathering ids by making 0.0
a special value encoded as NULL.

Suggested by Andi Kleen:
https://lore.kernel.org/lkml/[email protected]/
and seconded by Jiri Olsa:
https://lore.kernel.org/lkml/20200423112915.GH1136647@krava/

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/tests/expr.c | 32 ++++-----
tools/perf/tests/pmu-events.c | 22 +++---
tools/perf/util/expr.c | 125 ++++++++++++++++++----------------
tools/perf/util/expr.h | 22 +++---
tools/perf/util/expr.y | 22 +-----
tools/perf/util/metricgroup.c | 86 +++++++++++------------
tools/perf/util/stat-shadow.c | 46 ++++++++-----
7 files changed, 176 insertions(+), 179 deletions(-)

diff --git a/tools/perf/tests/expr.c b/tools/perf/tests/expr.c
index 3f742612776a..bb948226be1d 100644
--- a/tools/perf/tests/expr.c
+++ b/tools/perf/tests/expr.c
@@ -19,11 +19,9 @@ static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
{
const char *p;
- const char **other;
- double val;
- int i, ret;
+ double val, *val_ptr;
+ int ret;
struct expr_parse_ctx ctx;
- int num_other;

expr__ctx_init(&ctx);
expr__add_id(&ctx, "FOO", 1);
@@ -52,25 +50,23 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
ret = expr__parse(&val, &ctx, p, 1);
TEST_ASSERT_VAL("missing operand", ret == -1);

+ hashmap__clear(&ctx.ids);
TEST_ASSERT_VAL("find other",
- expr__find_other("FOO + BAR + BAZ + BOZO", "FOO", &other, &num_other, 1) == 0);
- TEST_ASSERT_VAL("find other", num_other == 3);
- TEST_ASSERT_VAL("find other", !strcmp(other[0], "BAR"));
- TEST_ASSERT_VAL("find other", !strcmp(other[1], "BAZ"));
- TEST_ASSERT_VAL("find other", !strcmp(other[2], "BOZO"));
- TEST_ASSERT_VAL("find other", other[3] == NULL);
+ expr__find_other("FOO + BAR + BAZ + BOZO", "FOO", &ctx, 1) == 0);
+ TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 3);
+ TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAR", (void**)&val_ptr));
+ TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAZ", (void**)&val_ptr));
+ TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BOZO", (void**)&val_ptr));

+ hashmap__clear(&ctx.ids);
TEST_ASSERT_VAL("find other",
expr__find_other("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@", NULL,
- &other, &num_other, 3) == 0);
- TEST_ASSERT_VAL("find other", num_other == 2);
- TEST_ASSERT_VAL("find other", !strcmp(other[0], "EVENT1,param=3/"));
- TEST_ASSERT_VAL("find other", !strcmp(other[1], "EVENT2,param=3/"));
- TEST_ASSERT_VAL("find other", other[2] == NULL);
+ &ctx, 3) == 0);
+ TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 2);
+ TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT1,param=3/", (void**)&val_ptr));
+ TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT2,param=3/", (void**)&val_ptr));

- for (i = 0; i < num_other; i++)
- zfree(&other[i]);
- free((void *)other);
+ expr__ctx_clear(&ctx);

return 0;
}
diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c
index c18b9ce8cace..6ce6b8a31e1f 100644
--- a/tools/perf/tests/pmu-events.c
+++ b/tools/perf/tests/pmu-events.c
@@ -428,8 +428,6 @@ static int test_parsing(void)
struct pmu_events_map *map;
struct pmu_event *pe;
int i, j, k;
- const char **ids;
- int idnum;
int ret = 0;
struct expr_parse_ctx ctx;
double result;
@@ -443,13 +441,16 @@ static int test_parsing(void)
}
j = 0;
for (;;) {
+ struct hashmap_entry *cur;
+ size_t bkt;
+
pe = &map->table[j++];
if (!pe->name && !pe->metric_group && !pe->metric_name)
break;
if (!pe->metric_expr)
continue;
- if (expr__find_other(pe->metric_expr, NULL,
- &ids, &idnum, 0) < 0) {
+ if (expr__find_other(pe->metric_expr, NULL, &ctx, 0)
+ < 0) {
pr_debug("Parse other failed for map %s %s %s\n",
map->cpuid, map->version, map->type);
pr_debug("On metric %s\n", pe->metric_name);
@@ -464,11 +465,12 @@ static int test_parsing(void)
* trigger divide by zero when subtracted and so try to
* make them unique.
*/
- for (k = 0; k < idnum; k++)
- expr__add_id(&ctx, ids[k], k + 1);
+ k = 1;
+ hashmap__for_each_entry((&ctx.ids), cur, bkt)
+ expr__add_id(&ctx, cur->key, k++);

- for (k = 0; k < idnum; k++) {
- if (check_parse_id(ids[k], map == cpus_map, pe))
+ hashmap__for_each_entry((&ctx.ids), cur, bkt) {
+ if (check_parse_id(cur->key, map == cpus_map, pe))
ret++;
}

@@ -479,9 +481,7 @@ static int test_parsing(void)
pr_debug("On expression %s\n", pe->metric_expr);
ret++;
}
- for (k = 0; k < idnum; k++)
- zfree(&ids[k]);
- free(ids);
+ expr__ctx_clear(&ctx);
}
}
return ret;
diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c
index 8b4ce704a68d..70e4e468d7c7 100644
--- a/tools/perf/util/expr.c
+++ b/tools/perf/util/expr.c
@@ -4,25 +4,73 @@
#include "expr.h"
#include "expr-bison.h"
#include "expr-flex.h"
+#include <linux/kernel.h>

#ifdef PARSER_DEBUG
extern int expr_debug;
#endif

+static size_t double_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 double_equal(const void *key1, const void *key2,
+ void *ctx __maybe_unused)
+{
+ return !strcmp((const char*)key1, (const char*)key2);
+}
+
/* Caller must make sure id is allocated */
-void expr__add_id(struct expr_parse_ctx *ctx, const char *name, double val)
+int expr__add_id(struct expr_parse_ctx *ctx, const char *name, double val)
{
- int idx;
+ double *val_ptr = NULL, *old_val = NULL;
+ char *old_key = NULL;
+ int ret;

- assert(ctx->num_ids < MAX_PARSE_ID);
- idx = ctx->num_ids++;
- ctx->ids[idx].name = name;
- ctx->ids[idx].val = val;
+ if (val != 0.0) {
+ val_ptr = malloc(sizeof(double));
+ if (!val_ptr)
+ return -ENOMEM;
+ *val_ptr = val;
+ }
+ ret = hashmap__set(&ctx->ids, name, val_ptr, (const void**)&old_key, (void**)&old_val);
+ free(old_key);
+ free(old_val);
+ return ret;
+}
+
+int expr__get_id(struct expr_parse_ctx *ctx, const char *id, double *val_ptr)
+{
+ double *data;
+ if (!hashmap__find(&ctx->ids, id, (void**)&data))
+ return -1;
+ *val_ptr = (data == NULL) ? 0.0 : *data;
+ return 0;
}

void expr__ctx_init(struct expr_parse_ctx *ctx)
{
- ctx->num_ids = 0;
+ hashmap__init(&ctx->ids, double_hash, double_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);
}

static int
@@ -56,61 +104,24 @@ __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
return ret;
}

-int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr, int runtime)
+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 expr__find_other(const char *expr, const char *one,
+ struct expr_parse_ctx *ctx, int runtime)
{
- 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;
+ double *old_val = NULL;
+ char *old_key = NULL;
+ int ret = __expr__parse(NULL, ctx, expr, EXPR_OTHER, runtime);
+
+ if (one) {
+ hashmap__delete(&ctx->ids, one, (const void**)&old_key, (void**)&old_val);
+ free(old_key);
+ free(old_val);
}

- *num_other = j;
- return err;
+ return ret;
}
diff --git a/tools/perf/util/expr.h b/tools/perf/util/expr.h
index 40fc452b0f2b..f01bd5ecb09d 100644
--- a/tools/perf/util/expr.h
+++ b/tools/perf/util/expr.h
@@ -2,17 +2,10 @@
#ifndef PARSE_CTX_H
#define PARSE_CTX_H 1

-#define EXPR_MAX_OTHER 64
-#define MAX_PARSE_ID EXPR_MAX_OTHER
-
-struct expr_parse_id {
- const char *name;
- double val;
-};
+#include <bpf/hashmap.h>

struct expr_parse_ctx {
- int num_ids;
- struct expr_parse_id ids[MAX_PARSE_ID];
+ struct hashmap ids;
};

struct expr_scanner_ctx {
@@ -21,9 +14,12 @@ struct expr_scanner_ctx {
};

void expr__ctx_init(struct expr_parse_ctx *ctx);
-void expr__add_id(struct expr_parse_ctx *ctx, const char *id, double val);
-int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr, int runtime);
-int expr__find_other(const char *expr, const char *one, const char ***other,
- int *num_other, int runtime);
+void expr__ctx_clear(struct expr_parse_ctx *ctx);
+int expr__add_id(struct expr_parse_ctx *ctx, const char *id, double val);
+int expr__get_id(struct expr_parse_ctx *ctx, const char *id, double *val_ptr);
+int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
+ const char *expr, int runtime);
+int expr__find_other(const char *expr, const char *one,
+ struct expr_parse_ctx *ids, int runtime);

#endif
diff --git a/tools/perf/util/expr.y b/tools/perf/util/expr.y
index 3b49b230b111..bf3e898e3055 100644
--- a/tools/perf/util/expr.y
+++ b/tools/perf/util/expr.y
@@ -47,19 +47,6 @@ static void expr_error(double *final_val __maybe_unused,
pr_debug("%s\n", s);
}

-static int lookup_id(struct expr_parse_ctx *ctx, char *id, double *val)
-{
- int i;
-
- for (i = 0; i < ctx->num_ids; i++) {
- if (!strcasecmp(ctx->ids[i].name, id)) {
- *val = ctx->ids[i].val;
- return 0;
- }
- }
- return -1;
-}
-
%}
%%

@@ -73,12 +60,7 @@ all_other: all_other other

other: ID
{
- if (ctx->num_ids + 1 >= EXPR_MAX_OTHER) {
- pr_err("failed: way too many variables");
- YYABORT;
- }
-
- ctx->ids[ctx->num_ids++].name = $1;
+ expr__add_id(ctx, $1, 0.0);
}
|
MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
@@ -93,7 +75,7 @@ if_expr:
;

expr: NUMBER
- | ID { if (lookup_id(ctx, $1, &$$) < 0) {
+ | ID { if (expr__get_id(ctx, $1, &$$)) {
pr_debug("%s not found\n", $1);
free($1);
YYABORT;
diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index b071df373f8b..2f92dbc05226 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -85,8 +85,7 @@ static void metricgroup__rblist_init(struct rblist *metric_events)

struct egroup {
struct list_head nd;
- int idnum;
- const char **ids;
+ struct expr_parse_ctx pctx;
const char *metric_name;
const char *metric_expr;
const char *metric_unit;
@@ -94,19 +93,21 @@ struct egroup {
};

static struct evsel *find_evsel_group(struct evlist *perf_evlist,
- const char **ids,
- int idnum,
+ struct expr_parse_ctx *pctx,
struct evsel **metric_events,
bool *evlist_used)
{
struct evsel *ev;
- int i = 0, j = 0;
bool leader_found;
+ const size_t idnum = hashmap__size(&pctx->ids);
+ size_t i = 0;
+ int j = 0;
+ double *val_ptr;

evlist__for_each_entry (perf_evlist, ev) {
if (evlist_used[j++])
continue;
- if (!strcmp(ev->name, ids[i])) {
+ if (hashmap__find(&pctx->ids, ev->name, (void**)&val_ptr)) {
if (!metric_events[i])
metric_events[i] = ev;
i++;
@@ -118,7 +119,7 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,
memset(metric_events, 0,
sizeof(struct evsel *) * idnum);

- if (!strcmp(ev->name, ids[i])) {
+ if (hashmap__find(&pctx->ids, ev->name, (void**)&val_ptr)) {
if (!metric_events[i])
metric_events[i] = ev;
i++;
@@ -175,19 +176,20 @@ static int metricgroup__setup_events(struct list_head *groups,
list_for_each_entry (eg, groups, nd) {
struct evsel **metric_events;

- metric_events = calloc(sizeof(void *), eg->idnum + 1);
+ metric_events = calloc(sizeof(void *),
+ hashmap__size(&eg->pctx.ids) + 1);
if (!metric_events) {
ret = -ENOMEM;
break;
}
- evsel = find_evsel_group(perf_evlist, eg->ids, eg->idnum,
- metric_events, evlist_used);
+ evsel = find_evsel_group(perf_evlist, &eg->pctx, metric_events,
+ evlist_used);
if (!evsel) {
pr_debug("Cannot resolve %s: %s\n",
eg->metric_name, eg->metric_expr);
continue;
}
- for (i = 0; i < eg->idnum; i++)
+ for (i = 0; metric_events[i]; i++)
metric_events[i]->collect_stat = true;
me = metricgroup__lookup(metric_events_list, evsel, true);
if (!me) {
@@ -415,20 +417,20 @@ void metricgroup__print(bool metrics, bool metricgroups, char *filter,
}

static void metricgroup__add_metric_weak_group(struct strbuf *events,
- const char **ids,
- int idnum)
+ struct expr_parse_ctx *ctx)
{
+ struct hashmap_entry *cur;
+ size_t bkt, i = 0;
bool no_group = false;
- int i;

- for (i = 0; i < idnum; i++) {
- pr_debug("found event %s\n", ids[i]);
+ hashmap__for_each_entry((&ctx->ids), cur, bkt) {
+ pr_debug("found event %s\n", (const char*)cur->key);
/*
* Duration time maps to a software event and can make
* groups not count. Always use it outside a
* group.
*/
- if (!strcmp(ids[i], "duration_time")) {
+ if (!strcmp(cur->key, "duration_time")) {
if (i > 0)
strbuf_addf(events, "}:W,");
strbuf_addf(events, "duration_time");
@@ -437,21 +439,22 @@ static void metricgroup__add_metric_weak_group(struct strbuf *events,
}
strbuf_addf(events, "%s%s",
i == 0 || no_group ? "{" : ",",
- ids[i]);
+ (const char*)cur->key);
no_group = false;
+ i++;
}
if (!no_group)
strbuf_addf(events, "}:W");
}

static void metricgroup__add_metric_non_group(struct strbuf *events,
- const char **ids,
- int idnum)
+ struct expr_parse_ctx *ctx)
{
- int i;
+ struct hashmap_entry *cur;
+ size_t bkt;

- for (i = 0; i < idnum; i++)
- strbuf_addf(events, ",%s", ids[i]);
+ hashmap__for_each_entry((&ctx->ids), cur, bkt)
+ strbuf_addf(events, ",%s", (const char*)cur->key);
}

static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
@@ -495,32 +498,32 @@ int __weak arch_get_runtimeparam(void)
static int __metricgroup__add_metric(struct strbuf *events,
struct list_head *group_list, struct pmu_event *pe, int runtime)
{
-
- const char **ids;
- int idnum;
struct egroup *eg;

- if (expr__find_other(pe->metric_expr, NULL, &ids, &idnum, runtime) < 0)
- return -EINVAL;
-
- if (events->len > 0)
- strbuf_addf(events, ",");
-
- if (metricgroup__has_constraint(pe))
- metricgroup__add_metric_non_group(events, ids, idnum);
- else
- metricgroup__add_metric_weak_group(events, ids, idnum);
-
eg = malloc(sizeof(*eg));
if (!eg)
return -ENOMEM;

- eg->ids = ids;
- eg->idnum = idnum;
+ expr__ctx_init(&eg->pctx);
eg->metric_name = pe->metric_name;
eg->metric_expr = pe->metric_expr;
eg->metric_unit = pe->unit;
eg->runtime = runtime;
+
+ if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) {
+ expr__ctx_clear(&eg->pctx);
+ free(eg);
+ return -EINVAL;
+ }
+
+ if (events->len > 0)
+ strbuf_addf(events, ",");
+
+ if (metricgroup__has_constraint(pe))
+ metricgroup__add_metric_non_group(events, &eg->pctx);
+ else
+ metricgroup__add_metric_weak_group(events, &eg->pctx);
+
list_add_tail(&eg->nd, group_list);

return 0;
@@ -603,12 +606,9 @@ static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
static void metricgroup__free_egroups(struct list_head *group_list)
{
struct egroup *eg, *egtmp;
- int i;

list_for_each_entry_safe (eg, egtmp, group_list, nd) {
- for (i = 0; i < eg->idnum; i++)
- zfree(&eg->ids[i]);
- zfree(&eg->ids);
+ expr__ctx_clear(&eg->pctx);
list_del_init(&eg->nd);
free(eg);
}
diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index 129b8c5f2538..f8ba66c305f5 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -323,35 +323,45 @@ void perf_stat__collect_metric_expr(struct evlist *evsel_list)
{
struct evsel *counter, *leader, **metric_events, *oc;
bool found;
- const char **metric_names;
+ struct expr_parse_ctx ctx;
+ struct hashmap_entry *cur;
+ size_t bkt;
int i;
- int num_metric_names;

+ expr__ctx_init(&ctx);
evlist__for_each_entry(evsel_list, counter) {
bool invalid = false;

leader = counter->leader;
if (!counter->metric_expr)
continue;
+
+ expr__ctx_clear(&ctx);
metric_events = counter->metric_events;
if (!metric_events) {
- if (expr__find_other(counter->metric_expr, counter->name,
- &metric_names, &num_metric_names, 1) < 0)
+ if (expr__find_other(counter->metric_expr,
+ counter->name,
+ &ctx, 1) < 0)
continue;

metric_events = calloc(sizeof(struct evsel *),
- num_metric_names + 1);
- if (!metric_events)
+ hashmap__size(&ctx.ids) + 1);
+ if (!metric_events) {
+ expr__ctx_clear(&ctx);
return;
+ }
counter->metric_events = metric_events;
}

- for (i = 0; i < num_metric_names; i++) {
+ i = 0;
+ hashmap__for_each_entry((&ctx.ids), cur, bkt) {
+ const char* metric_name = (const char*)cur->key;
+
found = false;
if (leader) {
/* Search in group */
for_each_group_member (oc, leader) {
- if (!strcasecmp(oc->name, metric_names[i]) &&
+ if (!strcasecmp(oc->name, metric_name) &&
!oc->collect_stat) {
found = true;
break;
@@ -360,7 +370,7 @@ void perf_stat__collect_metric_expr(struct evlist *evsel_list)
}
if (!found) {
/* Search ignoring groups */
- oc = perf_stat__find_event(evsel_list, metric_names[i]);
+ oc = perf_stat__find_event(evsel_list, metric_name);
}
if (!oc) {
/* Deduping one is good enough to handle duplicated PMUs. */
@@ -373,27 +383,27 @@ void perf_stat__collect_metric_expr(struct evlist *evsel_list)
* of events. So we ask the user instead to add the missing
* events.
*/
- if (!printed || strcasecmp(printed, metric_names[i])) {
+ if (!printed || strcasecmp(printed, metric_name)) {
fprintf(stderr,
"Add %s event to groups to get metric expression for %s\n",
- metric_names[i],
+ metric_name,
counter->name);
- printed = strdup(metric_names[i]);
+ printed = strdup(metric_name);
}
invalid = true;
continue;
}
- metric_events[i] = oc;
+ metric_events[i++] = oc;
oc->collect_stat = true;
}
metric_events[i] = NULL;
- free(metric_names);
if (invalid) {
free(metric_events);
counter->metric_events = NULL;
counter->metric_expr = NULL;
}
}
+ expr__ctx_clear(&ctx);
}

static double runtime_stat_avg(struct runtime_stat *st,
@@ -738,7 +748,10 @@ static void generic_metric(struct perf_stat_config *config,

expr__ctx_init(&pctx);
/* Must be first id entry */
- expr__add_id(&pctx, name, avg);
+ n = strdup(name);
+ if (!n)
+ return;
+ expr__add_id(&pctx, n, avg);
for (i = 0; metric_events[i]; i++) {
struct saved_value *v;
struct stats *stats;
@@ -814,8 +827,7 @@ static void generic_metric(struct perf_stat_config *config,
(metric_name ? metric_name : name) : "", 0);
}

- for (i = 1; i < pctx.num_ids; i++)
- zfree(&pctx.ids[i].name);
+ expr__ctx_clear(&pctx);
}

void perf_stat__print_shadow_stats(struct perf_stat_config *config,
--
2.26.2.526.g744177e7f7-goog

2020-05-07 13:52:42

by Jiri Olsa

[permalink] [raw]
Subject: Re: [RFC PATCH 0/7] Share events between metrics

On Thu, May 07, 2020 at 01:14:29AM -0700, Ian Rogers wrote:
> Metric groups contain metrics. Metrics create groups of events to
> ideally be scheduled together. Often metrics refer to the same events,
> for example, a cache hit and cache miss rate. Using separate event
> groups means these metrics are multiplexed at different times and the
> counts don't sum to 100%. More multiplexing also decreases the
> accuracy of the measurement.
>
> This change orders metrics from groups or the command line, so that
> the ones with the most events are set up first. Later metrics see if
> groups already provide their events, and reuse them if
> possible. Unnecessary events and groups are eliminated.
>
> RFC because:
> - without this change events within a metric may get scheduled
> together, after they may appear as part of a larger group and be
> multiplexed at different times, lowering accuracy - however, less
> multiplexing may compensate for this.
> - libbpf's hashmap is used, however, libbpf is an optional
> requirement for building perf.
> - other things I'm not thinking of.

hi,
I can't apply this, what branch/commit is this based on?

Applying: perf expr: migrate expr ids table to libbpf's hashmap
error: patch failed: tools/perf/tests/pmu-events.c:428
error: tools/perf/tests/pmu-events.c: patch does not apply
error: patch failed: tools/perf/util/expr.h:2
error: tools/perf/util/expr.h: patch does not apply
error: patch failed: tools/perf/util/expr.y:73
error: tools/perf/util/expr.y: patch does not apply
Patch failed at 0001 perf expr: migrate expr ids table to libbpf's hashmap

thanks,
jirka

>
> Thanks!
>
> Ian Rogers (7):
> perf expr: migrate expr ids table to libbpf's hashmap
> perf metricgroup: change evlist_used to a bitmap
> perf metricgroup: free metric_events on error
> perf metricgroup: always place duration_time last
> perf metricgroup: delay events string creation
> perf metricgroup: order event groups by size
> perf metricgroup: remove duped metric group events
>
> tools/perf/tests/expr.c | 32 ++---
> tools/perf/tests/pmu-events.c | 22 ++--
> tools/perf/util/expr.c | 125 ++++++++++--------
> tools/perf/util/expr.h | 22 ++--
> tools/perf/util/expr.y | 22 +---
> tools/perf/util/metricgroup.c | 242 +++++++++++++++++++++-------------
> tools/perf/util/stat-shadow.c | 46 ++++---
> 7 files changed, 280 insertions(+), 231 deletions(-)
>
> --
> 2.26.2.526.g744177e7f7-goog
>

2020-05-07 14:14:22

by Ian Rogers

[permalink] [raw]
Subject: Re: [RFC PATCH 0/7] Share events between metrics

On Thu, May 7, 2020 at 6:49 AM Jiri Olsa <[email protected]> wrote:
>
> On Thu, May 07, 2020 at 01:14:29AM -0700, Ian Rogers wrote:
> > Metric groups contain metrics. Metrics create groups of events to
> > ideally be scheduled together. Often metrics refer to the same events,
> > for example, a cache hit and cache miss rate. Using separate event
> > groups means these metrics are multiplexed at different times and the
> > counts don't sum to 100%. More multiplexing also decreases the
> > accuracy of the measurement.
> >
> > This change orders metrics from groups or the command line, so that
> > the ones with the most events are set up first. Later metrics see if
> > groups already provide their events, and reuse them if
> > possible. Unnecessary events and groups are eliminated.
> >
> > RFC because:
> > - without this change events within a metric may get scheduled
> > together, after they may appear as part of a larger group and be
> > multiplexed at different times, lowering accuracy - however, less
> > multiplexing may compensate for this.
> > - libbpf's hashmap is used, however, libbpf is an optional
> > requirement for building perf.
> > - other things I'm not thinking of.
>
> hi,
> I can't apply this, what branch/commit is this based on?
>
> Applying: perf expr: migrate expr ids table to libbpf's hashmap
> error: patch failed: tools/perf/tests/pmu-events.c:428
> error: tools/perf/tests/pmu-events.c: patch does not apply
> error: patch failed: tools/perf/util/expr.h:2
> error: tools/perf/util/expr.h: patch does not apply
> error: patch failed: tools/perf/util/expr.y:73
> error: tools/perf/util/expr.y: patch does not apply
> Patch failed at 0001 perf expr: migrate expr ids table to libbpf's hashmap
>
> thanks,
> jirka

Thanks for trying! I have resent the entire patch series here:
https://lore.kernel.org/lkml/[email protected]/
It is acme's perf/core tree + metric fix/test CLs + some minor fixes.
Details in the cover letter.

Thanks,
Ian

> >
> > Thanks!
> >
> > Ian Rogers (7):
> > perf expr: migrate expr ids table to libbpf's hashmap
> > perf metricgroup: change evlist_used to a bitmap
> > perf metricgroup: free metric_events on error
> > perf metricgroup: always place duration_time last
> > perf metricgroup: delay events string creation
> > perf metricgroup: order event groups by size
> > perf metricgroup: remove duped metric group events
> >
> > tools/perf/tests/expr.c | 32 ++---
> > tools/perf/tests/pmu-events.c | 22 ++--
> > tools/perf/util/expr.c | 125 ++++++++++--------
> > tools/perf/util/expr.h | 22 ++--
> > tools/perf/util/expr.y | 22 +---
> > tools/perf/util/metricgroup.c | 242 +++++++++++++++++++++-------------
> > tools/perf/util/stat-shadow.c | 46 ++++---
> > 7 files changed, 280 insertions(+), 231 deletions(-)
> >
> > --
> > 2.26.2.526.g744177e7f7-goog
> >
>

2020-05-07 17:50:31

by Andi Kleen

[permalink] [raw]
Subject: Re: [RFC PATCH 0/7] Share events between metrics

On Thu, May 07, 2020 at 01:14:29AM -0700, Ian Rogers wrote:
> Metric groups contain metrics. Metrics create groups of events to
> ideally be scheduled together. Often metrics refer to the same events,
> for example, a cache hit and cache miss rate. Using separate event
> groups means these metrics are multiplexed at different times and the
> counts don't sum to 100%. More multiplexing also decreases the
> accuracy of the measurement.
>
> This change orders metrics from groups or the command line, so that
> the ones with the most events are set up first. Later metrics see if
> groups already provide their events, and reuse them if
> possible. Unnecessary events and groups are eliminated.

Note this actually may make multiplexing errors worse.

For metrics it is often important that all the input values to
the metric run at the same time.

So e.g. if you have two metrics and they each fit into a group,
but not together, even though you have more multiplexing it
will give more accurate results for each metric.

I think you change can make sense for metrics that don't fit
into single groups anyways. perf currently doesn't quite know
this but some heuristic could be added.

But I wouldn't do it for simple metrics that fit into groups.
The result may well be worse.

My toplev tool has some heuristics for this, also some more
sophisticated ones that tracks subexpressions. That would
be far too complicated for perf likely.

-Andi

2020-05-07 18:19:20

by Ian Rogers

[permalink] [raw]
Subject: Re: [RFC PATCH 0/7] Share events between metrics

On Thu, May 7, 2020 at 10:48 AM Andi Kleen <[email protected]> wrote:
>
> On Thu, May 07, 2020 at 01:14:29AM -0700, Ian Rogers wrote:
> > Metric groups contain metrics. Metrics create groups of events to
> > ideally be scheduled together. Often metrics refer to the same events,
> > for example, a cache hit and cache miss rate. Using separate event
> > groups means these metrics are multiplexed at different times and the
> > counts don't sum to 100%. More multiplexing also decreases the
> > accuracy of the measurement.
> >
> > This change orders metrics from groups or the command line, so that
> > the ones with the most events are set up first. Later metrics see if
> > groups already provide their events, and reuse them if
> > possible. Unnecessary events and groups are eliminated.
>
> Note this actually may make multiplexing errors worse.
>
> For metrics it is often important that all the input values to
> the metric run at the same time.
>
> So e.g. if you have two metrics and they each fit into a group,
> but not together, even though you have more multiplexing it
> will give more accurate results for each metric.
>
> I think you change can make sense for metrics that don't fit
> into single groups anyways. perf currently doesn't quite know
> this but some heuristic could be added.
>
> But I wouldn't do it for simple metrics that fit into groups.
> The result may well be worse.
>
> My toplev tool has some heuristics for this, also some more
> sophisticated ones that tracks subexpressions. That would
> be far too complicated for perf likely.
>
> -Andi

Thanks Andi!

I was trying to be mindful of the multiplexing issue in the description:

> - without this change events within a metric may get scheduled
> together, after they may appear as part of a larger group and be
> multiplexed at different times, lowering accuracy - however, less
> multiplexing may compensate for this.

I agree the heuristic in this patch set is naive and would welcome to
improve it from your toplev experience. I think this change is
progress on TopDownL1 - would you agree?

I'm wondering if what is needed are flags to control behavior. For
example, avoiding the use of groups altogether. For TopDownL1 I see.

Currently:
27,294,614,172 idq_uops_not_delivered.core # 0.3
Frontend_Bound (49.96%)
24,498,363,923 cycles
(49.96%)
21,449,143,854 uops_issued.any # 0.1
Bad_Speculation (16.68%)
16,450,676,961 uops_retired.retire_slots
(16.68%)
880,423,103 int_misc.recovery_cycles
(16.68%)
24,180,775,568 cycles
(16.68%)
27,662,201,567 idq_uops_not_delivered.core # 0.5
Backend_Bound (16.67%)
25,354,331,290 cycles
(16.67%)
22,642,218,398 uops_issued.any
(16.67%)
17,564,211,383 uops_retired.retire_slots
(16.67%)
896,938,527 int_misc.recovery_cycles
(16.67%)
17,872,454,517 uops_retired.retire_slots # 0.2 Retiring
(16.68%)
25,122,100,836 cycles
(16.68%)
15,101,167,608 inst_retired.any # 0.6 IPC
(33.34%)
24,977,816,793 cpu_clk_unhalted.thread
(33.34%)
24,868,717,684 cycles
# 99474870736.0
SLOTS (49.98%)

With proposed (RFC) sharing of events over metrics:
22,780,823,620 cycles
# 91123294480.0
SLOTS
# 0.2 Retiring
# 0.3
Frontend_Bound
# 0.1
Bad_Speculation
# 0.4
Backend_Bound (50.01%)
26,097,362,439 idq_uops_not_delivered.core
(50.01%)
790,521,504 int_misc.recovery_cycles
(50.01%)
21,025,308,329 uops_issued.any
(50.01%)
17,041,506,149 uops_retired.retire_slots
(50.01%)
22,964,891,526 cpu_clk_unhalted.thread # 0.6 IPC
(49.99%)
14,531,724,741 inst_retired.any
(49.99%)

No groups:
1,517,455,258 cycles
# 6069821032.0 SLOTS
# 0.1 Retiring
# 0.3
Frontend_Bound
# 0.1
Bad_Speculation
# 0.5
Backend_Bound (85.64%)
1,943,047,724 idq_uops_not_delivered.core
(85.61%)
54,257,713 int_misc.recovery_cycles
(85.63%)
1,050,787,137 uops_issued.any
(85.63%)
881,310,530 uops_retired.retire_slots
(85.68%)
1,553,561,836 cpu_clk_unhalted.thread # 0.5 IPC
(71.81%)
742,087,439 inst_retired.any
(85.85%)

So with no groups there is a lot less multiplexing.

So I'm thinking of two flags:
- disable sharing of events between metrics - defaulted off - this
keeps the current behavior in case there is a use-case where
multiplexing is detrimental. I'm not sure how necessary this flag is,
if we could quantify it based on experience elsewhere it'd be nice.
Default off as without sharing metrics within a metric group fail to
add to 100%. Fwiw, I can imagine phony metrics that exist just to
cause sharing of events within a group.
- disable grouping of events in metrics - defaulted off - this would
change the behavior of groups like TopDownL1 as I show above for "no
groups".

I see in toplev:
https://github.com/andikleen/pmu-tools/wiki/toplev-manual
--no-group which is similar to the second flag.
Do you have any pointers in toplev for better grouping heuristics?

Thoughts and better ways to do this very much appreciated! Thanks,

Ian

2020-05-07 21:50:00

by Andi Kleen

[permalink] [raw]
Subject: Re: [RFC PATCH 0/7] Share events between metrics

> > - without this change events within a metric may get scheduled
> > together, after they may appear as part of a larger group and be
> > multiplexed at different times, lowering accuracy - however, less
> > multiplexing may compensate for this.
>
> I agree the heuristic in this patch set is naive and would welcome to
> improve it from your toplev experience. I think this change is
> progress on TopDownL1 - would you agree?

TopdownL1 in non SMT mode should always fit. Inside a group
deduping always makes sense.

The problem is SMT mode where it doesn't fit. toplev tries
to group each node and each level together.

>
> I'm wondering if what is needed are flags to control behavior. For
> example, avoiding the use of groups altogether. For TopDownL1 I see.

Yes the current situation isn't great.

For Topdown your patch clearly is an improvement, I'm not sure
it's for everything though.

Probably the advanced heuristics are only useful for a few
formulas, most are very simple. So maybe it's ok. I guess
would need some testing over the existing formulas.


-Andi

2020-05-08 05:45:41

by Ian Rogers

[permalink] [raw]
Subject: Re: [RFC PATCH 0/7] Share events between metrics

On Thu, May 7, 2020 at 2:47 PM Andi Kleen <[email protected]> wrote:
>
> > > - without this change events within a metric may get scheduled
> > > together, after they may appear as part of a larger group and be
> > > multiplexed at different times, lowering accuracy - however, less
> > > multiplexing may compensate for this.
> >
> > I agree the heuristic in this patch set is naive and would welcome to
> > improve it from your toplev experience. I think this change is
> > progress on TopDownL1 - would you agree?
>
> TopdownL1 in non SMT mode should always fit. Inside a group
> deduping always makes sense.
>
> The problem is SMT mode where it doesn't fit. toplev tries
> to group each node and each level together.

Thanks Andi, I've provided some examples of TopDownL3_SMT in the cover
letter of the v3 patch set:
https://lore.kernel.org/lkml/[email protected]/
I tested sandybridge and cascadelake and the results look similar to
the non-SMT version. Let me know if there's a different variant to
test.

> >
> > I'm wondering if what is needed are flags to control behavior. For
> > example, avoiding the use of groups altogether. For TopDownL1 I see.
>
> Yes the current situation isn't great.
>
> For Topdown your patch clearly is an improvement, I'm not sure
> it's for everything though.
>
> Probably the advanced heuristics are only useful for a few
> formulas, most are very simple. So maybe it's ok. I guess
> would need some testing over the existing formulas.

Agreed, do you have a pointer on a metric group where things would
obviously be worse? I started off with a cache miss and hit rate
metric and similar to topdown this approach is a benefit.

In v3 I've added a --metric-no-merge option to retain existing
grouping behavior, I've also added a --metric-no-group that avoids
groups for all metrics. This may be useful if the NMI watchdog can't
be disabled.

Thanks for the input!
Ian

> -Andi

2020-12-15 15:12:38

by Paul A. Clarke

[permalink] [raw]
Subject: Re: [RFC PATCH 0/7] Share events between metrics

On Thu, May 07, 2020 at 10:43:43PM -0700, Ian Rogers wrote:
> On Thu, May 7, 2020 at 2:47 PM Andi Kleen <[email protected]> wrote:
> >
> > > > - without this change events within a metric may get scheduled
> > > > together, after they may appear as part of a larger group and be
> > > > multiplexed at different times, lowering accuracy - however, less
> > > > multiplexing may compensate for this.

Does mutiplexing somewhat related events at different times actually reduce
accuracy, or is it just more likely to give that appearance?

It seems that perf measurements are only useful if the workload is in a
fairly steady state. If there is some wobbling, then measuring at the
same time is more accurate for the periods where the events are being
measured simultaneously, but may be far off for when they are not being
measured at all. Spreading them out over a longer duration may actually
increase accuracy by sampling over more varied intervals.

Or, is the concern more about trying to time-slice the results in a
fairly granular way and expecting accurate results then?

(Or, maybe my ignorance is showing again. :-)

PC

2020-12-15 18:44:11

by Andi Kleen

[permalink] [raw]
Subject: Re: [RFC PATCH 0/7] Share events between metrics

> Or, is the concern more about trying to time-slice the results in a
> fairly granular way and expecting accurate results then?

Usually the later. It's especially important for divisions. You want
both divisor and dividend to be in the same time slice, otherwise
the result usually doesn't make a lot of sense.

-Andi