DUMMY = 9
BPF_OUTPUT = 10
+class PerfEventSampleFormat:
+ # from perf_event_sample_format in uapi/linux/bpf.h
+ IP = (1 << 0)
+ TID = (1 << 1)
+ TIME = (1 << 2)
+ ADDR = (1 << 3)
+ READ = (1 << 4)
+ CALLCHAIN = (1 << 5)
+ ID = (1 << 6)
+ CPU = (1 << 7)
+ PERIOD = (1 << 8)
+ STREAM_ID = (1 << 9)
+ RAW = (1 << 10)
+ BRANCH_STACK = (1 << 11)
+ REGS_USER = (1 << 12)
+ STACK_USER = (1 << 13)
+ WEIGHT = (1 << 14)
+ DATA_SRC = (1 << 15)
+ IDENTIFIER = (1 << 16)
+ TRANSACTION = (1 << 17)
+ REGS_INTR = (1 << 18)
+ PHYS_ADDR = (1 << 19)
+ AUX = (1 << 20)
+ CGROUP = (1 << 21)
+ DATA_PAGE_SIZE = (1 << 22)
+ CODE_PAGE_SIZE = (1 << 23)
+ WEIGHT_STRUCT = (1 << 24)
+
class BPFProgType:
# From bpf_prog_type in uapi/linux/bpf.h
SOCKET_FILTER = 1
COMMAND ${TEST_WRAPPER} py_test_tracepoint sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_tracepoint.py)
add_test(NAME py_test_perf_event WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${TEST_WRAPPER} py_test_perf_event sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_perf_event.py)
+add_test(NAME py_test_attach_perf_event WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+ COMMAND ${TEST_WRAPPER} py_test_attach_perf_event sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_attach_perf_event.py)
add_test(NAME py_test_utils WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${TEST_WRAPPER} py_test_utils sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_utils.py)
add_test(NAME py_test_percpu WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
import os
import time
import unittest
-from bcc import BPF, PerfType, PerfHWConfig
+from bcc import BPF, PerfType, PerfHWConfig, PerfEventSampleFormat
from bcc import Perf
from time import sleep
+from utils import kernel_version_ge, mayFail
class TestPerfAttachRaw(unittest.TestCase):
+ @mayFail("This fails on github actions environment, hw perf events are not supported")
+ @unittest.skipUnless(kernel_version_ge(4,9), "requires kernel >= 4.9")
def test_attach_raw_event(self):
bpf_text="""
#include <linux/perf_event.h>
event_attr.type = Perf.PERF_TYPE_HARDWARE
event_attr.config = PerfHWConfig.CACHE_MISSES
event_attr.sample_period = 1000000
- event_attr.sample_type = 0x8 # PERF_SAMPLE_ADDR
+ event_attr.sample_type = PerfEventSampleFormat.ADDR
event_attr.exclude_kernel = 1
b.attach_perf_event_raw(attr=event_attr, fn_name="on_sample_hit", pid=-1, cpu=-1)
except Exception:
print("Failed to attach to a raw event. Please check the event attr used")
exit()
- print("Running for 4 seconds or hit Ctrl-C to end. Check trace file for samples information written by bpf_trace_printk.")
- sleep(4)
+ print("Running for 2 seconds or hit Ctrl-C to end. Check trace file for samples information written by bpf_trace_printk.")
+ sleep(2)
if __name__ == "__main__":
unittest.main()