summaryrefslogtreecommitdiffstats
path: root/src/kernel/tests/lib/tst_wallclock.c
blob: 282d6ada3008268b358932453817e89998248b6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2019 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 "tst_wallclock.h"
#include "lapi/posix_clocks.h"

static struct timespec real_begin, mono_begin;

static int clock_saved;

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)) {
		if (errno == EINVAL) {
			tst_brk(TCONF | TERRNO,
				"tst_clock_gettime() didn't support CLOCK_MONOTONIC_RAW");
		}

		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
	}

	clock_saved = 1;
}

void tst_wallclock_restore(void)
{
	static struct timespec mono_end, elapsed, adjust;

	if (!clock_saved)
		return;

	clock_saved = 0;

	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(real_begin, elapsed);

	/* restore realtime clock based on monotonic delta */

	if (tst_clock_settime(CLOCK_REALTIME, &adjust))
		tst_brk(TBROK | TERRNO, "tst_clock_settime() realtime failed");
}