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

chore(userspace/libsinsp): keep a reference on m_sinsp_stats_v2 where needed #1910

Merged
merged 5 commits into from
Jun 25, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/perf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
- name: Download latest master report
uses: dawidd6/action-download-artifact@v6
with:
search_artifacts: true
branch: master
event: push
name: perf_report
workflow: pages.yml

- name: Diff from master - perf unit tests
run: |
Expand Down Expand Up @@ -132,4 +132,4 @@ jobs:
tail -n 6 heaptrack_scap_diff.txt | grep "total memory leaked" | awk -F': ' '{print $2 }' | tr '.' ',' | numfmt --from=iec | awk '{if (substr($1,RSTART+RLENGTH)+0 > 0) print }' &> heaptrack_scap_diff_above_thresh.txt
if [ -s heaptrack_scap_diff_above_thresh.txt ]; then
exit 2
fi
fi
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 @@ -298,6 +298,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 @@ -308,9 +316,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 @@ -322,17 +330,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 @@ -369,9 +377,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 @@ -440,19 +448,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
Loading