Skip to content

Commit

Permalink
cleanup(sinsp): move iterate_entries to built_in_table
Browse files Browse the repository at this point in the history
Signed-off-by: Grzegorz Nosek <[email protected]>
  • Loading branch information
gnosek committed Dec 12, 2024
1 parent 35ea345 commit 556060e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 162 deletions.
150 changes: 0 additions & 150 deletions userspace/libsinsp/plugin_table_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,120 +331,6 @@ struct plugin_table_wrapper : public libsinsp::state::table<KeyType> {
}
};

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<plugin_field_infos>& 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<ds::field_infos>& defs) override {
if(defs && dynamic_cast<plugin_field_infos*>(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<plugin_field_infos*>(dynamic_fields().get()) != nullptr);
return *static_cast<plugin_field_infos*>(dynamic_fields().get());
}
};

plugin_table_wrapper(sinsp_plugin* o, const ss_plugin_table_input* i):
libsinsp::state::table<KeyType>(),
m_owner(o),
Expand Down Expand Up @@ -473,42 +359,6 @@ struct plugin_table_wrapper : public libsinsp::state::table<KeyType> {
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<bool(libsinsp::state::table_entry&)>* 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<table_iterator_state*>(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<bool(libsinsp::state::table_entry&)> 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<ss_plugin_table_iterator_state_t*>(&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,
Expand Down
24 changes: 12 additions & 12 deletions userspace/libsinsp/state/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool(table_entry& e)> pred) = 0;

virtual const ss_plugin_table_fieldinfo* list_fields(sinsp_table_owner* owner,
uint32_t* nfields) = 0;

Expand Down Expand Up @@ -331,6 +319,18 @@ class built_in_table : public table<KeyType> {
*/
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<bool(table_entry& e)> pred) = 0;

uint64_t get_size(sinsp_table_owner* owner) override;

const ss_plugin_table_fieldinfo* list_fields(sinsp_table_owner* owner,
Expand Down

0 comments on commit 556060e

Please sign in to comment.