From owner-svn-src-all@FreeBSD.ORG Wed May 7 20:26:27 2014 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id DEAE7B11; Wed, 7 May 2014 20:26:26 +0000 (UTC) Received: from mx1.stack.nl (relay04.stack.nl [IPv6:2001:610:1108:5010::107]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client CN "mailhost.stack.nl", Issuer "CA Cert Signing Authority" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 7145BAC8; Wed, 7 May 2014 20:26:26 +0000 (UTC) Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131]) by mx1.stack.nl (Postfix) with ESMTP id AB6E2B8069; Wed, 7 May 2014 22:26:23 +0200 (CEST) Received: by snail.stack.nl (Postfix, from userid 1677) id 97A3328497; Wed, 7 May 2014 22:26:23 +0200 (CEST) Date: Wed, 7 May 2014 22:26:23 +0200 From: Jilles Tjoelker To: Alan Somers Subject: Re: svn commit: r265472 - head/bin/dd Message-ID: <20140507202623.GA14233@stack.nl> References: <201405062206.s46M6dxW060155@svn.freebsd.org> <20140507113345.B923@besplex.bde.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Cc: "svn-src-head@freebsd.org" , "svn-src-all@freebsd.org" , "src-committers@freebsd.org" , Bruce Evans X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 May 2014 20:26:27 -0000 On Wed, May 07, 2014 at 12:10:31PM -0600, Alan Somers wrote: > On Tue, May 6, 2014 at 9:47 PM, Bruce Evans wrote: > > On Tue, 6 May 2014, Alan Somers wrote: > > > >> Log: > >> dd(1) uses gettimeofday(2) to compute the throughput statistics. > >> However, > >> gettimeofday returns the system clock, which may jump forward or back, > >> especially if NTP is in use. If the time jumps backwards, then dd will > >> see > >> negative elapsed time, round it up to 1usec, and print an absurdly fast > >> transfer rate. > >> > >> The solution is to use clock_gettime(2) with CLOCK_MONOTONIC_PRECISE as > >> the > >> clock_id. That clock advances steadily, regardless of changes to the > >> system > >> clock. > >> > >> Reviewed by: delphij > >> MFC after: 3 days > >> Sponsored by: Spectra Logic > >> ... > >> > >> Modified: head/bin/dd/dd.c > >> > >> ============================================================================== > >> --- head/bin/dd/dd.c Tue May 6 22:04:50 2014 (r265471) > >> +++ head/bin/dd/dd.c Tue May 6 22:06:39 2014 (r265472) > >> @@ -50,7 +50,6 @@ __FBSDID("$FreeBSD$"); > >> #include > >> #include > >> #include > >> -#include > >> > >> #include > >> #include > >> @@ -61,6 +60,8 @@ __FBSDID("$FreeBSD$"); > >> #include > >> #include > >> #include > >> +#include > > Use of is a style bug. It is not used in BSD or KNF code > > like dd used to be. > sysexits.h is recommended by the err(3) man page. Is that > recommendation meant to apply selectively, or is it obsolete, or is > some sort of edit war being waged by man page authors? The recommendation for was incompletely removed, yes. > [snip] > >> - st.start = tv.tv_sec + tv.tv_usec * 1e-6; > >> + if (clock_gettime(CLOCK_MONOTONIC_PRECISE, &tv)) > >> + err(EX_OSERR, "clock_gettime"); > [snip] > >> + st.start = tv.tv_sec + tv.tv_nsec * 1.0e-9; > >> } The floating point addition starts losing precision after 8388608 seconds (slightly more than 97 days, a plausible uptime for a server). It is better to subtract the timespecs to avoid this issue. With microseconds, the precision of a double is sufficient for 272 years, so that calculation is probably acceptable. I think I agree with Bruce that using CLOCK_MONOTONIC_PRECISE is not necessary here; the standard CLOCK_MONOTONIC should suffice. > [snip] > Even if nanosecond resolution isn't useful, monotonicity is. Nobody > should be using a nonmonotonic clock just to measure durations. I > started an audit of all of FreeBSD to look for other programs that use > gettimeofday to measure durations. I haven't finished, but I've > already found a lot, including xz, ping, hastd, fetch, systat, powerd, > and others. I don't have time to fix them, though. Would you be > interested, or do you know anyone else who would? I have a local patch for time(1). Whether the monotonic clock is right also depends on how long the durations typically are. For very long durations, users might refer to wall clocks and CLOCK_REALTIME may be more appropriate. -- Jilles Tjoelker