From 556060ef3787f32fd3ab049b42f83508c1873364 Mon Sep 17 00:00:00 2001 From: Grzegorz Nosek Date: Thu, 21 Nov 2024 23:18:23 +0100 Subject: [PATCH] cleanup(sinsp): move iterate_entries to built_in_table Signed-off-by: Grzegorz Nosek --- userspace/libsinsp/plugin_table_api.cpp | 150 ------------------------ userspace/libsinsp/state/table.h | 24 ++-- 2 files changed, 12 insertions(+), 162 deletions(-) diff --git a/userspace/libsinsp/plugin_table_api.cpp b/userspace/libsinsp/plugin_table_api.cpp index e5cb54c3f7d..6937eb724b5 100644 --- a/userspace/libsinsp/plugin_table_api.cpp +++ b/userspace/libsinsp/plugin_table_api.cpp @@ -331,120 +331,6 @@ struct plugin_table_wrapper : public libsinsp::state::table { } }; - struct plugin_table_entry : public libsinsp::state::table_entry { - plugin_table_entry(sinsp_plugin* o, - const owned_table_input_t& i, - const std::shared_ptr& fields, - ss_plugin_table_entry_t* e, - bool detached): - table_entry(fields), - m_owner(o), - m_input(i), - m_entry(e), - m_detached(detached) {}; - plugin_table_entry(const plugin_table_entry& o) = delete; - plugin_table_entry& operator=(const plugin_table_entry& o) = delete; - plugin_table_entry(plugin_table_entry&& o) = default; - plugin_table_entry& operator=(plugin_table_entry&& o) = default; - virtual ~plugin_table_entry() { - if(m_entry) { - if(m_detached) { - m_input->writer_ext->destroy_table_entry(m_input->table, m_entry); - } else { - m_input->reader_ext->release_table_entry(m_input->table, m_entry); - } - } - } - - sinsp_plugin* m_owner; - owned_table_input_t m_input; - ss_plugin_table_entry_t* m_entry; - bool m_detached; - - // note(jasondellaluce): dynamic cast is expensive but this is not expected - // to ever be ever invoked, because we set the fields shared pointer - // at construction time. This is just here as a consistency fence in - // case of misuse. - virtual void set_dynamic_fields(const std::shared_ptr& defs) override { - if(defs && dynamic_cast(defs.get()) == nullptr) { - throw sinsp_exception(table_input_error_prefix(m_owner, m_input.get()) + - "plugin table can only be set with plugin dynamic fields"); - } - table_entry::set_dynamic_fields(defs); - } - - virtual void get_dynamic_field(const ds::field_info& i, void* out) override { - if(i.info().type_id() == SS_PLUGIN_ST_TABLE) { - throw sinsp_exception(table_input_error_prefix(m_owner, m_input.get()) + - "read field failure: dynamic table fields not supported"); - } - const auto& infos = get_plugin_field_infos(); - ss_plugin_state_data dout; - auto rc = m_input->reader_ext->read_entry_field(m_input->table, - m_entry, - infos.m_accessors[i.index()], - &dout); - if(rc != SS_PLUGIN_SUCCESS) { - throw sinsp_exception(table_input_error_prefix(m_owner, m_input.get()) + - "read field failure: " + m_owner->get_last_error()); - } - - // note: strings are the only exception to the switch case below, - // because they are represented as std::string in libsinsp' typeinfo - // and as const char*s by the plugin API. - // todo(jasondellaluce): maybe find a common place for all this - // type conversions knowledge (also leaked in dynamic_struct.h) - if(i.info().type_id() == SS_PLUGIN_ST_STRING) { - *(const char**)out = dout.str; - } else { -#define _X(_type, _dtype) \ - { convert_types(dout._dtype, *((_type*)out)); } - __PLUGIN_STATETYPE_SWITCH(i.info().type_id()); -#undef _X - } - } - - virtual void set_dynamic_field(const ds::field_info& i, const void* in) override { - const auto& infos = get_plugin_field_infos(); - ss_plugin_state_data v; - - // note: strings are the only exception to the switch case below, - // because they are represented as std::string in libsinsp' typeinfo - // and as const char*s by the plugin API. - // todo(jasondellaluce): maybe find a common place for all this - // type conversions knowledge (also leaked in dynamic_struct.h) - if(i.info().type_id() == SS_PLUGIN_ST_STRING) { - v.str = *(const char**)in; - } else { -#define _X(_type, _dtype) \ - { convert_types(*((_type*)in), v._dtype); } - __PLUGIN_STATETYPE_SWITCH(i.info().type_id()); -#undef _X - } - - auto rc = m_input->writer_ext->write_entry_field(m_input->table, - m_entry, - infos.m_accessors[i.index()], - &v); - if(rc != SS_PLUGIN_SUCCESS) { - throw sinsp_exception(table_input_error_prefix(m_owner, m_input.get()) + - "write field failure: " + m_owner->get_last_error()); - } - } - - private: - const plugin_field_infos& get_plugin_field_infos() const { - if(dynamic_fields() == nullptr) { - throw sinsp_exception(table_input_error_prefix(m_owner, m_input.get()) + - "local fields definitions not set"); - } - // note: casting should be safe because we force the - // plugin_field_infos subtype both the constructor and the setter - ASSERT(dynamic_cast(dynamic_fields().get()) != nullptr); - return *static_cast(dynamic_fields().get()); - } - }; - plugin_table_wrapper(sinsp_plugin* o, const ss_plugin_table_input* i): libsinsp::state::table(), m_owner(o), @@ -473,42 +359,6 @@ struct plugin_table_wrapper : public libsinsp::state::table { return m_dyn_fields_as_base_class; } - // used only for foreach_entry below - struct table_iterator_state { - std::string err; - plugin_table_entry* m_entry; - std::function* m_it; - }; - - // used only for foreach_entry below - static ss_plugin_bool table_iterator_func(ss_plugin_table_iterator_state_t* s, - ss_plugin_table_entry_t* _e) { - auto state = static_cast(s); - state->m_entry->m_entry = _e; - __CATCH_ERR_MSG(state->err, { return (*state->m_it)(*state->m_entry) ? 1 : 0; }); - return 0; - } - - bool foreach_entry(std::function pred) override { - plugin_table_entry entry(m_owner, m_input, m_dyn_fields, NULL, false); - table_iterator_state state; - state.m_it = &pred; - state.m_entry = &entry; - auto s = static_cast(&state); - if(m_input->reader_ext->iterate_entries(m_input->table, table_iterator_func, s) == 0) { - // avoids invoking release_table_entry - entry.m_entry = NULL; - if(!state.err.empty()) { - throw sinsp_exception(table_input_error_prefix(m_owner, m_input.get()) + - "iterate entries failure: " + state.err); - } - return false; - } - // avoids invoking release_table_entry - entry.m_entry = NULL; - return true; - } - const char* name() const override { return m_input->name; } const ss_plugin_table_fieldinfo* list_fields(libsinsp::state::sinsp_table_owner* owner, diff --git a/userspace/libsinsp/state/table.h b/userspace/libsinsp/state/table.h index 05dd6728ddf..05a2ce2d8df 100644 --- a/userspace/libsinsp/state/table.h +++ b/userspace/libsinsp/state/table.h @@ -175,18 +175,6 @@ class base_table { m_dynamic_fields = dynf; } - /** - * @brief Iterates over all the entries contained in the table and invokes - * the given predicate for each of them. - * - * @param pred The predicate to invoke for all the table's entries. The - * predicate returns true if the iteration can proceed to the next entry, - * and false if the iteration needs to break out. - * @return true If the iteration proceeded successfully for all the entries. - * @return false If the iteration broke out. - */ - virtual bool foreach_entry(std::function pred) = 0; - virtual const ss_plugin_table_fieldinfo* list_fields(sinsp_table_owner* owner, uint32_t* nfields) = 0; @@ -331,6 +319,18 @@ class built_in_table : public table { */ virtual bool erase_entry(const KeyType& key) = 0; + /** + * @brief Iterates over all the entries contained in the table and invokes + * the given predicate for each of them. + * + * @param pred The predicate to invoke for all the table's entries. The + * predicate returns true if the iteration can proceed to the next entry, + * and false if the iteration needs to break out. + * @return true If the iteration proceeded successfully for all the entries. + * @return false If the iteration broke out. + */ + virtual bool foreach_entry(std::function pred) = 0; + uint64_t get_size(sinsp_table_owner* owner) override; const ss_plugin_table_fieldinfo* list_fields(sinsp_table_owner* owner,