Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

src: Make timestamp reading code adapt to whatever precision (up to



details:   https://anonhg.NetBSD.org/src/rev/cfe6a279287f
branches:  trunk
changeset: 318151:cfe6a279287f
user:      kre <kre%NetBSD.org@localhost>
date:      Sat Apr 14 01:37:34 2018 +0000
description:
Make timestamp reading code adapt to whatever precision (up to
nanoseconds, 9 digits) the kernel happens to send in the timestamps
in log messages.

Output (numeric) timestamps (when produced) are unchanged and always
in microseconds (for now).

diffstat:

 sbin/dmesg/dmesg.c |  66 ++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 57 insertions(+), 9 deletions(-)

diffs (150 lines):

diff -r c3b7bc903424 -r cfe6a279287f sbin/dmesg/dmesg.c
--- a/sbin/dmesg/dmesg.c        Sat Apr 14 01:34:47 2018 +0000
+++ b/sbin/dmesg/dmesg.c        Sat Apr 14 01:37:34 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dmesg.c,v 1.32 2018/04/11 06:41:23 wiz Exp $   */
+/*     $NetBSD: dmesg.c,v 1.33 2018/04/14 01:37:34 kre Exp $   */
 /*-
  * Copyright (c) 1991, 1993
  *     The Regents of the University of California.  All rights reserved.
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)dmesg.c    8.1 (Berkeley) 6/5/93";
 #else
-__RCSID("$NetBSD: dmesg.c,v 1.32 2018/04/11 06:41:23 wiz Exp $");
+__RCSID("$NetBSD: dmesg.c,v 1.33 2018/04/14 01:37:34 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -84,8 +84,10 @@
        struct timeval boottime;
        struct timespec lasttime;
        intmax_t sec;
-       long nsec;
+       long nsec, fsec;
+       int scale;
        int deltas, quiet, humantime;
+       bool frac;
        
        static const int bmib[] = { CTL_KERN, KERN_BOOTTIME };
        size = sizeof(boottime);
@@ -183,16 +185,23 @@
         * over cur.msg_bufs times.  Unused area is skipped since it
         * contains nul.
         */
+#ifndef SMALL
+       frac = false;
+       scale = 0;
+#endif
        for (tstamp = 0, newl = 1, log = i = 0, p = bufdata + cur.msg_bufx;
            i < cur.msg_bufs; i++, p++) {
+
 #ifndef SMALL
                if (p == bufdata + cur.msg_bufs)
                        p = bufdata;
 #define ADDC(c)                                \
-    do                                         \
+    do {                               \
        if (tstamp < sizeof(tbuf) - 1)  \
                tbuf[tstamp++] = (c);   \
-    while (/*CONSTCOND*/0)
+       if (frac)                       \
+               scale++;                \
+    } while (/*CONSTCOND*/0)
 #else
 #define ADDC(c)
 #endif
@@ -202,8 +211,16 @@
                /* Skip "\n<.*>" syslog sequences. */
                /* Gather timestamp sequences */
                if (newl) {
+#ifndef SMALL
+                       int j;
+#endif
+
                        switch (ch) {
                        case '[':
+#ifndef SMALL
+                               frac = false;
+                               scale = 0;
+#endif
                                ADDC(ch);
                                continue;
                        case '<':
@@ -213,16 +230,37 @@
                                log = 0;
                                continue;
                        case ']':
+#ifndef SMALL
+                               frac = false;
+#endif
                                ADDC(ch);
                                ADDC('\0');
                                tstamp = 0;
 #ifndef SMALL
-                               sscanf(tbuf, "[%jd.%ld]", &sec, &nsec);
+                               sec = fsec = 0;
+                               switch (sscanf(tbuf, "[%jd.%ld]", &sec, &fsec)){
+                               case EOF:
+                               case 0:
+                                       /*???*/
+                                       continue;
+                               case 1:
+                                       fsec = 0;
+                                       break;
+                               case 2:
+                                       break;
+                               default:
+                                       /* Help */
+                                       continue;
+                               }
+
+                               for (nsec = fsec, j = 9 - scale; --j >= 0; )
+                                       nsec *= 10;
                                if (!quiet || deltas)
                                        printf("[");
                                if (humantime) {
                                        time_t t;
                                        struct tm tm;
+
                                        t = boottime.tv_sec + sec;
                                        if (localtime_r(&t, &tm) != NULL) {
                                                strftime(tbuf, sizeof(tbuf),
@@ -231,17 +269,23 @@
                                                printf("%s", tbuf);
                                        }
                                } else if (!quiet) {
-                                       printf("% 9jd.%06ld",
-                                           sec, nsec / 1000);
+                                       if (scale > 6)
+                                               printf("% 5jd.%6.6ld",
+                                                   sec, (nsec + 499) / 1000);
+                                       else
+                                               printf("% 5jd.%*.*ld%.*s",
+                                                   sec, scale, scale, fsec,
+                                                   6 - scale, "000000");
                                }
                                if (deltas) {
                                        struct timespec nt = { sec, nsec };
                                        struct timespec dt;
+
                                        timespecsub(&nt, &lasttime, &dt);
                                        if (humantime || !quiet)
                                                printf(" ");
                                        printf("<% 4jd.%06ld>", (intmax_t)
-                                           dt.tv_sec, dt.tv_nsec / 1000);
+                                           dt.tv_sec, (dt.tv_nsec+499) / 1000);
                                        lasttime = nt;
                                }
                                if (!quiet || deltas)
@@ -255,6 +299,10 @@
                        default:
                                if (tstamp) {
                                    ADDC(ch);
+#ifndef SMALL
+                                   if (ch == '.')
+                                       frac = true;
+#endif
                                    continue;
                                }
                                if (log)



Home | Main Index | Thread Index | Old Index