Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(userspace/libsinsp): assorted pass-by-reference performance optimizations #1965

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion userspace/libsinsp/cgroup_limits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bool read_cgroup_vals(const std::string &path, std::istream &stream, int64_t &ou
* reasonable being [0; CGROUP_VAL_MAX)
*/
template<typename... Args>
bool read_cgroup_val(std::shared_ptr<std::string> &subsys,
bool read_cgroup_val(const std::shared_ptr<std::string> &subsys,
const std::string &cgroup,
const std::string &filename,
int64_t &out,
Expand Down
7 changes: 4 additions & 3 deletions userspace/libsinsp/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ std::string sinsp_container_manager::container_to_json(const sinsp_container_inf
return Json::FastWriter().write(obj);
}

bool sinsp_container_manager::container_to_sinsp_event(const std::string& json, sinsp_evt* evt, std::shared_ptr<sinsp_threadinfo> tinfo, char *scap_err)
bool sinsp_container_manager::container_to_sinsp_event(const std::string& json, sinsp_evt* evt, std::unique_ptr<sinsp_threadinfo> tinfo, char *scap_err)
{
uint32_t json_len = json.length() + 1;
size_t totlen = sizeof(scap_evt) + sizeof(uint32_t) + json_len;
Expand All @@ -300,8 +300,9 @@ bool sinsp_container_manager::container_to_sinsp_event(const std::string& json,
}

evt->init();
evt->set_tinfo_ref(tinfo);
evt->set_tinfo(tinfo.get());
std::shared_ptr<sinsp_threadinfo> stinfo = std::move(tinfo);
evt->set_tinfo_ref(stinfo);
evt->set_tinfo(stinfo.get());

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class sinsp_container_manager :


private:
bool container_to_sinsp_event(const std::string& json, sinsp_evt* evt, std::shared_ptr<sinsp_threadinfo> tinfo, char* scap_err);
bool container_to_sinsp_event(const std::string& json, sinsp_evt* evt, std::unique_ptr<sinsp_threadinfo> tinfo, char* scap_err);
std::string get_docker_env(const Json::Value &env_vars, const std::string &mti);

std::list<std::shared_ptr<libsinsp::container_engine::container_engine_base>> m_container_engines;
Expand Down
5 changes: 2 additions & 3 deletions userspace/libsinsp/container_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,16 @@ const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by
return NULL;
}

std::shared_ptr<sinsp_threadinfo> sinsp_container_info::get_tinfo(sinsp* inspector) const
std::unique_ptr<sinsp_threadinfo> sinsp_container_info::get_tinfo(sinsp* inspector) const
{
std::shared_ptr<sinsp_threadinfo> tinfo(inspector->build_threadinfo().release());
auto tinfo = inspector->build_threadinfo();
tinfo->m_tid = -1;
tinfo->m_pid = -1;
tinfo->m_vtid = -2;
tinfo->m_vpid = -2;
tinfo->m_comm = "container:" + m_id;
tinfo->m_exe = "container:" + m_id;
tinfo->m_container_id = m_id;

return tinfo;
}

Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/container_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class sinsp_container_info
return m_lookup.get_status();
}

std::shared_ptr<sinsp_threadinfo> get_tinfo(sinsp* inspector) const;
std::unique_ptr<sinsp_threadinfo> get_tinfo(sinsp* inspector) const;

// Match a process against the set of health probes
container_health_probe::probe_type match_health_probe(sinsp_threadinfo *tinfo) const;
Expand Down
8 changes: 4 additions & 4 deletions userspace/libsinsp/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,12 @@ class SINSP_PUBLIC sinsp_evt
return m_tinfo_ref;
}

inline std::shared_ptr<sinsp_threadinfo> get_tinfo_ref()
inline const std::shared_ptr<sinsp_threadinfo>& get_tinfo_ref()
{
return m_tinfo_ref;
}

inline void set_tinfo_ref(std::shared_ptr<sinsp_threadinfo> v)
inline void set_tinfo_ref(const std::shared_ptr<sinsp_threadinfo>& v)
{
m_tinfo_ref = v;
}
Expand All @@ -794,12 +794,12 @@ class SINSP_PUBLIC sinsp_evt
return m_fdinfo_ref;
}

inline std::shared_ptr<sinsp_fdinfo> get_fdinfo_ref()
inline const std::shared_ptr<sinsp_fdinfo>& get_fdinfo_ref()
{
return m_fdinfo_ref;
}

