From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 18/19] dmesg: more deterministic boot time detection
Date: Sun, 2 Jun 2013 18:51:24 +0100 [thread overview]
Message-ID: <1370195485-27907-19-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1370195485-27907-1-git-send-email-kerolasa@iki.fi>
This fixes an issue which made second in human readable time formats to
wiggle. One would expect the two dmesg runs below would be identical,
but the half second sleep should almost always make some timestamps to
differ.
diff -q <(dmesg --ctime) <(sleep 0.5 ; dmesg --ctime)
Unfortunately this patch is not perfect. The use of pid 1 directory
ctime is a bit off what kernel thinks is the start up time, which makes
the dmesg, journald, and kernel all to mutually disagree when system
started. On my laptop the /proc/uptime consider start time to be
18:22:33, the old dmesg claimed 18:22:35, the new dmesg 18:22:42, and
journald 18:22:44.
In the heart of the problem is linux kernel that does not tell, in high
resolution accurancy, when start up happen. I am almost sure the kernel
should log a message telling at boot, and at any time clock is skewed,
what time the kernel thinks it is. A klog message such as 'kernel time
announcement: <epoc>.<milliseconds>' might solve most if not all
problems.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
include/pathnames.h | 3 ++-
sys-utils/dmesg.c | 27 ++++++++++++++-------------
2 files changed, 16 insertions(+), 14 deletions(-)
diff --git a/include/pathnames.h b/include/pathnames.h
index cc8a20b..84eabd4 100644
--- a/include/pathnames.h
+++ b/include/pathnames.h
@@ -48,7 +48,8 @@
#define _PATH_SECURE "/etc/securesingle"
#define _PATH_USERTTY "/etc/usertty"
-/* used in login-utils/shutdown.c */
+/* used in sys-utils/dmesg.c */
+#define _PATH_PROC_PID1 "/proc/1"
/* used in login-utils/setpwnam.h and login-utils/islocal.c */
#define _PATH_PASSWD "/etc/passwd"
diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index d80b3e9..da58c42 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -34,6 +34,7 @@
#include "optutils.h"
#include "mangle.h"
#include "pager.h"
+#include "pathnames.h"
/* Close the log. Currently a NOP. */
#define SYSLOG_ACTION_CLOSE 0
@@ -141,7 +142,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_* */
@@ -464,18 +465,17 @@ static int get_syslog_buffer_size(void)
return n > 0 ? n : 0;
}
-static time_t get_boot_time(void)
+static struct timeval get_boot_time(void)
{
- struct sysinfo info;
- struct timeval tv;
+ struct stat st;
+ struct timeval ret;
+ memset(&ret, 0, sizeof(struct timeval));
- if (sysinfo(&info) != 0)
- warn(_("sysinfo failed"));
- else if (gettimeofday(&tv, NULL) != 0)
- warn(_("gettimeofday failed"));
+ if (stat(_PATH_PROC_PID1, &st))
+ warn(_("stat failed %s"), _PATH_PROC_PID1);
else
- return tv.tv_sec -= info.uptime;
- return 0;
+ ret.tv_sec = st.st_ctime;
+ return ret;
}
/*
@@ -763,8 +763,9 @@ 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;
- return localtime_r(&t, tm);
+ struct timeval res;
+ timeradd(&(ctl->boot_time), &(rec->tv), &res);
+ return localtime_r(&(res.tv_sec), tm);
}
static char *record_ctime(struct dmesg_control *ctl,
@@ -1371,7 +1372,7 @@ int main(int argc, char *argv[])
if (ctl.time_fmt & (TIMEFTM_RELTIME | TIMEFTM_CTIME | TIMEFTM_ISO8601)) {
ctl.boot_time = get_boot_time();
- if (!ctl.boot_time)
+ if (!ctl.boot_time.tv_sec)
ctl.time_fmt &= TIMEFTM_RELTIME | TIMEFTM_CTIME;
}
--
1.8.3
next prev parent reply other threads:[~2013-06-02 17:52 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-02 17:51 [PATCH 00/19] pull: various clean ups and couple bug fixes Sami Kerola
2013-06-02 17:51 ` [PATCH 01/19] lib: remove unused code Sami Kerola
2013-06-02 17:51 ` [PATCH 02/19] lscpu: add max MHz value to make cpu governor effects more visible Sami Kerola
2013-06-02 17:51 ` [PATCH 03/19] docs: add lscpu max mhz to manual and bash completion Sami Kerola
2013-06-02 17:51 ` [PATCH 04/19] sfdisk: use libc error printing function, and symbolic exit values Sami Kerola
2013-06-07 10:20 ` Karel Zak
2013-06-02 17:51 ` [PATCH 05/19] sfdisk: clean up usage() functions Sami Kerola
2013-06-02 17:51 ` [PATCH 06/19] sfdisk: use program_invocation_short_name to determine program name Sami Kerola
2013-06-02 17:51 ` [PATCH 07/19] docs: correct sfdisk --activate instructions Sami Kerola
2013-06-02 17:51 ` [PATCH 08/19] sfdisk: remove --unhide and related functions Sami Kerola
2013-06-02 17:51 ` [PATCH 09/19] sfdisk: replace my_warn() with warnx() Sami Kerola
2013-06-02 17:51 ` [PATCH 10/19] rev: stop adding new line at the end when input does not have it Sami Kerola
2013-06-02 17:51 ` [PATCH 11/19] rev: simplify new line detection and impossible test Sami Kerola
2013-06-02 17:51 ` [PATCH 12/19] rev: reduce stream checking when closing read-only file descriptor Sami Kerola
2013-06-02 17:51 ` [PATCH 13/19] dmesg: make time format parsing to use enum bit field Sami Kerola
2013-06-03 8:45 ` Karel Zak
2013-06-02 17:51 ` [PATCH 14/19] dmesg: add --time-format option Sami Kerola
2013-06-02 17:51 ` [PATCH 15/19] dmesg: add iso-8601 time format Sami Kerola
2013-06-02 17:51 ` [PATCH 16/19] docs: add --time-format option and ISO-8601 format to manual Sami Kerola
2013-06-02 17:51 ` [PATCH 17/19] dmesg: make usage() a little bit shorter Sami Kerola
2013-06-03 8:56 ` Karel Zak
2013-06-02 17:51 ` Sami Kerola [this message]
2013-06-03 9:35 ` [PATCH 18/19] dmesg: more deterministic boot time detection Karel Zak
2013-06-02 17:51 ` [PATCH 19/19] cal: fix few type mismatches Sami Kerola
2013-06-07 10:28 ` [PATCH 00/19] pull: various clean ups and couple bug fixes Karel Zak
2013-06-07 15:45 ` Sami Kerola
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=1370195485-27907-19-git-send-email-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--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