Skip to content

Commit ded80bd

Browse files
captain5050acmel
authored andcommitted
perf expr: Migrate expr ids table to a 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. Original map suggestion by Andi Kleen: https://lore.kernel.org/lkml/20200224210308.GQ160988@tassilo.jf.intel.com/ and seconded by Jiri Olsa: https://lore.kernel.org/lkml/20200423112915.GH1136647@krava/ Committer notes: There are fixes that need to land upstream before we can use libbpf's headers, for now use our copy unconditionally, since the data structures at this point are exactly the same, no problem. When the fixes for libbpf's hashmap land upstream, we can fix this up. Testing it: Building with LIBBPF=1, i.e. the default: $ perf -vv | grep -i bpf bpf: [ on ] # HAVE_LIBBPF_SUPPORT $ nm ~/bin/perf | grep -i libbpf_ | wc -l 39 $ nm ~/bin/perf | grep -i hashmap_ | wc -l 17 $ Explicitely building without LIBBPF: $ perf -vv | grep -i bpf bpf: [ OFF ] # HAVE_LIBBPF_SUPPORT $ $ nm ~/bin/perf | grep -i libbpf_ | wc -l 0 $ nm ~/bin/perf | grep -i hashmap_ | wc -l 9 $ Signed-off-by: Ian Rogers <irogers@google.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Andi Kleen <ak@linux.intel.com> Cc: Andrii Nakryiko <andriin@fb.com> Cc: Cong Wang <xiyou.wangcong@gmail.com> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Jin Yao <yao.jin@linux.intel.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: John Fastabend <john.fastabend@gmail.com> Cc: John Garry <john.garry@huawei.com> Cc: Kajol Jain <kjain@linux.ibm.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Kim Phillips <kim.phillips@amd.com> Cc: Leo Yan <leo.yan@linaro.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Martin KaFai Lau <kafai@fb.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Song Liu <songliubraving@fb.com> Cc: Stephane Eranian <eranian@google.com> Cc: Yonghong Song <yhs@fb.com> Cc: bpf@vger.kernel.org Cc: kp singh <kpsingh@chromium.org> Cc: netdev@vger.kernel.org Link: http://lore.kernel.org/lkml/20200515221732.44078-8-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent eee1950 commit ded80bd

7 files changed

Lines changed: 200 additions & 190 deletions

File tree

‎tools/perf/tests/expr.c‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
1919
int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
2020
{
2121
const char *p;
22-
const char **other;
23-
double val;
24-
int i, ret;
22+
double val, *val_ptr;
23+
int ret;
2524
struct expr_parse_ctx ctx;
26-
int num_other;
2725

2826
expr__ctx_init(&ctx);
29-
expr__add_id(&ctx, "FOO", 1);
30-
expr__add_id(&ctx, "BAR", 2);
27+
expr__add_id(&ctx, strdup("FOO"), 1);
28+
expr__add_id(&ctx, strdup("BAR"), 2);
3129

3230
ret = test(&ctx, "1+1", 2);
3331
ret |= test(&ctx, "FOO+BAR", 3);
@@ -52,25 +50,29 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
5250
ret = expr__parse(&val, &ctx, p, 1);
5351
TEST_ASSERT_VAL("missing operand", ret == -1);
5452

53+
expr__ctx_clear(&ctx);
5554
TEST_ASSERT_VAL("find other",
56-
expr__find_other("FOO + BAR + BAZ + BOZO", "FOO", &other, &num_other, 1) == 0);
57-
TEST_ASSERT_VAL("find other", num_other == 3);
58-
TEST_ASSERT_VAL("find other", !strcmp(other[0], "BAR"));
59-
TEST_ASSERT_VAL("find other", !strcmp(other[1], "BAZ"));
60-
TEST_ASSERT_VAL("find other", !strcmp(other[2], "BOZO"));
61-
TEST_ASSERT_VAL("find other", other[3] == NULL);
55+
expr__find_other("FOO + BAR + BAZ + BOZO", "FOO",
56+
&ctx, 1) == 0);
57+
TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 3);
58+
TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAR",
59+
(void **)&val_ptr));
60+
TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAZ",
61+
(void **)&val_ptr));
62+
TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BOZO",
63+
(void **)&val_ptr));
6264

65+
expr__ctx_clear(&ctx);
6366
TEST_ASSERT_VAL("find other",
64-
expr__find_other("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@", NULL,
65-
&other, &num_other, 3) == 0);
66-
TEST_ASSERT_VAL("find other", num_other == 2);
67-
TEST_ASSERT_VAL("find other", !strcmp(other[0], "EVENT1,param=3/"));
68-
TEST_ASSERT_VAL("find other", !strcmp(other[1], "EVENT2,param=3/"));
69-
TEST_ASSERT_VAL("find other", other[2] == NULL);
67+
expr__find_other("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
68+
NULL, &ctx, 3) == 0);
69+
TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 2);
70+
TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT1,param=3/",
71+
(void **)&val_ptr));
72+
TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT2,param=3/",
73+
(void **)&val_ptr));
7074

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

7577
return 0;
7678
}

‎tools/perf/tests/pmu-events.c‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,6 @@ static int test_parsing(void)
437437
struct pmu_events_map *map;
438438
struct pmu_event *pe;
439439
int i, j, k;
440-
const char **ids;
441-
int idnum;
442440
int ret = 0;
443441
struct expr_parse_ctx ctx;
444442
double result;
@@ -450,39 +448,42 @@ static int test_parsing(void)
450448
break;
451449
j = 0;
452450
for (;;) {
451+
struct hashmap_entry *cur;
452+
size_t bkt;
453+
453454
pe = &map->table[j++];
454455
if (!pe->name && !pe->metric_group && !pe->metric_name)
455456
break;
456457
if (!pe->metric_expr)
457458
continue;
458-
if (expr__find_other(pe->metric_expr, NULL,
459-
&ids, &idnum, 0) < 0) {
459+
expr__ctx_init(&ctx);
460+
if (expr__find_other(pe->metric_expr, NULL, &ctx, 0)
461+
< 0) {
460462
expr_failure("Parse other failed", map, pe);
461463
ret++;
462464
continue;
463465
}
464-
expr__ctx_init(&ctx);
465466

466467
/*
467468
* Add all ids with a made up value. The value may
468469
* trigger divide by zero when subtracted and so try to
469470
* make them unique.
470471
*/
471-
for (k = 0; k < idnum; k++)
472-
expr__add_id(&ctx, ids[k], k + 1);
472+
k = 1;
473+
hashmap__for_each_entry((&ctx.ids), cur, bkt)
474+
expr__add_id(&ctx, strdup(cur->key), k++);
473475

474-
for (k = 0; k < idnum; k++) {
475-
if (check_parse_id(ids[k], map == cpus_map, pe))
476+
hashmap__for_each_entry((&ctx.ids), cur, bkt) {
477+
if (check_parse_id(cur->key, map == cpus_map,
478+
pe))
476479
ret++;
477480
}
478481

479482
if (expr__parse(&result, &ctx, pe->metric_expr, 0)) {
480483
expr_failure("Parse failed", map, pe);
481484
ret++;
482485
}
483-
for (k = 0; k < idnum; k++)
484-
zfree(&ids[k]);
485-
free(ids);
486+
expr__ctx_clear(&ctx);
486487
}
487488
}
488489
/* TODO: fail when not ok */

‎tools/perf/util/expr.c‎

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,76 @@
44
#include "expr.h"
55
#include "expr-bison.h"
66
#include "expr-flex.h"
7+
#include <linux/kernel.h>
78

89
#ifdef PARSER_DEBUG
910
extern int expr_debug;
1011
#endif
1112

13+
static size_t key_hash(const void *key, void *ctx __maybe_unused)
14+
{
15+
const char *str = (const char *)key;
16+
size_t hash = 0;
17+
18+
while (*str != '\0') {
19+
hash *= 31;
20+
hash += *str;
21+
str++;
22+
}
23+
return hash;
24+
}
25+
26+
static bool key_equal(const void *key1, const void *key2,
27+
void *ctx __maybe_unused)
28+
{
29+
return !strcmp((const char *)key1, (const char *)key2);
30+
}
31+
1232
/* Caller must make sure id is allocated */
13-
void expr__add_id(struct expr_parse_ctx *ctx, const char *name, double val)
33+
int expr__add_id(struct expr_parse_ctx *ctx, const char *name, double val)
1434
{
15-
int idx;
35+
double *val_ptr = NULL, *old_val = NULL;
36+
char *old_key = NULL;
37+
int ret;
38+
39+
if (val != 0.0) {
40+
val_ptr = malloc(sizeof(double));
41+
if (!val_ptr)
42+
return -ENOMEM;
43+
*val_ptr = val;
44+
}
45+
ret = hashmap__set(&ctx->ids, name, val_ptr,
46+
(const void **)&old_key, (void **)&old_val);
47+
free(old_key);
48+
free(old_val);
49+
return ret;
50+
}
51+
52+
int expr__get_id(struct expr_parse_ctx *ctx, const char *id, double *val_ptr)
53+
{
54+
double *data;
1655

17-
assert(ctx->num_ids < MAX_PARSE_ID);
18-
idx = ctx->num_ids++;
19-
ctx->ids[idx].name = name;
20-
ctx->ids[idx].val = val;
56+
if (!hashmap__find(&ctx->ids, id, (void **)&data))
57+
return -1;
58+
*val_ptr = (data == NULL) ? 0.0 : *data;
59+
return 0;
2160
}
2261

2362
void expr__ctx_init(struct expr_parse_ctx *ctx)
2463
{
25-
ctx->num_ids = 0;
64+
hashmap__init(&ctx->ids, key_hash, key_equal, NULL);
65+
}
66+
67+
void expr__ctx_clear(struct expr_parse_ctx *ctx)
68+
{
69+
struct hashmap_entry *cur;
70+
size_t bkt;
71+
72+
hashmap__for_each_entry((&ctx->ids), cur, bkt) {
73+
free((char *)cur->key);
74+
free(cur->value);
75+
}
76+
hashmap__clear(&ctx->ids);
2677
}
2778

2879
static int
@@ -56,61 +107,25 @@ __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
56107
return ret;
57108
}
58109

