Skip to content

Commit

Permalink
cleanup(libsinsp, libscap): move boot_ts_epoch to scap m_machine_info
Browse files Browse the repository at this point in the history
Co-authored-by: Luca Guerra <[email protected]>
Signed-off-by: Melissa Kilby <[email protected]>
  • Loading branch information
incertum and LucaGuerra committed Dec 2, 2022
1 parent 140f612 commit fa80a6c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 35 deletions.
40 changes: 36 additions & 4 deletions userspace/libscap/scap.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@ const char* scap_getlasterr(scap_t* handle)
return handle ? handle->m_lasterr : "null scap handle";
}

uint64_t scap_get_current_time_ns()
{
struct timeval tv;
gettimeofday(&tv, NULL);

return tv.tv_sec * (uint64_t) 1000000000 + tv.tv_usec * 1000;
}

uint64_t scap_get_host_boot_time_ns()
{
char proc_dir[PPM_MAX_PATH_SIZE];
struct stat targetstat;

snprintf(proc_dir, sizeof(proc_dir), "%s/proc/1/", scap_get_host_root());
if (stat(proc_dir, &targetstat) == 0)
{
// This approach is constant between agent re-boots
return targetstat.st_ctim.tv_sec * (uint64_t) 1000000000 + targetstat.st_ctim.tv_nsec;
}

// Fall-back method from scap_bpf
struct timespec ts_uptime;
uint64_t now;
uint64_t uptime;

now = scap_get_current_time_ns();
clock_gettime(CLOCK_BOOTTIME, &ts_uptime);
uptime = ts_uptime.tv_sec * (uint64_t) 1000000000 + ts_uptime.tv_nsec;

return (now - uptime);
}

#if defined(HAS_ENGINE_KMOD) || defined(HAS_ENGINE_BPF) || defined(HAS_ENGINE_MODERN_BPF)
scap_t* scap_open_live_int(char *error, int32_t *rc, scap_open_args* oargs, const struct scap_vtable* vtable)
{
Expand Down Expand Up @@ -86,7 +118,7 @@ scap_t* scap_open_live_int(char *error, int32_t *rc, scap_open_args* oargs, cons
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);
gethostname(handle->m_machine_info.hostname, sizeof(handle->m_machine_info.hostname) / sizeof(handle->m_machine_info.hostname[0]));
handle->m_machine_info.reserved1 = 0;
handle->m_machine_info.boot_ts_epoch = scap_get_host_boot_time_ns();
handle->m_machine_info.reserved2 = 0;
handle->m_machine_info.reserved3 = 0;
handle->m_machine_info.reserved4 = 0;
Expand Down Expand Up @@ -213,7 +245,7 @@ scap_t* scap_open_udig_int(char *error, int32_t *rc, scap_open_args *oargs)
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);
gethostname(handle->m_machine_info.hostname, sizeof(handle->m_machine_info.hostname) / sizeof(handle->m_machine_info.hostname[0]));
handle->m_machine_info.reserved1 = 0;
handle->m_machine_info.boot_ts_epoch = scap_get_host_boot_time_ns();
handle->m_machine_info.reserved2 = 0;
handle->m_machine_info.reserved3 = 0;
handle->m_machine_info.reserved4 = 0;
Expand Down Expand Up @@ -524,7 +556,7 @@ scap_t* scap_open_nodriver_int(char *error, int32_t *rc,
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);
gethostname(handle->m_machine_info.hostname, sizeof(handle->m_machine_info.hostname) / sizeof(handle->m_machine_info.hostname[0]));
handle->m_machine_info.reserved1 = 0;
handle->m_machine_info.boot_ts_epoch = scap_get_host_boot_time_ns();
handle->m_machine_info.reserved2 = 0;
handle->m_machine_info.reserved3 = 0;
handle->m_machine_info.reserved4 = 0;
Expand Down Expand Up @@ -621,7 +653,7 @@ scap_t* scap_open_plugin_int(char *error, int32_t *rc, scap_open_args* oargs)
handle->m_machine_info.memory_size_bytes = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
#endif
gethostname(handle->m_machine_info.hostname, sizeof(handle->m_machine_info.hostname) / sizeof(handle->m_machine_info.hostname[0]));
handle->m_machine_info.reserved1 = 0;
handle->m_machine_info.boot_ts_epoch = 0; // plugin does not need boot_ts_epoch
handle->m_machine_info.reserved2 = 0;
handle->m_machine_info.reserved3 = 0;
handle->m_machine_info.reserved4 = 0;
Expand Down
12 changes: 11 additions & 1 deletion userspace/libscap/scap.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ typedef struct _scap_machine_info
uint64_t memory_size_bytes; ///< Physical memory size
uint64_t max_pid; ///< Highest PID number on this machine
char hostname[128]; ///< The machine hostname
uint64_t reserved1; ///< reserved for future use
uint64_t boot_ts_epoch; ///< Host boot ts in nanoseconds (epoch)
uint64_t reserved2; ///< reserved for future use
uint64_t reserved3; ///< reserved for future use
uint64_t reserved4; ///< reserved for future use
Expand Down Expand Up @@ -598,6 +598,16 @@ scap_os_platform scap_get_os_platform(scap_t* handle);
*/
const char* scap_getlasterr(scap_t* handle);

