Skip to content

Commit

Permalink
cleanup(libscap): scap_retrieve_machine_info function
Browse files Browse the repository at this point in the history
Co-authored-by: Grzegorz Nosek <grzegorz.nosek@sysdig.com>
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
  • Loading branch information
incertum and gnosek committed Mar 25, 2023
1 parent b34db39 commit c654917
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 41 deletions.
61 changes: 21 additions & 40 deletions userspace/libscap/scap.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,7 @@ int32_t scap_init_live_int(scap_t* handle, scap_open_args* oargs, const struct s
// Extract machine information
//

handle->m_machine_info.num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
handle->m_machine_info.memory_size_bytes = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
scap_gethostname(handle);
handle->m_machine_info.boot_ts_epoch = boot_time;
scap_get_bpf_stats_enabled(handle);
handle->m_machine_info.reserved3 = 0;
handle->m_machine_info.reserved4 = 0;
handle->m_driver_procinfo = NULL;
handle->m_fd_lookup_limit = 0;
scap_retrieve_machine_info(handle, boot_time);

//
// Extract agent information
Expand Down Expand Up @@ -207,15 +199,7 @@ int32_t scap_init_udig_int(scap_t* handle, scap_open_args* oargs)
// Extract machine information
//

handle->m_machine_info.num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
handle->m_machine_info.memory_size_bytes = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
scap_gethostname(handle);
handle->m_machine_info.boot_ts_epoch = boot_time;
scap_get_bpf_stats_enabled(handle);
handle->m_machine_info.reserved3 = 0;
handle->m_machine_info.reserved4 = 0;
handle->m_driver_procinfo = NULL;
handle->m_fd_lookup_limit = 0;
scap_retrieve_machine_info(handle, boot_time);

//
// Extract agent information
Expand Down Expand Up @@ -459,15 +443,7 @@ int32_t scap_init_nodriver_int(scap_t* handle, scap_open_args* oargs)
// Extract machine information
//

handle->m_machine_info.num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
handle->m_machine_info.memory_size_bytes = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
scap_gethostname(handle);
handle->m_machine_info.boot_ts_epoch = boot_time;
scap_get_bpf_stats_enabled(handle);
handle->m_machine_info.reserved3 = 0;
handle->m_machine_info.reserved4 = 0;
handle->m_driver_procinfo = NULL;

scap_retrieve_machine_info(handle, boot_time);
if(!engine_params || !engine_params->full_proc_scan)
{
handle->m_minimal_scan = true;
Expand Down Expand Up @@ -546,19 +522,7 @@ int32_t scap_init_plugin_int(scap_t* handle, scap_open_args* oargs)
//
// Extract machine information
//
#ifdef _WIN32
handle->m_machine_info.num_cpus = 0;
handle->m_machine_info.memory_size_bytes = 0;
#else
handle->m_machine_info.num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
handle->m_machine_info.memory_size_bytes = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
#endif
scap_gethostname(handle);
handle->m_machine_info.boot_ts_epoch = 0; // plugin does not need boot_ts_epoch
scap_get_bpf_stats_enabled(handle);
handle->m_machine_info.reserved3 = 0;
handle->m_machine_info.reserved4 = 0;
handle->m_driver_procinfo = NULL;
scap_retrieve_machine_info(handle, (uint64_t)0);
handle->m_fd_lookup_limit = SCAP_NODRIVER_MAX_FD_LOOKUP; // fd lookup is limited here because is very expensive

//
Expand Down Expand Up @@ -1265,6 +1229,23 @@ void scap_gethostname(scap_t* handle)
}
}

void scap_retrieve_machine_info(scap_t* handle, uint64_t boot_time)
{
#ifdef _WIN32
handle->m_machine_info.num_cpus = 0;
handle->m_machine_info.memory_size_bytes = 0;
#else
handle->m_machine_info.num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
handle->m_machine_info.memory_size_bytes = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
#endif
scap_gethostname(handle);
handle->m_machine_info.boot_ts_epoch = boot_time;
scap_get_bpf_stats_enabled(handle);
handle->m_machine_info.reserved3 = 0;
handle->m_machine_info.reserved4 = 0;
handle->m_driver_procinfo = NULL;
handle->m_fd_lookup_limit = 0;
}

long scap_get_hertz()
{
Expand Down
7 changes: 6 additions & 1 deletion userspace/libscap/scap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1032,10 +1032,15 @@ uint64_t scap_get_driver_schema_version(scap_t* handle);
void scap_gethostname(scap_t* handle);

/**
* Agent start ts in nanoseconds (epoch).
* Retrieve agent info.
*/
void scap_retrieve_agent_info(scap_t* handle);

/**
* Retrieve machine info.
*/
void scap_retrieve_machine_info(scap_t* handle, uint64_t boot_time);

/**
* Check is kernel.bpf_stats_enabled is set.
*/
Expand Down

0 comments on commit c654917

Please sign in to comment.