59-
int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr, int runtime)
110+
int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
111+
const char *expr, int runtime)
60112
{
61113
return __expr__parse(final_val, ctx, expr, EXPR_PARSE, runtime) ? -1 : 0;
62114
}
63115

64-
static bool
65-
already_seen(const char *val, const char *one, const char **other,
66-
int num_other)
67-
{
68-
int i;
69-
70-
if (one && !strcasecmp(one, val))
71-
return true;
72-
for (i = 0; i < num_other; i++)
73-
if (!strcasecmp(other[i], val))
74-
return true;
75-
return false;
76-
}
77-
78-
int expr__find_other(const char *expr, const char *one, const char ***other,
79-
int *num_other, int runtime)
116+
int expr__find_other(const char *expr, const char *one,
117+
struct expr_parse_ctx *ctx, int runtime)
80118
{
81-
int err, i = 0, j = 0;
82-
struct expr_parse_ctx ctx;
83-
84-
expr__ctx_init(&ctx);
85-
err = __expr__parse(NULL, &ctx, expr, EXPR_OTHER, runtime);
86-
if (err)
87-
return -1;
88-
89-
*other = malloc((ctx.num_ids + 1) * sizeof(char *));
90-
if (!*other)
91-
return -ENOMEM;
92-
93-
for (i = 0, j = 0; i < ctx.num_ids; i++) {
94-
const char *str = ctx.ids[i].name;
95-
96-
if (already_seen(str, one, *other, j))
97-
continue;
98-
99-
str = strdup(str);
100-
if (!str)
101-
goto out;
102-
(*other)[j++] = str;
103-
}
104-
(*other)[j] = NULL;
105-
106-
out:
107-
if (i != ctx.num_ids) {
108-
while (--j)
109-
free((char *) (*other)[i]);
110-
free(*other);
111-
err = -1;
119+
double *old_val = NULL;
120+
char *old_key = NULL;
121+
int ret = __expr__parse(NULL, ctx, expr, EXPR_OTHER, runtime);
122+
123+
if (one) {
124+
hashmap__delete(&ctx->ids, one,
125+
(const void **)&old_key, (void **)&old_val);
126+
free(old_key);
127+
free(old_val);
112128
}
113129

114-
*num_other = j;
115-
return err;
130+
return ret;
116131
}

‎tools/perf/util/expr.h‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
#ifndef PARSE_CTX_H
33
#define PARSE_CTX_H 1
44

5-
#define EXPR_MAX_OTHER 64
6-
#define MAX_PARSE_ID EXPR_MAX_OTHER
7-
8-
struct expr_parse_id {
9-
const char *name;
10-
double val;
11-
};
5+
// There are fixes that need to land upstream before we can use libbpf's headers,
6+
// for now use our copy uncoditionally, since the data structures at this point
7+
// are exactly the same, no problem.
8+
//#ifdef HAVE_LIBBPF_SUPPORT
9+
//#include <bpf/hashmap.h>
10+
//#else
11+
#include "util/hashmap.h"
12+
//#endif
1213

1314
struct expr_parse_ctx {
14-
int num_ids;
15-
struct expr_parse_id ids[MAX_PARSE_ID];
15+
struct hashmap ids;
1616
};
1717

1818
struct expr_scanner_ctx {
@@ -21,9 +21,12 @@ struct expr_scanner_ctx {
2121
};
2222

2323
void expr__ctx_init(struct expr_parse_ctx *ctx);
24-
void expr__add_id(struct expr_parse_ctx *ctx, const char *id, double val);
25-
int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr, int runtime);
26-
int expr__find_other(const char *expr, const char *one, const char ***other,
27-
int *num_other, int runtime);
24+
void expr__ctx_clear(struct expr_parse_ctx *ctx);
25+
int expr__add_id(struct expr_parse_ctx *ctx, const char *id, double val);
26+
int expr__get_id(struct expr_parse_ctx *ctx, const char *id, double *val_ptr);
27+
int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
28+
const char *expr, int runtime);
29+
int expr__find_other(const char *expr, const char *one,
30+
struct expr_parse_ctx *ids, int runtime);
2831

