clogger RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <bofh@yhbt.net>
To: clogger-public@yhbt.net
Subject: [PATCH] workaround for time(2) being non-monotonic
Date: Sun, 19 Mar 2023 20:24:29 +0000	[thread overview]
Message-ID: <20230319202429.2289004-1-bofh@yhbt.net> (raw)

Apparently, time(2) may go backwards in the first 1 - 2.5ms of
every second since Linux + glibc 2.31+ (and some proprietary
OSes).  Use clock_gettime(2) with CLOCK_REALTIME to workaround
the problem at the cost of a slight performance hit(*).  While git
will likely use gettimeofday(2) for compatibility with
proprietary OSes, gettimeofday(2) was declared obsolete in
POSIX.1-2008 and we don't support proprietary OSes.

(*) https://inbox.sourceware.org/libc-alpha/87ttywq0je.fsf@oldenburg.str.redhat.com/
Link: https://lore.kernel.org/git/20230319064353.686226-3-eggert@cs.ucla.edu/T/
---
 ext/clogger_ext/clogger.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c
index 7989472..23aea39 100644
--- a/ext/clogger_ext/clogger.c
+++ b/ext/clogger_ext/clogger.c
@@ -475,9 +475,21 @@ static void append_request_length(struct clogger *c)
 	}
 }
 
+/*
+ * time(2) may slip backwards, so use CLOCK_REALTIME for accuracy
+ * https://lore.kernel.org/git/20230319064353.686226-3-eggert@cs.ucla.edu/T/
+ */
+static time_t cur_time(void)
+{
+	struct timespec now;
+
+	(void)clock_gettime(CLOCK_REALTIME, &now);
+	return now.tv_sec;
+}
+
 static long local_gmtoffset(struct tm *tm)
 {
-	time_t t = time(NULL);
+	time_t t = cur_time();
 
 	tzset();
 	localtime_r(&t, tm);
@@ -537,7 +549,7 @@ static void append_time_utc(struct clogger *c)
 	char buf[sizeof("01/Jan/1970:00:00:00 +0000")];
 	struct tm tm;
 	int nr;
-	time_t t = time(NULL);
+	time_t t = cur_time();
 
 	gmtime_r(&t, &tm);
 	nr = snprintf(buf, sizeof(buf),
@@ -556,7 +568,7 @@ append_time(struct clogger *c, enum clogger_opcode op, VALUE fmt, VALUE buf)
 	size_t buf_size = RSTRING_LEN(buf) + 1; /* "\0" */
 	size_t nr;
 	struct tm tmp;
-	time_t t = time(NULL);
+	time_t t = cur_time();
 
 	if (op == CL_OP_TIME_LOCAL)
 		localtime_r(&t, &tmp);

                 reply	other threads:[~2023-03-19 20:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

  List information: https://yhbt.net/clogger/

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

  git send-email \
    --in-reply-to=20230319202429.2289004-1-bofh@yhbt.net \
    --to=bofh@yhbt.net \
    --cc=clogger-public@yhbt.net \
    /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.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/clogger.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).