public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: gengcixi@gmail.com <gengcixi@gmail.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 2/3] lib: implement rtctime_save and rtctime_restore function
Date: Wed, 23 Dec 2020 11:35:16 +0800	[thread overview]
Message-ID: <20201223033517.1464263-3-gengcixi@gmail.com> (raw)
In-Reply-To: <20201223033517.1464263-1-gengcixi@gmail.com>

From: Cixi Geng <cixi.geng1@unisoc.com>

* Reading the RTC in the setup as well as timestamping with monotonic
  timer
* Getting a second monotonic timestamp in the cleanup
* Setting the RTC to the value taken in setup plus the difference in
  the monotonic timer timestamps

Signed-off-by: Cixi Geng <cixi.geng1@unisoc.com>
---
 include/tst_wallclock.h |  4 ++++
 lib/tst_wallclock.c     | 42 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/include/tst_wallclock.h b/include/tst_wallclock.h
index 7d6723a7a..5103c8321 100644
--- a/include/tst_wallclock.h
+++ b/include/tst_wallclock.h
@@ -12,4 +12,8 @@ void tst_wallclock_save(void);
 
 void tst_wallclock_restore(void);
 
+void tst_rtctime_save(void);
+
+void tst_rtctime_restore(void);
+
 #endif	/* TST_WALLCLK_H__ */
diff --git a/lib/tst_wallclock.c b/lib/tst_wallclock.c
index 282d6ada3..5be1f46d8 100644
--- a/lib/tst_wallclock.c
+++ b/lib/tst_wallclock.c
@@ -11,11 +11,14 @@
 #include "tst_test.h"
 #include "tst_timer.h"
 #include "tst_clocks.h"
+#include "tst_rtctime.h"
 #include "tst_wallclock.h"
 #include "lapi/posix_clocks.h"
 
 static struct timespec real_begin, mono_begin;
 
+static struct rtc_time rtc_begin;
+
 static int clock_saved;
 
 void tst_wallclock_save(void)
@@ -58,3 +61,42 @@ void tst_wallclock_restore(void)
 	if (tst_clock_settime(CLOCK_REALTIME, &adjust))
 		tst_brk(TBROK | TERRNO, "tst_clock_settime() realtime failed");
 }
+
+void tst_rtctime_save(void)
+{
+	/* save initial monotonic time to restore it when needed */
+	if (tst_rtc_gettime(&rtc_begin))
+		tst_brk(TBROK | TERRNO, "tst_rtc_gettime() realtime failed");
+
+	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin))
+		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
+
+	clock_saved = 1;
+}
+
+void tst_rtctime_restore(void)
+{
+	static struct timespec mono_end, elapsed;
+	static struct timespec rtc_begin_tm, rtc_adjust;
+	static struct rtc_time rtc_restore;
+
+	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);
+
+	rtc_begin_tm.tv_sec = tst_rtc_tm_to_time(&rtc_begin);
+
+	rtc_adjust = tst_timespec_add(rtc_begin_tm, elapsed);
+
+	tst_rtc_time_to_tm(rtc_adjust.tv_sec, &rtc_restore);
+
+	/* restore realtime clock based on monotonic delta */
+	if (tst_rtc_settime(&rtc_restore))
+		tst_brk(TBROK | TERRNO, "tst_rtc_settime() realtime failed");
+}
-- 
2.25.1


  parent reply	other threads:[~2020-12-23  3:35 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-23  3:35 [LTP] [PATCH 0/3] add rtctime libs and rtc02 case gengcixi
2020-12-23  3:35 ` [LTP] [PATCH 1/3] lib: add tst_rtctime* for rtc test gengcixi
2021-01-07 13:50   ` Cyril Hrubis
2021-01-08  8:30     ` Cixi Geng
2020-12-23  3:35 ` gengcixi [this message]
2021-01-07 13:57   ` [LTP] [PATCH 2/3] lib: implement rtctime_save and rtctime_restore function Cyril Hrubis
2021-01-08  9:19     ` Cixi Geng
2021-01-08  9:34       ` Cyril Hrubis
2021-01-08  9:45         ` Cixi Geng
2020-12-23  3:35 ` [LTP] [PATCH 3/3] rtc02: add rtc set time verify case gengcixi
2021-01-07 14:32   ` Cyril Hrubis
2021-01-07 14:33 ` [LTP] [PATCH 0/3] add rtctime libs and rtc02 case 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=20201223033517.1464263-3-gengcixi@gmail.com \
    --to=gengcixi@gmail.com \
    --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