From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alexander Aring Date: Thu, 2 Dec 2021 15:20:39 -0500 Subject: [Cluster-devel] [PATCH dlm-tool 6/6] python: add ebpf histogram example In-Reply-To: <20211202202039.1386193-1-aahringo@redhat.com> References: <20211202202039.1386193-1-aahringo@redhat.com> Message-ID: <20211202202039.1386193-7-aahringo@redhat.com> List-Id: To: cluster-devel.redhat.com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 +#include + +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