Skip to content

Commit 5c1e710

Browse files
committed
Add Python notebook examples for current BCC examples
1 parent 576fa2f commit 5c1e710

File tree

8 files changed

+862
-2
lines changed

8 files changed

+862
-2
lines changed

BCC-Examples/hello_fields.ipynb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "28cf2e27-41e2-461c-a39c-147417141a4e",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from pythonbpf import bpf, section, bpfglobal, BPF, trace_fields\n",
11+
"from ctypes import c_void_p, c_int64"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"id": "133190e5-5a99-4585-b6e1-91224ed973c2",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"@bpf\n",
22+
"@section(\"tracepoint/syscalls/sys_enter_clone\")\n",
23+
"def hello_world(ctx: c_void_p) -> c_int64:\n",
24+
" print(\"Hello, World!\")\n",
25+
" return 0\n",
26+
"\n",
27+
"\n",
28+
"@bpf\n",
29+
"@bpfglobal\n",
30+
"def LICENSE() -> str:\n",
31+
" return \"GPL\"\n",
32+
"\n",
33+
"\n",
34+
"# Compile and load\n",
35+
"b = BPF()\n",
36+
"b.load()\n",
37+
"b.attach_all()"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"id": "d3934efb-4043-4545-ae4c-c50ec40a24fd",
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"# header\n",
48+
"print(f\"{'TIME(s)':<18} {'COMM':<16} {'PID':<6} {'MESSAGE'}\")\n",
49+
"\n",
50+
"# format output\n",
51+
"while True:\n",
52+
" try:\n",
53+
" (task, pid, cpu, flags, ts, msg) = trace_fields()\n",
54+
" except ValueError:\n",
55+
" continue\n",
56+
" except KeyboardInterrupt:\n",
57+
" exit()\n",
58+
" print(f\"{ts:<18} {task:<16} {pid:<6} {msg}\")"
59+
]
60+
}
61+
],
62+
"metadata": {
63+
"kernelspec": {
64+
"display_name": "Python 3 (ipykernel)",
65+
"language": "python",
66+
"name": "python3"
67+
},
68+
"language_info": {
69+
"codemirror_mode": {
70+
"name": "ipython",
71+
"version": 3
72+
},
73+
"file_extension": ".py",
74+
"mimetype": "text/x-python",
75+
"name": "python",
76+
"nbconvert_exporter": "python",
77+
"pygments_lexer": "ipython3",
78+
"version": "3.13.3"
79+
}
80+
},
81+
"nbformat": 4,
82+
"nbformat_minor": 5
83+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "79b74928-f4b4-4320-96e3-d973997de2f4",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from pythonbpf import bpf, map, struct, section, bpfglobal, BPF\n",
11+
"from pythonbpf.helper import ktime, pid, comm\n",
12+
"from pythonbpf.maps import PerfEventArray\n",
13+
"from ctypes import c_void_p, c_int64"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "5bdb0329-ae2d-45e8-808e-5ed5b1374204",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"@bpf\n",
24+
"@struct\n",
25+
"class data_t:\n",
26+
" pid: c_int64\n",
27+
" ts: c_int64\n",
28+
" comm: str(16)\n",
29+
"\n",
30+
"\n",
31+
"@bpf\n",
32+
"@map\n",
33+
"def events() -> PerfEventArray:\n",
34+
" return PerfEventArray(key_size=c_int64, value_size=c_int64)\n",
35+
"\n",
36+
"\n",
37+
"@bpf\n",
38+
"@section(\"tracepoint/syscalls/sys_enter_clone\")\n",
39+
"def hello(ctx: c_void_p) -> c_int64:\n",
40+
" dataobj = data_t()\n",
41+
" dataobj.pid, dataobj.ts = pid(), ktime()\n",
42+
" comm(dataobj.comm)\n",
43+
" events.output(dataobj)\n",
44+
" return 0\n",
45+
"\n",
46+
"\n",
47+
"@bpf\n",
48+
"@bpfglobal\n",
49+
"def LICENSE() -> str:\n",
50+
" return \"GPL\"\n",
51+
"\n",
52+
"\n",
53+
"# Compile and load\n",
54+
"b = BPF()\n",
55+
"b.load()\n",
56+
"b.attach_all()"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"id": "4bcc7d57-6cc4-48a3-bbd2-42ad6263afdf",
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"start = 0\n",
67+
"\n",
68+
"\n",
69+
"def callback(cpu, event):\n",
70+
" global start\n",
71+
" if start == 0:\n",
72+
" start = event.ts\n",
73+
" ts = (event.ts - start) / 1e9\n",
74+
" print(f\"[CPU {cpu}] PID: {event.pid}, TS: {ts}, COMM: {event.comm.decode()}\")\n",
75+
"\n",
76+
"\n",
77+
"perf = b[\"events\"].open_perf_buffer(callback, struct_name=\"data_t\")\n",
78+
"print(\"Starting to poll... (Ctrl+C to stop)\")\n",
79+
"print(\"Try running: fork() or clone() system calls to trigger events\")\n",
80+
"\n",
81+
"try:\n",
82+
" while True:\n",
83+
" b[\"events\"].poll(1000)\n",
84+
"except KeyboardInterrupt:\n",
85+
" print(\"Stopping...\")"
86+
]
87+
}
88+
],
89+
"metadata": {
90+
"kernelspec": {
91+
"display_name": "Python 3 (ipykernel)",
92+
"language": "python",
93+
"name": "python3"
94+
},
95+
"language_info": {
96+
"codemirror_mode": {
97+
"name": "ipython",
98+
"version": 3
99+
},
100+
"file_extension": ".py",
101+
"mimetype": "text/x-python",
102+
"name": "python",
103+
"nbconvert_exporter": "python",
104+
"pygments_lexer": "ipython3",
105+
"version": "3.13.3"
106+
}
107+
},
108+
"nbformat": 4,
109+
"nbformat_minor": 5
110+
}

BCC-Examples/sync_count.ipynb

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)