Author: ian
Date: Mon Feb 12 16:25:56 2018
New Revision: 329170
URL: https://svnweb.freebsd.org/changeset/base/329170

Log:
  Replace the existing print_ct() private debugging function with a set of
  three public functions to format and print the three major data structures
  used by realtime clock drivers (clocktime, bcd_clocktime, and timespec).

Modified:
  head/sys/kern/subr_clock.c
  head/sys/sys/clock.h

Modified: head/sys/kern/subr_clock.c
==============================================================================
--- head/sys/kern/subr_clock.c  Mon Feb 12 15:48:12 2018        (r329169)
+++ head/sys/kern/subr_clock.c  Mon Feb 12 16:25:56 2018        (r329170)
@@ -108,6 +108,14 @@ static const int recent_base_year = 2017;
 static const int recent_base_days = 17167;
 
 /*
+ * Table to 'calculate' pow(10, 9 - nsdigits) via lookup of nsdigits.
+ * Before doing the lookup, the code asserts 0 <= nsdigits <= 9.
+ */
+static u_int nsdivisors[] = {
+    1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1
+};
+
+/*
  * This inline avoids some unnecessary modulo operations
  * as compared with the usual macro:
  *   ( ((year % 4) == 0 &&
@@ -131,23 +139,15 @@ leapyear(int year)
        return (rv);
 }
 
-static void
-print_ct(const struct clocktime *ct)
-{
-       printf("[%04d-%02d-%02d %02d:%02d:%02d]",
-           ct->year, ct->mon, ct->day,
-           ct->hour, ct->min, ct->sec);
-}
-
 int
 clock_ct_to_ts(const struct clocktime *ct, struct timespec *ts)
 {
        int i, year, days;
 
        if (ct_debug) {
-               printf("ct_to_ts(");
-               print_ct(ct);
-               printf(")");
+               printf("ct_to_ts([");
+               clock_print_ct(ct, 9);
+               printf("])");
        }
 
        /*
@@ -288,10 +288,10 @@ clock_ts_to_ct(const struct timespec *ts, struct clock
        ct->sec  = rsec;
        ct->nsec = ts->tv_nsec;
        if (ct_debug) {
-               printf("ts_to_ct(%jd.%09ld) = ",
+               printf("ts_to_ct(%jd.%09ld) = [",
                    (intmax_t)ts->tv_sec, ts->tv_nsec);
-               print_ct(ct);
-               printf("\n");
+               clock_print_ct(ct, 9);
+               printf("]\n");
        }
 
        KASSERT(ct->year >= 0 && ct->year < 10000,
@@ -335,6 +335,51 @@ clock_ts_to_bcd(const struct timespec *ts, struct bcd_
        bct->sec  = TOBCD(ct.sec);
        bct->dow  = ct.dow;
        bct->nsec = ct.nsec;
+}
+
+void
+clock_print_bcd(const struct bcd_clocktime *bct, int nsdigits)
+{
+
+       KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
+
+       if (nsdigits > 0) {
+               printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x.%*.*ld",
+                   bct->year, bct->mon, bct->day,
+                   bct->hour, bct->min, bct->sec,
+                   nsdigits, nsdigits, bct->nsec / nsdivisors[nsdigits]);
+       } else {
+               printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x",
+                   bct->year, bct->mon, bct->day,
+                   bct->hour, bct->min, bct->sec);
+       }
+}
+
+void
+clock_print_ct(const struct clocktime *ct, int nsdigits)
+{
+
+       KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
+
+       if (nsdigits > 0) {
+               printf("%04d-%02d-%02d %02d:%02d:%02d.%*.*ld",
+                   ct->year, ct->mon, ct->day,
+                   ct->hour, ct->min, ct->sec,
+                   nsdigits, nsdigits, ct->nsec / nsdivisors[nsdigits]);
+       } else {
+               printf("%04d-%02d-%02d %02d:%02d:%02d",
+                   ct->year, ct->mon, ct->day,
+                   ct->hour, ct->min, ct->sec);
+       }
+}
+
+void
+clock_print_ts(const struct timespec *ts, int nsdigits)
+{
+       struct clocktime ct;
+
+       clock_ts_to_ct(ts, &ct);
+       clock_print_ct(&ct, nsdigits);
 }
 
 int

Modified: head/sys/sys/clock.h
==============================================================================
--- head/sys/sys/clock.h        Mon Feb 12 15:48:12 2018        (r329169)
+++ head/sys/sys/clock.h        Mon Feb 12 16:25:56 2018        (r329170)
@@ -182,6 +182,15 @@ void timespec2fattime(const struct timespec *tsp, int 
 void fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc,
     struct timespec *tsp);
 
+/*
+ * Print a [bcd_]clocktime or timespec, optionally with fractional seconds.  
The
+ * nsdig argument can range from 0-9, and specifies how many decimal digits to
+ * display for fractional seconds.
+ */
+void clock_print_bcd(const struct bcd_clocktime *bct, int nsdig);
+void clock_print_ct(const struct clocktime *ct, int nsdig);
+void clock_print_ts(const struct timespec  *ts, int nsdig);
+
 #endif /* _KERNEL */
 
 #endif /* !_SYS_CLOCK_H_ */
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to