From: Xin Hao <xhao@linux.alibaba.com>
To: ast@kernel.org
Cc: daniel@iogearbox.net, kafai@fb.com, andriin@fb.com,
xhao@linux.alibaba.com, bpf@vger.kernel.org
Subject: [bpf-next 1/3] sample/bpf: Avoid repetitive definition of log2 func
Date: Sun, 20 Sep 2020 22:45:45 +0800 [thread overview]
Message-ID: <20200920144547.56771-2-xhao@linux.alibaba.com> (raw)
In-Reply-To: <20200920144547.56771-1-xhao@linux.alibaba.com>
log2 func is defined and used in following three files:
samples/bpf/lathist_kern.c
samples/bpf/lwt_len_hist_kern.c
samples/bpf/tracex2_kern.c
There's no need to repeat define them many times, so i added
a "common.h" file which maintains common codes, you just need
to include it in your file and future we can put more common codes
into this file.
Signed-off-by: Xin Hao <xhao@linux.alibaba.com>
---
samples/bpf/common_kern.h | 30 ++++++++++++++++++++++++++++++
samples/bpf/lathist_kern.c | 25 +------------------------
samples/bpf/lwt_len_hist_kern.c | 23 +----------------------
samples/bpf/tracex2_kern.c | 23 +----------------------
4 files changed, 33 insertions(+), 68 deletions(-)
create mode 100644 samples/bpf/common_kern.h
diff --git a/samples/bpf/common_kern.h b/samples/bpf/common_kern.h
new file mode 100644
index 000000000000..c23b44238d02
--- /dev/null
+++ b/samples/bpf/common_kern.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ */
+
+static unsigned int log2(unsigned int v)
+{
+ unsigned int r;
+ unsigned int shift;
+
+ r = (v > 0xFFFF) << 4; v >>= r;
+ shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
+ shift = (v > 0xF) << 2; v >>= shift; r |= shift;
+ shift = (v > 0x3) << 1; v >>= shift; r |= shift;
+ r |= (v >> 1);
+
+ return r;
+}
+
+static unsigned int log2l(unsigned long v)
+{
+ unsigned int hi = v >> 32;
+
+ if (hi)
+ return log2(hi) + 32;
+ else
+ return log2(v);
+}
diff --git a/samples/bpf/lathist_kern.c b/samples/bpf/lathist_kern.c
index ca9c2e4e69aa..0978e24dd01c 100644
--- a/samples/bpf/lathist_kern.c
+++ b/samples/bpf/lathist_kern.c
@@ -9,6 +9,7 @@
#include <linux/ptrace.h>
#include <uapi/linux/bpf.h>
#include <bpf/bpf_helpers.h>
+#include "common_kern.h"
#define MAX_ENTRIES 20
#define MAX_CPU 4
@@ -37,30 +38,6 @@ int bpf_prog1(struct pt_regs *ctx)
return 0;
}
-static unsigned int log2(unsigned int v)
-{
- unsigned int r;
- unsigned int shift;
-
- r = (v > 0xFFFF) << 4; v >>= r;
- shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
- shift = (v > 0xF) << 2; v >>= shift; r |= shift;
- shift = (v > 0x3) << 1; v >>= shift; r |= shift;
- r |= (v >> 1);
-
- return r;
-}
-
-static unsigned int log2l(unsigned long v)
-{
- unsigned int hi = v >> 32;
-
- if (hi)
- return log2(hi) + 32;
- else
- return log2(v);
-}
-
struct bpf_map_def SEC("maps") my_lat = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
diff --git a/samples/bpf/lwt_len_hist_kern.c b/samples/bpf/lwt_len_hist_kern.c
index 9ed63e10e170..6e61072d602b 100644
--- a/samples/bpf/lwt_len_hist_kern.c
+++ b/samples/bpf/lwt_len_hist_kern.c
@@ -15,6 +15,7 @@
#include <uapi/linux/ip.h>
#include <uapi/linux/in.h>
#include <bpf/bpf_helpers.h>
+#include "common_kern.h"
# define printk(fmt, ...) \
({ \
@@ -41,28 +42,6 @@ struct bpf_elf_map SEC("maps") lwt_len_hist_map = {
.max_elem = 1024,
};
-static unsigned int log2(unsigned int v)
-{
- unsigned int r;
- unsigned int shift;
-
- r = (v > 0xFFFF) << 4; v >>= r;
- shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
- shift = (v > 0xF) << 2; v >>= shift; r |= shift;
- shift = (v > 0x3) << 1; v >>= shift; r |= shift;
- r |= (v >> 1);
- return r;
-}
-
-static unsigned int log2l(unsigned long v)
-{
- unsigned int hi = v >> 32;
- if (hi)
- return log2(hi) + 32;
- else
- return log2(v);
-}
-
SEC("len_hist")
int do_len_hist(struct __sk_buff *skb)
{
diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c
index 5bc696bac27d..7a8cda216d54 100644
--- a/samples/bpf/tracex2_kern.c
+++ b/samples/bpf/tracex2_kern.c
@@ -11,6 +11,7 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "trace_common.h"
+#include "common_kern.h"
struct {
__uint(type, BPF_MAP_TYPE_HASH);
@@ -42,28 +43,6 @@ int bpf_prog2(struct pt_regs *ctx)
return 0;
}
-static unsigned int log2(unsigned int v)
-{
- unsigned int r;
- unsigned int shift;
-
- r = (v > 0xFFFF) << 4; v >>= r;
- shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
- shift = (v > 0xF) << 2; v >>= shift; r |= shift;
- shift = (v > 0x3) << 1; v >>= shift; r |= shift;
- r |= (v >> 1);
- return r;
-}
-
-static unsigned int log2l(unsigned long v)
-{
- unsigned int hi = v >> 32;
- if (hi)
- return log2(hi) + 32;
- else
- return log2(v);
-}
-
struct hist_key {
char comm[16];
u64 pid_tgid;
--
2.28.0
next prev parent reply other threads:[~2020-09-20 14:46 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-20 14:45 [bpf-next 0/3] Using log2 histogram to display the statistics of every softirq execution Xin Hao
2020-09-20 14:45 ` Xin Hao [this message]
2020-09-21 16:56 ` [bpf-next 1/3] sample/bpf: Avoid repetitive definition of log2 func John Fastabend
2020-09-20 14:45 ` [bpf-next 2/3] sample/bpf: Add log2 histogram function support Xin Hao
2020-09-21 17:16 ` John Fastabend
2020-09-21 21:40 ` Andrii Nakryiko
2020-09-22 2:06 ` Xin Hao
2020-09-23 5:32 ` Wenbo Zhang
2020-09-20 14:45 ` [bpf-next 3/3] samples/bpf: Add soft irq execution time statistics Xin Hao
2020-09-21 17:33 ` John Fastabend
2020-09-21 21:33 ` Andrii Nakryiko
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=20200920144547.56771-2-xhao@linux.alibaba.com \
--to=xhao@linux.alibaba.com \
--cc=andriin@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kafai@fb.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