From 254e1bd3a38f1317496de4fe8af794f50ec51183 Mon Sep 17 00:00:00 2001 From: Gianmatteo Palmieri Date: Fri, 19 Jul 2024 15:56:36 +0200 Subject: [PATCH] fix(libsinsp): disable thread pool on webassembly Signed-off-by: Gianmatteo Palmieri --- userspace/libsinsp/plugin.cpp | 10 ++++++++++ userspace/libsinsp/sinsp.cpp | 7 ++++++- userspace/libsinsp/test/plugins.ut.cpp | 6 ++++++ userspace/libsinsp/test/thread_pool.ut.cpp | 4 +++- userspace/plugin/plugin_api.h | 2 +- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/userspace/libsinsp/plugin.cpp b/userspace/libsinsp/plugin.cpp index 4b91a4a2ec..ec98143c62 100755 --- a/userspace/libsinsp/plugin.cpp +++ b/userspace/libsinsp/plugin.cpp @@ -824,6 +824,11 @@ std::vector sinsp_plugin::get_metrics() const thread_pool::routine_id_t sinsp_plugin::subscribe_routine(ss_plugin_routine_fn_t routine_fn, ss_plugin_routine_state_t* routine_state) { + if(!m_thread_pool) + { + return static_cast(nullptr); + } + auto f = [this, routine_fn, routine_state]() -> bool { return static_cast(routine_fn(m_state, routine_state)); }; @@ -833,6 +838,11 @@ thread_pool::routine_id_t sinsp_plugin::subscribe_routine(ss_plugin_routine_fn_t void sinsp_plugin::unsubscribe_routine(thread_pool::routine_id_t routine_id) { + if(!m_thread_pool || !routine_id) + { + return; + } + m_thread_pool->unsubscribe(routine_id); } diff --git a/userspace/libsinsp/sinsp.cpp b/userspace/libsinsp/sinsp.cpp index 3eb745941d..cd0b813662 100644 --- a/userspace/libsinsp/sinsp.cpp +++ b/userspace/libsinsp/sinsp.cpp @@ -132,7 +132,9 @@ sinsp::sinsp(bool static_container, const std::string &static_id, const std::str m_table_registry = std::make_shared(); m_table_registry->add_table(m_thread_manager.get()); +#if !defined(__EMSCRIPTEN__) m_thread_pool = std::make_shared(); +#endif } sinsp::~sinsp() @@ -798,7 +800,10 @@ void sinsp::close() } // purge pending routines and wait for the running ones - m_thread_pool->purge(); + if(m_thread_pool) + { + m_thread_pool->purge(); + } m_mode = SINSP_MODE_NONE; } diff --git a/userspace/libsinsp/test/plugins.ut.cpp b/userspace/libsinsp/test/plugins.ut.cpp index d3a413c30f..de24a3bc64 100644 --- a/userspace/libsinsp/test/plugins.ut.cpp +++ b/userspace/libsinsp/test/plugins.ut.cpp @@ -921,11 +921,15 @@ TEST_F(sinsp_with_test_input, plugin_metrics) #endif +#if !defined(__EMSCRIPTEN__) + TEST_F(sinsp_with_test_input, plugin_routines) { auto p = register_plugin(&m_inspector, get_plugin_api_sample_routines); open_inspector(); + ASSERT_NE(m_inspector.m_thread_pool, nullptr); + // step #0: the plugins subscribes a routine on capture open auto routines_num = m_inspector.m_thread_pool->routines_num(); ASSERT_EQ(routines_num, 1); @@ -957,3 +961,5 @@ TEST_F(sinsp_with_test_input, plugin_routines) routines_num = m_inspector.m_thread_pool->routines_num(); ASSERT_EQ(routines_num, 0); } + +#endif diff --git a/userspace/libsinsp/test/thread_pool.ut.cpp b/userspace/libsinsp/test/thread_pool.ut.cpp index ec8265c487..b4124110fc 100644 --- a/userspace/libsinsp/test/thread_pool.ut.cpp +++ b/userspace/libsinsp/test/thread_pool.ut.cpp @@ -19,6 +19,7 @@ limitations under the License. #include #include +#if !defined(__EMSCRIPTEN__) TEST_F(sinsp_with_test_input, thread_pool) { open_inspector(); @@ -69,4 +70,5 @@ TEST_F(sinsp_with_test_input, thread_pool) ASSERT_EQ(tp->routines_num(), 1); m_inspector.close(); ASSERT_EQ(tp->routines_num(), 0); -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/userspace/plugin/plugin_api.h b/userspace/plugin/plugin_api.h index b0b6a476fb..14a373e78b 100644 --- a/userspace/plugin/plugin_api.h +++ b/userspace/plugin/plugin_api.h @@ -416,7 +416,7 @@ typedef struct // - f: the function executed by the routine on each iteration // - i: the routine's state // - // Return value: A routine handle that can be used to later unsubscribe the routine. + // Return value: A routine handle that can be used to later unsubscribe the routine. Returns null in case of failure. ss_plugin_routine_t* (*subscribe)(ss_plugin_owner_t* o, ss_plugin_routine_fn_t f, ss_plugin_routine_state_t* i); //