Skip to content

Commit

Permalink
refactor(userspace/falco): use move semantics to improve logger perfo…
Browse files Browse the repository at this point in the history
…rmance

Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
  • Loading branch information
jasondellaluce committed Jun 23, 2022
1 parent e897ae9 commit b0dcb37
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 23 deletions.
24 changes: 8 additions & 16 deletions userspace/falco/event_drops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,16 @@ bool syscall_evt_drop_mgr::perform_actions(uint64_t now, scap_stats &delta, bool
std::string rule = "Falco internal: syscall event drop";
std::string msg = rule + ". " + std::to_string(delta.n_drops) + " system calls dropped in last second.";

bool should_exit = false;

for(auto &act : m_actions)
{
switch(act)
{
case syscall_evt_drop_action::IGNORE:
break;
return true;

case syscall_evt_drop_action::LOG:
falco_logger::log(LOG_DEBUG, msg);
break;
falco_logger::log(LOG_DEBUG, std::move(msg));
return true;

case syscall_evt_drop_action::ALERT:
{
Expand All @@ -154,24 +152,18 @@ bool syscall_evt_drop_mgr::perform_actions(uint64_t now, scap_stats &delta, bool
output_fields["n_drops_bug"] = std::to_string(delta.n_drops_bug);
output_fields["ebpf_enabled"] = std::to_string(bpf_enabled);
m_outputs->handle_msg(now, falco_common::PRIORITY_DEBUG, msg, rule, output_fields);
break;
return true;
}
case syscall_evt_drop_action::EXIT:
should_exit = true;
break;
falco_logger::log(LOG_CRIT, std::move(msg));
falco_logger::log(LOG_CRIT, "Exiting.");
return false;

default:
falco_logger::log(LOG_ERR, "Ignoring unknown action " + std::to_string(int(act)));
break;
return true;
}
}

if(should_exit)
{
falco_logger::log(LOG_CRIT, msg);
falco_logger::log(LOG_CRIT, "Exiting.");
return false;
}

return true;
}
6 changes: 3 additions & 3 deletions userspace/falco/falco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ limitations under the License.
#include "logger.h"
#include "banned.h" // This raises a compilation error when certain functions are used

static void display_fatal_err(const string &msg)
static void display_fatal_err(const string &&msg)
{
falco_logger::log(LOG_ERR, msg);

/**
* If stderr logging is not enabled, also log to stderr. When
* daemonized this will simply write to /dev/null.
Expand All @@ -35,6 +33,8 @@ static void display_fatal_err(const string &msg)
{
std::cerr << msg;
}

falco_logger::log(LOG_ERR, std::move(msg));
}

//
Expand Down
2 changes: 1 addition & 1 deletion userspace/falco/grpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static void gpr_log_dispatcher_func(gpr_log_func_args* args)
string copy = "grpc: ";
copy.append(args->message);
copy.push_back('\n');
falco_logger::log(priority, copy);
falco_logger::log(priority, std::move(copy));
}

void falco::grpc::server::thread_process(int thread_index)
Expand Down
4 changes: 2 additions & 2 deletions userspace/falco/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void falco_logger::set_sinsp_logging(bool enable)
g_logger.add_callback_log(
[](std::string&& str, const sinsp_logger::severity sev)
{
falco_logger::log(level_sinsp_to_falco(sev), str);
falco_logger::log(level_sinsp_to_falco(sev), std::move(str));
});
g_logger.disable_timestamps();
g_logger.set_severity(level_falco_to_sinsp(falco_logger::level));
Expand All @@ -139,7 +139,7 @@ void falco_logger::set_sinsp_logging(bool enable)
bool falco_logger::log_stderr = true;
bool falco_logger::log_syslog = true;

void falco_logger::log(int priority, const string msg)
void falco_logger::log(int priority, const string&& msg)
{

if(priority > falco_logger::level)
Expand Down
2 changes: 1 addition & 1 deletion userspace/falco/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class falco_logger

static void set_sinsp_logging(bool enable);

static void log(int priority, const string msg);
static void log(int priority, const string&& msg);

static int level;
static bool log_stderr;
Expand Down

0 comments on commit b0dcb37

Please sign in to comment.