2932
#endif

‎tools/perf/util/expr.y‎

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,6 @@ static void expr_error(double *final_val __maybe_unused,
4747
pr_debug("%s\n", s);
4848
}
4949

50-
static int lookup_id(struct expr_parse_ctx *ctx, char *id, double *val)
51-
{
52-
int i;
53-
54-
for (i = 0; i < ctx->num_ids; i++) {
55-
if (!strcasecmp(ctx->ids[i].name, id)) {
56-
*val = ctx->ids[i].val;
57-
return 0;
58-
}
59-
}
60-
return -1;
61-
}
62-
6350
%}
6451
%%
6552

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

7461
other: ID
7562
{
76-
if (ctx->num_ids + 1 >= EXPR_MAX_OTHER) {
77-
pr_err("failed: way too many variables");
78-
YYABORT;
79-
}
80-
81-
ctx->ids[ctx->num_ids++].name = $1;
63+
expr__add_id(ctx, $1, 0.0);
8264
}
8365
|
8466
MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
@@ -93,7 +75,7 @@ if_expr:
9375
;
9476

9577
expr: NUMBER
96-
| ID { if (lookup_id(ctx, $1, &$$) < 0) {
78+
| ID { if (expr__get_id(ctx, $1, &$$)) {
9779
pr_debug("%s not found\n", $1);
9880
free($1);
9981
YYABORT;

0 commit comments

Comments
 (0)