BPF List
 help / color / mirror / Atom feed
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 2/3] sample/bpf: Add log2 histogram function support
Date: Sun, 20 Sep 2020 22:45:46 +0800	[thread overview]
Message-ID: <20200920144547.56771-3-xhao@linux.alibaba.com> (raw)
In-Reply-To: <20200920144547.56771-1-xhao@linux.alibaba.com>

The relative functions is copy from bcc tools
source code: libbpf-tools/trace_helpers.c.
URL: https://github.com/iovisor/bcc.git

Log2 histogram can display the change of the collected
data more conveniently.

Signed-off-by: Xin Hao <xhao@linux.alibaba.com>
---
 samples/bpf/common.h | 67 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 samples/bpf/common.h

diff --git a/samples/bpf/common.h b/samples/bpf/common.h
new file mode 100644
index 000000000000..ec60fb665544
--- /dev/null
+++ b/samples/bpf/common.h
@@ -0,0 +1,67 @@
+/* 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.
+ */
+
+#define min(x, y) ({				 \
+	typeof(x) _min1 = (x);			 \
+	typeof(y) _min2 = (y);			 \
+	(void) (&_min1 == &_min2);		 \
+	_min1 < _min2 ? _min1 : _min2; })
+
+static void print_stars(unsigned int val, unsigned int val_max, int width)
+{
+	int num_stars, num_spaces, i;
+	bool need_plus;
+
+	num_stars = min(val, val_max) * width / val_max;
+	num_spaces = width - num_stars;
+	need_plus = val > val_max;
+
+	for (i = 0; i < num_stars; i++)
+		printf("*");
+	for (i = 0; i < num_spaces; i++)
+		printf(" ");
+	if (need_plus)
+		printf("+");
+}
+
+static void print_log2_hist(unsigned int *vals, int vals_size, char *val_type)
+{
+	int stars_max = 40, idx_max = -1;
+	unsigned int val, val_max = 0;
+	unsigned long long low, high;
+	int stars, width, i;
+
+	for (i = 0; i < vals_size; i++) {
+		val = vals[i];
+		if (val > 0)
+			idx_max = i;
+		if (val > val_max)
+			val_max = val;
+	}
+
+	if (idx_max < 0)
+		return;
+
+	printf("%*s%-*s : count    distribution\n", idx_max <= 32 ? 5 : 15, "",
+		idx_max <= 32 ? 19 : 29, val_type);
+	if (idx_max <= 32)
+		stars = stars_max;
+	else
+		stars = stars_max / 2;
+
+	for (i = 0; i <= idx_max; i++) {
+		low = (1ULL << (i + 1)) >> 1;
+		high = (1ULL << (i + 1)) - 1;
+		if (low == high)
+			low -= 1;
+		val = vals[i];
+		width = idx_max <= 32 ? 10 : 20;
+		printf("%*lld -> %-*lld : %-8d |", width, low, width, high, val);
+		print_stars(val, val_max, stars);
+		printf("|\n");
+	}
+}
-- 
2.28.0


  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 ` [bpf-next 1/3] sample/bpf: Avoid repetitive definition of log2 func Xin Hao
2020-09-21 16:56   ` John Fastabend
2020-09-20 14:45 ` Xin Hao [this message]
2020-09-21 17:16   ` [bpf-next 2/3] sample/bpf: Add log2 histogram function support 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-3-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