inline void set_fdinfo_ref(std::shared_ptr<sinsp_fdinfo> v)
inline void set_fdinfo_ref(const std::shared_ptr<sinsp_fdinfo>& v)
{
m_fdinfo_ref = v;
}
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/eventformatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class SINSP_PUBLIC sinsp_evt_formatter
bool has_transformers = false;

resolution_token(const std::string& n, token_t t, bool h)
: name(n), token(t), has_transformers(h) { }
: name(n), token(std::move(t)), has_transformers(h) { }
};

output_format m_output_format;
Expand Down
10 changes: 5 additions & 5 deletions userspace/libsinsp/fdinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const char* sinsp_fdinfo::get_typestring() const
}
}

sinsp_fdinfo::sinsp_fdinfo(std::shared_ptr<libsinsp::state::dynamic_struct::field_infos> dyn_fields)
sinsp_fdinfo::sinsp_fdinfo(const std::shared_ptr<libsinsp::state::dynamic_struct::field_infos>& dyn_fields)
: table_entry(dyn_fields)
{
}
Expand Down Expand Up @@ -309,7 +309,7 @@ sinsp_fdtable::sinsp_fdtable(sinsp* inspector)
reset_cache();
}

inline std::shared_ptr<sinsp_fdinfo> sinsp_fdtable::find_ref(int64_t fd)
inline const std::shared_ptr<sinsp_fdinfo>& sinsp_fdtable::find_ref(int64_t fd)
{
//
// Try looking up in our simple cache
Expand All @@ -334,7 +334,7 @@ inline std::shared_ptr<sinsp_fdinfo> sinsp_fdtable::find_ref(int64_t fd)
{
m_sinsp_stats_v2->m_n_failed_fd_lookups++;
}
return nullptr;
return m_nullptr_ret;
}
else
{
Expand All @@ -350,7 +350,7 @@ inline std::shared_ptr<sinsp_fdinfo> sinsp_fdtable::find_ref(int64_t fd)
}
}

inline std::shared_ptr<sinsp_fdinfo> sinsp_fdtable::add_ref(int64_t fd, std::unique_ptr<sinsp_fdinfo> fdinfo)
inline const std::shared_ptr<sinsp_fdinfo>& sinsp_fdtable::add_ref(int64_t fd, std::unique_ptr<sinsp_fdinfo> fdinfo)
{
if (fdinfo->dynamic_fields() != dynamic_fields())
{
Expand Down Expand Up @@ -386,7 +386,7 @@ inline std::shared_ptr<sinsp_fdinfo> sinsp_fdtable::add_ref(int64_t fd, std::uni
}
else
{
return nullptr;
return m_nullptr_ret;
}
}
else
Expand Down
7 changes: 4 additions & 3 deletions userspace/libsinsp/fdinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class SINSP_PUBLIC sinsp_fdinfo : public libsinsp::state::table_entry
FLAGS_CONNECTION_FAILED = (1 << 16),
};

sinsp_fdinfo(std::shared_ptr<libsinsp::state::dynamic_struct::field_infos> dyn_fields = nullptr);
sinsp_fdinfo(const std::shared_ptr<libsinsp::state::dynamic_struct::field_infos>& dyn_fields = nullptr);
sinsp_fdinfo(sinsp_fdinfo&& o) = default;
sinsp_fdinfo& operator=(sinsp_fdinfo&& o) = default;
sinsp_fdinfo(const sinsp_fdinfo& o) = default;
Expand Down Expand Up @@ -558,9 +558,10 @@ class sinsp_fdtable : public libsinsp::state::table<int64_t>
int64_t m_last_accessed_fd;
std::shared_ptr<sinsp_fdinfo> m_last_accessed_fdinfo;
uint64_t m_tid;
std::shared_ptr<sinsp_fdinfo> m_nullptr_ret; // needed for returning a reference

private:
inline void lookup_device(sinsp_fdinfo* fdi, uint64_t fd);
std::shared_ptr<sinsp_fdinfo> find_ref(int64_t fd);
std::shared_ptr<sinsp_fdinfo> add_ref(int64_t fd, std::unique_ptr<sinsp_fdinfo> fdinfo);
const std::shared_ptr<sinsp_fdinfo>& find_ref(int64_t fd);
const std::shared_ptr<sinsp_fdinfo>& add_ref(int64_t fd, std::unique_ptr<sinsp_fdinfo> fdinfo);
};
10 changes: 5 additions & 5 deletions userspace/libsinsp/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,27 @@ void sinsp_filter::add_check(std::unique_ptr<sinsp_filter_check> chk)
sinsp_filter_compiler::sinsp_filter_compiler(
sinsp* inspector,
const std::string& fltstr,
std::shared_ptr<sinsp_filter_cache_factory> cache_factory)
const std::shared_ptr<sinsp_filter_cache_factory>& cache_factory)
: m_flt_str(fltstr),
m_factory(std::make_shared<sinsp_filter_factory>(inspector, m_default_filterlist)),
m_cache_factory(cache_factory)
{
}

sinsp_filter_compiler::sinsp_filter_compiler(
std::shared_ptr<sinsp_filter_factory> factory,
const std::shared_ptr<sinsp_filter_factory>& factory,
const std::string& fltstr,
std::shared_ptr<sinsp_filter_cache_factory> cache_factory)
const std::shared_ptr<sinsp_filter_cache_factory>& cache_factory)
: m_flt_str(fltstr),
m_factory(factory),
m_cache_factory(cache_factory)
{
}

sinsp_filter_compiler::sinsp_filter_compiler(
std::shared_ptr<sinsp_filter_factory> factory,
const std::shared_ptr<sinsp_filter_factory>& factory,
const libsinsp::filter::ast::expr* fltast,
std::shared_ptr<sinsp_filter_cache_factory> cache_factory)
const std::shared_ptr<sinsp_filter_cache_factory>& cache_factory)
: m_flt_ast(fltast),
m_factory(factory),
m_cache_factory(cache_factory)
Expand Down
14 changes: 7 additions & 7 deletions userspace/libsinsp/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class SINSP_PUBLIC sinsp_filter_compiler:
sinsp_filter_compiler(
sinsp* inspector,
const std::string& fltstr,
std::shared_ptr<sinsp_filter_cache_factory> cache_factory = nullptr);
const std::shared_ptr<sinsp_filter_cache_factory>& cache_factory = nullptr);

/*!
\brief Constructs the compiler
Expand All @@ -212,9 +212,9 @@ class SINSP_PUBLIC sinsp_filter_compiler:
\param fltstr The filter string to compile
*/
sinsp_filter_compiler(
std::shared_ptr<sinsp_filter_factory> factory,
const std::shared_ptr<sinsp_filter_factory>& factory,
const std::string& fltstr,
std::shared_ptr<sinsp_filter_cache_factory> cache_factory = nullptr);
const std::shared_ptr<sinsp_filter_cache_factory>& cache_factory = nullptr);

/*!
\brief Constructs the compiler
Expand All @@ -225,9 +225,9 @@ class SINSP_PUBLIC sinsp_filter_compiler:
tree
*/
sinsp_filter_compiler(
std::shared_ptr<sinsp_filter_factory> factory,
const std::shared_ptr<sinsp_filter_factory>& factory,
const libsinsp::filter::ast::expr* fltast,
std::shared_ptr<sinsp_filter_cache_factory> cache_factory = nullptr);
const std::shared_ptr<sinsp_filter_cache_factory>& cache_factory = nullptr);

/*!
\brief Builds a filtercheck tree and bundles it in sinsp_filter
Expand All @@ -237,9 +237,9 @@ class SINSP_PUBLIC sinsp_filter_compiler:
*/
std::unique_ptr<sinsp_filter> compile();

std::shared_ptr<const libsinsp::filter::ast::expr> get_filter_ast() const { return m_internal_flt_ast; }
const std::shared_ptr<libsinsp::filter::ast::expr> get_filter_ast() const { return m_internal_flt_ast; }

std::shared_ptr<libsinsp::filter::ast::expr> get_filter_ast() { return m_internal_flt_ast; }
const std::shared_ptr<libsinsp::filter::ast::expr>& get_filter_ast() { return m_internal_flt_ast; }

