Skip to contentSkip to conversation

Copy hashmap to libapi, use in perf expr

PATCH Superseded Ian Rogers 3 participants 14 messages May 15, 2020

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

cover Copy hashmap to libapi, use in perf expr

Perf's expr code currently builds an array of strings then removes duplicates. The array is larger than necessary and has recently been increased in size. When this was done it was commented that a hashmap would be preferable.

libbpf has a hashmap but libbpf isn't currently required to build perf. To satisfy various concerns this change copies libbpf's hashmap into libapi, it then adds a check in perf that the two are in sync.

Andrii's patch to hashmap from bpf-next is brought into this set to fix issues with hashmap__clear.

Three minor changes to libbpf's hashmap are made that remove an unused dependency, fix a compiler warning and make sure the hashmap isn't part of the symbols in a static build of libbpf (dsos are handled by the existing version script).

Two perf test are also brought in as they need refactoring to account for the expr API change and it is expected they will land aheadof this.
https://lore.kernel.org/lkml/[email protected]/

Tested with 'perf test' and make -C tools/perf build-test.

The hashmap change was originally part of an RFC:
https://lore.kernel.org/lkml/[email protected]/

Andrii Nakryiko (1):
libbpf: Fix memory leak and possible double-free in hashmap__clear

Ian Rogers (7):
libbpf hashmap: Remove unused #include
libbpf hashmap: Fix signedness warnings
libbpf hashmap: Localize static hashmap__* symbols tools lib/api: Copy libbpf hashmap to libapi
perf test: Provide a subtest callback to ask for the reason for
skipping a subtest
perf test: Improve pmu event metric testing
perf expr: Migrate expr ids table to a hashmap

 tools/lib/api/Build             |   1 +
 tools/lib/api/hashmap.c         | 238 ++++++++++++++++++++++++++++++++
 tools/lib/api/hashmap.h         | 177 ++++++++++++++++++++++++
 tools/lib/bpf/Makefile          |   2 +
 tools/lib/bpf/hashmap.c         |  10 +-
 tools/lib/bpf/hashmap.h         |   1 -
 tools/perf/check-headers.sh     |   4 +
 tools/perf/tests/builtin-test.c |  18 ++-
 tools/perf/tests/expr.c         |  40 +++---
 tools/perf/tests/pmu-events.c   | 169 ++++++++++++++++++++++-
 tools/perf/tests/tests.h        |   4 +
 tools/perf/util/expr.c          | 129 +++++++++--------
 tools/perf/util/expr.h          |  22 ++-
 tools/perf/util/expr.y          |  22 +--
 tools/perf/util/metricgroup.c   |  87 ++++++------
 tools/perf/util/stat-shadow.c   |  49 ++++---
 16 files changed, 793 insertions(+), 180 deletions(-)
 create mode 100644 tools/lib/api/hashmap.c
 create mode 100644 tools/lib/api/hashmap.h
Signature · 1 line
2.26.2.761.g0e0b3e54be-goog
author reviewer
#1 lore raw

PATCH 1/8 libbpf: Fix memory leak and possible double-free in hashmap__clear

From: Andrii Nakryiko <[email protected]>

Fix memory leak in hashmap_clear() not freeing hashmap_entry structs for each of the remaining entries. Also NULL-out bucket list to prevent possible double-free between hashmap__clear() and hashmap__free().

Running test_progs-asan flavor clearly showed this problem.

---
 tools/lib/bpf/hashmap.c | 7 +++++++
 1 file changed, 7 insertions(+)
tools/lib/bpf/hashmap.c +7 -0
index 54c30c802070..cffb96202e0d 100644
@@ -59,7 +59,14 @@ struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
 
 void hashmap__clear(struct hashmap *map)
 {
+	struct hashmap_entry *cur, *tmp;
+	int bkt;
+
+	hashmap__for_each_entry_safe(map, cur, tmp, bkt) {
+		free(cur);
+	}
 	free(map->buckets);
+	map->buckets = NULL;
 	map->cap = map->cap_bits = map->sz = 0;
 }
 
Signature · 1 line
2.26.2.761.g0e0b3e54be-goog
author reviewer
#2 lore raw

PATCH 2/8 libbpf hashmap: Remove unused #include

