|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "dcab010c-f5e9-446f-9f9f-056cc794ad14", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "from pythonbpf import bpf, map, section, bpfglobal, BPF, trace_fields\n", |
| 11 | + "from pythonbpf.helper import ktime\n", |
| 12 | + "from pythonbpf.maps import HashMap\n", |
| 13 | + "\n", |
| 14 | + "from ctypes import c_void_p, c_int64" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": null, |
| 20 | + "id": "720797e8-9c81-4af6-a385-80f1ec4c0f15", |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "@bpf\n", |
| 25 | + "@map\n", |
| 26 | + "def last() -> HashMap:\n", |
| 27 | + " return HashMap(key=c_int64, value=c_int64, max_entries=2)\n", |
| 28 | + "\n", |
| 29 | + "\n", |
| 30 | + "@bpf\n", |
| 31 | + "@section(\"tracepoint/syscalls/sys_enter_sync\")\n", |
| 32 | + "def do_trace(ctx: c_void_p) -> c_int64:\n", |
| 33 | + " ts_key, cnt_key = 0, 1\n", |
| 34 | + " tsp, cntp = last.lookup(ts_key), last.lookup(cnt_key)\n", |
| 35 | + " if not cntp:\n", |
| 36 | + " last.update(cnt_key, 0)\n", |
| 37 | + " cntp = last.lookup(cnt_key)\n", |
| 38 | + " if tsp:\n", |
| 39 | + " delta = ktime() - tsp\n", |
| 40 | + " if delta < 1000000000:\n", |
| 41 | + " time_ms = delta // 1000000\n", |
| 42 | + " print(f\"{time_ms} {cntp}\")\n", |
| 43 | + " last.delete(ts_key)\n", |
| 44 | + " else:\n", |
| 45 | + " last.update(ts_key, ktime())\n", |
| 46 | + " last.update(cnt_key, cntp + 1)\n", |
| 47 | + " return 0\n", |
| 48 | + "\n", |
| 49 | + "\n", |
| 50 | + "@bpf\n", |
| 51 | + "@bpfglobal\n", |
| 52 | + "def LICENSE() -> str:\n", |
| 53 | + " return \"GPL\"\n", |
| 54 | + "\n", |
| 55 | + "\n", |
| 56 | + "# Compile and load\n", |
| 57 | + "b = BPF()\n", |
| 58 | + "b.load()\n", |
| 59 | + "b.attach_all()" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": null, |
| 65 | + "id": "78a8b82c-7c5f-43c1-9de1-cd982a0f345b", |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "print(\"Tracing for quick sync's... Ctrl-C to end\")\n", |
| 70 | + "\n", |
| 71 | + "# format output\n", |
| 72 | + "start = 0\n", |
| 73 | + "while True:\n", |
| 74 | + " try:\n", |
| 75 | + " task, pid, cpu, flags, ts, msg = trace_fields()\n", |
| 76 | + " if start == 0:\n", |
| 77 | + " start = ts\n", |
| 78 | + " ts -= start\n", |
| 79 | + " ms, cnt = msg.split()\n", |
| 80 | + " print(f\"At time {ts} s: Multiple syncs detected, last {ms} ms ago. Count {cnt}\")\n", |
| 81 | + " except KeyboardInterrupt:\n", |
| 82 | + " exit()" |
| 83 | + ] |
| 84 | + } |
| 85 | + ], |
| 86 | + "metadata": { |
| 87 | + "kernelspec": { |
| 88 | + "display_name": "Python 3 (ipykernel)", |
| 89 | + "language": "python", |
| 90 | + "name": "python3" |
| 91 | + }, |
| 92 | + "language_info": { |
| 93 | + "codemirror_mode": { |
| 94 | + "name": "ipython", |
| 95 | + "version": 3 |
| 96 | + }, |
| 97 | + "file_extension": ".py", |
| 98 | + "mimetype": "text/x-python", |
| 99 | + "name": "python", |
| 100 | + "nbconvert_exporter": "python", |
| 101 | + "pygments_lexer": "ipython3", |
| 102 | + "version": "3.13.3" |
| 103 | + } |
| 104 | + }, |
| 105 | + "nbformat": 4, |
| 106 | + "nbformat_minor": 5 |
| 107 | +} |
0 commit comments