const libsinsp::filter::ast::pos_info& get_pos() const { return m_pos; }

Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/gvisor_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ static const std::vector<gvisor_point_info_t> s_gvisor_points = {

constexpr unsigned int max_retries = 3;

std::string generate(std::string socket_path)
std::string generate(const std::string& socket_path)
{
Json::Value jpoints;
for(const auto &point_info : s_gvisor_points)
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/gvisor_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ limitations under the License.

namespace gvisor_config
{
std::string generate(std::string socket_path);
std::string generate(const std::string& socket_path);
}
3 changes: 2 additions & 1 deletion userspace/libsinsp/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ sinsp_logger::severity sinsp_logger::get_severity() const
return m_sev;
}

void sinsp_logger::log(std::string msg, const severity sev)
void sinsp_logger::log(const std::string& m, const severity sev)
{
sinsp_logger_callback cb = nullptr;

Expand All @@ -153,6 +153,7 @@ void sinsp_logger::log(std::string msg, const severity sev)
return;
}

std::string msg = m;
if((m_flags & sinsp_logger::OT_NOTS) == 0)
{
struct timeval ts = {};
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class SINSP_PUBLIC sinsp_logger
* Emit the given msg to the configured log sink if the given sev
* is greater than or equal to the minimum configured logging severity.
*/
void log(std::string msg, severity sev = SEV_INFO);
void log(const std::string& m, severity sev = SEV_INFO);

/**
* Write the given printf-style log message of the given severity
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/metrics_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ void libs_resource_utilization::get_container_memory_used()
fclose(f);
}

libs_state_counters::libs_state_counters(std::shared_ptr<sinsp_stats_v2> sinsp_stats_v2, sinsp_thread_manager* thread_manager) : m_sinsp_stats_v2(sinsp_stats_v2), m_n_fds(0), m_n_threads(0) {
libs_state_counters::libs_state_counters(const std::shared_ptr<sinsp_stats_v2>& sinsp_stats_v2, sinsp_thread_manager* thread_manager) : m_sinsp_stats_v2(sinsp_stats_v2), m_n_fds(0), m_n_threads(0) {
if (thread_manager != nullptr)
{
m_n_threads = thread_manager->get_thread_count();
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/metrics_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class libs_resource_utilization : libsinsp_metrics
class libs_state_counters : libsinsp_metrics
{
public:
libs_state_counters(std::shared_ptr<sinsp_stats_v2> sinsp_stats_v2, sinsp_thread_manager* thread_manager);
libs_state_counters(const std::shared_ptr<sinsp_stats_v2>& sinsp_stats_v2, sinsp_thread_manager* thread_manager);

std::vector<metrics_v2> to_metrics() override;

Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ std::shared_ptr<sinsp_plugin> sinsp_plugin::create(
return plugin;
}

bool sinsp_plugin::is_plugin_loaded(std::string &filepath)
bool sinsp_plugin::is_plugin_loaded(const std::string &filepath)
{
return plugin_is_loaded(filepath.c_str());
}
Expand Down Expand Up @@ -958,7 +958,7 @@ std::vector<metrics_v2> sinsp_plugin::get_metrics() const

/** Field Extraction CAP **/

std::unique_ptr<sinsp_filter_check> sinsp_plugin::new_filtercheck(std::shared_ptr<sinsp_plugin> plugin)
std::unique_ptr<sinsp_filter_check> sinsp_plugin::new_filtercheck(const std::shared_ptr<sinsp_plugin>& plugin)
{
return std::make_unique<sinsp_filter_check_plugin>(plugin);
}
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ class sinsp_plugin
/**
* @brief Return whether a filesystem dynamic library object is loaded.
*/
static bool is_plugin_loaded(std::string& filepath);
static bool is_plugin_loaded(const std::string& filepath);

/**
* @brief If the plugin has CAP_EXTRACTION capability, returns a
* filtercheck with its exported fields. Returns NULL otherwise.
*
* todo(jasondellaluce): make this return a unique_ptr
*/
static std::unique_ptr<sinsp_filter_check> new_filtercheck(std::shared_ptr<sinsp_plugin> plugin);
static std::unique_ptr<sinsp_filter_check> new_filtercheck(const std::shared_ptr<sinsp_plugin>& plugin);

/**
* @brief Returns true if the source is compatible with the given set
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/plugin_filtercheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sinsp_filter_check_plugin::sinsp_filter_check_plugin()
m_eplugin = nullptr;
}

sinsp_filter_check_plugin::sinsp_filter_check_plugin(std::shared_ptr<sinsp_plugin> plugin)
sinsp_filter_check_plugin::sinsp_filter_check_plugin(const std::shared_ptr<sinsp_plugin>& plugin)
{
if (!(plugin->caps() & CAP_EXTRACTION))
{
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/plugin_filtercheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class sinsp_filter_check_plugin : public sinsp_filter_check
public:
sinsp_filter_check_plugin();

explicit sinsp_filter_check_plugin(std::shared_ptr<sinsp_plugin> plugin);
explicit sinsp_filter_check_plugin(const std::shared_ptr<sinsp_plugin>& plugin);

explicit sinsp_filter_check_plugin(const sinsp_filter_check_plugin &p);

Expand Down
Loading
Loading