In reply to Ian Rogers

Remove #include of libbpf_internal.h that is unused. Discussed in this thread:
https://lore.kernel.org/lkml/CAEf4BzZRmiEds_8R8g4vaAeWvJzPb4xYLnpF0X2VNY8oTzkphQ@mail.gmail.com/

---
 tools/lib/bpf/hashmap.h | 1 -
 1 file changed, 1 deletion(-)
tools/lib/bpf/hashmap.h +0 -1
index bae8879cdf58..e823b35e7371 100644
@@ -15,7 +15,6 @@
 #else
 #include <bits/reg.h>
 #endif
-#include "libbpf_internal.h"
 
 static inline size_t hash_bits(size_t h, int bits)
 {
Signature · 1 line
2.26.2.761.g0e0b3e54be-goog
author reviewer
#3 lore raw

PATCH 3/8 libbpf hashmap: Fix signedness warnings

In reply to Ian Rogers

Fixes the following warnings:

hashmap.c: In function ‘hashmap__clear’:
hashmap.h:150:20: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
150 | for (bkt = 0; bkt < map->cap; bkt++) \

hashmap.c: In function ‘hashmap_grow’:
hashmap.h:150:20: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
150 | for (bkt = 0; bkt < map->cap; bkt++) \

---
 tools/lib/bpf/hashmap.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/lib/bpf/hashmap.c +2 -3
index cffb96202e0d..a405dad068f5 100644
@@ -60,7 +60,7 @@ struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
 void hashmap__clear(struct hashmap *map)
 {
 	struct hashmap_entry *cur, *tmp;
-	int bkt;
+	size_t bkt;
 
 	hashmap__for_each_entry_safe(map, cur, tmp, bkt) {
 		free(cur);
@@ -100,8 +100,7 @@ static int hashmap_grow(struct hashmap *map)
 	struct hashmap_entry **new_buckets;
 	struct hashmap_entry *cur, *tmp;
 	size_t new_cap_bits, new_cap;
-	size_t h;
-	int bkt;
+	size_t h, bkt;
 
 	new_cap_bits = map->cap_bits + 1;
 	if (new_cap_bits < HASHMAP_MIN_CAP_BITS)
Signature · 1 line
2.26.2.761.g0e0b3e54be-goog
author reviewer
#4 lore raw

PATCH 4/8 libbpf hashmap: Localize static hashmap__* symbols

In reply to Ian Rogers

Localize the hashmap__* symbols in libbpf.a. To allow for a version in libapi.

Before:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 T hashmap__capacity
000000000002099c T hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 T hashmap__delete
0000000000020f29 T hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 T hashmap__free
0000000000020b08 t hashmap_grow
00000000000208dd T hashmap__init
0000000000020d35 T hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 T hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 T hashmap__size
...

After:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 t hashmap__capacity
000000000002099c t hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 t hashmap__delete
0000000000020f29 t hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 t hashmap__free
0000000000020b08 t hashmap_grow
00000000000208dd t hashmap__init
0000000000020d35 t hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 t hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 t hashmap__size
...
---
 tools/lib/bpf/Makefile | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/Makefile +2 -0
index aee7f1a83c77..4a1cdbceb04e 100644
@@ -20,6 +20,7 @@ srctree := $(patsubst %/,%,$(dir $(srctree)))
 endif
 
 INSTALL = install
+OBJCOPY ?= objcopy
 
 # Use DESTDIR for installing into a different root directory.
 # This is useful for building a package. The program will be
@@ -181,6 +182,7 @@ $(BPF_IN_SHARED): force elfdep zdep bpfdep $(BPF_HELPER_DEFS)
 
 $(BPF_IN_STATIC): force elfdep zdep bpfdep $(BPF_HELPER_DEFS)
 	$(Q)$(MAKE) $(build)=libbpf OUTPUT=$(STATIC_OBJDIR)
+	$(Q)$(OBJCOPY) -w -L hashmap__\* $@
 
 $(BPF_HELPER_DEFS): $(srctree)/tools/include/uapi/linux/bpf.h
 	$(QUIET_GEN)$(srctree)/scripts/bpf_helpers_doc.py --header \
Signature · 1 line
2.26.2.761.g0e0b3e54be-goog
#5 lore raw

Re: [PATCH 4/8] libbpf hashmap: Localize static hashmap__* symbols

Quoted 38 lines · Ian Rogers

Localize the hashmap__* symbols in libbpf.a. To allow for a version in libapi.

Before:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 T hashmap__capacity
000000000002099c T hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 T hashmap__delete
0000000000020f29 T hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 T hashmap__free
0000000000020b08 t hashmap_grow
00000000000208dd T hashmap__init
0000000000020d35 T hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 T hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 T hashmap__size
...

After:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 t hashmap__capacity
000000000002099c t hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 t hashmap__delete
0000000000020f29 t hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 t hashmap__free
0000000000020b08 t hashmap_grow
Quoted 8 lines
00000000000208dd t hashmap__init
0000000000020d35 t hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 t hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 t hashmap__size
...

I think this will break bpf selftests which use hashmap, we need to find some other way to include this

either to use it from libbpf directly, or use the api version only if the libbpf is not compiled in perf, we could use following to detect that:

      CFLAGS += -DHAVE_LIBBPF_SUPPORT
      $(call detected,CONFIG_LIBBPF)

jirka

#6 lore raw

Re: [PATCH 4/8] libbpf hashmap: Localize static hashmap__* symbols

Em Fri, May 15, 2020 at 11:17:07AM +0200, Jiri Olsa escreveu:

Quoted 51 lines · Ian Rogers
Quoted 38 lines

Localize the hashmap__* symbols in libbpf.a. To allow for a version in libapi.

Before:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 T hashmap__capacity
000000000002099c T hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 T hashmap__delete
0000000000020f29 T hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 T hashmap__free
0000000000020b08 t hashmap_grow
00000000000208dd T hashmap__init
0000000000020d35 T hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 T hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 T hashmap__size
...

After:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 t hashmap__capacity
000000000002099c t hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 t hashmap__delete
0000000000020f29 t hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 t hashmap__free
0000000000020b08 t hashmap_grow
Quoted 8 lines
00000000000208dd t hashmap__init
0000000000020d35 t hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 t hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 t hashmap__size
...

I think this will break bpf selftests which use hashmap, we need to find some other way to include this

Quoted 6 lines

either to use it from libbpf directly, or use the api version only if the libbpf is not compiled in perf, we could use following to detect that:

      CFLAGS += -DHAVE_LIBBPF_SUPPORT
      $(call detected,CONFIG_LIBBPF)

And have it in tools/perf/util/ instead?

- Arnaldo

author reviewer
#7 lore raw

Re: [PATCH 4/8] libbpf hashmap: Localize static hashmap__* symbols

Quoted 57 lines · Arnaldo Carvalho de Melo

Em Fri, May 15, 2020 at 11:17:07AM +0200, Jiri Olsa escreveu:

Quoted 55 lines · Ian Rogers
Quoted 38 lines

Localize the hashmap__* symbols in libbpf.a. To allow for a version in libapi.

Before:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 T hashmap__capacity
000000000002099c T hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 T hashmap__delete
0000000000020f29 T hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 T hashmap__free
0000000000020b08 t hashmap_grow
00000000000208dd T hashmap__init
0000000000020d35 T hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 T hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 T hashmap__size
...

After:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 t hashmap__capacity
000000000002099c t hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 t hashmap__delete
0000000000020f29 t hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 t hashmap__free
0000000000020b08 t hashmap_grow
Quoted 8 lines
00000000000208dd t hashmap__init
0000000000020d35 t hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 t hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 t hashmap__size
...

I think this will break bpf selftests which use hashmap, we need to find some other way to include this

either to use it from libbpf directly, or use the api version only if the libbpf is not compiled in perf, we could use following to detect that:

Quoted 7 lines
Quoted 2 lines
      CFLAGS += -DHAVE_LIBBPF_SUPPORT
      $(call detected,CONFIG_LIBBPF)

And have it in tools/perf/util/ instead?

- Arnaldo

*sigh*

$ make -C tools/testing/selftests/bpf test_hashmap
make: Entering directory
'/usr/local/google/home/irogers/kernel-trees/kernel.org/tip/tools/testing/s
elftests/bpf'
 BINARY   test_hashmap
/usr/bin/ld: /tmp/ccEGGNw5.o: in function `test_hashmap_generic':
/usr/local/google/home/irogers/kernel-trees/kernel.org/tip/tools/testing/selftests/bpf/test_hashmap.
c:61: undefined reference to `hashmap__new'
...

My preference was to make hashmap a sharable API in tools, to benefit not just perf but say things like libsymbol, libperf, etc. Moving it into perf and using conditional compilation is kinda gross but having libbpf tests depend on libapi also isn't ideal I guess. It is tempting to just cut a hashmap from fresh cloth to avoid this and to share among tools/. I don't know if the bpf folks have opinions?

I'll do a v2 using conditional compilation to see how bad it looks.

Thanks,
Ian

#8 lore raw

Re: [PATCH 4/8] libbpf hashmap: Localize static hashmap__* symbols

Em Fri, May 15, 2020 at 07:53:33AM -0700, Ian Rogers escreveu:

Quoted 76 lines
Quoted 53 lines · Arnaldo Carvalho de Melo

Em Fri, May 15, 2020 at 11:17:07AM +0200, Jiri Olsa escreveu:

Quoted 51 lines · Ian Rogers
Quoted 38 lines

Localize the hashmap__* symbols in libbpf.a. To allow for a version in libapi.

Before:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 T hashmap__capacity
000000000002099c T hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 T hashmap__delete
0000000000020f29 T hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 T hashmap__free
0000000000020b08 t hashmap_grow
00000000000208dd T hashmap__init
0000000000020d35 T hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 T hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 T hashmap__size
...

After:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 t hashmap__capacity
000000000002099c t hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 t hashmap__delete
0000000000020f29 t hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 t hashmap__free
0000000000020b08 t hashmap_grow
Quoted 8 lines
00000000000208dd t hashmap__init
0000000000020d35 t hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 t hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 t hashmap__size
...

I think this will break bpf selftests which use hashmap, we need to find some other way to include this

Quoted 8 lines
Quoted 6 lines

either to use it from libbpf directly, or use the api version only if the libbpf is not compiled in perf, we could use following to detect that:

      CFLAGS += -DHAVE_LIBBPF_SUPPORT
      $(call detected,CONFIG_LIBBPF)

And have it in tools/perf/util/ instead?

*sigh*

$ make -C tools/testing/selftests/bpf test_hashmap
make: Entering directory
'/usr/local/google/home/irogers/kernel-trees/kernel.org/tip/tools/testing/s
elftests/bpf'
 BINARY   test_hashmap
/usr/bin/ld: /tmp/ccEGGNw5.o: in function `test_hashmap_generic':
/usr/local/google/home/irogers/kernel-trees/kernel.org/tip/tools/testing/selftests/bpf/test_hashmap.
c:61: undefined reference to `hashmap__new'
...
Quoted 1 line

My preference was to make hashmap a sharable API in tools, to benefit

That is my preference as well, I'm not defending having it in tools/perf/util/, just saying that that is a possible way to make progress with the current situation...

Quoted 7 lines

not just perf but say things like libsymbol, libperf, etc. Moving it into perf and using conditional compilation is kinda gross but having libbpf tests depend on libapi also isn't ideal I guess. It is tempting to just cut a hashmap from fresh cloth to avoid this and to share among tools/. I don't know if the bpf folks have opinions?

I'll do a v2 using conditional compilation to see how bad it looks.

Cool, lets see how it looks.

- Arnaldo

author reviewer
#9 lore raw

Re: [PATCH 4/8] libbpf hashmap: Localize static hashmap__* symbols

Quoted 78 lines · Arnaldo Carvalho de Melo

Em Fri, May 15, 2020 at 07:53:33AM -0700, Ian Rogers escreveu:

Quoted 75 lines
Quoted 53 lines · Arnaldo Carvalho de Melo

Em Fri, May 15, 2020 at 11:17:07AM +0200, Jiri Olsa escreveu:

Quoted 51 lines · Ian Rogers
Quoted 38 lines

Localize the hashmap__* symbols in libbpf.a. To allow for a version in libapi.

Before:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 T hashmap__capacity
000000000002099c T hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 T hashmap__delete
0000000000020f29 T hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 T hashmap__free
0000000000020b08 t hashmap_grow
00000000000208dd T hashmap__init
0000000000020d35 T hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 T hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 T hashmap__size
...

After:

$ nm libbpf.a
...
000000000002088a t hashmap_add_entry
000000000001712a t hashmap__append
0000000000020aa3 t hashmap__capacity
000000000002099c t hashmap__clear
00000000000208b3 t hashmap_del_entry
0000000000020fc1 t hashmap__delete
0000000000020f29 t hashmap__find
0000000000020c6c t hashmap_find_entry
0000000000020a61 t hashmap__free
0000000000020b08 t hashmap_grow
Quoted 8 lines
00000000000208dd t hashmap__init
0000000000020d35 t hashmap__insert
0000000000020ab5 t hashmap_needs_to_grow
0000000000020947 t hashmap__new
0000000000000775 t hashmap__set
00000000000212f8 t hashmap__set
0000000000020a91 t hashmap__size
...

I think this will break bpf selftests which use hashmap, we need to find some other way to include this

Quoted 8 lines
Quoted 6 lines

either to use it from libbpf directly, or use the api version only if the libbpf is not compiled in perf, we could use following to detect that:

      CFLAGS += -DHAVE_LIBBPF_SUPPORT
      $(call detected,CONFIG_LIBBPF)

And have it in tools/perf/util/ instead?

*sigh*

$ make -C tools/testing/selftests/bpf test_hashmap
make: Entering directory
'/usr/local/google/home/irogers/kernel-trees/kernel.org/tip/tools/testing/s
elftests/bpf'
 BINARY   test_hashmap
/usr/bin/ld: /tmp/ccEGGNw5.o: in function `test_hashmap_generic':
/usr/local/google/home/irogers/kernel-trees/kernel.org/tip/tools/testing/selftests/bpf/test_hashmap.
c:61: undefined reference to `hashmap__new'
...
Quoted 5 lines
Quoted 1 line

My preference was to make hashmap a sharable API in tools, to benefit

That is my preference as well, I'm not defending having it in tools/perf/util/, just saying that that is a possible way to make progress with the current situation...

Thanks, it'd be nice to be expedient as both Jiri and myself are changing code in this area. v2 is up for review here:
https://lore.kernel.org/lkml/[email protected]/
An ifdef when the hashmap.h is used, and one in the build. It could be worse.

Thanks,
Ian

Quoted 11 lines
Quoted 7 lines

not just perf but say things like libsymbol, libperf, etc. Moving it into perf and using conditional compilation is kinda gross but having libbpf tests depend on libapi also isn't ideal I guess. It is tempting to just cut a hashmap from fresh cloth to avoid this and to share among tools/. I don't know if the bpf folks have opinions?

I'll do a v2 using conditional compilation to see how bad it looks.

Cool, lets see how it looks.

- Arnaldo

author reviewer
#10 lore raw

PATCH 5/8 tools lib/api: Copy libbpf hashmap to libapi

In reply to Ian Rogers

Allow use of hashmap in more than just libbpf, where it isn't an exported symbol. Place in libapi as that is a required part of tools/perf whereas libbpf is currently optional.
Modify perf's check-headers.sh script to check that the files are kept in sync, in the same way kernel headers are checked. This will warn if they are out of sync at the start of a perf build.

---
 tools/lib/api/Build         |   1 +
 tools/lib/api/hashmap.c     | 238 ++++++++++++++++++++++++++++++++++++
 tools/lib/api/hashmap.h     | 177 +++++++++++++++++++++++++++
 tools/perf/check-headers.sh |   4 +
 4 files changed, 420 insertions(+)
 create mode 100644 tools/lib/api/hashmap.c
 create mode 100644 tools/lib/api/hashmap.h
tools/lib/api/Build +1 -0
index 6e2373db5598..2c8787a88080 100644
@@ -2,6 +2,7 @@ libapi-y += fd/
 libapi-y += fs/
 libapi-y += cpu.o
 libapi-y += debug.o
+libapi-y += hashmap.o
 libapi-y += str_error_r.o
 
 $(OUTPUT)str_error_r.o: ../str_error_r.c FORCE
tools/lib/api/hashmap.c new file +238 -0

238 lines not on this page · raw message

tools/lib/api/hashmap.h new file +177 -0

177 lines not on this page · raw message

tools/perf/check-headers.sh +4 -0
index cf147db4e5ca..87fcffa33a4d 100755
@@ -128,4 +128,8 @@ check arch/x86/lib/insn.c             '-I "^#include [\"<]\(../include/\)*asm/in
 # diff non-symmetric files
 check_2 tools/perf/arch/x86/entry/syscalls/syscall_64.tbl arch/x86/entry/syscalls/syscall_64.tbl
 
+# check duplicated library files
+check_2 tools/lib/api/hashmap.h tools/lib/bpf/hashmap.h
+check_2 tools/lib/api/hashmap.c tools/lib/bpf/hashmap.c
+
 cd tools/perf
Signature · 1 line
2.26.2.761.g0e0b3e54be-goog
author reviewer
#11 lore raw

PATCH 6/8 perf test: Provide a subtest callback to ask for the reason for skipping a subtest

In reply to Ian Rogers

Now subtests can inform why a test was skipped. The upcoming patch improvint PMU event metric testing will use it.

Cc Adrian Hunter <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Jin Yao <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc John Garry <[email protected]>
Cc Kajol Jain <[email protected]>
Cc Kan Liang <[email protected]>
Cc Leo Yan <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Clarke <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
[ split from a larger patch ]
---
 tools/perf/tests/builtin-test.c | 11 +++++++++--
 tools/perf/tests/tests.h        |  1 +
 2 files changed, 10 insertions(+), 2 deletions(-)
tools/perf/tests/builtin-test.c +9 -2
index 3471ec52ea11..baee735e6aa5 100644
@@ -429,8 +429,15 @@ static int test_and_print(struct test *t, bool force_skip, int subtest)
 	case TEST_OK:
 		pr_info(" Ok\n");
 		break;
-	case TEST_SKIP:
-		color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
+	case TEST_SKIP: {
+		const char *skip_reason = NULL;
+		if (t->subtest.skip_reason)
+			skip_reason = t->subtest.skip_reason(subtest);
+		if (skip_reason)
+			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", skip_reason);
+		else
+			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
+	}
 		break;
 	case TEST_FAIL:
 	default:
tools/perf/tests/tests.h +1 -0
index d6d4ac34eeb7..88e45aeab94f 100644
@@ -34,6 +34,7 @@ struct test {
 		bool skip_if_fail;
 		int (*get_nr)(void);
 		const char *(*get_desc)(int subtest);
+		const char *(*skip_reason)(int subtest);
 	} subtest;
 	bool (*is_supported)(void);
 	void *priv;
Signature · 1 line
2.26.2.761.g0e0b3e54be-goog
author reviewer
#12 lore raw

PATCH 7/8 perf test: Improve pmu event metric testing

In reply to Ian Rogers

Break pmu-events test into 2 and add a test to verify that all pmu metric expressions simply parse. Try to parse all metric ids/events, skip/warn if metrics for the current architecture fail to parse. To support warning for a skip, and an ability for a subtest to describe why it skips.

Tested on power9, skylakex, haswell, broadwell, westmere, sandybridge and ivybridge.

May skip/warn on other architectures if metrics are invalid. In particular s390 is untested, but its expressions are trivial. The untested architectures with expressions are power8, cascadelakex, tremontx, skylake, jaketown, ivytown and variants of haswell and broadwell.

v3. addresses review comments from John Garry <[email protected]>, Jiri Olsa <[email protected]> and Arnaldo Carvalho de Melo <[email protected]>.
v2. changes the commit message as event parsing errors no longer cause the test to fail.

Cc Adrian Hunter <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Jin Yao <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc John Garry <[email protected]>
Cc Kajol Jain <[email protected]>
Cc Kan Liang <[email protected]>
Cc Leo Yan <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Clarke <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
[ split from a larger patch ]
---
 tools/perf/tests/builtin-test.c |   7 ++
 tools/perf/tests/pmu-events.c   | 168 ++++++++++++++++++++++++++++++--
 tools/perf/tests/tests.h        |   3 +
 3 files changed, 172 insertions(+), 6 deletions(-)
tools/perf/tests/builtin-test.c +7 -0
index baee735e6aa5..9553f8061772 100644
@@ -75,6 +75,13 @@ static struct test generic_tests[] = {
 	{
 		.desc = "PMU events",
 		.func = test__pmu_events,
+		.subtest = {
+			.skip_if_fail	= false,
+			.get_nr		= test__pmu_events_subtest_get_nr,
+			.get_desc	= test__pmu_events_subtest_get_desc,
+			.skip_reason	= test__pmu_events_subtest_skip_reason,
+		},
+
 	},
 	{
 		.desc = "DSO data read",
tools/perf/tests/pmu-events.c +162 -6

192 lines not on this page · raw message

tools/perf/tests/tests.h +3 -0
index 88e45aeab94f..6c6c4b6a4796 100644
@@ -51,6 +51,9 @@ int test__perf_evsel__tp_sched_test(struct test *test, int subtest);
 int test__syscall_openat_tp_fields(struct test *test, int subtest);
 int test__pmu(struct test *test, int subtest);
 int test__pmu_events(struct test *test, int subtest);
+const char *test__pmu_events_subtest_get_desc(int subtest);
+const char *test__pmu_events_subtest_skip_reason(int subtest);
+int test__pmu_events_subtest_get_nr(void);
 int test__attr(struct test *test, int subtest);
 int test__dso_data(struct test *test, int subtest);
 int test__dso_data_cache(struct test *test, int subtest);
Signature · 1 line
2.26.2.761.g0e0b3e54be-goog
author reviewer
#13 lore raw

PATCH 8/8 perf expr: Migrate expr ids table to a hashmap

In reply to Ian Rogers

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/[email protected]/
and seconded by Jiri Olsa:
https://lore.kernel.org/lkml/20200423112915.GH1136647@krava/

---
 tools/perf/tests/expr.c       |  40 ++++++-----
 tools/perf/tests/pmu-events.c |  25 +++----
 tools/perf/util/expr.c        | 129 +++++++++++++++++++---------------
 tools/perf/util/expr.h        |  22 +++---
 tools/perf/util/expr.y        |  22 +-----
 tools/perf/util/metricgroup.c |  87 +++++++++++------------
 tools/perf/util/stat-shadow.c |  49 ++++++++-----
 7 files changed, 193 insertions(+), 181 deletions(-)
tools/perf/tests/expr.c +21 -19
index 3f742612776a..5e606fd5a2c6 100644
@@ -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,29 @@ 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);
+			expr__find_other("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
+					 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;
 }
tools/perf/tests/pmu-events.c +13 -12
index e21f0addcfbb..3de59564deb0 100644
@@ -433,8 +433,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;
@@ -446,29 +444,34 @@ static int test_parsing(void)
 			break;
 		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) {
+			expr__ctx_init(&ctx);
+			if (expr__find_other(pe->metric_expr, NULL, &ctx, 0)
+				  < 0) {
 				expr_failure("Parse other failed", map, pe);
 				ret++;
 				continue;
 			}
-			expr__ctx_init(&ctx);
 
 			/*
 			 * Add all ids with a made up value. The value may
 			 * 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, strdup(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++;
 			}
 
@@ -476,9 +479,7 @@ static int test_parsing(void)
 				expr_failure("Parse failed", map, pe);
 				ret++;
 			}
-			for (k = 0; k < idnum; k++)
-				zfree(&ids[k]);
-			free(ids);
+			expr__ctx_clear(&ctx);
 		}
 	}
 	/* TODO: fail when not ok */
tools/perf/util/expr.c +72 -57

158 lines not on this page · raw message

tools/perf/util/expr.h +9 -13
index 40fc452b0f2b..03c961e85e65 100644
@@ -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 <api/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
tools/perf/util/expr.y +2 -20
index 21e82a1e11a2..ec5a48bf5f34 100644
@@ -46,19 +46,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;
-}
-
 %}
 %%
 
@@ -72,12 +59,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 | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
@@ -92,7 +74,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);
 					YYABORT;
 				  }
tools/perf/util/metricgroup.c +44 -43

182 lines not on this page · raw message

tools/perf/util/stat-shadow.c +32 -17

116 lines not on this page · raw message

Signature · 1 line
2.26.2.761.g0e0b3e54be-goog