public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v3 4/6] lib: new restore_wallclock field to restore realtime clock
Date: Thu, 24 Jan 2019 16:12:37 +0100	[thread overview]
Message-ID: <20190124151237.GB16804@rei.lan> (raw)
In-Reply-To: <20181212203723.18810-4-rafael.tinoco@linaro.org>

Hi!
> Some tests around clocks need to restore the correct date and time after
> the tests, including possible iterations, run.
> 
> This commit introduces a new field to tst_test called
> "restore_wallclock", which makes the test to save current realtime clock
> during setup phase, and, later, during cleanup, restore it to the
> appropriate time using a monotonic raw clock difference.
> 
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
> ---
>  include/tst_test.h  |  1 +
>  lib/tst_test.c      |  6 ++++++
>  lib/tst_wallclock.c | 44 ++++++++++++++++++++++++++++++++++++++++++++

We need to add include/tst_wallclock.h for the function prototypes which
should be then included in the tst_test.c.

And we should definitelly add a paragraph about this functionality into
the doc/test-writing-guidelines.txt.

>  3 files changed, 51 insertions(+)
>  create mode 100644 lib/tst_wallclock.c
> 
> diff --git a/include/tst_test.h b/include/tst_test.h
> index 2ebf746eb..170bddc21 100644
> --- a/include/tst_test.h
> +++ b/include/tst_test.h
> @@ -131,6 +131,7 @@ struct tst_test {
>  	int needs_rofs:1;
>  	int child_needs_reinit:1;
>  	int needs_devfs:1;
> +	int restore_wallclock:1;
>  	/*
>  	 * If set the test function will be executed for all available
>  	 * filesystems and the current filesytem type would be set in the
> diff --git a/lib/tst_test.c b/lib/tst_test.c
> index 661fbbfce..aa3d674f0 100644
> --- a/lib/tst_test.c
> +++ b/lib/tst_test.c
> @@ -868,6 +868,9 @@ static void do_setup(int argc, char *argv[])
>  
>  	if (tst_test->resource_files)
>  		copy_resources();
> +
> +	if (tst_test->restore_wallclock)
> +		tst_wallclock_save();
>  }
>  
>  static void do_test_setup(void)
> @@ -899,6 +902,9 @@ static void do_cleanup(void)
>  		tst_sys_conf_restore(0);
>  
>  	cleanup_ipc();
> +
> +	if (tst_test->restore_wallclock)
> +		tst_wallclock_restore();
>  }
>  
>  static void run_tests(void)
> diff --git a/lib/tst_wallclock.c b/lib/tst_wallclock.c
> new file mode 100644
> index 000000000..ef08e1dba
> --- /dev/null
> +++ b/lib/tst_wallclock.c
> @@ -0,0 +1,44 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +#include <errno.h>
> +
> +#define TST_NO_DEFAULT_MAIN
> +
> +#include "tst_test.h"
> +#include "tst_timer.h"
> +#include "tst_clocks.h"
> +#include "lapi/posix_clocks.h"
> +
> +static struct timespec real_begin, mono_begin;
> +
> +void tst_wallclock_save(void)
> +{
> +	/* save initial monotonic time to restore it when needed */
> +
> +	if (tst_clock_gettime(CLOCK_REALTIME, &real_begin))
> +		tst_brk(TBROK | TERRNO, "tst_clock_gettime() realtime failed");
> +
> +	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin))
> +		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
> +}
> +
> +void tst_wallclock_restore(void)
> +{
> +	static struct timespec mono_end, elapsed, adjust;
> +
> +	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end))
> +		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
> +
> +	elapsed = tst_timespec_diff(mono_end, mono_begin);
> +
> +	adjust = tst_timespec_add_us(real_begin, tst_timespec_to_us(elapsed));

It may be a bit cleaner to add tst_timespec_add() so that we do not have
to convert the value between ns and us back and forth, but that is very
minor as well.

> +	/* restore realtime clock based on monotonic delta */
> +
> +	if (tst_clock_settime(CLOCK_REALTIME, &adjust))
> +		tst_brk(TBROK | TERRNO, "tst_clock_settime() realtime failed");
> +}
> -- 
> 2.20.0.rc1
> 

-- 
Cyril Hrubis
chrubis@suse.cz

  reply	other threads:[~2019-01-24 15:12 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-05 19:30 [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Rafael David Tinoco
2018-12-05 19:30 ` [LTP] [PATCH 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
2018-12-06 13:03 ` [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Cyril Hrubis
2018-12-06 14:49   ` Rafael David Tinoco
2018-12-06 19:07   ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
2018-12-06 19:07     ` [LTP] [PATCH v2 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
2018-12-06 19:11     ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
2018-12-11 14:27     ` Cyril Hrubis
2018-12-11 16:05       ` Rafael David Tinoco
2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 2/6] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 3/6] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 4/6] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
2019-01-24 15:12           ` Cyril Hrubis [this message]
2018-12-12 20:37         ` [LTP] [PATCH v3 5/6] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
2018-12-12 20:46           ` Rafael David Tinoco
2019-01-24 16:11           ` Cyril Hrubis
2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 2/8] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 3/8] tst_timer: Add tst_timespec_add() Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 4/8] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
2019-01-30 13:53                 ` Cyril Hrubis
2019-01-29 17:36               ` [LTP] [PATCH v4 5/8] tst_timer: Add tst_timespec_sub_us() Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 6/8] tst_timer: Turn clock_name() function public Rafael David Tinoco
2019-01-30 13:54                 ` Cyril Hrubis
2019-01-29 17:36               ` [LTP] [PATCH v4 7/8] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
2019-01-30 13:56                 ` Cyril Hrubis
2019-01-29 17:36               ` [LTP] [PATCH v4 8/8] timers/clock_settime: remove " Rafael David Tinoco
2019-01-30 13:50               ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Cyril Hrubis
2019-01-30 14:50                 ` Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
2019-01-08 12:04           ` Rafael David Tinoco
2019-01-08 12:42             ` Cyril Hrubis
2019-01-08 12:46               ` Rafael David Tinoco
2019-01-24 12:58             ` Rafael David Tinoco
2019-01-24 13:18               ` Cyril Hrubis
2019-01-24 15:06         ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190124151237.GB16804@rei.lan \
    --to=chrubis@suse.cz \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox