From owner-svn-src-head@freebsd.org Mon Feb 12 16:25:57 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 9ADCCF057B7; Mon, 12 Feb 2018 16:25:57 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CB457D42A; Mon, 12 Feb 2018 16:25:57 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 432611451F; Mon, 12 Feb 2018 16:25:57 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w1CGPv5l034019; Mon, 12 Feb 2018 16:25:57 GMT (envelope-from ian@FreeBSD.org) Received: (from ian@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w1CGPu81034017; Mon, 12 Feb 2018 16:25:56 GMT (envelope-from ian@FreeBSD.org) Message-Id: <201802121625.w1CGPu81034017@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ian set sender to ian@FreeBSD.org using -f From: Ian Lepore Date: Mon, 12 Feb 2018 16:25:56 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r329170 - in head/sys: kern sys X-SVN-Group: head X-SVN-Commit-Author: ian X-SVN-Commit-Paths: in head/sys: kern sys X-SVN-Commit-Revision: 329170 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Feb 2018 16:25:57 -0000 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_ */