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

new: add a drop_pct referred to the global number of events #2130

Merged
merged 4 commits into from
Jul 14, 2022
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
57 changes: 18 additions & 39 deletions userspace/falco/statsfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ limitations under the License.

#include <sys/time.h>
#include <signal.h>
#include <nlohmann/json.hpp>

#include "statsfilewriter.h"
#include "banned.h" // This raises a compilation error when certain functions are used
#include "logger.h"

using namespace std;

Expand All @@ -28,8 +30,6 @@ static void timer_handler (int signum)
g_save_stats = true;
}

extern char **environ;

StatsFileWriter::StatsFileWriter()
: m_num_stats(0)
{
Expand Down Expand Up @@ -67,30 +67,6 @@ bool StatsFileWriter::init(std::shared_ptr<sinsp> inspector, string &filename, u
return false;
}

// (Undocumented) feature. Take any environment keys prefixed
FedeDP marked this conversation as resolved.
Show resolved Hide resolved
// with FALCO_STATS_EXTRA_XXX and add them to the output. Used by
// run_performance_tests.sh.
for(uint32_t i=0; environ[i]; i++)
{
char *p = strstr(environ[i], "=");
if(!p)
{
errstr = string("Could not find environment separator in ") + string(environ[i]);
return false;
}
string key(environ[i], p-environ[i]);
string val(p+1, strlen(environ[i])-(p-environ[i])-1);
if(key.compare(0, 18, "FALCO_STATS_EXTRA_") == 0)
{
string sub = key.substr(18);
if (m_extra != "")
{
m_extra += ", ";
}
m_extra += "\"" + sub + "\": " + "\"" + val + "\"";
}
}

return true;
}

Expand All @@ -100,6 +76,7 @@ void StatsFileWriter::handle()
{
scap_stats cstats;
scap_stats delta;
nlohmann::json jmsg;

g_save_stats = false;
m_num_stats++;
Expand All @@ -116,21 +93,23 @@ void StatsFileWriter::handle()
delta.n_preemptions = cstats.n_preemptions - m_last_stats.n_preemptions;
}

m_output << "{\"sample\": " << m_num_stats;
if(m_extra != "")
try
{
jmsg["sample"] = m_num_stats;
jmsg["cur"]["events"] = cstats.n_evts;
jmsg["cur"]["drops"] = cstats.n_drops;
jmsg["cur"]["preemptions"] = cstats.n_preemptions;
jmsg["cur"]["drop_pct"] = (cstats.n_evts == 0 ? 0 : (100.0*cstats.n_drops/cstats.n_evts));
jmsg["delta"]["events"] = delta.n_evts;
jmsg["delta"]["drops"] = delta.n_drops;
jmsg["delta"]["preemptions"] = delta.n_preemptions;
jmsg["delta"]["drop_pct"] = (delta.n_evts == 0 ? 0 : (100.0*delta.n_drops/delta.n_evts));
m_output << jmsg.dump() << endl;
}
catch(const exception &e)
{
m_output << ", " << m_extra;
falco_logger::log(LOG_ERR, "StatsFileWriter (handle): " + string(e.what()) + "\n");
}
m_output << ", \"cur\": {" <<
"\"events\": " << cstats.n_evts <<
", \"drops\": " << cstats.n_drops <<
", \"preemptions\": " << cstats.n_preemptions <<
"}, \"delta\": {" <<
"\"events\": " << delta.n_evts <<
", \"drops\": " << delta.n_drops <<
", \"preemptions\": " << delta.n_preemptions <<
"}, \"drop_pct\": " << (delta.n_evts == 0 ? 0 : (100.0*delta.n_drops/delta.n_evts)) <<
"}," << endl;

m_last_stats = cstats;
}
Expand Down
1 change: 0 additions & 1 deletion userspace/falco/statsfilewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ class StatsFileWriter {
uint32_t m_num_stats;
std::shared_ptr<sinsp> m_inspector;
std::ofstream m_output;
std::string m_extra;
scap_stats m_last_stats;
};