summaryrefslogtreecommitdiff
path: root/src/libvmon/vmon.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2021-09-09 19:25:41 -0700
committerVito Caputo <vcaputo@pengaru.com>2021-09-09 19:40:15 -0700
commitb2d4629d1776dbdef9924a0ded29e733de150ccc (patch)
treedffbd58a579ec25081b60cb61dc3b6a8cf2ddc09 /src/libvmon/vmon.c
parenteac2122b04b68f4abf09d2584f96c8fd8c67a144 (diff)
lbvmon: expose clock_gettime(CLOCK_BOOTTIME)
Exposed as VMON_SYS_STAT_BOOTTIME, so part of VMON_WANT_SYS_STAT, in units of ticks to normalize with SYS_STAT_CPU* times. This also introduces vmon->ticks_per_sec, which callers can access as well for convenience since vmon_t is all public and this library doesn't aspire to keep anything private. It's initialized via sysconf(_SC_CLK_TCK) @ vmon_init().
Diffstat (limited to 'src/libvmon/vmon.c')
-rw-r--r--src/libvmon/vmon.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/libvmon/vmon.c b/src/libvmon/vmon.c
index 78ad905..83d78c4 100644
--- a/src/libvmon/vmon.c
+++ b/src/libvmon/vmon.c
@@ -28,6 +28,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
+#include <time.h>
#include <inttypes.h>
#include <dirent.h>
#include <errno.h>
@@ -864,9 +865,11 @@ typedef enum _vmon_sys_stat_fsm_t {
/* system-wide stat sampling, things like CPU usages, stuff in /proc/stat */
static sample_ret_t sys_sample_stat(vmon_t *vmon, vmon_sys_stat_t **store)
{
- int i, len, total = 0;
- int changes = 0;
- vmon_sys_stat_fsm_t state = VMON_PARSER_STATE_SYS_STAT_CPU_PREFIX; /* this could be defined as the "VMON_PARSER_INITIAL_STATE" */
+ int i, len, total = 0;
+ int changes = 0;
+ vmon_sys_stat_fsm_t state = VMON_PARSER_STATE_SYS_STAT_CPU_PREFIX; /* this could be defined as the "VMON_PARSER_INITIAL_STATE" */
+ struct timespec ts;
+ typeof((*store)->boottime) boottime;
#define VMON_PREPARE_PARSER
#include "defs/sys_stat.def"
@@ -880,6 +883,20 @@ static sample_ret_t sys_sample_stat(vmon_t *vmon, vmon_sys_stat_t **store)
(*store)->stat_fd = openat(dirfd(vmon->proc_dir), "stat", O_RDONLY);
}
+#ifndef NSEC_PER_SEC
+#define NSEC_PER_SEC 1000000000
+#endif
+
+ /* VMON_SYS_STAT_BOOTTIME */
+ clock_gettime(CLOCK_BOOTTIME, &ts);
+ boottime = ts.tv_sec * vmon->ticks_per_sec;
+ boottime += ts.tv_nsec * vmon->ticks_per_sec / NSEC_PER_SEC;
+ if ((*store)->boottime != boottime) {
+ (*store)->boottime = boottime;
+ BITSET((*store)->changed, VMON_SYS_STAT_BOOTTIME);
+ changes++;
+ }
+
while ((len = try_pread((*store)->stat_fd, vmon->buf, sizeof(vmon->buf), total)) > 0) {
total += len;
@@ -983,6 +1000,7 @@ int vmon_init(vmon_t *vmon, vmon_flags_t flags, vmon_sys_wants_t sys_wants, vmon
vmon->flags = flags;
vmon->sys_wants = sys_wants;
vmon->proc_wants = proc_wants;
+ vmon->ticks_per_sec = sysconf(_SC_CLK_TCK);
/* here we populate the sys and proc function tables */
#define vmon_want(_sym, _name, _func) \
© All Rights Reserved