public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Wang Nan <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: peterz@infradead.org, wangnan0@huawei.com,
	adrian.hunter@intel.com, masami.hiramatsu.pt@hitachi.com,
	jeremie.galarneau@efficios.com, brendan.d.gregg@gmail.com,
	hekuang@huawei.com, jolsa@kernel.org,
	linux-kernel@vger.kernel.org, acme@redhat.com,
	tglx@linutronix.de, kirr@nexedi.com, namhyung@kernel.org,
	hpa@zytor.com, ast@kernel.org, lizefan@huawei.com,
	dev@codyps.com, mingo@kernel.org
Subject: [tip:perf/core] perf record: Apply config to BPF objects before recording
Date: Wed, 24 Feb 2016 21:39:42 -0800	[thread overview]
Message-ID: <tip-8690a2a773703e4ad2a07a7f3912ea6b131307cc@git.kernel.org> (raw)
In-Reply-To: <1456132275-98875-6-git-send-email-wangnan0@huawei.com>

Commit-ID:  8690a2a773703e4ad2a07a7f3912ea6b131307cc
Gitweb:     http://git.kernel.org/tip/8690a2a773703e4ad2a07a7f3912ea6b131307cc
Author:     Wang Nan <wangnan0@huawei.com>
AuthorDate: Mon, 22 Feb 2016 09:10:32 +0000
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Mon, 22 Feb 2016 12:28:02 -0300

perf record: Apply config to BPF objects before recording

bpf__apply_obj_config() is introduced as the core API to apply object
config options to all BPF objects. This patch also does the real work
for setting values for BPF_MAP_TYPE_PERF_ARRAY maps by inserting value
stored in map's private field into the BPF map.

This patch is required because we are not always able to set all BPF
config during parsing. Further patch will set events created by perf to
BPF_MAP_TYPE_PERF_EVENT_ARRAY maps, which is not exist until
perf_evsel__open().

bpf_map_foreach_key() is introduced to iterate over each key needs to be
configured. This function would be extended to support more map types
and different key settings.

In perf record, before start recording, call bpf__apply_config() to turn
on all BPF config options.

Test result:

  # cat ./test_bpf_map_1.c
  /************************ BEGIN **************************/
  #include <uapi/linux/bpf.h>
  #define SEC(NAME) __attribute__((section(NAME), used))
  struct bpf_map_def {
      unsigned int type;
      unsigned int key_size;
      unsigned int value_size;
      unsigned int max_entries;
  };
  static void *(*map_lookup_elem)(struct bpf_map_def *, void *) =
      (void *)BPF_FUNC_map_lookup_elem;
  static int (*trace_printk)(const char *fmt, int fmt_size, ...) =
      (void *)BPF_FUNC_trace_printk;
  struct bpf_map_def SEC("maps") channel = {
      .type = BPF_MAP_TYPE_ARRAY,
      .key_size = sizeof(int),
      .value_size = sizeof(int),
      .max_entries = 1,
  };
  SEC("func=sys_nanosleep")
  int func(void *ctx)
  {
      int key = 0;
      char fmt[] = "%d\n";
      int *pval = map_lookup_elem(&channel, &key);
      if (!pval)
          return 0;
      trace_printk(fmt, sizeof(fmt), *pval);
      return 0;
  }
  char _license[] SEC("license") = "GPL";
  int _version SEC("version") = LINUX_VERSION_CODE;
  /************************* END ***************************/

  # echo "" > /sys/kernel/debug/tracing/trace
  # ./perf record -e './test_bpf_map_1.c/map:channel.value=11/' usleep 10
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 0.012 MB perf.data ]
  # cat /sys/kernel/debug/tracing/trace
  # tracer: nop
  #
  # entries-in-buffer/entries-written: 1/1   #P:8
  [SNIP]
  #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
  #              | |       |   ||||       |         |
             usleep-18593 [007] d... 2394714.395539: : 11
  # ./perf record -e './test_bpf_map_1.c/map:channel.value=101/' usleep 10
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 0.012 MB perf.data ]
  # cat /sys/kernel/debug/tracing/trace
  # tracer: nop
  #
  # entries-in-buffer/entries-written: 1/1   #P:8
  [SNIP]
  #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
  #              | |       |   ||||       |         |
             usleep-18593 [007] d... 2394714.395539: : 11
             usleep-19000 [006] d... 2394831.057840: : 101

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Cody P Schafer <dev@codyps.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jeremie Galarneau <jeremie.galarneau@efficios.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kirill Smelkov <kirr@nexedi.com>
Cc: Li Zefan <lizefan@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1456132275-98875-6-git-send-email-wangnan0@huawei.com
Signed-off-by: He Kuang <hekuang@huawei.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-record.c  |  11 +++
 tools/perf/util/bpf-loader.c | 184 +++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/bpf-loader.h |  15 ++++
 3 files changed, 210 insertions(+)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index cf3a28d..7d11162 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -32,6 +32,7 @@
 #include "util/parse-branch-options.h"
 #include "util/parse-regs-options.h"
 #include "util/llvm-utils.h"
