Skip to content

Commit

Permalink
new(sinsp): introduce a common print stats function
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Terzolo <andreaterzolo3@gmail.com>
  • Loading branch information
Andreagit97 committed Aug 21, 2023
1 parent b548353 commit 6024bda
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 40 deletions.
11 changes: 11 additions & 0 deletions userspace/libsinsp/capture_stats_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.

#include "sinsp_public.h"
#include <stdint.h>
#include "logger.h"

struct scap_stats;

Expand Down Expand Up @@ -45,6 +46,16 @@ class SINSP_PUBLIC capture_stats_source
*/
virtual void get_capture_stats(scap_stats* stats) const = 0;

/**
* Print a log with statistics about the currently
* open capture.
*
* @note This may not work for a file-based capture source.
*
* @param[in] sev severity used to log
*/
virtual void print_capture_stats(sinsp_logger::severity sev) const = 0;

/**
* Get engine statistics (including counters and `bpftool prog show` like stats).
*
Expand Down
90 changes: 50 additions & 40 deletions userspace/libsinsp/sinsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1378,46 +1378,7 @@ int32_t sinsp::next(OUT sinsp_evt **puevt)
{
if(m_next_stats_print_time_ns)
{
scap_stats stats;
get_capture_stats(&stats);

g_logger.format(sinsp_logger::SEV_DEBUG,
"n_evts:%" PRIu64
" n_drops:%" PRIu64
" n_drops_buffer:%" PRIu64
" n_drops_buffer_clone_fork_enter:%" PRIu64
" n_drops_buffer_clone_fork_exit:%" PRIu64
" n_drops_buffer_execve_enter:%" PRIu64
" n_drops_buffer_execve_exit:%" PRIu64
" n_drops_buffer_connect_enter:%" PRIu64
" n_drops_buffer_connect_exit:%" PRIu64
" n_drops_buffer_open_enter:%" PRIu64
" n_drops_buffer_open_exit:%" PRIu64
" n_drops_buffer_dir_file_enter:%" PRIu64
" n_drops_buffer_dir_file_exit:%" PRIu64
" n_drops_buffer_other_interest_enter:%" PRIu64
" n_drops_buffer_other_interest_exit:%" PRIu64
" n_drops_scratch_map:%" PRIu64
" n_drops_pf:%" PRIu64
" n_drops_bug:%" PRIu64,
stats.n_evts,
stats.n_drops,
stats.n_drops_buffer,
stats.n_drops_buffer_clone_fork_enter,
stats.n_drops_buffer_clone_fork_exit,
stats.n_drops_buffer_execve_enter,
stats.n_drops_buffer_execve_exit,
stats.n_drops_buffer_connect_enter,
stats.n_drops_buffer_connect_exit,
stats.n_drops_buffer_open_enter,
stats.n_drops_buffer_open_exit,
stats.n_drops_buffer_dir_file_enter,
stats.n_drops_buffer_dir_file_exit,
stats.n_drops_buffer_other_interest_enter,
stats.n_drops_buffer_other_interest_exit,
stats.n_drops_scratch_map,
stats.n_drops_pf,
stats.n_drops_bug);
print_capture_stats(sinsp_logger::SEV_DEBUG);
}

m_next_stats_print_time_ns = ts - (ts % ONE_SECOND_IN_NS) + ONE_SECOND_IN_NS;
Expand Down Expand Up @@ -1967,6 +1928,55 @@ void sinsp::get_capture_stats(scap_stats* stats) const
scap_get_stats(m_h, stats);
}

void sinsp::print_capture_stats(sinsp_logger::severity sev) const
{
scap_stats stats;
get_capture_stats(&stats);

g_logger.format(sev,
"\nn_evts:%" PRIu64
"\nn_drops:%" PRIu64
"\nn_drops_buffer:%" PRIu64
"\nn_drops_buffer_clone_fork_enter:%" PRIu64
"\nn_drops_buffer_clone_fork_exit:%" PRIu64
"\nn_drops_buffer_execve_enter:%" PRIu64
"\nn_drops_buffer_execve_exit:%" PRIu64
"\nn_drops_buffer_connect_enter:%" PRIu64
"\nn_drops_buffer_connect_exit:%" PRIu64
"\nn_drops_buffer_open_enter:%" PRIu64
"\nn_drops_buffer_open_exit:%" PRIu64
"\nn_drops_buffer_dir_file_enter:%" PRIu64
"\nn_drops_buffer_dir_file_exit:%" PRIu64
"\nn_drops_buffer_other_interest_enter:%" PRIu64
"\nn_drops_buffer_other_interest_exit:%" PRIu64
"\nn_drops_buffer_close_exit:%" PRIu64
"\nn_drops_buffer_proc_exit:%" PRIu64
"\nn_drops_scratch_map:%" PRIu64
"\nn_drops_pf:%" PRIu64
"\nn_drops_bug:%" PRIu64
"\n",
stats.n_evts,
stats.n_drops,
stats.n_drops_buffer,
stats.n_drops_buffer_clone_fork_enter,
stats.n_drops_buffer_clone_fork_exit,
stats.n_drops_buffer_execve_enter,
stats.n_drops_buffer_execve_exit,
stats.n_drops_buffer_connect_enter,
stats.n_drops_buffer_connect_exit,
stats.n_drops_buffer_open_enter,
stats.n_drops_buffer_open_exit,
stats.n_drops_buffer_dir_file_enter,
stats.n_drops_buffer_dir_file_exit,
stats.n_drops_buffer_other_interest_enter,
stats.n_drops_buffer_other_interest_exit,
stats.n_drops_buffer_close_exit,
stats.n_drops_buffer_proc_exit,
stats.n_drops_scratch_map,
stats.n_drops_pf,
stats.n_drops_bug);
}

const struct scap_stats_v2* sinsp::get_capture_stats_v2(uint32_t flags, uint32_t* nstats, int32_t* rc) const
{
/* On purpose ignoring failures to not interrupt in case of stats retrieval failure. */
Expand Down
6 changes: 6 additions & 0 deletions userspace/libsinsp/sinsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,12 @@ class SINSP_PUBLIC sinsp : public capture_stats_source
*/
void get_capture_stats(scap_stats* stats) const override;

/*!
\brief Print a log with statistics about the currently
open capture. Use the severity specified as the first parameter.
*/
void print_capture_stats(sinsp_logger::severity sev) const override;

/*!
\brief Get engine statistics (including counters and `bpftool prog show` like stats).
Expand Down

0 comments on commit 6024bda

Please sign in to comment.