{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "dcab010c-f5e9-446f-9f9f-056cc794ad14", "metadata": {}, "outputs": [], "source": [ "from pythonbpf import bpf, map, section, bpfglobal, BPF, trace_fields\n", "from pythonbpf.helper import ktime\n", "from pythonbpf.maps import HashMap\n", "\n", "from ctypes import c_void_p, c_int64" ] }, { "cell_type": "code", "execution_count": null, "id": "720797e8-9c81-4af6-a385-80f1ec4c0f15", "metadata": {}, "outputs": [], "source": [ "@bpf\n", "@map\n", "def last() -> HashMap:\n", " return HashMap(key=c_int64, value=c_int64, max_entries=2)\n", "\n", "\n", "@bpf\n", "@section(\"tracepoint/syscalls/sys_enter_sync\")\n", "def do_trace(ctx: c_void_p) -> c_int64:\n", " ts_key, cnt_key = 0, 1\n", " tsp, cntp = last.lookup(ts_key), last.lookup(cnt_key)\n", " if not cntp:\n", " last.update(cnt_key, 0)\n", " cntp = last.lookup(cnt_key)\n", " if tsp:\n", " delta = ktime() - tsp\n", " if delta < 1000000000:\n", " time_ms = delta // 1000000\n", " print(f\"{time_ms} {cntp}\")\n", " last.delete(ts_key)\n", " else:\n", " last.update(ts_key, ktime())\n", " last.update(cnt_key, cntp + 1)\n", " return 0\n", "\n", "\n", "@bpf\n", "@bpfglobal\n", "def LICENSE() -> str:\n", " return \"GPL\"\n", "\n", "\n", "# Compile and load\n", "b = BPF()\n", "b.load()\n", "b.attach_all()" ] }, { "cell_type": "code", "execution_count": null, "id": "78a8b82c-7c5f-43c1-9de1-cd982a0f345b", "metadata": {}, "outputs": [], "source": [ "print(\"Tracing for quick sync's... Ctrl-C to end\")\n", "\n", "# format output\n", "start = 0\n", "while True:\n", " try:\n", " task, pid, cpu, flags, ts, msg = trace_fields()\n", " if start == 0:\n", " start = ts\n", " ts -= start\n", " ms, cnt = msg.split()\n", " print(f\"At time {ts} s: Multiple syncs detected, last {ms} ms ago. Count {cnt}\")\n", " except KeyboardInterrupt:\n", " exit()" ] } ], "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.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }