{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "c3520e58-e50f-4bc1-8f9d-a6fecbf6e9f0", "metadata": {}, "outputs": [], "source": [ "from vmlinux import struct_request, struct_pt_regs\n", "from pythonbpf import bpf, section, bpfglobal, map, BPF\n", "from pythonbpf.helper import ktime\n", "from pythonbpf.maps import HashMap\n", "from ctypes import c_int64, c_uint64, c_int32\n", "\n", "REQ_WRITE = 1\n", "\n", "\n", "@bpf\n", "@map\n", "def start() -> HashMap:\n", " return HashMap(key=c_uint64, value=c_uint64, max_entries=10240)\n", "\n", "\n", "@bpf\n", "@section(\"kprobe/blk_mq_end_request\")\n", "def trace_completion(ctx: struct_pt_regs) -> c_int64:\n", " # Get request pointer from first argument\n", " req_ptr = ctx.di\n", " req = struct_request(ctx.di)\n", " # Print: data_len, cmd_flags, latency_us\n", " data_len = req.__data_len\n", " cmd_flags = req.cmd_flags\n", " # Lookup start timestamp\n", " req_tsp = start.lookup(req_ptr)\n", " if req_tsp:\n", " # Calculate delta in nanoseconds\n", " delta = ktime() - req_tsp\n", "\n", " # Convert to microseconds for printing\n", " delta_us = delta // 1000\n", "\n", " print(f\"{data_len} {cmd_flags:x} {delta_us}\\n\")\n", "\n", " # Delete the entry\n", " start.delete(req_ptr)\n", "\n", " return c_int64(0)\n", "\n", "\n", "@bpf\n", "@section(\"kprobe/blk_mq_start_request\")\n", "def trace_start(ctx1: struct_pt_regs) -> c_int32:\n", " req = ctx1.di\n", " ts = ktime()\n", " start.update(req, ts)\n", " return c_int32(0)\n", "\n", "\n", "@bpf\n", "@bpfglobal\n", "def LICENSE() -> str:\n", " return \"GPL\"\n", "\n", "\n", "b = BPF()" ] }, { "cell_type": "code", "execution_count": null, "id": "97040f73-98e0-4993-94c6-125d1b42d931", "metadata": {}, "outputs": [], "source": [ "b.load()\n", "b.attach_all()" ] }, { "cell_type": "code", "execution_count": null, "id": "b1bd4f51-fa25-42e1-877c-e48a2605189f", "metadata": {}, "outputs": [], "source": [ "from pythonbpf import trace_pipe" ] }, { "cell_type": "code", "execution_count": null, "id": "96b4b59b-b0db-4952-9534-7a714f685089", "metadata": {}, "outputs": [], "source": [ "trace_pipe()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }