bcc/python: Add test_attach_perf_event.py to CMake tests
authorDave Marchevsky <davemarchevsky@fb.com>
Sat, 14 Aug 2021 04:02:12 +0000 (21:02 -0700)
committeryonghong-song <ys114321@gmail.com>
Thu, 19 Aug 2021 17:24:22 +0000 (10:24 -0700)
Add to CMakeLists.txt of tests so that the test is run as part of github
actions test suite. Shorten the sleep duration so test finishes faster -
since it's just testing attach currently the extra time isn't producing
more signal.

Also add python equivalent of `perf_event_sample_format` enum so
`sample_type` can be more clearly set.

v2: The test doesn't work on ubuntu 16.04 due to old kernel headers. It
doesn't work on the rest of the github actions VMs due to hardware perf
events not being supported, so add necessary check / skip.

src/python/bcc/__init__.py
tests/python/CMakeLists.txt
tests/python/test_attach_perf_event.py

index 76678e6efdeab09f3d88d3efda132328ea1a2ad4..c8986388e0373d06f168cb47d9ac9e4a1adb6ab1 100644 (file)
@@ -141,6 +141,34 @@ class PerfSWConfig:
     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
index 3cd96031ceb1de7d763dc92882965b0742236519..d86fcab6b9d6283609eb5798715d056b1945d426 100644 (file)
@@ -63,6 +63,8 @@ add_test(NAME py_test_tracepoint WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
   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}
index c53f450e060a375423491e96e62a6f83113f3f51..d0cea065f712826166d6086a9513d0b4bfc003d7 100755 (executable)
@@ -6,11 +6,14 @@ import bcc
 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>
@@ -50,15 +53,15 @@ int on_sample_hit(struct bpf_perf_event_data *ctx) {
             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()