public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi, Kay Sievers <kay@vrfy.org>
Subject: [PATCH 16/15] dmesg: make time stamps to be printed consistently
Date: Sun, 30 Jun 2013 19:11:22 +0100	[thread overview]
Message-ID: <1372615882-11589-2-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1372615882-11589-1-git-send-email-kerolasa@iki.fi>

Earlier uptime determination, which was done with sysinfo(2), had one
second resolution, which made time stamps to be rounded unstable way
depending on when a dmesg command was executed.  In practical terms; the
command below was supposed not to differ but it did.

$ diff -q <(dmesg --ctime) <(sleep 0.5 ; dmesg --ctime)

CC: Kay Sievers <kay@vrfy.org>
References: https://lkml.org/lkml/2013/6/30/37
Buglink: https://github.com/karelzak/util-linux/issues/24
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 sys-utils/dmesg.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index 480c897..c429a82 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -145,7 +145,7 @@ struct dmesg_control {
 
 	struct timeval	lasttime;	/* last printed timestamp */
 	struct tm	lasttm;		/* last localtime */
-	time_t		boot_time;	/* system boot time */
+	struct timeval	boot_time;	/* system boot time */
 
 	int		action;		/* SYSLOG_ACTION_* */
 	int		method;		/* DMESG_METHOD_* */
@@ -472,18 +472,19 @@ static int get_syslog_buffer_size(void)
 	return n > 0 ? n : 0;
 }
 
-static time_t get_boot_time(void)
+static void get_boot_time(struct timeval *boot_time)
 {
-	struct sysinfo info;
-	struct timeval tv;
+	struct timespec hires_uptime;
+	struct timeval lores_uptime, now;
 
-	if (sysinfo(&info) != 0)
-		warn(_("sysinfo failed"));
-	else if (gettimeofday(&tv, NULL) != 0)
+	if (clock_gettime(CLOCK_BOOTTIME, &hires_uptime) != 0)
+		warn(_("clock_gettime failed"));
+	else if (gettimeofday(&now, NULL) != 0)
 		warn(_("gettimeofday failed"));
-	else
-		return tv.tv_sec -= info.uptime;
-	return 0;
+	else {
+		TIMESPEC_TO_TIMEVAL(&lores_uptime, &hires_uptime);
+		timersub(&now, &lores_uptime, boot_time);
+	}
 }
 
 /*
@@ -771,7 +772,7 @@ static struct tm *record_localtime(struct dmesg_control *ctl,
 				   struct dmesg_record *rec,
 				   struct tm *tm)
 {
-	time_t t = ctl->boot_time + rec->tv.tv_sec;
+	time_t t = ctl->boot_time.tv_sec + rec->tv.tv_sec;
 	return localtime_r(&t, tm);
 }
 
@@ -1338,8 +1339,8 @@ int main(int argc, char *argv[])
 		usage(stderr);
 
 	if (is_timefmt(ctl, RELTIME) || is_timefmt(ctl, CTIME) || is_timefmt(ctl, ISO8601)) {
-		ctl.boot_time = get_boot_time();
-		if (!ctl.boot_time)
+		get_boot_time(&(ctl.boot_time));
+		if (ctl.boot_time.tv_sec == 0 && ctl.boot_time.tv_usec == 0)
 			ctl.time_fmt = DMESG_TIMEFTM_NONE;
 	}
 
-- 
1.8.3.2


  reply	other threads:[~2013-06-30 18:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-30 18:11 Addition to earlier pull request Sami Kerola
2013-06-30 18:11 ` Sami Kerola [this message]
2013-07-01 12:33   ` [PATCH 16/15] dmesg: make time stamps to be printed consistently Karel Zak

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=1372615882-11589-2-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=kay@vrfy.org \
    --cc=util-linux@vger.kernel.org \
    /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