@@ -33,7 +33,7 @@ def my_map() -> HashMap:
3333
3434#### Parameters
3535
36- * ` key ` - The type of the key (must be a ctypes type)
36+ * ` key ` - The type of the key (must be a ctypes type or struct )
3737* ` value ` - The type of the value (must be a ctypes type or struct)
3838* ` max_entries ` - Maximum number of entries the map can hold
3939
@@ -47,11 +47,10 @@ Look up a value by key. Returns the value if found, `None` otherwise.
4747@bpf
4848@section (" tracepoint/syscalls/sys_enter_open" )
4949def trace_open (ctx : c_void_p) -> c_int64:
50- key = c_uint32(1 )
51- value = my_map.lookup(key)
50+ value = my_map.lookup(1 )
5251 if value:
5352 print (f " Found value: { value} " )
54- return c_int64( 0 )
53+ return 0
5554```
5655
5756##### update(key, value, flags=None)
@@ -67,8 +66,8 @@ def track_opens(ctx: c_void_p) -> c_int64:
6766 if count:
6867 my_map.update(key, count + 1 )
6968 else :
70- my_map.update(key, c_uint64( 1 ) )
71- return c_int64( 0 )
69+ my_map.update(key, 1 )
70+ return 0
7271```
7372
7473##### delete(key)
@@ -78,9 +77,8 @@ Remove an entry from the map.
7877``` python
7978@bpf
8079def cleanup (ctx : c_void_p) -> c_int64:
81- key = c_uint32(1 )
82- my_map.delete(key)
83- return c_int64(0 )
80+ my_map.delete(1 )
81+ return 0
8482```
8583
8684#### Use Cases
@@ -108,14 +106,14 @@ def process_count() -> HashMap:
108106def count_processes (ctx : c_void_p) -> c_int64:
109107 process_id = pid()
110108 count = process_count.lookup(process_id)
111-
109+
112110 if count:
113111 new_count = count + 1
114112 process_count.update(process_id, new_count)
115113 else :
116- process_count.update(process_id, c_uint64( 1 ) )
117-
118- return c_int64( 0 )
114+ process_count.update(process_id, 1 )
115+
116+ return 0
119117
120118@bpf
121119@bpfglobal
@@ -179,7 +177,7 @@ def send_event(ctx: c_void_p) -> c_int64:
179177 event.pid = pid()
180178 event.timestamp = ktime()
181179 events.output(event)
182- return c_int64( 0 )
180+ return 0
183181```
184182
185183#### Use Cases
@@ -215,10 +213,9 @@ def log_exec(ctx: c_void_p) -> c_int64:
215213 event = ProcessEvent()
216214 event.timestamp = ktime()
217215 event.pid = pid()
218- # Note: comm() requires a buffer parameter
219- # comm(event.comm) # Fills event.comm with process name
216+ comm(event.comm) # Fills event.comm with process name
220217 events.output(event)
221- return c_int64( 0 )
218+ return 0
222219
223220@bpf
224221@bpfglobal
@@ -258,7 +255,7 @@ def log_event(ctx: c_void_p) -> c_int64:
258255 event = Event()
259256 event.pid = pid()
260257 events.output(event)
261- return c_int64( 0 )
258+ return 0
262259```
263260
264261##### reserve(size)
@@ -272,7 +269,7 @@ def reserve_space(ctx: c_void_p) -> c_int64:
272269 if ptr:
273270 # Use the reserved space
274271 events.submit(ptr)
275- return c_int64( 0 )
272+ return 0
276273```
277274
278275##### submit(data, flags=0)
@@ -343,18 +340,18 @@ def process_stats() -> HashMap:
343340def track_stats (ctx : c_void_p) -> c_int64:
344341 process_id = pid()
345342 stats = process_stats.lookup(process_id)
346-
343+
347344 if stats:
348345 stats.count = stats.count + 1
349346 process_stats.update(process_id, stats)
350347 else :
351348 new_stats = Stats()
352- new_stats.count = c_uint64( 1 )
353- new_stats.total_time = c_uint64( 0 )
354- new_stats.max_time = c_uint64( 0 )
349+ new_stats.count = 1
350+ new_stats.total_time = 0
351+ new_stats.max_time = 0
355352 process_stats.update(process_id, new_stats)
356-
357- return c_int64( 0 )
353+
354+ return 0
358355```
359356
360357## Accessing Maps from Userspace
@@ -392,32 +389,6 @@ map_obj[key] = new_value
392389del map_obj[key]
393390```
394391
395- ## Best Practices
396-
397- 1 . ** Choose the right map type**
398- * Use ` HashMap ` for key-value storage
399- * Use ` RingBuffer ` for event streaming (kernel 5.8+)
400- * Use ` PerfEventArray ` for older kernels
401-
402- 2 . ** Size maps appropriately**
403- * Consider maximum expected entries
404- * Balance memory usage vs. capacity
405- * Use LRU maps for automatic eviction
406-
407- 3 . ** Handle lookup failures**
408- * Always check if ` lookup() ` returns ` None `
409- * Initialize new entries properly
410-
411- 4 . ** Minimize map operations**
412- * BPF has instruction limits
413- * Reduce unnecessary lookups
414- * Batch operations when possible
415-
416- 5 . ** Use structs for complex data**
417- * More efficient than multiple lookups
418- * Atomic updates of related fields
419- * Better cache locality
420-
421392## Common Patterns
422393
423394### Counter Pattern
@@ -427,7 +398,7 @@ count = my_map.lookup(key)
427398if count:
428399 my_map.update(key, count + 1 )
429400else :
430- my_map.update(key, c_uint64( 1 ) )
401+ my_map.update(key, 1 )
431402```
432403
433404### Latency Tracking
@@ -452,7 +423,7 @@ if start_time:
452423count = counter.lookup(key)
453424if count and (count % 100 ) == 0 :
454425 events.output(data)
455- counter.update(key, count + 1 if count else c_uint64( 1 ) )
426+ counter.update(key, count + 1 if count else 1 )
456427```
457428
458429## Troubleshooting
@@ -476,7 +447,6 @@ If updates fail due to map being full:
476447If you get type-related errors:
477448* Verify key and value types match the definition
478449* Check that structs are properly defined
479- * Ensure ctypes are used correctly
480450
481451## Next Steps
482452
0 commit comments