From b8c44a15543a62b9e1172cd195e43bc3ee7754b6 Mon Sep 17 00:00:00 2001 From: Roberto Scolaro Date: Sat, 21 Sep 2024 13:16:26 +0000 Subject: [PATCH] new inspector for each test Signed-off-by: Roberto Scolaro --- test/libsinsp_e2e/event_capture.cpp | 60 ++++++++++++++--------------- test/libsinsp_e2e/event_capture.h | 6 +-- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/test/libsinsp_e2e/event_capture.cpp b/test/libsinsp_e2e/event_capture.cpp index fac31f85e1..134eac59a9 100644 --- a/test/libsinsp_e2e/event_capture.cpp +++ b/test/libsinsp_e2e/event_capture.cpp @@ -31,24 +31,26 @@ unsigned long event_capture::m_buffer_dim = DEFAULT_DRIVER_BUFFER_BYTES_DIM; bool event_capture::inspector_ok = false; concurrent_object_handle event_capture::get_inspector_handle() { - return {get_inspector(), m_inspector_mutex}; + return {m_inspector.get(), m_inspector_mutex}; } void event_capture::init_inspector() { - get_inspector()->m_thread_manager->set_max_thread_table_size(m_max_thread_table_size); - get_inspector()->m_thread_timeout_ns = m_thread_timeout_ns; - get_inspector()->set_auto_threads_purging_interval_s(m_inactive_thread_scan_time_ns); - get_inspector()->set_auto_threads_purging(false); - get_inspector()->set_get_procs_cpu_from_driver(true); + m_inspector = std::unique_ptr(new sinsp); + m_inspector->m_thread_manager->set_max_thread_table_size(m_max_thread_table_size); + m_inspector->m_thread_timeout_ns = m_thread_timeout_ns; + m_inspector->set_auto_threads_purging_interval_s(m_inactive_thread_scan_time_ns); + m_inspector->set_auto_threads_purging(false); - ASSERT_FALSE(get_inspector()->is_capture()); - ASSERT_FALSE(get_inspector()->is_live()); - ASSERT_FALSE(get_inspector()->is_nodriver()); + m_inspector->set_get_procs_cpu_from_driver(true); + + ASSERT_FALSE(m_inspector->is_capture()); + ASSERT_FALSE(m_inspector->is_live()); + ASSERT_FALSE(m_inspector->is_nodriver()); try { if(m_mode == SINSP_MODE_NODRIVER) { - get_inspector()->open_nodriver(); + m_inspector->open_nodriver(); } else { open_engine(event_capture::get_engine(), {}); } @@ -56,7 +58,7 @@ void event_capture::init_inspector() { m_start_failed = true; m_start_failure_message = "couldn't open inspector (maybe driver hasn't been loaded yet?) err=" + - get_inspector()->getlasterr() + " exception=" + e.what(); + m_inspector->getlasterr() + " exception=" + e.what(); { m_capture_started = true; m_condition_started.notify_one(); @@ -64,8 +66,8 @@ void event_capture::init_inspector() { return; } - get_inspector()->set_debug_mode(true); - get_inspector()->set_hostname_and_port_resolution_mode(false); + m_inspector->set_debug_mode(true); + m_inspector->set_hostname_and_port_resolution_mode(false); } void event_capture::capture() { @@ -75,25 +77,21 @@ void event_capture::capture() { { std::scoped_lock init_lock(m_inspector_mutex, m_object_state_mutex); - if(!inspector_ok) { - init_inspector(); - if(!m_start_failed) { - inspector_ok = true; - } else { - std::cerr << m_start_failure_message << std::endl; - return; - } + init_inspector(); + if(m_start_failed) { + std::cerr << m_start_failure_message << std::endl; + return; } - m_param.m_inspector = get_inspector(); + m_param.m_inspector = m_inspector.get(); - m_before_open(get_inspector()); + m_before_open(m_inspector.get()); - get_inspector()->start_capture(); + m_inspector->start_capture(); if(m_mode != SINSP_MODE_NODRIVER) { m_dump_filename = std::string(LIBSINSP_TEST_CAPTURES_PATH) + test_info->test_case_name() + "_" + test_info->name() + ".scap"; - dumper = std::make_unique(get_inspector(), + dumper = std::make_unique(m_inspector.get(), m_dump_filename.c_str(), 0, 0, @@ -110,7 +108,7 @@ void event_capture::capture() { while(!::testing::Test::HasFatalFailure()) { { std::scoped_lock inspector_next_lock(m_inspector_mutex); - next_result = get_inspector()->next(&event); + next_result = m_inspector->next(&event); } if(next_result == SCAP_SUCCESS) { if(!m_capture_started) { @@ -135,9 +133,9 @@ void event_capture::capture() { { // Begin teardown synchronized section std::scoped_lock teardown_lock(m_inspector_mutex, m_object_state_mutex); - m_before_close(get_inspector()); + m_before_close(m_inspector.get()); - get_inspector()->stop_capture(); + m_inspector->stop_capture(); if(m_mode != SINSP_MODE_NODRIVER) { dumper->close(); } @@ -210,7 +208,7 @@ void event_capture::open_engine(const std::string& engine_string, } #ifdef HAS_ENGINE_KMOD else if(!engine_string.compare(KMOD_ENGINE)) { - get_inspector()->open_kmod(m_buffer_dim); + m_inspector->open_kmod(m_buffer_dim); } #endif #ifdef HAS_ENGINE_BPF @@ -220,12 +218,12 @@ void event_capture::open_engine(const std::string& engine_string, << std::endl; exit(EXIT_FAILURE); } - get_inspector()->open_bpf(event_capture::get_engine_path().c_str(), m_buffer_dim); + m_inspector->open_bpf(event_capture::get_engine_path().c_str(), m_buffer_dim); } #endif #ifdef HAS_ENGINE_MODERN_BPF else if(!engine_string.compare(MODERN_BPF_ENGINE)) { - get_inspector()->open_modern_bpf(m_buffer_dim); + m_inspector->open_modern_bpf(m_buffer_dim); } #endif else { diff --git a/test/libsinsp_e2e/event_capture.h b/test/libsinsp_e2e/event_capture.h index 30151cbc41..472e393830 100644 --- a/test/libsinsp_e2e/event_capture.h +++ b/test/libsinsp_e2e/event_capture.h @@ -119,11 +119,6 @@ class event_capture { static bool always_continue() { return true; } - sinsp* get_inspector() { - static sinsp inspector = sinsp(); - return &inspector; - } - static void run(run_callback_t run_function, captured_event_callback_t captured_event_callback, event_filter_t filter, @@ -210,6 +205,7 @@ class event_capture { std::string m_start_failure_message; std::string m_dump_filename; callback_param m_param; + std::unique_ptr m_inspector; static bool inspector_ok; sinsp_mode_t m_mode; uint64_t m_max_timeouts;