Skip to content

Commit

Permalink
fix(libsinsp): disable thread pool on webassembly
Browse files Browse the repository at this point in the history
Signed-off-by: Gianmatteo Palmieri <mail@gian.im>
  • Loading branch information
mrgian committed Aug 29, 2024
1 parent 13ad718 commit f33a73f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
10 changes: 10 additions & 0 deletions userspace/libsinsp/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,11 @@ std::vector<metrics_v2> 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<thread_pool::routine_id_t>(nullptr);
}

auto f = [this, routine_fn, routine_state]() -> bool {
return static_cast<bool>(routine_fn(m_state, routine_state));
};
Expand All @@ -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);
}

Expand Down
7 changes: 6 additions & 1 deletion userspace/libsinsp/sinsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ sinsp::sinsp(bool with_metrics) :
m_table_registry = std::make_shared<libsinsp::state::table_registry>();
m_table_registry->add_table(m_thread_manager.get());

#if !defined(__EMSCRIPTEN__)
m_thread_pool = std::make_shared<bs_thread_pool>();
#endif
}

sinsp::~sinsp()
Expand Down Expand Up @@ -806,7 +808,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;
}
Expand Down
6 changes: 6 additions & 0 deletions userspace/libsinsp/test/plugins.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,11 +930,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);
Expand Down Expand Up @@ -966,3 +970,5 @@ TEST_F(sinsp_with_test_input, plugin_routines)
routines_num = m_inspector.m_thread_pool->routines_num();
ASSERT_EQ(routines_num, 0);
}

#endif
4 changes: 3 additions & 1 deletion userspace/libsinsp/test/thread_pool.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
#include <gtest/gtest.h>
#include <sinsp_with_test_input.h>

#if !defined(__EMSCRIPTEN__)
TEST_F(sinsp_with_test_input, thread_pool)
{
open_inspector();
Expand Down Expand Up @@ -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);
}
}
#endif
2 changes: 1 addition & 1 deletion userspace/plugin/plugin_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

//
Expand Down

0 comments on commit f33a73f

Please sign in to comment.