Skip to contentSkip to conversation

perf/core improvements and fixes

GIT PULL New Arnaldo Carvalho de Melo 3 participants 13 messages Nov 28, 2015 – Nov 29, 2015

Subsystems (3)
lore
Share & tools
lore

Arnaldo Carvalho de Melo

author maintainer
#0 lore raw

From: Arnaldo Carvalho de Melo <[email protected]>

Hi Ingo,

Please consider applying,

- Arnaldo

The following changes since commit 3f3b1a46bfdbce97dd3f9594d8a95db82baa554b:

Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux into perf/core (2015-11-27 08:28:44 +0100)

are available in the git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git tags/perf-core-for-mingo

for you to fetch changes up to 0bb93490170477224f8bd4cc9ce8920517461643:

perf bpf: Rename bpf config to program config (2015-11-27 22:00:46 -0300)

----------------------------------------------------------------
perf/core improvements and fixes:

User visible:

- Fix 'perf list' segfault due to lack of support for PERF_CONF_SW_BPF_OUTPUT in an array used just for printing available events, robustify the code involved (Arnaldo Carvalho de Melo)

- 'perf test unwind' should create kernel maps, now that entry works and the test passes (Jiri Olsa)

- Fix showing the running kernel build id in 'perf buildid-list' (Michael Petlan)

- Fix command line symbol filtering in 'perf report' (Namhyung Kim)

Infrastructure:

- Extract and collect map info from BPF object files in libbpf (Wang Nan)

----------------------------------------------------------------
Arnaldo Carvalho de Melo (2):
perf list: Add support for PERF_COUNT_SW_BPF_OUT perf list: Robustify event printing routine

Jiri Olsa (1):
perf test: 'unwind' test should create kernel maps

Michael Petlan (1):
perf buildid-list: Show running kernel build id fix

Namhyung Kim (3):
perf report: Show error message when processing sample fails perf hists: Do not skip elided fields when processing samples perf hists browser: Update nr entries regardless of min percent

Wang Nan (3):
tools lib bpf: Collect map definition in bpf_object tools lib bpf: Extract and collect map names from BPF object file perf bpf: Rename bpf config to program config

 tools/lib/bpf/libbpf.c          | 252 ++++++++++++++++++++++++++++++----------
 tools/lib/bpf/libbpf.h          |  24 ++++
 tools/perf/builtin-report.c     |  12 +-
 tools/perf/tests/dwarf-unwind.c |   5 +
 tools/perf/ui/browsers/hists.c  |   5 +-
 tools/perf/util/bpf-loader.c    |  65 +++++------
 tools/perf/util/bpf-loader.h    |   2 +-
 tools/perf/util/build-id.c      |   2 +-
 tools/perf/util/hist.c          |   6 -
 tools/perf/util/parse-events.c  |   6 +-
 10 files changed, 268 insertions(+), 111 deletions(-)
author maintainer
#1 lore raw

PATCH 1/10 perf buildid-list: Show running kernel build id fix

From: Michael Petlan <[email protected]>

The --kernel option of perf buildid-list tool should show the running kernel buildid. The functionality has been lost during other changes of the related code.

The build_id__sprintf() function should return length of the build-id string, but it was the length of the build-id raw data instead. Due to that, some return value checking caused that the final string was not printed out.

With this patch the build_id__sprintf() returns the correct value, so the --kernel option works again.

Before:

	# perf buildid-list --kernel
	#

After:

	# perf buildid-list --kernel
	972c1edab5bdc06cc224af45d510af662a3c6972
	#
Cc Jiri Olsa <[email protected]>
Cc Masami Hiramatsu <[email protected]>

LPU-Reference: [email protected]

---
 tools/perf/util/build-id.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/build-id.c +1 -1
index 217b5a60e2ab..6a7e273a514a 100644
@@ -91,7 +91,7 @@ int build_id__sprintf(const u8 *build_id, int len, char *bf)
 		bid += 2;
 	}
 
-	return raw - build_id;
+	return (bid - bf) + 1;
 }
 
 int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
Signature · 1 line
2.1.0
author maintainer
#2 lore raw

PATCH 2/10 perf test: 'unwind' test should create kernel maps

In reply to Arnaldo Carvalho de Melo

From: Jiri Olsa <[email protected]>

The 'perf test unwind' is failing because it forgot to create the kernel maps, fix it.

After the patch:

  # perf test unwind
  40: Test dwarf unwind         : Ok
---
 tools/perf/tests/dwarf-unwind.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/tests/dwarf-unwind.c +5 -0
index b2357e8115a2..3cce13b19cbb 100644
@@ -173,6 +173,11 @@ int test__dwarf_unwind(int subtest __maybe_unused)
 		return -1;
 	}
 
+	if (machine__create_kernel_maps(machine)) {
+		pr_err("Failed to create kernel maps\n");
+		return -1;
+	}
+
 	callchain_param.record_mode = CALLCHAIN_DWARF;
 
 	if (init_live_machine(machine)) {
Signature · 1 line
2.1.0
author maintainer
#3 lore raw

PATCH 3/10 perf list: Add support for PERF_COUNT_SW_BPF_OUT

In reply to Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

When PERF_COUNT_SW_BPF_OUTPUT was added to the kernel we should've added it to tools/perf, where it is used just to list events.

This ended up causing a segfault in commands like "perf list stall".

Fix it by adding that new software counter.

A patch to robustify perf to not segfault when the next counter gets added in the kernel will follow this one.

---
 tools/perf/util/parse-events.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/perf/util/parse-events.c +4 -0
index e48d9da75707..40ae92a8673c 100644
@@ -124,6 +124,10 @@ struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
 		.symbol = "dummy",
 		.alias  = "",
 	},