+#include "util/bpf-loader.h"
 
 #include <unistd.h>
 #include <sched.h>
@@ -536,6 +537,16 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 		goto out_child;
 	}
 
+	err = bpf__apply_obj_config();
+	if (err) {
+		char errbuf[BUFSIZ];
+
+		bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
+		pr_err("ERROR: Apply config to BPF failed: %s\n",
+			 errbuf);
+		goto out_child;
+	}
+
 	/*
 	 * Normally perf_session__new would do this, but it doesn't have the
 	 * evlist.
diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index caeef9e..dbbd17c 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -7,6 +7,7 @@
 
 #include <linux/bpf.h>
 #include <bpf/libbpf.h>
+#include <bpf/bpf.h>
 #include <linux/err.h>
 #include <linux/string.h>
 #include "perf.h"
@@ -994,6 +995,182 @@ out:
 
 }
 
+typedef int (*map_config_func_t)(const char *name, int map_fd,
+				 struct bpf_map_def *pdef,
+				 struct bpf_map_op *op,
+				 void *pkey, void *arg);
+
+static int
+foreach_key_array_all(map_config_func_t func,
+		      void *arg, const char *name,
+		      int map_fd, struct bpf_map_def *pdef,
+		      struct bpf_map_op *op)
+{
+	unsigned int i;
+	int err;
+
+	for (i = 0; i < pdef->max_entries; i++) {
+		err = func(name, map_fd, pdef, op, &i, arg);
+		if (err) {
+			pr_debug("ERROR: failed to insert value to %s[%u]\n",
+				 name, i);
+			return err;
+		}
+	}
+	return 0;
+}
+
+static int
+bpf_map_config_foreach_key(struct bpf_map *map,
+			   map_config_func_t func,
+			   void *arg)
+{
+	int err, map_fd;
+	const char *name;
+	struct bpf_map_op *op;
+	struct bpf_map_def def;
+	struct bpf_map_priv *priv;
+
+	name = bpf_map__get_name(map);
+
+	err = bpf_map__get_private(map, (void **)&priv);
+	if (err) {
+		pr_debug("ERROR: failed to get private from map %s\n", name);
+		return -BPF_LOADER_ERRNO__INTERNAL;
+	}
+	if (!priv || list_empty(&priv->ops_list)) {
+		pr_debug("INFO: nothing to config for map %s\n", name);
+		return 0;
+	}
+
+	err = bpf_map__get_def(map, &def);
+	if (err) {
+		pr_debug("ERROR: failed to get definition from map %s\n", name);
+		return -BPF_LOADER_ERRNO__INTERNAL;
+	}
+	map_fd = bpf_map__get_fd(map);
+	if (map_fd < 0) {
+		pr_debug("ERROR: failed to get fd from map %s\n", name);
+		return map_fd;
+	}
+
+	list_for_each_entry(op, &priv->ops_list, list) {
+		switch (def.type) {
+		case BPF_MAP_TYPE_ARRAY:
+			switch (op->key_type) {
+			case BPF_MAP_KEY_ALL:
+				err = foreach_key_array_all(func, arg, name,
+							    map_fd, &def, op);
+				if (err)
+					return err;
+				break;
+			default:
+				pr_debug("ERROR: keytype for map '%s' invalid\n",
+					 name);
+				return -BPF_LOADER_ERRNO__INTERNAL;
+			}
+			break;
+		default:
+			pr_debug("ERROR: type of '%s' incorrect\n", name);
+			return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
+		}
+	}
+
+	return 0;
+}
+
+static int
+apply_config_value_for_key(int map_fd, void *pkey,
+			   size_t val_size, u64 val)
+{
+	int err = 0;
+
+	switch (val_size) {
+	case 1: {
+		u8 _val = (u8)(val);
+		err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
+		break;
+	}
+	case 2: {
+		u16 _val = (u16)(val);
+		err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
+		break;
+	}
+	case 4: {
+		u32 _val = (u32)(val);
+		err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
+		break;
+	}
+	case 8: {
+		err = bpf_map_update_elem(map_fd, pkey, &val, BPF_ANY);
+		break;
+	}
+	default:
+		pr_debug("ERROR: invalid value size\n");
+		return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE;
+	}
+	if (err && errno)
+		err = -errno;
+	return err;
+}
+
+static int
+apply_obj_config_map_for_key(const char *name, int map_fd,
+			     struct bpf_map_def *pdef __maybe_unused,
+			     struct bpf_map_op *op,
+			     void *pkey, void *arg __maybe_unused)
+{
+	int err;
+
+	switch (op->op_type) {
+	case BPF_MAP_OP_SET_VALUE:
+		err = apply_config_value_for_key(map_fd, pkey,
+						 pdef->value_size,
+						 op->v.value);
+		break;
+	default:
+		pr_debug("ERROR: unknown value type for '%s'\n", name);
+		err = -BPF_LOADER_ERRNO__INTERNAL;
+	}
+	return err;
+}
+
+static int
+apply_obj_config_map(struct bpf_map *map)
+{
+	return bpf_map_config_foreach_key(map,
+					  apply_obj_config_map_for_key,
+					  NULL);
+}
+
+static int
+apply_obj_config_object(struct bpf_object *obj)
+{
+	struct bpf_map *map;
+	int err;
+
+	bpf_map__for_each(map, obj) {
+		err = apply_obj_config_map(map);
+		if (err)
+			return err;
+	}
+	return 0;
+}
+
+int bpf__apply_obj_config(void)
+{
+	struct bpf_object *obj, *tmp;
+	int err;
+
+	bpf_object__for_each_safe(obj, tmp) {
+		err = apply_obj_config_object(obj);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 #define ERRNO_OFFSET(e)		((e) - __BPF_LOADER_ERRNO__START)
 #define ERRCODE_OFFSET(c)	ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)
 #define NR_ERRNO	(__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
@@ -1148,3 +1325,10 @@ int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
 	bpf__strerror_end(buf, size);
 	return 0;
 }
+
+int bpf__strerror_apply_obj_config(int err, char *buf, size_t size)
+{
+	bpf__strerror_head(err, buf, size);
+	bpf__strerror_end(buf, size);
+	return 0;
+}
diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h
index cc46a07..5d3b931 100644
--- a/tools/perf/util/bpf-loader.h
+++ b/tools/perf/util/bpf-loader.h
@@ -71,6 +71,8 @@ int bpf__strerror_config_obj(struct bpf_object *obj,
 			     struct perf_evlist *evlist,
 			     int *error_pos, int err, char *buf,
 			     size_t size);
+int bpf__apply_obj_config(void);
+int bpf__strerror_apply_obj_config(int err, char *buf, size_t size);
 #else
 static inline struct bpf_object *
 bpf__prepare_load(const char *filename __maybe_unused,
@@ -111,6 +113,12 @@ bpf__config_obj(struct bpf_object *obj __maybe_unused,
 }
 
 static inline int
+bpf__apply_obj_config(void)
+{
+	return 0;
+}
+
+static inline int
 __bpf_strerror(char *buf, size_t size)
 {
 	if (!size)
@@ -156,5 +164,12 @@ bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
 {
 	return __bpf_strerror(buf, size);
 }
+
+static inline int
+bpf__strerror_apply_obj_config(int err __maybe_unused,
+			       char *buf, size_t size)
+{
+	return __bpf_strerror(buf, size);
+}
 #endif
 #endif

  reply	other threads:[~2016-02-25  6:27 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-22  9:10 [PATCH 00/48] perf tools: Bugfix, BPF improvements and overwrite ring buffer support Wang Nan
2016-02-22  9:10 ` [PATCH 01/48] perf tools: Record text offset in dso to calculate objdump address Wang Nan
2016-02-22  9:10 ` [PATCH 02/48] perf tools: Adjust symbol for shared objects Wang Nan
2016-02-22  9:10 ` [PATCH 03/48] perf bpf: Add API to set values to map entries in a bpf object Wang Nan
2016-02-25  5:39   ` [tip:perf/core] " tip-bot for Wang Nan
2016-02-22  9:10 ` [PATCH 04/48] perf tools: Enable BPF object configure syntax Wang Nan
2016-02-25  5:39   ` [tip:perf/core] " tip-bot for Wang Nan
2016-02-22  9:10 ` [PATCH 05/48] perf record: Apply config to BPF objects before recording Wang Nan
2016-02-25  5:39   ` tip-bot for Wang Nan [this message]
2016-02-22  9:10 ` [PATCH 06/48] perf tools: Enable passing event to BPF object Wang Nan
2016-02-25  5:40   ` [tip:perf/core] " tip-bot for Wang Nan
2016-02-22  9:10 ` [PATCH 07/48] perf tools: Support setting different slots in a BPF map separately Wang Nan
2016-02-25  5:40   ` [tip:perf/core] " tip-bot for Wang Nan
2016-02-22  9:10 ` [PATCH 08/48] perf tools: Enable indices setting syntax for BPF map Wang Nan
2016-02-25  5:40   ` [tip:perf/core] " tip-bot for Wang Nan
2016-02-22  9:10 ` [PATCH 09/48] perf tools: Pass tracepoint options to BPF script Wang Nan
2016-02-25  5:41   ` [tip:perf/core] perf tools: Apply tracepoint event definition " tip-bot for Wang Nan
2016-02-22  9:10 ` [PATCH 10/48] perf tools: Introduce bpf-output event Wang Nan
2016-02-23 17:45   ` Arnaldo Carvalho de Melo
2016-02-24  1:58     ` Wangnan (F)
2016-02-24  2:04       ` Wangnan (F)
2016-02-24  4:03         ` Wangnan (F)
2016-02-24  5:03           ` Wangnan (F)
2016-02-24 13:36       ` Arnaldo Carvalho de Melo
2016-02-25  5:41   ` [tip:perf/core] " tip-bot for Wang Nan
2016-02-22  9:10 ` [PATCH 11/48] perf data: Support converting data from bpf_perf_event_output() Wang Nan
2016-02-23 16:14   ` Arnaldo Carvalho de Melo
2016-02-23 17:23   ` Jiri Olsa
2016-02-23 17:24     ` Jiri Olsa
2016-02-23 19:22   ` Jiri Olsa
2016-02-22  9:10 ` [PATCH 12/48] perf data: Explicitly set byte order for integer types Wang Nan
2016-02-22  9:10 ` [PATCH 13/48] perf core: Introduce new ioctl options to pause and resume ring buffer Wang Nan
2016-02-22  9:10 ` [PATCH 14/48] perf core: Set event's default overflow_handler Wang Nan
2016-02-22  9:10 ` [PATCH 15/48] perf core: Prepare writing into ring buffer from end Wang Nan
2016-02-22  9:10 ` [PATCH 16/48] perf core: Add backward attribute to perf event Wang Nan
2016-02-24 13:08   ` Jiri Olsa
2016-02-24 13:21     ` Jiri Olsa
2016-02-22  9:10 ` [PATCH 17/48] perf core: Reduce perf event output overhead by new overflow handler Wang Nan
2016-02-22  9:10 ` [PATCH 18/48] perf tools: Only validate is_pos for tracking evsels Wang Nan
2016-02-24 14:21   ` Jiri Olsa
2016-02-22  9:10 ` [PATCH 19/48] perf tools: Print write_backward value in perf_event_attr__fprintf Wang Nan
2016-02-22  9:10 ` [PATCH 20/48] perf tools: Make ordered_events reusable Wang Nan
2016-02-24 14:18   ` Jiri Olsa
2016-02-22  9:10 ` [PATCH 21/48] perf record: Extract synthesize code to record__synthesize() Wang Nan
2016-02-24 14:29   ` Jiri Olsa
2016-02-22  9:10 ` [PATCH 22/48] perf tools: Add perf_data_file__switch() helper Wang Nan
2016-02-24 14:34   ` Jiri Olsa
2016-02-22  9:10 ` [PATCH 23/48] perf record: Turns auxtrace_snapshot_enable into 3 states Wang Nan
2016-02-24 14:43   ` Jiri Olsa
2016-02-22  9:10 ` [PATCH 24/48] perf record: Introduce record__finish_output() to finish a perf.data Wang Nan
2016-02-22  9:10 ` [PATCH 25/48] perf record: Add '--timestamp-filename' option to append timestamp to output filename Wang Nan
2016-02-22  9:10 ` [PATCH 26/48] perf record: Split output into multiple files via '--switch-output' Wang Nan
2016-02-22  9:10 ` [PATCH 27/48] perf record: Force enable --timestamp-filename when --switch-output is provided Wang Nan
2016-02-22  9:10 ` [PATCH 28/48] perf record: Disable buildid cache options by default in switch output mode Wang Nan
2016-02-22  9:10 ` [PATCH 29/48] perf record: Re-synthesize tracking events after output switching Wang Nan
2016-02-24 14:57   ` Jiri Olsa
2016-02-22  9:10 ` [PATCH 30/48] perf record: Generate tracking events for process forked by perf Wang Nan
2016-02-24 15:01   ` Jiri Olsa
2016-02-22  9:10 ` [PATCH 31/48] perf record: Ensure return non-zero rc when mmap fail Wang Nan
2016-02-22  9:10 ` [PATCH 32/48] perf record: Prevent reading invalid data in record__mmap_read Wang Nan
2016-02-22  9:11 ` [PATCH 33/48] perf tools: Add evlist channel helpers Wang Nan
2016-02-22  9:11 ` [PATCH 34/48] perf tools: Automatically add new channel according to evlist Wang Nan
2016-02-22  9:11 ` [PATCH 35/48] perf tools: Operate multiple channels Wang Nan
2016-02-22  9:11 ` [PATCH 36/48] perf tools: Squash overwrite setting into channel Wang Nan
2016-02-22  9:11 ` [PATCH 37/48] perf record: Don't read from and poll overwrite channel Wang Nan
2016-02-22  9:11 ` [PATCH 38/48] perf record: Don't poll on " Wang Nan
2016-02-22  9:11 ` [PATCH 39/48] perf tools: Detect avalibility of write_backward Wang Nan
2016-02-22  9:11 ` [PATCH 40/48] perf tools: Enable overwrite settings Wang Nan
2016-02-22  9:11 ` [PATCH 41/48] perf tools: Set write_backward attribut bit for overwrite events Wang Nan
2016-02-22  9:11 ` [PATCH 42/48] perf tools: Record fd into perf_mmap Wang Nan
2016-02-22  9:11 ` [PATCH 43/48] perf tools: Add API to pause a channel Wang Nan
2016-02-22  9:11 ` [PATCH 44/48] perf record: Toggle overwrite ring buffer for reading Wang Nan
2016-02-22  9:11 ` [PATCH 45/48] perf record: Rename variable to make code clear Wang Nan
2016-02-22  9:11 ` [PATCH 46/48] perf record: Read from backward ring buffer Wang Nan
2016-02-22  9:11 ` [PATCH 47/48] perf record: Allow generate tracking events at the end of output Wang Nan
2016-02-22  9:11 ` [PATCH 48/48] perf tools: Don't warn about out of order event if write_backward is used Wang Nan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-8690a2a773703e4ad2a07a7f3912ea6b131307cc@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=acme@redhat.com \
    --cc=adrian.hunter@intel.com \
    --cc=ast@kernel.org \
    --cc=brendan.d.gregg@gmail.com \
    --cc=dev@codyps.com \
    --cc=hekuang@huawei.com \
    --cc=hpa@zytor.com \
    --cc=jeremie.galarneau@efficios.com \
    --cc=jolsa@kernel.org \
    --cc=kirr@nexedi.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=wangnan0@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox