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

cleanup(libsinsp): consolidation and extension of libsinsp stats / metrics sinsp_stats_v2 #1433

Merged
merged 10 commits into from
Nov 17, 2023
1 change: 1 addition & 0 deletions userspace/libscap/scap_stats_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern "C" {
#define PPM_SCAP_STATS_KERNEL_COUNTERS (1 << 0)
#define PPM_SCAP_STATS_LIBBPF_STATS (1 << 1)
#define PPM_SCAP_STATS_RESOURCE_UTILIZATION (1 << 2)
#define PPM_SCAP_STATS_STATE_COUNTERS (1 << 3)

typedef union scap_stats_v2_value {
uint32_t u32;
Expand Down
2 changes: 0 additions & 2 deletions userspace/libsinsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ set(SINSP_SOURCES
ifinfo.cpp
memmem.cpp
tracers.cpp
internal_metrics.cpp
logger.cpp
parsers.cpp
../plugin/plugin_loader.c
Expand Down Expand Up @@ -170,7 +169,6 @@ if(NOT MINIMAL_BUILD AND NOT EMSCRIPTEN)
list(APPEND SINSP_SOURCES
cgroup_limits.cpp
container_engine/cri.cpp
sinsp_resource_utilization.cpp
grpc_channel_registry.cpp
)
endif()
Expand Down
17 changes: 16 additions & 1 deletion userspace/libsinsp/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,26 @@ bool sinsp_container_manager::remove_inactive_containers()
});

auto containers = m_containers.lock();
if (m_inspector->m_sinsp_stats_v2)
{
m_inspector->m_sinsp_stats_v2->m_n_missing_container_images = 0;
// Will include pod sanboxes, but that's ok
m_inspector->m_sinsp_stats_v2->m_n_containers = containers->size();
}
for(auto it = containers->begin(); it != containers->end();)
{
sinsp_container_info::ptr_t container = it->second;
if (m_inspector->m_sinsp_stats_v2)
{
auto container_info = container.get();
if (!container_info || (container_info && !container_info->m_is_pod_sandbox && container_info->m_image.empty()))
{
// Only count missing container images and exclude sandboxes
m_inspector->m_sinsp_stats_v2->m_n_missing_container_images++;
}
}
if(containers_in_use.find(it->first) == containers_in_use.end())
{
sinsp_container_info::ptr_t container = it->second;
for(const auto &remove_cb : m_remove_callbacks)
{
remove_cb(*container);
Expand Down
67 changes: 57 additions & 10 deletions userspace/libsinsp/fdinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,49 @@ sinsp_fdtable::sinsp_fdtable(sinsp* inspector)
reset_cache();
}

sinsp_fdinfo_t* sinsp_fdtable::find(int64_t fd)
incertum marked this conversation as resolved.
Show resolved Hide resolved
{
std::unordered_map<int64_t, sinsp_fdinfo_t>::iterator fdit;

//
// Try looking up in our simple cache
//
if(m_last_accessed_fd != -1 && fd == m_last_accessed_fd)
{
if (m_inspector->m_sinsp_stats_v2)
{
m_inspector->m_sinsp_stats_v2->m_n_cached_fd_lookups++;
}
return m_last_accessed_fdinfo;
}

//
// Caching failed, do a real lookup
//
fdit = m_table.find(fd);

if(fdit == m_table.end())
{
if (m_inspector->m_sinsp_stats_v2)
{
m_inspector->m_sinsp_stats_v2->m_n_failed_fd_lookups++;
}
return NULL;
}
else
{
if (m_inspector->m_sinsp_stats_v2)
{
m_inspector->m_sinsp_stats_v2->m_n_noncached_fd_lookups++;
}

m_last_accessed_fd = fd;
m_last_accessed_fdinfo = &(fdit->second);
lookup_device(&(fdit->second), fd);
return &(fdit->second);
}
}

sinsp_fdinfo_t* sinsp_fdtable::add(int64_t fd, sinsp_fdinfo_t* fdinfo)
{
//
Expand All @@ -365,9 +408,11 @@ sinsp_fdinfo_t* sinsp_fdtable::add(int64_t fd, sinsp_fdinfo_t* fdinfo)
// No entry in the table, this is the normal case
//
m_last_accessed_fd = -1;
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats->m_n_added_fds++;
#endif
if (m_inspector->m_sinsp_stats_v2)
{
m_inspector->m_sinsp_stats_v2->m_n_added_fds++;
}

std::pair<std::unordered_map<int64_t, sinsp_fdinfo_t>::iterator, bool> insert_res = m_table.emplace(fd, *fdinfo);
return &(insert_res.first->second);
}
Expand Down Expand Up @@ -435,17 +480,19 @@ void sinsp_fdtable::erase(int64_t fd)
// keep going.
//
ASSERT(false);
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats->m_n_failed_fd_lookups++;
#endif
if (m_inspector->m_sinsp_stats_v2)
{
m_inspector->m_sinsp_stats_v2->m_n_failed_fd_lookups++;
}
}
else
{
m_table.erase(fdit);
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats->m_n_noncached_fd_lookups++;
m_inspector->m_stats->m_n_removed_fds++;
#endif
if (m_inspector->m_sinsp_stats_v2)
{
m_inspector->m_sinsp_stats_v2->m_n_noncached_fd_lookups++;
m_inspector->m_sinsp_stats_v2->m_n_removed_fds++;
}
}
}

Expand Down
39 changes: 1 addition & 38 deletions userspace/libsinsp/fdinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -532,44 +532,7 @@ class sinsp_fdtable
public:
sinsp_fdtable(sinsp* inspector);

inline sinsp_fdinfo_t* find(int64_t fd)
{
std::unordered_map<int64_t, sinsp_fdinfo_t>::iterator fdit;

//
// Try looking up in our simple cache
//
if(m_last_accessed_fd != -1 && fd == m_last_accessed_fd)
{
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats->m_n_cached_fd_lookups++;
#endif
return m_last_accessed_fdinfo;
}

//
// Caching failed, do a real lookup
//
fdit = m_table.find(fd);

if(fdit == m_table.end())
{
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats->m_n_failed_fd_lookups++;
#endif
return NULL;
}
else
{
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats->m_n_noncached_fd_lookups++;
#endif
m_last_accessed_fd = fd;
m_last_accessed_fdinfo = &(fdit->second);
lookup_device(&(fdit->second), fd);
return &(fdit->second);
}
}
sinsp_fdinfo_t* find(int64_t fd);

// If the key is already present, overwrite the existing value and return false.
sinsp_fdinfo_t* add(int64_t fd, sinsp_fdinfo_t* fdinfo);
Expand Down
45 changes: 0 additions & 45 deletions userspace/libsinsp/internal_metrics.cpp

This file was deleted.

151 changes: 0 additions & 151 deletions userspace/libsinsp/internal_metrics.h

This file was deleted.

Loading