/*!
\brief Return current time in nanoseconds.
*/
uint64_t scap_get_current_time_ns();

/*!
\brief Return host boot ts in nanoseconds (epoch).
*/
uint64_t scap_get_host_boot_time_ns();

/*!
* \brief returns the maximum amount of memory used by any driver queue
*/
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1647,10 +1647,10 @@ void sinsp_parser::parse_clone_exit(sinsp_evt *evt)
ASSERT(parinfo->m_len == sizeof(uint64_t));
if (tinfo->m_vpid != tinfo->m_pid)
{
tinfo->m_pidns_init_start_ts = *(uint64_t *)parinfo->m_val + m_inspector->m_boot_ts_epoch;
tinfo->m_pidns_init_start_ts = *(uint64_t *)parinfo->m_val + m_inspector->m_machine_info->boot_ts_epoch;
} else
{
tinfo->m_pidns_init_start_ts = m_inspector->m_boot_ts_epoch;
tinfo->m_pidns_init_start_ts = m_inspector->m_machine_info->boot_ts_epoch;
}
}

Expand Down
2 changes: 0 additions & 2 deletions userspace/libsinsp/sinsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,6 @@ void sinsp::init()
m_num_cpus = 0;
}

m_boot_ts_epoch = sinsp_utils::get_host_boot_time_ns();

//
// XXX
// This will need to be integrated in the machine info
Expand Down
1 change: 0 additions & 1 deletion userspace/libsinsp/sinsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,6 @@ VISIBILITY_PRIVATE
bool m_is_dumping;
bool m_filter_proc_table_when_saving;
const scap_machine_info* m_machine_info;
uint64_t m_boot_ts_epoch;
uint32_t m_num_cpus;
sinsp_thread_privatestate_manager m_thread_privatestate_manager;
bool m_is_tracers_capture_enabled;
Expand Down
24 changes: 0 additions & 24 deletions userspace/libsinsp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,30 +858,6 @@ uint64_t sinsp_utils::get_current_time_ns()
return tv.tv_sec * (uint64_t) 1000000000 + tv.tv_usec * 1000;
}

uint64_t sinsp_utils::get_host_boot_time_ns()
{
char proc_dir[PPM_MAX_PATH_SIZE];
struct stat targetstat;

snprintf(proc_dir, sizeof(proc_dir), "%s/proc/1/", scap_get_host_root());
if (stat(proc_dir, &targetstat) == 0)
{
// This approach is constant between agent re-boots
return targetstat.st_ctim.tv_sec * (uint64_t) 1000000000 + targetstat.st_ctim.tv_nsec;
}

// Fall-back method from scap_bpf
struct timespec ts_uptime;
uint64_t now;
uint64_t uptime;

now = sinsp_utils::get_current_time_ns();
clock_gettime(CLOCK_BOOTTIME, &ts_uptime);
uptime = ts_uptime.tv_sec * (uint64_t) 1000000000 + ts_uptime.tv_nsec;

return (now - uptime);
}

bool sinsp_utils::glob_match(const char *pattern, const char *string)
{
#ifdef _WIN32
Expand Down
1 change: 0 additions & 1 deletion userspace/libsinsp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class sinsp_utils
static void get_filtercheck_fields_info(std::vector<const filter_check_info*>& list);

static uint64_t get_current_time_ns();
static uint64_t get_host_boot_time_ns();

static bool glob_match(const char *pattern, const char *string);

Expand Down

0 comments on commit fa80a6c

Please sign in to comment.