diff mbox series

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

Message ID 20240112230018.34044-2-frode.nordahl@canonical.com
State Changes Requested
Delegated to: Ilya Maximets
Headers show
Series Introduce cooperative multitasking to improve OVSDB RAFT cluster operation. | expand

Checks

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

Commit Message

Frode Nordahl Jan. 12, 2024, 11 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(-)

Comments

Ilya Maximets Jan. 16, 2024, 8:20 p.m. UTC | #1
On 1/13/24 00:00, Frode Nordahl wrote:
> 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(-)

Thanks, Frode!  Looks good in general, couple of nits below.

> 
> diff --git a/lib/timeval.c b/lib/timeval.c
> index 193c7bab1..f9d442818 100644
> --- a/lib/timeval.c
> +++ b/lib/timeval.c
> @@ -763,17 +763,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);
>  }
>  
> @@ -814,6 +819,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 */

Nit: Period.

> +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..d71279740 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);

Nit: Would be better to add an argument name here, since the type
     is not descriptive.

> +
>  #ifdef  __cplusplus
>  }
>  #endif
diff mbox series

Patch

diff --git a/lib/timeval.c b/lib/timeval.c
index 193c7bab1..f9d442818 100644
--- a/lib/timeval.c
+++ b/lib/timeval.c
@@ -763,17 +763,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);
 }
 
@@ -814,6 +819,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..d71279740 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);
+
 #ifdef  __cplusplus
 }
 #endif