Skip to content

Commit

Permalink
nlbwmon: utilize uloop interval timer if available
Browse files Browse the repository at this point in the history
When available, use uloop interval timers instead of using potentially
drifting, self-rearming timeouts for scheduling regular tasks.

Signed-off-by: Jo-Philipp Wich <[email protected]>
  • Loading branch information
jow- committed Feb 21, 2024
1 parent 0ef61c3 commit c7616bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.0)
include(CheckFunctionExists)

project(nlbwmon C)
add_definitions(-Os -Wall -Werror --std=gnu99 -g3 -Wmissing-declarations -D_GNU_SOURCE)
Expand All @@ -22,6 +23,14 @@ else()
include_directories(${LIBNL_LIBRARY_INCLUDE_DIR})
endif()

set(CMAKE_REQUIRED_LIBRARIES ubox)
check_function_exists(uloop_interval_set INTERVAL_FUNCTION_EXISTS)
unset(CMAKE_REQUIRED_LIBRARIES)

if (INTERVAL_FUNCTION_EXISTS)
add_definitions(-DHAVE_ULOOP_INTERVAL)
endif()

target_link_libraries(nlbwmon ubox z)

set(CMAKE_INSTALL_PREFIX /usr)
Expand Down
26 changes: 18 additions & 8 deletions nlbwmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,18 @@
#include "client.h"
#include "utils.h"

static struct uloop_timeout commit_tm = { };
static struct uloop_timeout refresh_tm = { };
#ifdef HAVE_ULOOP_INTERVAL
# define uloop_timer_type uloop_interval
# define uloop_timer_set(tm, ms) uloop_interval_set(tm, ms)
# define uloop_timer_reset(tm, ms)
#else
# define uloop_timer_type uloop_timeout
# define uloop_timer_set(tm, ms) uloop_timeout_set(tm, ms)
# define uloop_timer_reset(tm, ms) uloop_timeout_set(tm, ms)
#endif

static struct uloop_timer_type commit_tm = { };
static struct uloop_timer_type refresh_tm = { };

struct options opt = {
.commit_interval = 86400,
Expand Down Expand Up @@ -116,20 +126,20 @@ static void handle_shutdown(int sig)
}

static void
handle_commit(struct uloop_timeout *tm)
handle_commit(struct uloop_timer_type *tm)
{
uint32_t timestamp = interval_timestamp(&opt.archive_interval, 0);

uloop_timeout_set(tm, opt.commit_interval * 1000);
uloop_timer_reset(tm, opt.commit_interval * 1000);
save_persistent(timestamp);
}

static void
handle_refresh(struct uloop_timeout *tm)
handle_refresh(struct uloop_timer_type *tm)
{
int err;

uloop_timeout_set(tm, opt.refresh_interval * 1000);
uloop_timer_reset(tm, opt.refresh_interval * 1000);

err = database_archive(gdbh);

Expand Down Expand Up @@ -354,10 +364,10 @@ server_main(int argc, char **argv)
sigaction(SIGUSR1, &sa, NULL);

commit_tm.cb = handle_commit;
uloop_timeout_set(&commit_tm, opt.commit_interval * 1000);
uloop_timer_set(&commit_tm, opt.commit_interval * 1000);

refresh_tm.cb = handle_refresh;
uloop_timeout_set(&refresh_tm, opt.refresh_interval * 1000);
uloop_timer_set(&refresh_tm, opt.refresh_interval * 1000);

uloop_run();

Expand Down

0 comments on commit c7616bc

Please sign in to comment.