From ae6b268d84298c0908a169cdbea7781a596beda1 Mon Sep 17 00:00:00 2001 From: Jason Dellaluce Date: Wed, 10 Jul 2024 07:47:57 +0000 Subject: [PATCH] refactor(userspace/libsinsp): pass by reference when possible Signed-off-by: Jason Dellaluce --- userspace/libsinsp/gvisor_config.cpp | 2 +- userspace/libsinsp/gvisor_config.h | 2 +- userspace/libsinsp/logger.cpp | 3 ++- userspace/libsinsp/logger.h | 2 +- userspace/libsinsp/plugin.cpp | 2 +- userspace/libsinsp/plugin.h | 2 +- userspace/libsinsp/sinsp.cpp | 4 ++-- userspace/libsinsp/sinsp.h | 4 ++-- userspace/libsinsp/threadinfo.cpp | 10 +++++----- userspace/libsinsp/threadinfo.h | 4 ++-- userspace/libsinsp/utils.cpp | 2 +- 11 files changed, 19 insertions(+), 18 deletions(-) diff --git a/userspace/libsinsp/gvisor_config.cpp b/userspace/libsinsp/gvisor_config.cpp index 666096920f..3569826610 100644 --- a/userspace/libsinsp/gvisor_config.cpp +++ b/userspace/libsinsp/gvisor_config.cpp @@ -333,7 +333,7 @@ static const std::vector 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) diff --git a/userspace/libsinsp/gvisor_config.h b/userspace/libsinsp/gvisor_config.h index 93a90f6bf8..b0d874b9ce 100644 --- a/userspace/libsinsp/gvisor_config.h +++ b/userspace/libsinsp/gvisor_config.h @@ -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); } diff --git a/userspace/libsinsp/logger.cpp b/userspace/libsinsp/logger.cpp index 4d4066d555..f6b4b4388f 100644 --- a/userspace/libsinsp/logger.cpp +++ b/userspace/libsinsp/logger.cpp @@ -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; @@ -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 = {}; diff --git a/userspace/libsinsp/logger.h b/userspace/libsinsp/logger.h index 250c7f0303..80425486b1 100644 --- a/userspace/libsinsp/logger.h +++ b/userspace/libsinsp/logger.h @@ -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 diff --git a/userspace/libsinsp/plugin.cpp b/userspace/libsinsp/plugin.cpp index 44a932781b..3de7dcc657 100755 --- a/userspace/libsinsp/plugin.cpp +++ b/userspace/libsinsp/plugin.cpp @@ -134,7 +134,7 @@ std::shared_ptr 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()); } diff --git a/userspace/libsinsp/plugin.h b/userspace/libsinsp/plugin.h index e9972be73b..4f8b546023 100755 --- a/userspace/libsinsp/plugin.h +++ b/userspace/libsinsp/plugin.h @@ -72,7 +72,7 @@ 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 diff --git a/userspace/libsinsp/sinsp.cpp b/userspace/libsinsp/sinsp.cpp index 025a6b254a..86b722df41 100644 --- a/userspace/libsinsp/sinsp.cpp +++ b/userspace/libsinsp/sinsp.cpp @@ -670,7 +670,7 @@ bool sinsp::check_current_engine(const std::string& engine_name) const /*=============================== Engine related ===============================*/ -std::string sinsp::generate_gvisor_config(std::string socket_path) +std::string sinsp::generate_gvisor_config(const std::string& socket_path) { return gvisor_config::generate(socket_path); } @@ -1800,7 +1800,7 @@ void sinsp::set_log_callback(sinsp_logger_callback cb) } } -void sinsp::set_log_file(std::string filename) +void sinsp::set_log_file(const std::string& filename) { libsinsp_logger()->add_file_log(filename); } diff --git a/userspace/libsinsp/sinsp.h b/userspace/libsinsp/sinsp.h index 8fed6f9cda..66632da5ae 100644 --- a/userspace/libsinsp/sinsp.h +++ b/userspace/libsinsp/sinsp.h @@ -184,7 +184,7 @@ class SINSP_PUBLIC sinsp : public capture_stats_source scap_fseek(m_h, filepos); } - std::string generate_gvisor_config(std::string socket_path); + std::string generate_gvisor_config(const std::string& socket_path); /*! @@ -338,7 +338,7 @@ class SINSP_PUBLIC sinsp : public capture_stats_source /*! \brief Instruct sinsp to write its log messages to the given file. */ - void set_log_file(std::string filename); + void set_log_file(const std::string& filename); /*! \brief Instruct sinsp to write its log messages to stderr. diff --git a/userspace/libsinsp/threadinfo.cpp b/userspace/libsinsp/threadinfo.cpp index 6cf2577b18..c1ea6e0dbd 100644 --- a/userspace/libsinsp/threadinfo.cpp +++ b/userspace/libsinsp/threadinfo.cpp @@ -628,7 +628,7 @@ void sinsp_threadinfo::set_args(const char* args, size_t len) set_args(sinsp_split(args, len, '\0')); } -void sinsp_threadinfo::set_args(std::vector args) +void sinsp_threadinfo::set_args(const std::vector& args) { m_args = args; } @@ -742,11 +742,11 @@ void sinsp_threadinfo::set_cgroups(const char* cgroups, size_t len) set_cgroups(sinsp_split(cgroups, len, '\0')); } -void sinsp_threadinfo::set_cgroups(std::vector cgroups) +void sinsp_threadinfo::set_cgroups(const std::vector& cgroups) { - decltype(m_cgroups) tmp_cgroups(new cgroups_t); + auto tmp_cgroups = std::make_unique(); - for( auto &def : cgroups) + for(const auto &def : cgroups) { std::string::size_type eq_pos = def.find("="); if (eq_pos == std::string::npos) @@ -2050,7 +2050,7 @@ const threadinfo_map_t::ptr_t& sinsp_thread_manager::get_thread_ref(int64_t tid, { libsinsp_logger()->format(sinsp_logger::SEV_INFO, "%s: Unable to complete for tid=%" PRIu64 ": sinsp::scap_t* is uninitialized", __func__, tid); - return NULL; + return m_nullptr_tinfo_ret; } scap_threadinfo scap_proc {}; diff --git a/userspace/libsinsp/threadinfo.h b/userspace/libsinsp/threadinfo.h index 08dd68381d..82fa9c3728 100644 --- a/userspace/libsinsp/threadinfo.h +++ b/userspace/libsinsp/threadinfo.h @@ -640,10 +640,10 @@ class SINSP_PUBLIC sinsp_threadinfo : public libsinsp::state::table_entry void remove_fd(int64_t fd); void update_cwd(std::string_view cwd); void set_args(const char* args, size_t len); - void set_args(std::vector args); + void set_args(const std::vector& args); void set_env(const char* env, size_t len); void set_cgroups(const char* cgroups, size_t len); - void set_cgroups(std::vector cgroups); + void set_cgroups(const std::vector& cgroups); bool is_lastevent_data_valid() const; inline void set_lastevent_data_validity(bool isvalid) { diff --git a/userspace/libsinsp/utils.cpp b/userspace/libsinsp/utils.cpp index b14c8209b8..9f0c608d34 100644 --- a/userspace/libsinsp/utils.cpp +++ b/userspace/libsinsp/utils.cpp @@ -869,7 +869,7 @@ void sinsp_utils::split_container_image(const std::string &image, std::string &digest, bool split_repo) { - auto split = [](const std::string &src, std::string &part1, std::string &part2, const std::string sep) + auto split = [](const std::string &src, std::string &part1, std::string &part2, const std::string& sep) { size_t pos = src.find(sep); if(pos != std::string::npos)