From 9871201443482837bf40474a16bbf9f22cd0b193 Mon Sep 17 00:00:00 2001 From: Dave Marchevsky Date: Fri, 13 Aug 2021 21:02:12 -0700 Subject: [PATCH] bcc/python: Add test_attach_perf_event.py to CMake tests 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 | 28 ++++++++++++++++++++++++++ tests/python/CMakeLists.txt | 2 ++ tests/python/test_attach_perf_event.py | 11 ++++++---- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/python/bcc/__init__.py b/src/python/bcc/__init__.py index 76678e6e..c8986388 100644 --- a/src/python/bcc/__init__.py +++ b/src/python/bcc/__init__.py @@ -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 diff --git a/tests/python/CMakeLists.txt b/tests/python/CMakeLists.txt index 3cd96031..d86fcab6 100644 --- a/tests/python/CMakeLists.txt +++ b/tests/python/CMakeLists.txt @@ -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} diff --git a/tests/python/test_attach_perf_event.py b/tests/python/test_attach_perf_event.py index c53f450e..d0cea065 100755 --- a/tests/python/test_attach_perf_event.py +++ b/tests/python/test_attach_perf_event.py @@ -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 @@ -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() -- 2.34.1