bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5 bpf-next 0/3] bpf: sharing bpf runtime stats with
@ 2020-04-27 23:44 Song Liu
  2020-04-27 23:44 ` [PATCH v5 bpf-next 1/3] bpf: sharing bpf runtime stats with BPF_ENABLE_STATS Song Liu
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Song Liu @ 2020-04-27 23:44 UTC (permalink / raw)
  To: bpf, netdev; +Cc: ast, daniel, kernel-team, Song Liu

run_time_ns is a useful stats for BPF programs. However, it is gated by
sysctl kernel.bpf_stats_enabled. When multiple user space tools are
toggling kernl.bpf_stats_enabled at the same time, they may confuse each
other.

Solve this problem with a new BPF command BPF_ENABLE_STATS.

Changes v4 => v5:
  1. Use memset to zero bpf_attr in bpf_enable_stats() (Andrii).

Changes v3 => v4:
  1. Add libbpf support and selftest;
  2. Avoid cleaning trailing space.

Changes v2 => v3:
  1. Rename the command to BPF_ENABLE_STATS, and make it extendible.
  2. fix commit log;
  3. remove unnecessary headers.

Song Liu (3):
  bpf: sharing bpf runtime stats with BPF_ENABLE_STATS
  libbpf: add support for command BPF_ENABLE_STATS
  bpf: add selftest for BPF_ENABLE_STATS

 include/linux/bpf.h                           |  1 +
 include/uapi/linux/bpf.h                      | 11 ++++
 kernel/bpf/syscall.c                          | 50 +++++++++++++++++++
 kernel/sysctl.c                               | 36 ++++++++++++-
 tools/include/uapi/linux/bpf.h                | 11 ++++
 tools/lib/bpf/bpf.c                           | 10 ++++
 tools/lib/bpf/bpf.h                           |  1 +
 tools/lib/bpf/libbpf.map                      |  5 ++
 .../selftests/bpf/prog_tests/enable_stats.c   | 45 +++++++++++++++++
 .../selftests/bpf/progs/test_enable_stats.c   | 28 +++++++++++
 10 files changed, 197 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/enable_stats.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_enable_stats.c

--
2.24.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v5 bpf-next 1/3] bpf: sharing bpf runtime stats with BPF_ENABLE_STATS
  2020-04-27 23:44 [PATCH v5 bpf-next 0/3] bpf: sharing bpf runtime stats with Song Liu
@ 2020-04-27 23:44 ` Song Liu
  2020-04-27 23:44 ` [PATCH v5 bpf-next 2/3] libbpf: add support for command BPF_ENABLE_STATS Song Liu
  2020-04-27 23:44 ` [PATCH v5 bpf-next 3/3] bpf: add selftest for BPF_ENABLE_STATS Song Liu
  2 siblings, 0 replies; 7+ messages in thread
From: Song Liu @ 2020-04-27 23:44 UTC (permalink / raw)
  To: bpf, netdev; +Cc: ast, daniel, kernel-team, Song Liu

Currently, sysctl kernel.bpf_stats_enabled controls BPF runtime stats.
Typical userspace tools use kernel.bpf_stats_enabled as follows:

  1. Enable kernel.bpf_stats_enabled;
  2. Check program run_time_ns;
  3. Sleep for the monitoring period;
  4. Check program run_time_ns again, calculate the difference;
  5. Disable kernel.bpf_stats_enabled.

The problem with this approach is that only one userspace tool can toggle
this sysctl. If multiple tools toggle the sysctl at the same time, the
measurement may be inaccurate.

To fix this problem while keep backward compatibility, introduce a new
bpf command BPF_ENABLE_STATS. On success, this command enables stats and
returns a valid fd. BPF_ENABLE_STATS takes argument "type". Currently,
only one type, BPF_STATS_RUNTIME_CNT, is supported. We can extend the
command to support other types of stats in the future.

With BPF_ENABLE_STATS, user space tool would have the following flow:

  1. Get a fd with BPF_ENABLE_STATS, and make sure it is valid;
  2. Check program run_time_ns;
  3. Sleep for the monitoring period;
  4. Check program run_time_ns again, calculate the difference;
  5. Close the fd.