+	[PERF_COUNT_SW_BPF_OUTPUT] = {
+		.symbol = "bpf-output",
+		.alias  = "",
+	},
 };
 
 #define __PERF_EVENT_FIELD(config, name) \
Signature · 1 line
2.1.0
author maintainer
#4 lore raw

PATCH 4/10 perf list: Robustify event printing routine

In reply to Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

When a43eec304259 ("bpf: introduce bpf_perf_event_output() helper") added PERF_COUNT_SW_BPF_OUTPUT we ended up with a new entry in the event_symbols_sw array that wasn't initialized, thus set to NULL, fix print_symbol_events() to check for that case so that we don't crash if this happens again.

(gdb) bt

  #0  __match_glob (ignore_space=false, pat=<optimized out>, str=<optimized out>) at util/string.c:198
  #1  strglobmatch (str=<optimized out>, pat=pat@entry=0x7fffffffe61d "stall") at util/string.c:252
  #2  0x00000000004993a5 in print_symbol_events (type=1, syms=0x872880 <event_symbols_sw+160>, max=11, name_only=false, event_glob=0x7fffffffe61d "stall")
      at util/parse-events.c:1615
  #3  print_events (event_glob=event_glob@entry=0x7fffffffe61d "stall", name_only=false) at util/parse-events.c:1675
  #4  0x000000000042c79e in cmd_list (argc=1, argv=0x7fffffffe390, prefix=<optimized out>) at builtin-list.c:68
  #5  0x00000000004788a5 in run_builtin (p=p@entry=0x871758 <commands+120>, argc=argc@entry=2, argv=argv@entry=0x7fffffffe390) at perf.c:370
  #6  0x0000000000420ab0 in handle_internal_command (argv=0x7fffffffe390, argc=2) at perf.c:429
  #7  run_argv (argv=0x7fffffffe110, argcp=0x7fffffffe11c) at perf.c:473
  #8  main (argc=2, argv=0x7fffffffe390) at perf.c:588
  (gdb) p event_symbols_sw[PERF_COUNT_SW_BPF_OUTPUT]
  $4 = {symbol = 0x0, alias = 0x0}
  (gdb)

A patch to robustify perf to not segfault when the next counter gets added in the kernel will follow this one.

---
 tools/perf/util/parse-events.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/parse-events.c +1 -1
index 40ae92a8673c..6fc8cd753e1a 100644
@@ -1883,7 +1883,7 @@ restart:
 
 	for (i = 0; i < max; i++, syms++) {
 
-		if (event_glob != NULL &&
+		if (event_glob != NULL && syms->symbol != NULL &&
 		    !(strglobmatch(syms->symbol, event_glob) ||
 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
 			continue;
Signature · 1 line
2.1.0
author maintainer
#5 lore raw

PATCH 5/10 perf report: Show error message when processing sample fails

In reply to Arnaldo Carvalho de Melo

From: Namhyung Kim <[email protected]>

Currently when perf fails to process samples for some reason, it doesn't show any message about the failure. This is very inconvenient for users especially on TUI as screen is reset after the failure.

---
 tools/perf/builtin-report.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/perf/builtin-report.c +9 -3
index 8a9c6908f54e..af5db885ea9c 100644
@@ -513,20 +513,26 @@ static int __cmd_report(struct report *rep)
 	if (rep->cpu_list) {
 		ret = perf_session__cpu_bitmap(session, rep->cpu_list,
 					       rep->cpu_bitmap);
-		if (ret)
+		if (ret) {
+			ui__error("failed to set cpu bitmap\n");
 			return ret;
+		}
 	}
 
 	if (rep->show_threads)
 		perf_read_values_init(&rep->show_threads_values);
 
 	ret = report__setup_sample_type(rep);
-	if (ret)
+	if (ret) {
+		/* report__setup_sample_type() already showed error message */
 		return ret;
+	}
 
 	ret = perf_session__process_events(session);
-	if (ret)
+	if (ret) {
+		ui__error("failed to process sample\n");
 		return ret;
+	}
 
 	report__warn_kptr_restrict(rep);
 
Signature · 1 line
2.1.0
author maintainer
#6 lore raw

PATCH 6/10 perf hists: Do not skip elided fields when processing samples

In reply to Arnaldo Carvalho de Melo

From: Namhyung Kim <[email protected]>

If user gives a filter, perf marks the corresponding column elided and omits the output. But it should process and aggregates samples using the field, otherwise samples will be aggregated as if the column was not there resulted in incorrect output.

For example, I'd like to set a filter on native_write_msr_safe. The original overhead of the function is negligible.

  $ perf report | grep native_write_msr_safe
      0.00%  swapper  [kernel.vmlinux]  native_write_msr_safe
      0.00%  perf     [kernel.vmlinux]  native_write_msr_safe

However adding -S option gives different output.

  $ perf report -S native_write_msr_safe --percentage absolute | \
Quoted 1 line

grep -e swapper -e perf

     51.47%  swapper  [kernel.vmlinux]
      4.14%  perf     [kernel.vmlinux]

Since it aggregated samples using comm and dso only. In fact, the above values are same when it sorts with -s comm,dso.

  $ perf report -s comm,dso | grep -e swapper -e perf
     51.47%  swapper  [kernel.vmlinux]
      4.14%  perf     [kernel.vmlinux]

This resulted in TUI failure with -ERANGE since it tries to increase sample hit count for annotation with wrong symbols due to incorrect aggregation.

This patch fixes it not to skip elided fields when comparing samples in order to insert them to the hists.

Commiter note:

After the patch, with a different workloads:

  # perf report --show-total-period -S native_write_msr_safe --stdio
  #
  # symbol: native_write_msr_safe
  #
  # Samples: 455  of event 'cycles:pp'
  # Event count (approx.): 134787489
  #
  # Overhead Period Command         Shared Object
  # ........ ...... ............... ................
  #
       0.22% 293081 qemu-system-x86 [vmlinux]
       0.19% 255914 swapper         [vmlinux]
       0.00%   2054 Timer           [vmlinux]
       0.00%   1021 firefox         [vmlinux]
       0.00%      2 perf            [vmlinux]
  # perf report --show-total-period | grep native_write_msr_safe
  Failed to open /tmp/perf-14838.map, continuing without symbols
       0.22% 293081 qemu-system-x86 [vmlinux]  [k] native_write_msr_safe
       0.19% 255914 swapper         [vmlinux]  [k] native_write_msr_safe
       0.00%   2054 Timer           [vmlinux]  [k] native_write_msr_safe
       0.00%   1021 firefox         [vmlinux]  [k] native_write_msr_safe
       0.00%      2 perf            [vmlinux]  [k] native_write_msr_safe
  #
---
 tools/perf/util/hist.c | 6 ------
 1 file changed, 6 deletions(-)
tools/perf/util/hist.c +0 -6
index 4fd37d6708cb..6e8e0ee9ec37 100644
@@ -924,9 +924,6 @@ hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
 	int64_t cmp = 0;
 
 	perf_hpp__for_each_sort_list(fmt) {
-		if (perf_hpp__should_skip(fmt))
-			continue;
-
 		cmp = fmt->cmp(fmt, left, right);
 		if (cmp)
 			break;
@@ -942,9 +939,6 @@ hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
 	int64_t cmp = 0;
 
 	perf_hpp__for_each_sort_list(fmt) {
-		if (perf_hpp__should_skip(fmt))
-			continue;
-
 		cmp = fmt->collapse(fmt, left, right);
 		if (cmp)
 			break;
Signature · 1 line
2.1.0
author maintainer
#7 lore raw

PATCH 7/10 perf hists browser: Update nr entries regardless of min percent

In reply to Arnaldo Carvalho de Melo

From: Namhyung Kim <[email protected]>

When perf report on TUI was called with -S symbol filter, it should update nr entries even if min_pcnt is 0. IIRC the reason was to update nr entries after applying minimum percent threshold. But if symbol filter was given on command line (with -S option), it should use hists->nr_non_filtered_entries instead of hists->nr_entries.

So this patch fixes a bug of navigating hists browser that the cursor goes beyond the number of entries when -S (or similar) option is used.

---
 tools/perf/ui/browsers/hists.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/perf/ui/browsers/hists.c +2 -3
index a211b7b6a81e..dcdcbafb078b 100644
@@ -2055,10 +2055,9 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
 	SLang_reset_tty();
 	SLang_init_tty(0, 0, 0);
 
-	if (min_pcnt) {
+	if (min_pcnt)
 		browser->min_pcnt = min_pcnt;
-		hist_browser__update_nr_entries(browser);
-	}
+	hist_browser__update_nr_entries(browser);
 
 	browser->pstack = pstack__new(3);
 	if (browser->pstack == NULL)
Signature · 1 line
2.1.0
author maintainer
#8 lore raw

PATCH 8/10 tools lib bpf: Collect map definition in bpf_object

In reply to Arnaldo Carvalho de Melo

From: Wang Nan <[email protected]>

This patch collects more information from maps sections in BPF object files into 'struct bpf_object', enables later patches access those information (such as the type and size of the map).

In this patch, a new handler 'struct bpf_map' is extracted in parallel with bpf_object and bpf_program. Its iterator and accessor is also created.

---
 tools/lib/bpf/libbpf.c | 187 +++++++++++++++++++++++++++++++++----------------
 tools/lib/bpf/libbpf.h |  21 ++++++
 2 files changed, 148 insertions(+), 60 deletions(-)
tools/lib/bpf/libbpf.c +127 -60
index e3f4c3379f14..f50982579aa8 100644
@@ -163,22 +163,24 @@ struct bpf_program {
 	bpf_program_clear_priv_t clear_priv;
 };
 
+struct bpf_map {
+	int fd;
+	struct bpf_map_def def;
+	void *priv;
+	bpf_map_clear_priv_t clear_priv;
+};
+
 static LIST_HEAD(bpf_objects_list);
 
 struct bpf_object {
 	char license[64];
 	u32 kern_version;
-	void *maps_buf;
-	size_t maps_buf_sz;
 
 	struct bpf_program *programs;
 	size_t nr_programs;
-	int *map_fds;
-	/*
-	 * This field is required because maps_buf will be freed and
-	 * maps_buf_sz will be set to 0 after loaded.
-	 */
-	size_t nr_map_fds;
+	struct bpf_map *maps;
+	size_t nr_maps;
+
 	bool loaded;
 
 	/*
@@ -489,21 +491,38 @@ static int
 bpf_object__init_maps(struct bpf_object *obj, void *data,
 		      size_t size)
 {
-	if (size == 0) {
+	size_t nr_maps;
+	int i;
+
+	nr_maps = size / sizeof(struct bpf_map_def);
+	if (!data || !nr_maps) {
 		pr_debug("%s doesn't need map definition\n",
 			 obj->path);
 		return 0;
 	}
 
-	obj->maps_buf = malloc(size);
-	if (!obj->maps_buf) {
-		pr_warning("malloc maps failed: %s\n", obj->path);
+	pr_debug("maps in %s: %zd bytes\n", obj->path, size);
+
+	obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
+	if (!obj->maps) {
+		pr_warning("alloc maps for object failed\n");
 		return -ENOMEM;
 	}
+	obj->nr_maps = nr_maps;
+
+	for (i = 0; i < nr_maps; i++) {
+		struct bpf_map_def *def = &obj->maps[i].def;
 
-	obj->maps_buf_sz = size;
-	memcpy(obj->maps_buf, data, size);
-	pr_debug("maps in %s: %ld bytes\n", obj->path, (long)size);
+		/*
+		 * fill all fd with -1 so won't close incorrect
+		 * fd (fd=0 is stdin) when failure (zclose won't close
+		 * negative fd)).
+		 */
+		obj->maps[i].fd = -1;
+
+		/* Save map definition into obj->maps */
+		*def = ((struct bpf_map_def *)data)[i];
+	}
 	return 0;
 }
 
@@ -688,37 +707,15 @@ static int
 bpf_object__create_maps(struct bpf_object *obj)
 {
 	unsigned int i;
-	size_t nr_maps;
-	int *pfd;
-
-	nr_maps = obj->maps_buf_sz / sizeof(struct bpf_map_def);
-	if (!obj->maps_buf || !nr_maps) {
-		pr_debug("don't need create maps for %s\n",
-			 obj->path);
-		return 0;
-	}
 
-	obj->map_fds = malloc(sizeof(int) * nr_maps);
-	if (!obj->map_fds) {
-		pr_warning("realloc perf_bpf_map_fds failed\n");
-		return -ENOMEM;
-	}
-	obj->nr_map_fds = nr_maps;
-
-	/* fill all fd with -1 */
-	memset(obj->map_fds, -1, sizeof(int) * nr_maps);
+	for (i = 0; i < obj->nr_maps; i++) {
+		struct bpf_map_def *def = &obj->maps[i].def;
+		int *pfd = &obj->maps[i].fd;
 
-	pfd = obj->map_fds;
-	for (i = 0; i < nr_maps; i++) {
-		struct bpf_map_def def;
-
-		def = *(struct bpf_map_def *)(obj->maps_buf +
-				i * sizeof(struct bpf_map_def));
-
-		*pfd = bpf_create_map(def.type,
-				      def.key_size,
-				      def.value_size,
-				      def.max_entries);
+		*pfd = bpf_create_map(def->type,
+				      def->key_size,
+				      def->value_size,
+				      def->max_entries);
 		if (*pfd < 0) {
 			size_t j;
 			int err = *pfd;
@@ -726,22 +723,17 @@ bpf_object__create_maps(struct bpf_object *obj)
 			pr_warning("failed to create map: %s\n",
 				   strerror(errno));
 			for (j = 0; j < i; j++)
-				zclose(obj->map_fds[j]);
-			obj->nr_map_fds = 0;
-			zfree(&obj->map_fds);
+				zclose(obj->maps[j].fd);
 			return err;
 		}
 		pr_debug("create map: fd=%d\n", *pfd);
-		pfd++;
 	}
 
-	zfree(&obj->maps_buf);
-	obj->maps_buf_sz = 0;
 	return 0;
 }
 
 static int
-bpf_program__relocate(struct bpf_program *prog, int *map_fds)
+bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
 {
 	int i;
 
@@ -761,7 +753,7 @@ bpf_program__relocate(struct bpf_program *prog, int *map_fds)
 			return -LIBBPF_ERRNO__RELOC;
 		}
 		insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
-		insns[insn_idx].imm = map_fds[map_idx];
+		insns[insn_idx].imm = obj->maps[map_idx].fd;
 	}
 
 	zfree(&prog->reloc_desc);
@@ -780,7 +772,7 @@ bpf_object__relocate(struct bpf_object *obj)
 	for (i = 0; i < obj->nr_programs; i++) {
 		prog = &obj->programs[i];
 
-		err = bpf_program__relocate(prog, obj->map_fds);
+		err = bpf_program__relocate(prog, obj);
 		if (err) {
 			pr_warning("failed to relocate '%s'\n",
 				   prog->section_name);
@@ -804,8 +796,7 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
 		Elf_Data *data = obj->efile.reloc[i].data;
 		int idx = shdr->sh_info;
 		struct bpf_program *prog;
-		size_t nr_maps = obj->maps_buf_sz /
-				 sizeof(struct bpf_map_def);
+		size_t nr_maps = obj->nr_maps;
 
 		if (shdr->sh_type != SHT_REL) {
 			pr_warning("internal error at %d\n", __LINE__);
@@ -1050,10 +1041,8 @@ int bpf_object__unload(struct bpf_object *obj)
 	if (!obj)
 		return -EINVAL;
 
-	for (i = 0; i < obj->nr_map_fds; i++)
-		zclose(obj->map_fds[i]);
-	zfree(&obj->map_fds);
-	obj->nr_map_fds = 0;
+	for (i = 0; i < obj->nr_maps; i++)
+		zclose(obj->maps[i].fd);
 
 	for (i = 0; i < obj->nr_programs; i++)
 		bpf_program__unload(&obj->programs[i]);
@@ -1096,7 +1085,15 @@ void bpf_object__close(struct bpf_object *obj)
 	bpf_object__elf_finish(obj);
 	bpf_object__unload(obj);
 
-	zfree(&obj->maps_buf);
+	for (i = 0; i < obj->nr_maps; i++) {
+		if (obj->maps[i].clear_priv)
+			obj->maps[i].clear_priv(&obj->maps[i],
+						obj->maps[i].priv);
+		obj->maps[i].priv = NULL;
+		obj->maps[i].clear_priv = NULL;
+	}
+	zfree(&obj->maps);
+	obj->nr_maps = 0;
 
 	if (obj->programs && obj->nr_programs) {
 		for (i = 0; i < obj->nr_programs; i++)
@@ -1251,3 +1248,73 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)
 
 	return fd;
 }
+
+int bpf_map__get_fd(struct bpf_map *map)
+{
+	if (!map)
+		return -EINVAL;
+
+	return map->fd;
+}
+
+int bpf_map__get_def(struct bpf_map *map, struct bpf_map_def *pdef)
+{
+	if (!map || !pdef)
+		return -EINVAL;
+
+	*pdef = map->def;
+	return 0;
+}
+
+int bpf_map__set_private(struct bpf_map *map, void *priv,
+			 bpf_map_clear_priv_t clear_priv)
+{
+	if (!map)
+		return -EINVAL;
+
+	if (map->priv) {
+		if (map->clear_priv)
+			map->clear_priv(map, map->priv);
+	}
+
+	map->priv = priv;
+	map->clear_priv = clear_priv;
+	return 0;
+}
+
+int bpf_map__get_private(struct bpf_map *map, void **ppriv)
+{
+	if (!map)
+		return -EINVAL;
+
+	if (ppriv)
+		*ppriv = map->priv;
+	return 0;
+}
+
+struct bpf_map *
+bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
+{
+	size_t idx;
+	struct bpf_map *s, *e;
+
+	if (!obj || !obj->maps)
+		return NULL;
+
+	s = obj->maps;
+	e = obj->maps + obj->nr_maps;
+
+	if (prev == NULL)
+		return s;
+
+	if ((prev < s) || (prev >= e)) {
+		pr_warning("error in %s: map handler doesn't belong to object\n",
+			   __func__);
+		return NULL;
+	}
+
+	idx = (prev - obj->maps) + 1;
+	if (idx >= obj->nr_maps)
+		return NULL;
+	return &obj->maps[idx];
+}
tools/lib/bpf/libbpf.h +21 -0
index 949df4b346cf..ef631255dfaa 100644
@@ -165,4 +165,25 @@ struct bpf_map_def {
 	unsigned int max_entries;
 };
 
+/*
+ * There is another 'struct bpf_map' in include/linux/map.h. However,
+ * it is not a uapi header so no need to consider name clash.
+ */
+struct bpf_map;
+
+struct bpf_map *
+bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
+#define bpf_map__for_each(pos, obj)		\
+	for ((pos) = bpf_map__next(NULL, (obj));	\
+	     (pos) != NULL;				\
+	     (pos) = bpf_map__next((pos), (obj)))
+
+int bpf_map__get_fd(struct bpf_map *map);
+int bpf_map__get_def(struct bpf_map *map, struct bpf_map_def *pdef);
+
+typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
+int bpf_map__set_private(struct bpf_map *map, void *priv,
+			 bpf_map_clear_priv_t clear_priv);
+int bpf_map__get_private(struct bpf_map *map, void **ppriv);
+
 #endif
Signature · 1 line
2.1.0
author maintainer
#9 lore raw

PATCH 9/10 tools lib bpf: Extract and collect map names from BPF object file

In reply to Arnaldo Carvalho de Melo

From: Wang Nan <[email protected]>

This patch collects name of maps in BPF object files and saves them into 'maps' field in 'struct bpf_object'. 'bpf_object__get_map_by_name' is introduced to retrive fd and definitions of a map through its name.

---
 tools/lib/bpf/libbpf.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++---
 tools/lib/bpf/libbpf.h |  3 +++
 2 files changed, 65 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +62 -3
index f50982579aa8..a298614ad091 100644
@@ -165,6 +165,7 @@ struct bpf_program {
 
 struct bpf_map {
 	int fd;
+	char *name;
 	struct bpf_map_def def;
 	void *priv;
 	bpf_map_clear_priv_t clear_priv;
@@ -526,12 +527,46 @@ bpf_object__init_maps(struct bpf_object *obj, void *data,
 	return 0;
 }
 
+static void
+bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx)
+{
+	int i;
+	Elf_Data *symbols = obj->efile.symbols;
+
+	if (!symbols || maps_shndx < 0)
+		return;
+
+	for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
+		GElf_Sym sym;
+		size_t map_idx;
+		const char *map_name;
+
+		if (!gelf_getsym(symbols, i, &sym))
+			continue;
+		if (sym.st_shndx != maps_shndx)
+			continue;
+
+		map_name = elf_strptr(obj->efile.elf,
+				      obj->efile.ehdr.e_shstrndx,
+				      sym.st_name);
+		map_idx = sym.st_value / sizeof(struct bpf_map_def);
+		if (map_idx >= obj->nr_maps) {
+			pr_warning("index of map \"%s\" is buggy: %zu > %zu\n",
+				   map_name, map_idx, obj->nr_maps);
+			continue;
+		}
+		obj->maps[map_idx].name = strdup(map_name);
+		pr_debug("map %zu is \"%s\"\n", map_idx,
+			 obj->maps[map_idx].name);
+	}
+}
+
 static int bpf_object__elf_collect(struct bpf_object *obj)
 {
 	Elf *elf = obj->efile.elf;
 	GElf_Ehdr *ep = &obj->efile.ehdr;
 	Elf_Scn *scn = NULL;
-	int idx = 0, err = 0;
+	int idx = 0, err = 0, maps_shndx = -1;
 
 	/* Elf is corrupted/truncated, avoid calling elf_strptr. */
 	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
@@ -581,10 +616,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 			err = bpf_object__init_kversion(obj,
 							data->d_buf,
 							data->d_size);
-		else if (strcmp(name, "maps") == 0)
+		else if (strcmp(name, "maps") == 0) {
 			err = bpf_object__init_maps(obj, data->d_buf,
 						    data->d_size);
-		else if (sh.sh_type == SHT_SYMTAB) {
+			maps_shndx = idx;
+		} else if (sh.sh_type == SHT_SYMTAB) {
 			if (obj->efile.symbols) {
 				pr_warning("bpf: multiple SYMTAB in %s\n",
 					   obj->path);
@@ -625,6 +661,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 		if (err)
 			goto out;
 	}
+
+	if (maps_shndx >= 0)
+		bpf_object__init_maps_name(obj, maps_shndx);
 out:
 	return err;
 }
@@ -1086,6 +1125,7 @@ void bpf_object__close(struct bpf_object *obj)
 	bpf_object__unload(obj);
 
 	for (i = 0; i < obj->nr_maps; i++) {
+		zfree(&obj->maps[i].name);
 		if (obj->maps[i].clear_priv)
 			obj->maps[i].clear_priv(&obj->maps[i],
 						obj->maps[i].priv);
@@ -1266,6 +1306,13 @@ int bpf_map__get_def(struct bpf_map *map, struct bpf_map_def *pdef)
 	return 0;
 }
 
+const char *bpf_map__get_name(struct bpf_map *map)
+{
+	if (!map)
+		return NULL;
+	return map->name;
+}
+
 int bpf_map__set_private(struct bpf_map *map, void *priv,
 			 bpf_map_clear_priv_t clear_priv)
 {
@@ -1318,3 +1365,15 @@ bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
 		return NULL;
 	return &obj->maps[idx];
 }
+
+struct bpf_map *
+bpf_object__get_map_by_name(struct bpf_object *obj, const char *name)
+{
+	struct bpf_map *pos;
+
+	bpf_map__for_each(pos, obj) {
+		if (strcmp(pos->name, name) == 0)
+			return pos;
+	}
+	return NULL;
+}
tools/lib/bpf/libbpf.h +3 -0
index ef631255dfaa..a51594c7b518 100644
@@ -170,6 +170,8 @@ struct bpf_map_def {
  * it is not a uapi header so no need to consider name clash.
  */
 struct bpf_map;
+struct bpf_map *
+bpf_object__get_map_by_name(struct bpf_object *obj, const char *name);
 
 struct bpf_map *
 bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
@@ -180,6 +182,7 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
 
 int bpf_map__get_fd(struct bpf_map *map);
 int bpf_map__get_def(struct bpf_map *map, struct bpf_map_def *pdef);
+const char *bpf_map__get_name(struct bpf_map *map);
 
 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
 int bpf_map__set_private(struct bpf_map *map, void *priv,
Signature · 1 line
2.1.0
author maintainer
#10 lore raw

PATCH 10/10 perf bpf: Rename bpf config to program config

In reply to Arnaldo Carvalho de Melo

From: Wang Nan <[email protected]>

Following patches are going to introduce BPF object level configuration to enable setting values into BPF maps. To avoid confusion, this patch renames existing 'config' in bpf-loader.c to 'program config'. Following patches would introduce 'object config'.

---
 tools/perf/util/bpf-loader.c | 65 ++++++++++++++++++++++----------------------
 tools/perf/util/bpf-loader.h |  2 +-
 2 files changed, 33 insertions(+), 34 deletions(-)
tools/perf/util/bpf-loader.c +32 -33
index 36544e5ece43..540a7efa657e 100644
@@ -120,7 +120,7 @@ bpf_prog_priv__clear(struct bpf_program *prog __maybe_unused,
 }
 
 static int
-config__exec(const char *value, struct perf_probe_event *pev)
+prog_config__exec(const char *value, struct perf_probe_event *pev)
 {
 	pev->uprobes = true;
 	pev->target = strdup(value);
@@ -130,7 +130,7 @@ config__exec(const char *value, struct perf_probe_event *pev)
 }
 
 static int
-config__module(const char *value, struct perf_probe_event *pev)
+prog_config__module(const char *value, struct perf_probe_event *pev)
 {
 	pev->uprobes = false;
 	pev->target = strdup(value);
@@ -140,8 +140,7 @@ config__module(const char *value, struct perf_probe_event *pev)
 }
 
 static int
-config__bool(const char *value,
-	     bool *pbool, bool invert)
+prog_config__bool(const char *value, bool *pbool, bool invert)
 {
 	int err;
 	bool bool_value;
@@ -158,17 +157,17 @@ config__bool(const char *value,
 }
 
 static int
-config__inlines(const char *value,
-		struct perf_probe_event *pev __maybe_unused)
+prog_config__inlines(const char *value,
+		     struct perf_probe_event *pev __maybe_unused)
 {
-	return config__bool(value, &probe_conf.no_inlines, true);
+	return prog_config__bool(value, &probe_conf.no_inlines, true);
 }
 
 static int
-config__force(const char *value,
-	      struct perf_probe_event *pev __maybe_unused)
+prog_config__force(const char *value,
+		   struct perf_probe_event *pev __maybe_unused)
 {
-	return config__bool(value, &probe_conf.force_add, false);
+	return prog_config__bool(value, &probe_conf.force_add, false);
 }
 
 static struct {
@@ -176,58 +175,58 @@ static struct {
 	const char *usage;
 	const char *desc;
 	int (*func)(const char *, struct perf_probe_event *);
-} bpf_config_terms[] = {
+} bpf_prog_config_terms[] = {
 	{
 		.key	= "exec",
 		.usage	= "exec=<full path of file>",
 		.desc	= "Set uprobe target",
-		.func	= config__exec,
+		.func	= prog_config__exec,
 	},
 	{
 		.key	= "module",
 		.usage	= "module=<module name>    ",
 		.desc	= "Set kprobe module",
-		.func	= config__module,
+		.func	= prog_config__module,
 	},
 	{
 		.key	= "inlines",
 		.usage	= "inlines=[yes|no]        ",
 		.desc	= "Probe at inline symbol",
-		.func	= config__inlines,
+		.func	= prog_config__inlines,
 	},
 	{
 		.key	= "force",
 		.usage	= "force=[yes|no]          ",
 		.desc	= "Forcibly add events with existing name",
-		.func	= config__force,
+		.func	= prog_config__force,
 	},
 };
 
 static int
-do_config(const char *key, const char *value,
-	  struct perf_probe_event *pev)
+do_prog_config(const char *key, const char *value,
+	       struct perf_probe_event *pev)
 {
 	unsigned int i;
 
 	pr_debug("config bpf program: %s=%s\n", key, value);
-	for (i = 0; i < ARRAY_SIZE(bpf_config_terms); i++)
-		if (strcmp(key, bpf_config_terms[i].key) == 0)
-			return bpf_config_terms[i].func(value, pev);
+	for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++)
+		if (strcmp(key, bpf_prog_config_terms[i].key) == 0)
+			return bpf_prog_config_terms[i].func(value, pev);
 
-	pr_debug("BPF: ERROR: invalid config option in object: %s=%s\n",
+	pr_debug("BPF: ERROR: invalid program config option: %s=%s\n",
 		 key, value);
 
-	pr_debug("\nHint: Currently valid options are:\n");
-	for (i = 0; i < ARRAY_SIZE(bpf_config_terms); i++)
-		pr_debug("\t%s:\t%s\n", bpf_config_terms[i].usage,
-			 bpf_config_terms[i].desc);
+	pr_debug("\nHint: Valid options are:\n");
+	for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++)
+		pr_debug("\t%s:\t%s\n", bpf_prog_config_terms[i].usage,
+			 bpf_prog_config_terms[i].desc);
 	pr_debug("\n");
 
-	return -BPF_LOADER_ERRNO__CONFIG_TERM;
+	return -BPF_LOADER_ERRNO__PROGCONF_TERM;
 }
 
 static const char *
-parse_config_kvpair(const char *config_str, struct perf_probe_event *pev)
+parse_prog_config_kvpair(const char *config_str, struct perf_probe_event *pev)
 {
 	char *text = strdup(config_str);
 	char *sep, *line;
@@ -253,7 +252,7 @@ parse_config_kvpair(const char *config_str, struct perf_probe_event *pev)
 		}
 		*equ = '\0';
 
-		err = do_config(line, equ + 1, pev);
+		err = do_prog_config(line, equ + 1, pev);
 		if (err)
 			break;
 nextline:
@@ -268,10 +267,10 @@ nextline:
 }
 
 static int
-parse_config(const char *config_str, struct perf_probe_event *pev)
+parse_prog_config(const char *config_str, struct perf_probe_event *pev)
 {
 	int err;
-	const char *main_str = parse_config_kvpair(config_str, pev);
+	const char *main_str = parse_prog_config_kvpair(config_str, pev);
 
 	if (IS_ERR(main_str))
 		return PTR_ERR(main_str);
@@ -312,7 +311,7 @@ config_bpf_program(struct bpf_program *prog)
 	pev = &priv->pev;
 
 	pr_debug("bpf: config program '%s'\n", config_str);
-	err = parse_config(config_str, pev);
+	err = parse_prog_config(config_str, pev);
 	if (err)
 		goto errout;
 
@@ -750,7 +749,7 @@ static const char *bpf_loader_strerror_table[NR_ERRNO] = {
 	[ERRCODE_OFFSET(EVENTNAME)]	= "No event name found in config string",
 	[ERRCODE_OFFSET(INTERNAL)]	= "BPF loader internal error",
 	[ERRCODE_OFFSET(COMPILE)]	= "Error when compiling BPF scriptlet",
-	[ERRCODE_OFFSET(CONFIG_TERM)]	= "Invalid config term in config string",
+	[ERRCODE_OFFSET(PROGCONF_TERM)]	= "Invalid program config term in config string",
 	[ERRCODE_OFFSET(PROLOGUE)]	= "Failed to generate prologue",
 	[ERRCODE_OFFSET(PROLOGUE2BIG)]	= "Prologue too big for program",
 	[ERRCODE_OFFSET(PROLOGUEOOB)]	= "Offset out of bound for prologue",
@@ -834,7 +833,7 @@ int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
 			int err, char *buf, size_t size)
 {
 	bpf__strerror_head(err, buf, size);
-	case BPF_LOADER_ERRNO__CONFIG_TERM: {
+	case BPF_LOADER_ERRNO__PROGCONF_TERM: {
 		scnprintf(buf, size, "%s (add -v to see detail)", emsg);
 		break;
 	}
tools/perf/util/bpf-loader.h +1 -1
index a58740b0f31e..6fdc0457e2b6 100644
@@ -20,7 +20,7 @@ enum bpf_loader_errno {
 	BPF_LOADER_ERRNO__EVENTNAME,	/* Event name is missing */
 	BPF_LOADER_ERRNO__INTERNAL,	/* BPF loader internal error */
 	BPF_LOADER_ERRNO__COMPILE,	/* Error when compiling BPF scriptlet */
-	BPF_LOADER_ERRNO__CONFIG_TERM,	/* Invalid config term in config term */
+	BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */
 	BPF_LOADER_ERRNO__PROLOGUE,	/* Failed to generate prologue */
 	BPF_LOADER_ERRNO__PROLOGUE2BIG,	/* Prologue too big for program */
 	BPF_LOADER_ERRNO__PROLOGUEOOB,	/* Offset out of bound for prologue */
Signature · 1 line
2.1.0
#11 lore raw
In reply to Arnaldo Carvalho de Melo
Quoted 63 lines · Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

Hi Ingo,

Please consider applying,

- Arnaldo

The following changes since commit 3f3b1a46bfdbce97dd3f9594d8a95db82baa554b:

Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux into perf/core (2015-11-27 08:28:44 +0100)

are available in the git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git tags/perf-core-for-mingo

for you to fetch changes up to 0bb93490170477224f8bd4cc9ce8920517461643:

perf bpf: Rename bpf config to program config (2015-11-27 22:00:46 -0300)

----------------------------------------------------------------
perf/core improvements and fixes:

User visible:

- Fix 'perf list' segfault due to lack of support for PERF_CONF_SW_BPF_OUTPUT in an array used just for printing available events, robustify the code involved (Arnaldo Carvalho de Melo)

- 'perf test unwind' should create kernel maps, now that entry works and the test passes (Jiri Olsa)

- Fix showing the running kernel build id in 'perf buildid-list' (Michael Petlan)

- Fix command line symbol filtering in 'perf report' (Namhyung Kim)

Infrastructure:

- Extract and collect map info from BPF object files in libbpf (Wang Nan)

----------------------------------------------------------------
Arnaldo Carvalho de Melo (2):
perf list: Add support for PERF_COUNT_SW_BPF_OUT perf list: Robustify event printing routine

Jiri Olsa (1):
perf test: 'unwind' test should create kernel maps

Michael Petlan (1):
perf buildid-list: Show running kernel build id fix

Namhyung Kim (3):
perf report: Show error message when processing sample fails perf hists: Do not skip elided fields when processing samples perf hists browser: Update nr entries regardless of min percent

Wang Nan (3):
tools lib bpf: Collect map definition in bpf_object tools lib bpf: Extract and collect map names from BPF object file perf bpf: Rename bpf config to program config

Quoted 11 lines
 tools/lib/bpf/libbpf.c          | 252 ++++++++++++++++++++++++++++++----------
 tools/lib/bpf/libbpf.h          |  24 ++++
 tools/perf/builtin-report.c     |  12 +-
 tools/perf/tests/dwarf-unwind.c |   5 +
 tools/perf/ui/browsers/hists.c  |   5 +-
 tools/perf/util/bpf-loader.c    |  65 +++++------
 tools/perf/util/bpf-loader.h    |   2 +-
 tools/perf/util/build-id.c      |   2 +-
 tools/perf/util/hist.c          |   6 -
 tools/perf/util/parse-events.c  |   6 +-
 10 files changed, 268 insertions(+), 111 deletions(-)

Pulled, thanks a lot Arnaldo!

Should any of the above fixes be cherry-picked over into perf/urgent, or are these all new regressions?

Thanks,

Ingo

maintainer
#12 lore raw

Hi Ingo,

Quoted 78 lines · Ingo Molnar
Quoted 63 lines · Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

Hi Ingo,

Please consider applying,

- Arnaldo

The following changes since commit 3f3b1a46bfdbce97dd3f9594d8a95db82baa554b:

Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux into perf/core (2015-11-27 08:28:44 +0100)

are available in the git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git tags/perf-core-for-mingo

for you to fetch changes up to 0bb93490170477224f8bd4cc9ce8920517461643:

perf bpf: Rename bpf config to program config (2015-11-27 22:00:46 -0300)

----------------------------------------------------------------
perf/core improvements and fixes:

User visible:

- Fix 'perf list' segfault due to lack of support for PERF_CONF_SW_BPF_OUTPUT in an array used just for printing available events, robustify the code involved (Arnaldo Carvalho de Melo)

- 'perf test unwind' should create kernel maps, now that entry works and the test passes (Jiri Olsa)

- Fix showing the running kernel build id in 'perf buildid-list' (Michael Petlan)

- Fix command line symbol filtering in 'perf report' (Namhyung Kim)

Infrastructure:

- Extract and collect map info from BPF object files in libbpf (Wang Nan)

----------------------------------------------------------------
Arnaldo Carvalho de Melo (2):
perf list: Add support for PERF_COUNT_SW_BPF_OUT perf list: Robustify event printing routine

Jiri Olsa (1):
perf test: 'unwind' test should create kernel maps

Michael Petlan (1):
perf buildid-list: Show running kernel build id fix

Namhyung Kim (3):
perf report: Show error message when processing sample fails perf hists: Do not skip elided fields when processing samples perf hists browser: Update nr entries regardless of min percent

Wang Nan (3):
tools lib bpf: Collect map definition in bpf_object tools lib bpf: Extract and collect map names from BPF object file perf bpf: Rename bpf config to program config

Quoted 11 lines
 tools/lib/bpf/libbpf.c          | 252 ++++++++++++++++++++++++++++++----------
 tools/lib/bpf/libbpf.h          |  24 ++++
 tools/perf/builtin-report.c     |  12 +-
 tools/perf/tests/dwarf-unwind.c |   5 +
 tools/perf/ui/browsers/hists.c  |   5 +-
 tools/perf/util/bpf-loader.c    |  65 +++++------
 tools/perf/util/bpf-loader.h    |   2 +-
 tools/perf/util/build-id.c      |   2 +-
 tools/perf/util/hist.c          |   6 -
 tools/perf/util/parse-events.c  |   6 +-
 10 files changed, 268 insertions(+), 111 deletions(-)
Quoted 4 lines

Pulled, thanks a lot Arnaldo!

Should any of the above fixes be cherry-picked over into perf/urgent, or are these all new regressions?

At least my changes should go into perf/urgent and probably stable too.

Thanks,
Namhyung