From 360a7fd3a85360764f906feaade29a2d56c04ba9 Mon Sep 17 00:00:00 2001 From: Alexander Aring Date: Dec 02 2021 19:50:35 +0000 Subject: python: add ebpf histogram example 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. --- diff --git a/python/ebpf/README b/python/ebpf/README new file mode 100644 index 0000000..dec59ed --- /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 0000000..ed1eab4 --- /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")