Signed-off-by: Song Liu <songliubraving@fb.com>
---
 include/linux/bpf.h            |  1 +
 include/uapi/linux/bpf.h       | 11 ++++++++
 kernel/bpf/syscall.c           | 50 ++++++++++++++++++++++++++++++++++
 kernel/sysctl.c                | 36 +++++++++++++++++++++++-
 tools/include/uapi/linux/bpf.h | 11 ++++++++
 5 files changed, 108 insertions(+), 1 deletion(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 10960cfabea4..09ba490a0500 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -987,6 +987,7 @@ _out:							\
 
 #ifdef CONFIG_BPF_SYSCALL
 DECLARE_PER_CPU(int, bpf_prog_active);
+extern struct mutex bpf_stats_enabled_mutex;
 
 /*
  * Block execution of BPF programs attached to instrumentation (perf,
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 4a6c47f3febe..fd7c64139f21 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -113,6 +113,7 @@ enum bpf_cmd {
 	BPF_MAP_DELETE_BATCH,
 	BPF_LINK_CREATE,
 	BPF_LINK_UPDATE,
+	BPF_ENABLE_STATS,
 };
 
 enum bpf_map_type {
@@ -379,6 +380,12 @@ enum {
  */
 #define BPF_F_QUERY_EFFECTIVE	(1U << 0)
 
+/* type for BPF_ENABLE_STATS */
+enum bpf_stats_type {
+	/* enabled run_time_ns and run_cnt */
+	BPF_STATS_RUNTIME_CNT = 0,
+};
+
 enum bpf_stack_build_id_status {
 	/* user space need an empty entry to identify end of a trace */
 	BPF_STACK_BUILD_ID_EMPTY = 0,
@@ -589,6 +596,10 @@ union bpf_attr {
 		__u32		old_prog_fd;
 	} link_update;
 
+	struct { /* struct used by BPF_ENABLE_STATS command */
+		__u32		type;
+	} enable_stats;
+
 } __attribute__((aligned(8)));
 
 /* The description below is an attempt at providing documentation to eBPF
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 7626b8024471..1740a87b99f3 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3663,6 +3663,53 @@ static int link_update(union bpf_attr *attr)
 	return ret;
 }
 
+DEFINE_MUTEX(bpf_stats_enabled_mutex);
+
+static int bpf_stats_release(struct inode *inode, struct file *file)
+{
+	mutex_lock(&bpf_stats_enabled_mutex);
+	static_key_slow_dec(&bpf_stats_enabled_key.key);
+	mutex_unlock(&bpf_stats_enabled_mutex);
+	return 0;
+}
+
+static const struct file_operations bpf_stats_fops = {
+	.release = bpf_stats_release,
+};
+
+static int bpf_enable_runtime_stats(void)
+{
+	int fd;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	mutex_lock(&bpf_stats_enabled_mutex);
+	/* Set a very high limit to avoid overflow */
+	if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
+		mutex_unlock(&bpf_stats_enabled_mutex);
+		return -EBUSY;
+	}
+
+	fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, 0);
+	if (fd >= 0)
+		static_key_slow_inc(&bpf_stats_enabled_key.key);
+
+	mutex_unlock(&bpf_stats_enabled_mutex);
+	return fd;
+}
+
+static int bpf_enable_stats(union bpf_attr *attr)
+{
+	switch (attr->enable_stats.type) {
+	case BPF_STATS_RUNTIME_CNT:
+		return bpf_enable_runtime_stats();
+	default:
+		break;
+	}
+	return -EINVAL;
+}
+
 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
 {
 	union bpf_attr attr;
@@ -3780,6 +3827,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
 	case BPF_LINK_UPDATE:
 		err = link_update(&attr);
 		break;
+	case BPF_ENABLE_STATS:
+		err = bpf_enable_stats(&attr);
+		break;
 	default:
 		err = -EINVAL;
 		break;
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 8a176d8727a3..2d72f4ad3063 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -304,6 +304,40 @@ static int min_extfrag_threshold;
 static int max_extfrag_threshold = 1000;
 #endif
 
+#ifdef CONFIG_BPF_SYSCALL
+static int bpf_stats_handler(struct ctl_table *table, int write,
+			     void __user *buffer, size_t *lenp,
+			     loff_t *ppos)
+{
+	struct static_key *key = (struct static_key *)table->data;
+	static int saved_val;
+	int val, ret;
+	struct ctl_table tmp = {
+		.data   = &val,
+		.maxlen = sizeof(val),
+		.mode   = table->mode,
+		.extra1 = SYSCTL_ZERO,
+		.extra2 = SYSCTL_ONE,
+	};
+
+	if (write && !capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	mutex_lock(&bpf_stats_enabled_mutex);
+	val = saved_val;
+	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
+	if (write && !ret && val != saved_val) {
+		if (val)
+			static_key_slow_inc(key);
+		else
+			static_key_slow_dec(key);
+		saved_val = val;
+	}
+	mutex_unlock(&bpf_stats_enabled_mutex);
+	return ret;
+}
+#endif
+
 static struct ctl_table kern_table[] = {
 	{
 		.procname	= "sched_child_runs_first",
@@ -1244,7 +1278,7 @@ static struct ctl_table kern_table[] = {
 		.data		= &bpf_stats_enabled_key.key,
 		.maxlen		= sizeof(bpf_stats_enabled_key),
 		.mode		= 0644,
-		.proc_handler	= proc_do_static_key,
+		.proc_handler	= bpf_stats_handler,
 	},
 #endif
 #if defined(CONFIG_TREE_RCU)
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 4a6c47f3febe..fd7c64139f21 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -113,6 +113,7 @@ enum bpf_cmd {
 	BPF_MAP_DELETE_BATCH,
 	BPF_LINK_CREATE,
 	BPF_LINK_UPDATE,
+	BPF_ENABLE_STATS,
 };
 
 enum bpf_map_type {
@@ -379,6 +380,12 @@ enum {
  */
 #define BPF_F_QUERY_EFFECTIVE	(1U << 0)
 
+/* type for BPF_ENABLE_STATS */
+enum bpf_stats_type {
+	/* enabled run_time_ns and run_cnt */
+	BPF_STATS_RUNTIME_CNT = 0,
+};
+
 enum bpf_stack_build_id_status {
 	/* user space need an empty entry to identify end of a trace */
 	BPF_STACK_BUILD_ID_EMPTY = 0,
@@ -589,6 +596,10 @@ union bpf_attr {
 		__u32		old_prog_fd;
 	} link_update;
 
+	struct { /* struct used by BPF_ENABLE_STATS command */
+		__u32		type;
+	} enable_stats;
+
 } __attribute__((aligned(8)));
 
 /* The description below is an attempt at providing documentation to eBPF
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v5 bpf-next 2/3] libbpf: add support for command BPF_ENABLE_STATS
  2020-04-27 23:44 [PATCH v5 bpf-next 0/3] bpf: sharing bpf runtime stats with Song Liu
  2020-04-27 23:44 ` [PATCH v5 bpf-next 1/3] bpf: sharing bpf runtime stats with BPF_ENABLE_STATS Song Liu
@ 2020-04-27 23:44 ` Song Liu
  2020-04-27 23:44 ` [PATCH v5 bpf-next 3/3] bpf: add selftest for BPF_ENABLE_STATS Song Liu
  2 siblings, 0 replies; 7+ messages in thread
From: Song Liu @ 2020-04-27 23:44 UTC (permalink / raw)
  To: bpf, netdev; +Cc: ast, daniel, kernel-team, Song Liu

bpf_enable_stats() is added to enable given stats.

Signed-off-by: Song Liu <songliubraving@fb.com>
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  1 +
 tools/lib/bpf/libbpf.map |  5 +++++
 3 files changed, 16 insertions(+)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 5cc1b0785d18..17bb4ad06c0e 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -826,3 +826,13 @@ int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
 
 	return err;
 }
+
+int bpf_enable_stats(enum bpf_stats_type type)
+{
+	union bpf_attr attr;
+
+	memset(&attr, 0, sizeof(attr));
+	attr.enable_stats.type = type;
+
+	return sys_bpf(BPF_ENABLE_STATS, &attr, sizeof(attr));
+}
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 46d47afdd887..5996e64d324c 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -229,6 +229,7 @@ LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf,
 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
 				 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
 				 __u64 *probe_offset, __u64 *probe_addr);
+LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
 
 #ifdef __cplusplus
 } /* extern "C" */
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index bb8831605b25..ebd946faada5 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -254,3 +254,8 @@ LIBBPF_0.0.8 {
 		bpf_program__set_lsm;
 		bpf_set_link_xdp_fd_opts;
 } LIBBPF_0.0.7;
+
+LIBBPF_0.0.9 {
+	global:
+		bpf_enable_stats;
+} LIBBPF_0.0.8;
\ No newline at end of file
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v5 bpf-next 3/3] bpf: add selftest for BPF_ENABLE_STATS
  2020-04-27 23:44 [PATCH v5 bpf-next 0/3] bpf: sharing bpf runtime stats with Song Liu
  2020-04-27 23:44 ` [PATCH v5 bpf-next 1/3] bpf: sharing bpf runtime stats with BPF_ENABLE_STATS Song Liu
  2020-04-27 23:44 ` [PATCH v5 bpf-next 2/3] libbpf: add support for command BPF_ENABLE_STATS Song Liu
@ 2020-04-27 23:44 ` Song Liu
  2020-04-28  6:04   ` Yonghong Song
  2 siblings, 1 reply; 7+ messages in thread
From: Song Liu @ 2020-04-27 23:44 UTC (permalink / raw)
  To: bpf, netdev; +Cc: ast, daniel, kernel-team, Song Liu

Add test for  BPF_ENABLE_STATS, which should enable run_time_ns stats.

~/selftests/bpf# ./test_progs -t enable_stats  -v
test_enable_stats:PASS:skel_open_and_load 0 nsec
test_enable_stats:PASS:get_stats_fd 0 nsec
test_enable_stats:PASS:attach_raw_tp 0 nsec
test_enable_stats:PASS:get_prog_info 0 nsec
test_enable_stats:PASS:check_stats_enabled 0 nsec
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Song Liu <songliubraving@fb.com>
---
 .../selftests/bpf/prog_tests/enable_stats.c   | 45 +++++++++++++++++++
 .../selftests/bpf/progs/test_enable_stats.c   | 28 ++++++++++++
 2 files changed, 73 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/enable_stats.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_enable_stats.c

diff --git a/tools/testing/selftests/bpf/prog_tests/enable_stats.c b/tools/testing/selftests/bpf/prog_tests/enable_stats.c
new file mode 100644
index 000000000000..987fc743ab75
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/enable_stats.c
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <sys/mman.h>
+#include "test_enable_stats.skel.h"
+
+void test_enable_stats(void)
+{
+	struct test_enable_stats *skel;
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info);
+	int stats_fd, err, prog_fd;
+	int duration = 0;
+
+	skel = test_enable_stats__open_and_load();
+
+	if (CHECK(!skel, "skel_open_and_load", "skeleton open/load failed\n"))
+		return;
+
+	stats_fd = bpf_enable_stats(BPF_STATS_RUNTIME_CNT);
+
+	if (CHECK(stats_fd < 0, "get_stats_fd", "failed %d\n", errno))
+		goto cleanup;
+
+	err = test_enable_stats__attach(skel);
+
+	if (CHECK(err, "attach_raw_tp", "err %d\n", err))
+		goto cleanup;
+
+	usleep(1000);
+
+	prog_fd = bpf_program__fd(skel->progs.test_enable_stats);
+	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
+
+	if (CHECK(err, "get_prog_info",
+		  "failed to get bpf_prog_info for fd %d\n", prog_fd))
+		goto cleanup;
+
+	CHECK(info.run_time_ns == 0, "check_stats_enabled",
+	      "failed to enable run_time_ns stats\n");
+
+cleanup:
+	test_enable_stats__destroy(skel);
+	if (stats_fd >= 0)
+		close(stats_fd);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_enable_stats.c b/tools/testing/selftests/bpf/progs/test_enable_stats.c
new file mode 100644
index 000000000000..f95ac0c94639
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_enable_stats.c
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2020 Facebook
+
+#include <linux/bpf.h>
+#include <stdint.h>
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u64);
+} count SEC(".maps");
+
+SEC("raw_tracepoint/sys_enter")
+int test_enable_stats(void *ctx)
+{
+	__u32 key = 0;
+	__u64 *val;
+
+	val = bpf_map_lookup_elem(&count, &key);
+	if (val)
+		*val += 1;
+
+	return 0;
+}
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 bpf-next 3/3] bpf: add selftest for BPF_ENABLE_STATS
  2020-04-27 23:44 ` [PATCH v5 bpf-next 3/3] bpf: add selftest for BPF_ENABLE_STATS Song Liu
@ 2020-04-28  6:04   ` Yonghong Song
  2020-04-28 22:09     ` Song Liu
  0 siblings, 1 reply; 7+ messages in thread
From: Yonghong Song @ 2020-04-28  6:04 UTC (permalink / raw)
  To: Song Liu, bpf, netdev; +Cc: ast, daniel, kernel-team



On 4/27/20 4:44 PM, Song Liu wrote:
> Add test for  BPF_ENABLE_STATS, which should enable run_time_ns stats.
> 
> ~/selftests/bpf# ./test_progs -t enable_stats  -v
> test_enable_stats:PASS:skel_open_and_load 0 nsec
> test_enable_stats:PASS:get_stats_fd 0 nsec
> test_enable_stats:PASS:attach_raw_tp 0 nsec
> test_enable_stats:PASS:get_prog_info 0 nsec
> test_enable_stats:PASS:check_stats_enabled 0 nsec
> Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
> 
> Signed-off-by: Song Liu <songliubraving@fb.com>
> ---
>   .../selftests/bpf/prog_tests/enable_stats.c   | 45 +++++++++++++++++++
>   .../selftests/bpf/progs/test_enable_stats.c   | 28 ++++++++++++
>   2 files changed, 73 insertions(+)
>   create mode 100644 tools/testing/selftests/bpf/prog_tests/enable_stats.c
>   create mode 100644 tools/testing/selftests/bpf/progs/test_enable_stats.c
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/enable_stats.c b/tools/testing/selftests/bpf/prog_tests/enable_stats.c
> new file mode 100644
> index 000000000000..987fc743ab75
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/enable_stats.c
> @@ -0,0 +1,45 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <test_progs.h>
> +#include <sys/mman.h>
> +#include "test_enable_stats.skel.h"
> +
> +void test_enable_stats(void)
> +{
> +	struct test_enable_stats *skel;
> +	struct bpf_prog_info info = {};
> +	__u32 info_len = sizeof(info);
> +	int stats_fd, err, prog_fd;
> +	int duration = 0;
> +
> +	skel = test_enable_stats__open_and_load();
> +
> +	if (CHECK(!skel, "skel_open_and_load", "skeleton open/load failed\n"))
> +		return;
> +
> +	stats_fd = bpf_enable_stats(BPF_STATS_RUNTIME_CNT);
> +
> +	if (CHECK(stats_fd < 0, "get_stats_fd", "failed %d\n", errno))
> +		goto cleanup;
> +
> +	err = test_enable_stats__attach(skel);
> +
> +	if (CHECK(err, "attach_raw_tp", "err %d\n", err))
> +		goto cleanup;
> +
> +	usleep(1000);
> +
> +	prog_fd = bpf_program__fd(skel->progs.test_enable_stats);
> +	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
> +
> +	if (CHECK(err, "get_prog_info",
> +		  "failed to get bpf_prog_info for fd %d\n", prog_fd))
> +		goto cleanup;
> +
> +	CHECK(info.run_time_ns == 0, "check_stats_enabled",
> +	      "failed to enable run_time_ns stats\n");
> +
> +cleanup:
> +	test_enable_stats__destroy(skel);
> +	if (stats_fd >= 0)
> +		close(stats_fd);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/test_enable_stats.c b/tools/testing/selftests/bpf/progs/test_enable_stats.c
> new file mode 100644
> index 000000000000..f95ac0c94639
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_enable_stats.c
> @@ -0,0 +1,28 @@
> +// SPDX-License-Identifier: GPL-2.0
> +// Copyright (c) 2020 Facebook
> +
> +#include <linux/bpf.h>
> +#include <stdint.h>
> +#include <bpf/bpf_helpers.h>
> +
> +char _license[] SEC("license") = "GPL";
> +
> +struct {
> +	__uint(type, BPF_MAP_TYPE_ARRAY);
> +	__uint(max_entries, 1);
> +	__type(key, __u32);
> +	__type(value, __u64);
> +} count SEC(".maps");

Looks like a global variable can be used here?

> +
> +SEC("raw_tracepoint/sys_enter")
> +int test_enable_stats(void *ctx)
> +{
> +	__u32 key = 0;
> +	__u64 *val;
> +
> +	val = bpf_map_lookup_elem(&count, &key);
> +	if (val)
> +		*val += 1;
> +
> +	return 0;
> +}
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 bpf-next 3/3] bpf: add selftest for BPF_ENABLE_STATS
  2020-04-28  6:04   ` Yonghong Song
@ 2020-04-28 22:09     ` Song Liu
  2020-04-28 22:13       ` Alexei Starovoitov
  0 siblings, 1 reply; 7+ messages in thread
From: Song Liu @ 2020-04-28 22:09 UTC (permalink / raw)
  To: Yonghong Song; +Cc: bpf, netdev, ast, daniel, Kernel Team



> On Apr 27, 2020, at 11:04 PM, Yonghong Song <yhs@fb.com> wrote:
> 
> 
> 
> On 4/27/20 4:44 PM, Song Liu wrote:
>> Add test for  BPF_ENABLE_STATS, which should enable run_time_ns stats.
>> ~/selftests/bpf# ./test_progs -t enable_stats  -v
>> test_enable_stats:PASS:skel_open_and_load 0 nsec
>> test_enable_stats:PASS:get_stats_fd 0 nsec
>> test_enable_stats:PASS:attach_raw_tp 0 nsec
>> test_enable_stats:PASS:get_prog_info 0 nsec
>> test_enable_stats:PASS:check_stats_enabled 0 nsec
>> Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
>> Signed-off-by: Song Liu <songliubraving@fb.com>
>> ---
>>  .../selftests/bpf/prog_tests/enable_stats.c   | 45 +++++++++++++++++++
>>  .../selftests/bpf/progs/test_enable_stats.c   | 28 ++++++++++++
>>  2 files changed, 73 insertions(+)
>>  create mode 100644 tools/testing/selftests/bpf/prog_tests/enable_stats.c
>>  create mode 100644 tools/testing/selftests/bpf/progs/test_enable_stats.c
>> diff --git a/tools/testing/selftests/bpf/prog_tests/enable_stats.c b/tools/testing/selftests/bpf/prog_tests/enable_stats.c
>> new file mode 100644
>> index 000000000000..987fc743ab75
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/prog_tests/enable_stats.c
>> @@ -0,0 +1,45 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +#include <test_progs.h>
>> +#include <sys/mman.h>
>> +#include "test_enable_stats.skel.h"
>> +
>> +void test_enable_stats(void)
>> +{
>> +	struct test_enable_stats *skel;
>> +	struct bpf_prog_info info = {};
>> +	__u32 info_len = sizeof(info);
>> +	int stats_fd, err, prog_fd;
>> +	int duration = 0;
>> +
>> +	skel = test_enable_stats__open_and_load();
>> +
>> +	if (CHECK(!skel, "skel_open_and_load", "skeleton open/load failed\n"))
>> +		return;
>> +
>> +	stats_fd = bpf_enable_stats(BPF_STATS_RUNTIME_CNT);
>> +
>> +	if (CHECK(stats_fd < 0, "get_stats_fd", "failed %d\n", errno))
>> +		goto cleanup;
>> +
>> +	err = test_enable_stats__attach(skel);
>> +
>> +	if (CHECK(err, "attach_raw_tp", "err %d\n", err))
>> +		goto cleanup;
>> +
>> +	usleep(1000);
>> +
>> +	prog_fd = bpf_program__fd(skel->progs.test_enable_stats);
>> +	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
>> +
>> +	if (CHECK(err, "get_prog_info",
>> +		  "failed to get bpf_prog_info for fd %d\n", prog_fd))
>> +		goto cleanup;
>> +
>> +	CHECK(info.run_time_ns == 0, "check_stats_enabled",
>> +	      "failed to enable run_time_ns stats\n");
>> +
>> +cleanup:
>> +	test_enable_stats__destroy(skel);
>> +	if (stats_fd >= 0)
>> +		close(stats_fd);
>> +}
>> diff --git a/tools/testing/selftests/bpf/progs/test_enable_stats.c b/tools/testing/selftests/bpf/progs/test_enable_stats.c
>> new file mode 100644
>> index 000000000000..f95ac0c94639
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/progs/test_enable_stats.c
>> @@ -0,0 +1,28 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +// Copyright (c) 2020 Facebook
>> +
>> +#include <linux/bpf.h>
>> +#include <stdint.h>
>> +#include <bpf/bpf_helpers.h>
>> +
>> +char _license[] SEC("license") = "GPL";
>> +
>> +struct {
>> +	__uint(type, BPF_MAP_TYPE_ARRAY);
>> +	__uint(max_entries, 1);
>> +	__type(key, __u32);
>> +	__type(value, __u64);
>> +} count SEC(".maps");
> 
> Looks like a global variable can be used here?

We can use global variable. But it doesn't really matter for this test.
Any BPF program will work here. Do we really need a v6 for this change? 

Thanks,
Song


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 bpf-next 3/3] bpf: add selftest for BPF_ENABLE_STATS
  2020-04-28 22:09     ` Song Liu
@ 2020-04-28 22:13       ` Alexei Starovoitov
  0 siblings, 0 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2020-04-28 22:13 UTC (permalink / raw)
  To: Song Liu; +Cc: Yonghong Song, bpf, netdev, ast, daniel, Kernel Team

On Tue, Apr 28, 2020 at 3:09 PM Song Liu <songliubraving@fb.com> wrote:
> >> +
> >> +struct {
> >> +    __uint(type, BPF_MAP_TYPE_ARRAY);
> >> +    __uint(max_entries, 1);
> >> +    __type(key, __u32);
> >> +    __type(value, __u64);
> >> +} count SEC(".maps");
> >
> > Looks like a global variable can be used here?
>
> We can use global variable. But it doesn't really matter for this test.
> Any BPF program will work here. Do we really need a v6 for this change?

yes. please.
Otherwise folks will copy paste this inefficiency into future tests.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-04-28 22:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-27 23:44 [PATCH v5 bpf-next 0/3] bpf: sharing bpf runtime stats with Song Liu
2020-04-27 23:44 ` [PATCH v5 bpf-next 1/3] bpf: sharing bpf runtime stats with BPF_ENABLE_STATS Song Liu
2020-04-27 23:44 ` [PATCH v5 bpf-next 2/3] libbpf: add support for command BPF_ENABLE_STATS Song Liu
2020-04-27 23:44 ` [PATCH v5 bpf-next 3/3] bpf: add selftest for BPF_ENABLE_STATS Song Liu
2020-04-28  6:04   ` Yonghong Song
2020-04-28 22:09     ` Song Liu
2020-04-28 22:13       ` Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).