From: Alexander Aring <aahringo@redhat.com>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] [PATCH dlm-tool 6/6] python: add ebpf histogram example
Date: Thu, 2 Dec 2021 15:20:39 -0500 [thread overview]
Message-ID: <20211202202039.1386193-7-aahringo@redhat.com> (raw)
In-Reply-To: <20211202202039.1386193-1-aahringo@redhat.com>
This patch adds an example how a measurement between a kernel dlm_lock()
and their ast callback can be captured to get at the end a histogram.
This is hopefully a start of more useful dlm tracing collection with use
of ebpf.
---
python/ebpf/README | 6 ++++
python/ebpf/dlmhist.py | 78 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+)
create mode 100644 python/ebpf/README
create mode 100755 python/ebpf/dlmhist.py
diff --git a/python/ebpf/README b/python/ebpf/README
new file mode 100644
index 00000000..dec59ed0
--- /dev/null
+++ b/python/ebpf/README
@@ -0,0 +1,6 @@
+In this directory are some ebpf tracing examples. It requires bcc toolchain
+(usually known as bcc package name) and the python bcc module (usually known
+as python3-bcc package name).
+
+Also the current kernel headers need to be available or activate
+CONFIG_IKHEADERS in your kernel configuration.
diff --git a/python/ebpf/dlmhist.py b/python/ebpf/dlmhist.py
new file mode 100755
index 00000000..ed1eab4f
--- /dev/null
+++ b/python/ebpf/dlmhist.py
@@ -0,0 +1,78 @@
+#!/usr/bin/python
+#
+# This example shows how to capture latency between a dlm_lock() kernel
+# call for DLM_LOCK_EX requests with flag DLM_LKF_NOQUEUE and the ast
+# response.
+#
+# You will probably see two line peaks, one in case of that the current
+# node is the lock master and another one which requires network
+# communication. There is currently no way to filter them out, so the
+# second peak is interessting to get an idea what time it takes to
+# call dlm_lock() and get a response back.
+
+from bcc import BPF
+
+import threading
+
+b = BPF(text="""
+#include <uapi/linux/ptrace.h>
+#include <uapi/linux/dlm.h>
+
+BPF_HASH(start, u32);
+BPF_HISTOGRAM(dist, u64);
+
+#define DLM_HASH(args) (args->ls_id ^ args->lkb_id)
+
+TRACEPOINT_PROBE(dlm, dlm_lock_start)
+{
+ u64 ts = bpf_ktime_get_ns();
+ u32 hash = DLM_HASH(args);
+
+ if (args->flags & DLM_LKF_NOQUEUE &&
+ args->mode == DLM_LOCK_EX)
+ start.update(&hash, &ts);
+
+ return 0;
+}
+
+TRACEPOINT_PROBE(dlm, dlm_lock_end)
+{
+ u32 hash = DLM_HASH(args);
+
+ if (args->error != 0)
+ start.delete(&hash);
+
+ return 0;
+}
+
+TRACEPOINT_PROBE(dlm, dlm_ast)
+{
+ u32 hash = DLM_HASH(args);
+ u64 *tsp, delta;
+
+ tsp = start.lookup(&hash);
+ if (tsp != 0) {
+ start.delete(&hash);
+ delta = bpf_ktime_get_ns() - *tsp;
+
+ if (args->sb_status != 0)
+ return 0;
+
+ dist.increment(bpf_log2l(delta));
+ }
+
+ return 0;
+}
+""")
+
+print("Tracing... Hit Ctrl-C anytime to end.")
+
+forever = threading.Event()
+try:
+ forever.wait()
+except KeyboardInterrupt:
+ print()
+
+print("log2 histogram")
+print("--------------")
+b["dist"].print_log2_hist("ns")
--
2.27.0
prev parent reply other threads:[~2021-12-02 20:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-02 20:20 [Cluster-devel] [PATCH dlm-tool 0/6] dlm: debian patches and python Alexander Aring
2021-12-02 20:20 ` [Cluster-devel] [PATCH dlm-tool 1/6] init: Enable systemctl help dlm to show relevant man pages Alexander Aring
2021-12-02 20:20 ` [Cluster-devel] [PATCH dlm-tool 2/6] dlm_controld: man: Fix typo: specfic => specific Alexander Aring
2021-12-02 20:20 ` [Cluster-devel] [PATCH dlm-tool 3/6] stonith_helper: Fix gcc build warnings Alexander Aring
2021-12-02 20:20 ` [Cluster-devel] [PATCH dlm-tool 4/6] stonith_helper: Don't link dlm_stonith against libxml2 Alexander Aring
2021-12-02 20:20 ` [Cluster-devel] [PATCH dlm-tool 5/6] python: add bindings and test example Alexander Aring
2021-12-02 20:20 ` Alexander Aring [this message]
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=20211202202039.1386193-7-aahringo@redhat.com \
--to=aahringo@redhat.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;
as well as URLs for NNTP newsgroup(s).