diff mbox series

[ovs-dev,v3,1/5] timeval: Add internal timewarp interface.

Message ID 20240116225205.38112-2-frode.nordahl@canonical.com
State Accepted
Commit 6ece3d57b279e276ebf8b69c36d9ae545ee1d2e8
Delegated to: Ilya Maximets
Headers show
Series Introduce cooperative multitasking to improve OVSDB RAFT cluster operation. | expand

Checks

Context Check Description
ovsrobot/intel-ovs-compilation fail test: fail
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed

Commit Message

Frode Nordahl Jan. 16, 2024, 10:52 p.m. UTC
It may be desirable to make use of time warp functionality in unit
tests.

Separate logic from time/stop unixctl into timeval_stop() and add
a new timeval_warp() interface for directing monotonic clock into
slow path and advancing the current monotonic directly.

This will be used in a patch that implements unit tests for the
cooperative multitasking module.

Signed-off-by: Frode Nordahl <frode.nordahl@canonical.com>
---
 lib/timeval.c | 28 ++++++++++++++++++++++++----
 lib/timeval.h |  3 +++
 2 files changed, 27 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/lib/timeval.c b/lib/timeval.c
index 0abe7e555..10c1b9ca1 100644
--- a/lib/timeval.c
+++ b/lib/timeval.c
@@ -767,17 +767,22 @@  get_cpu_usage(void)
 
 /* "time/stop" stops the monotonic time returned by e.g. time_msec() from
  * advancing, except due to later calls to "time/warp". */
-static void
-timeval_stop_cb(struct unixctl_conn *conn,
-                 int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
-                 void *aux OVS_UNUSED)
+void
+timeval_stop(void)
 {
     ovs_mutex_lock(&monotonic_clock.mutex);
     atomic_store_relaxed(&monotonic_clock.slow_path, true);
     monotonic_clock.stopped = true;
     xclock_gettime(monotonic_clock.id, &monotonic_clock.cache);
     ovs_mutex_unlock(&monotonic_clock.mutex);
+}
 
+static void
+timeval_stop_cb(struct unixctl_conn *conn,
+                int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
+                void *aux OVS_UNUSED)
+{
+    timeval_stop();
     unixctl_command_reply(conn, NULL);
 }
 
@@ -818,6 +823,21 @@  timeval_warp_cb(struct unixctl_conn *conn,
     timewarp_work();
 }
 
+/* Direct monotonic clock into slow path and advance the current monotonic
+ * time by 'msecs' milliseconds directly.  This is for use in unit tests. */
+void
+timeval_warp(long long int msecs)
+{
+    struct clock *c = &monotonic_clock;
+    struct timespec warp;
+
+    ovs_mutex_lock(&monotonic_clock.mutex);
+    atomic_store_relaxed(&monotonic_clock.slow_path, true);
+    msec_to_timespec(msecs, &warp);
+    timespec_add(&c->warp, &c->warp, &warp);
+    ovs_mutex_unlock(&monotonic_clock.mutex);
+}
+
 void
 timeval_dummy_register(void)
 {
diff --git a/lib/timeval.h b/lib/timeval.h
index 502f703d4..1c40530e2 100644
--- a/lib/timeval.h
+++ b/lib/timeval.h
@@ -81,6 +81,9 @@  long long int time_boot_msec(void);
 
 void timewarp_run(void);
 
+void timeval_stop(void);
+void timeval_warp(long long int msecs);
+
 #ifdef  __cplusplus
 }
 #endif