From 77dfeccef6c230e12fab6bddcba55a9b016136ca Mon Sep 17 00:00:00 2001 From: Poul-Henning Kamp Date: Tue, 12 Nov 1996 23:09:15 +0000 Subject: [PATCH] Increase precision of duration to milliseconds. Some heuristics to avoid overflow in calculation attempted. --- bin/dd/args.c | 3 ++- bin/dd/conv.c | 3 ++- bin/dd/dd.c | 4 ++-- bin/dd/dd.h | 4 ++-- bin/dd/misc.c | 34 ++++++++++++++++++++++++++-------- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/bin/dd/args.c b/bin/dd/args.c index 61b479451826..59ccefe4321f 100644 --- a/bin/dd/args.c +++ b/bin/dd/args.c @@ -34,7 +34,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: args.c,v 1.3 1994/09/24 02:54:42 davidg Exp $ */ #ifndef lint @@ -49,6 +49,7 @@ static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94"; #include #include #include +#include #include "dd.h" #include "extern.h" diff --git a/bin/dd/conv.c b/bin/dd/conv.c index e87c396440da..41c5909c03de 100644 --- a/bin/dd/conv.c +++ b/bin/dd/conv.c @@ -34,7 +34,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: conv.c,v 1.3 1994/09/24 02:54:44 davidg Exp $ */ #ifndef lint @@ -45,6 +45,7 @@ static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94"; #include #include +#include #include "dd.h" #include "extern.h" diff --git a/bin/dd/dd.c b/bin/dd/dd.c index 55c4e76a06c8..a0c6f7a2c346 100644 --- a/bin/dd/dd.c +++ b/bin/dd/dd.c @@ -34,7 +34,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: dd.c,v 1.4 1995/01/17 23:04:29 ache Exp $ + * $Id: dd.c,v 1.5 1995/10/23 21:31:48 ache Exp $ */ #ifndef lint @@ -213,7 +213,7 @@ setup() ctab[cnt] = cnt; } } - (void)time(&st.start); /* Statistics timestamp. */ + (void)gettimeofday(&st.start, 0); /* Statistics timestamp. */ } static void diff --git a/bin/dd/dd.h b/bin/dd/dd.h index 4ad97473e25d..6ebc3a74719a 100644 --- a/bin/dd/dd.h +++ b/bin/dd/dd.h @@ -35,7 +35,7 @@ * SUCH DAMAGE. * * @(#)dd.h 8.3 (Berkeley) 4/2/94 - * $Id$ + * $Id: dd.h,v 1.2 1994/09/24 02:54:54 davidg Exp $ */ /* Input/output stream state. */ @@ -70,7 +70,7 @@ typedef struct { u_long trunc; /* # of truncated records */ u_long swab; /* # of odd-length swab blocks */ u_long bytes; /* # of bytes written */ - time_t start; /* start time of dd */ + struct timeval start; /* start time of dd */ } STAT; /* Flags (in ddflags). */ diff --git a/bin/dd/misc.c b/bin/dd/misc.c index a135c85496a7..7e6932feee9c 100644 --- a/bin/dd/misc.c +++ b/bin/dd/misc.c @@ -34,7 +34,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: misc.c,v 1.2 1994/09/24 02:55:01 davidg Exp $ */ #ifndef lint @@ -49,6 +49,7 @@ static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94"; #include #include #include +#include #include "dd.h" #include "extern.h" @@ -56,12 +57,23 @@ static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94"; void summary() { - time_t secs; + struct timeval tv; + long msec; char buf[100]; - (void)time(&secs); - if ((secs -= st.start) == 0) - secs = 1; + (void)gettimeofday(&tv, 0); + tv.tv_sec -= st.start.tv_sec; + tv.tv_usec -= st.start.tv_usec; + if (tv.tv_usec < 0) { + tv.tv_usec += 1000000; + tv.tv_sec--; + } + + msec = tv.tv_sec * 1000; + msec += tv.tv_usec / 1000; + + if (msec == 0) + msec = 1; /* Use snprintf(3) so that we don't reenter stdio(3). */ (void)snprintf(buf, sizeof(buf), "%u+%u records in\n%u+%u records out\n", @@ -77,9 +89,15 @@ summary() st.trunc, (st.trunc == 1) ? "block" : "blocks"); (void)write(STDERR_FILENO, buf, strlen(buf)); } - (void)snprintf(buf, sizeof(buf), - "%u bytes transferred in %u secs (%u bytes/sec)\n", - st.bytes, secs, st.bytes / secs); + if (msec > 1000000) { + (void)snprintf(buf, sizeof(buf), + "%u bytes transferred in %u.%03d secs (%u bytes/sec)\n", + st.bytes, tv.tv_sec, 0, st.bytes / tv.tv_sec); + } else { + (void)snprintf(buf, sizeof(buf), + "%u bytes transferred in %u.%03d secs (%u bytes/sec)\n", + st.bytes, msec/1000, msec%1000, st.bytes*1000 / msec); + } (void)write(STDERR_FILENO, buf, strlen(buf)); }