Skip to content

Commit

Permalink
cleanup(userspace/libsinsp): small perf improvements.
Browse files Browse the repository at this point in the history
Properly keep a reference on m_sinsp_stats_v2 where needed, instead of fetching it every time.
Moreover, improve perf in `sinsp_utils::ts_to_string`: cache `gmt2local` result instead of fetching it every time as it is an heavy operation.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Jun 14, 2024
1 parent e3d0ab8 commit 1572c78
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 160 deletions.
18 changes: 13 additions & 5 deletions userspace/libsinsp/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ sinsp_container_manager::sinsp_container_manager(sinsp* inspector, bool static_c
m_static_image(static_image),
m_container_engine_mask(~0ULL)
{
if (m_inspector != nullptr)
{
m_sinsp_stats_v2 = m_inspector->get_sinsp_stats_v2();
}
else
{
m_sinsp_stats_v2 = nullptr;
}
}

bool sinsp_container_manager::remove_inactive_containers()
Expand Down Expand Up @@ -83,22 +91,22 @@ bool sinsp_container_manager::remove_inactive_containers()
});

auto containers = m_containers.lock();
if (m_inspector != nullptr && m_inspector->get_sinsp_stats_v2())
if (m_sinsp_stats_v2 != nullptr)
{
m_inspector->get_sinsp_stats_v2()->m_n_missing_container_images = 0;
m_sinsp_stats_v2->m_n_missing_container_images = 0;
// Will include pod sanboxes, but that's ok
m_inspector->get_sinsp_stats_v2()->m_n_containers = containers->size();
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 != nullptr && m_inspector->get_sinsp_stats_v2())
if (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->get_sinsp_stats_v2()->m_n_missing_container_images++;
m_sinsp_stats_v2->m_n_missing_container_images++;
}
}
if(containers_in_use.find(it->first) == containers_in_use.end())
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class sinsp_container_manager :
std::map<sinsp_container_type, std::shared_ptr<libsinsp::container_engine::container_engine_base>> m_container_engine_by_type;

sinsp* m_inspector;
std::shared_ptr<sinsp_stats_v2> m_sinsp_stats_v2;
libsinsp::Mutex<std::unordered_map<std::string, std::shared_ptr<const sinsp_container_info>>> m_containers;
std::unordered_map<std::string, std::unordered_map<sinsp_container_type, sinsp_container_lookup::state>> m_lookups;
std::list<new_container_cb> m_new_callbacks;
Expand Down
36 changes: 22 additions & 14 deletions userspace/libsinsp/fdinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ sinsp_fdtable::sinsp_fdtable(sinsp* inspector)
{
m_tid = 0;
m_inspector = inspector;
if (m_inspector != nullptr)
{
m_sinsp_stats_v2 = m_inspector->get_sinsp_stats_v2();
}
else
{
m_sinsp_stats_v2 = nullptr;
}
reset_cache();
}

Expand All @@ -292,9 +300,9 @@ inline std::shared_ptr<sinsp_fdinfo> sinsp_fdtable::find_ref(int64_t fd)
//
if(m_last_accessed_fd != -1 && fd == m_last_accessed_fd)
{
if (m_inspector != nullptr && m_inspector->get_sinsp_stats_v2())
if (m_sinsp_stats_v2)
{
m_inspector->get_sinsp_stats_v2()->m_n_cached_fd_lookups++;
m_sinsp_stats_v2->m_n_cached_fd_lookups++;
}
return m_last_accessed_fdinfo;
}
Expand All @@ -306,17 +314,17 @@ inline std::shared_ptr<sinsp_fdinfo> sinsp_fdtable::find_ref(int64_t fd)

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

m_last_accessed_fd = fd;
Expand Down Expand Up @@ -353,9 +361,9 @@ inline std::shared_ptr<sinsp_fdinfo> sinsp_fdtable::add_ref(int64_t fd, std::uni
// No entry in the table, this is the normal case
//
m_last_accessed_fd = -1;
if (m_inspector != nullptr && m_inspector->get_sinsp_stats_v2())
if (m_sinsp_stats_v2 != nullptr)
{
m_inspector->get_sinsp_stats_v2()->m_n_added_fds++;
m_sinsp_stats_v2->m_n_added_fds++;
}

return m_table.emplace(fd, std::move(fdinfo)).first->second;
Expand Down Expand Up @@ -424,19 +432,19 @@ bool sinsp_fdtable::erase(int64_t fd)
// call that created this fd. The assertion will detect it, while in release mode we just
// keep going.
//
if (m_inspector != nullptr && m_inspector->get_sinsp_stats_v2())
if (m_sinsp_stats_v2 != nullptr)
{
m_inspector->get_sinsp_stats_v2()->m_n_failed_fd_lookups++;
m_sinsp_stats_v2->m_n_failed_fd_lookups++;
}
return false;
}
else
{
m_table.erase(fdit);
if (m_inspector != nullptr && m_inspector->get_sinsp_stats_v2())
if (m_sinsp_stats_v2 != nullptr)
{
m_inspector->get_sinsp_stats_v2()->m_n_noncached_fd_lookups++;
m_inspector->get_sinsp_stats_v2()->m_n_removed_fds++;
m_sinsp_stats_v2->m_n_noncached_fd_lookups++;
m_sinsp_stats_v2->m_n_removed_fds++;
}
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions userspace/libsinsp/fdinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ class SINSP_PUBLIC sinsp_fdinfo : public libsinsp::state::table_entry

/*@}*/

// Forward declare sinsp_stats_v2 to avoid including metrics_collector.h here.
struct sinsp_stats_v2;

///////////////////////////////////////////////////////////////////////////////
// fd info table
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -547,6 +550,7 @@ class sinsp_fdtable : public libsinsp::state::table<int64_t>
private:
sinsp* m_inspector;
std::unordered_map<int64_t, std::shared_ptr<sinsp_fdinfo>> m_table;
std::shared_ptr<sinsp_stats_v2> m_sinsp_stats_v2;

//
// Simple fd cache
Expand Down
Loading

0 comments on commit 1572c78

Please sign in to comment.