From owner-svn-src-head@freebsd.org Wed Aug 15 19:46:15 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 5AFF8106E551; Wed, 15 Aug 2018 19:46:15 +0000 (UTC) (envelope-from kevans@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 0E4BE77B97; Wed, 15 Aug 2018 19:46:15 +0000 (UTC) (envelope-from kevans@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 E39F17019; Wed, 15 Aug 2018 19:46:14 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w7FJkEEl034127; Wed, 15 Aug 2018 19:46:14 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w7FJkD6i034122; Wed, 15 Aug 2018 19:46:13 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201808151946.w7FJkD6i034122@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Wed, 15 Aug 2018 19:46:13 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r337865 - head/bin/dd X-SVN-Group: head X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: head/bin/dd X-SVN-Commit-Revision: 337865 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.27 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: Wed, 15 Aug 2018 19:46:15 -0000 Author: kevans Date: Wed Aug 15 19:46:13 2018 New Revision: 337865 URL: https://svnweb.freebsd.org/changeset/base/337865 Log: dd: Incorporate some changes from imp for status=progress Notable changes from what landed in r337505: - sigalarm handler isn't setup unless we're actually using it - Humanized versions of the amount of data transferred in the progress update Submitted by: imp Reviewed by: kevans MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D16642 Modified: head/bin/dd/Makefile head/bin/dd/dd.c head/bin/dd/extern.h head/bin/dd/misc.c head/bin/dd/position.c Modified: head/bin/dd/Makefile ============================================================================== --- head/bin/dd/Makefile Wed Aug 15 19:28:48 2018 (r337864) +++ head/bin/dd/Makefile Wed Aug 15 19:46:13 2018 (r337865) @@ -6,6 +6,7 @@ PACKAGE=runtime PROG= dd SRCS= args.c conv.c conv_tab.c dd.c misc.c position.c +LIBADD= util # # Test the character conversion functions. We have to be explicit about Modified: head/bin/dd/dd.c ============================================================================== --- head/bin/dd/dd.c Wed Aug 15 19:28:48 2018 (r337864) +++ head/bin/dd/dd.c Wed Aug 15 19:46:13 2018 (r337865) @@ -95,6 +95,8 @@ volatile sig_atomic_t need_progress; int main(int argc __unused, char *argv[]) { + struct itimerval itv = { { 1, 0 }, { 1, 0 } }; /* SIGALARM every second, if needed */ + (void)setlocale(LC_CTYPE, ""); jcl(argv); setup(); @@ -104,7 +106,10 @@ main(int argc __unused, char *argv[]) err(1, "unable to enter capability mode"); (void)signal(SIGINFO, siginfo_handler); - (void)signal(SIGALRM, sigalrm_handler); + if (ddflags & C_PROGRESS) { + (void)signal(SIGALRM, sigalarm_handler); + setitimer(ITIMER_REAL, &itv, NULL); + } (void)signal(SIGINT, terminate); atexit(summary); @@ -284,14 +289,6 @@ setup(void) ctab = casetab; } - if ((ddflags & C_PROGRESS)) { - struct itimerval timer = { - .it_interval = { .tv_sec = 1, .tv_usec = 0 }, - .it_value = { .tv_sec = 1, .tv_usec = 0 }, - }; - setitimer(ITIMER_REAL, &timer, NULL); - } - if (clock_gettime(CLOCK_MONOTONIC, &st.start)) err(1, "clock_gettime"); } @@ -469,12 +466,10 @@ dd_in(void) in.dbp += in.dbrcnt; (*cfunc)(); - if (need_summary) { + if (need_summary) summary(); - } - if (need_progress) { + if (need_progress) progress(); - } } } Modified: head/bin/dd/extern.h ============================================================================== --- head/bin/dd/extern.h Wed Aug 15 19:28:48 2018 (r337864) +++ head/bin/dd/extern.h Wed Aug 15 19:46:13 2018 (r337865) @@ -45,10 +45,11 @@ void jcl(char **); void pos_in(void); void pos_out(void); double secs_elapsed(void); +void progress(void); void summary(void); void progress(void); +void sigalarm_handler(int); void siginfo_handler(int); -void sigalrm_handler(int); void terminate(int); void unblock(void); void unblock_close(void); Modified: head/bin/dd/misc.c ============================================================================== --- head/bin/dd/misc.c Wed Aug 15 19:28:48 2018 (r337864) +++ head/bin/dd/misc.c Wed Aug 15 19:46:13 2018 (r337865) @@ -46,6 +46,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -56,8 +57,6 @@ __FBSDID("$FreeBSD$"); #include "dd.h" #include "extern.h" -static int need_newline; - double secs_elapsed(void) { @@ -85,7 +84,7 @@ summary(void) if (ddflags & C_NOINFO) return; - if (need_newline && !need_summary) + if (ddflags & C_PROGRESS) fprintf(stderr, "\n"); secs = secs_elapsed(); @@ -110,22 +109,25 @@ summary(void) void progress(void) { + static int outlen; + char si[4 + 1 + 2 + 1]; /* 123 NUL */ + char iec[4 + 1 + 2 + 1]; /* 123 NUL */ + char persec[4 + 1 + 2 + 1]; /* 123 NUL */ + char *buf; double secs; - static int lastlen; - int len; secs = secs_elapsed(); - len = fprintf(stderr, - "\r%ju bytes transferred in %.0f secs (%.0f bytes/sec)", - st.bytes, secs, st.bytes / secs); - - if (len > 0) { - if (len < lastlen) - (void)fprintf(stderr, "%*s", len - lastlen, ""); - lastlen = len; - } - - need_newline = 1; + humanize_number(si, sizeof(si), (int64_t)st.bytes, "B", HN_AUTOSCALE, + HN_DECIMAL | HN_DIVISOR_1000); + humanize_number(iec, sizeof(iec), (int64_t)st.bytes, "B", HN_AUTOSCALE, + HN_DECIMAL | HN_IEC_PREFIXES); + humanize_number(persec, sizeof(iec), (int64_t)(st.bytes / secs), "B", + HN_AUTOSCALE, HN_DECIMAL | HN_DIVISOR_1000); + asprintf(&buf, " %'ju bytes (%s, %s) transferred %.3fs, %s/s", + (uintmax_t)st.bytes, si, iec, secs, persec); + outlen = fprintf(stderr, "%-*s\r", outlen, buf); + fflush(stderr); + free(buf); need_progress = 0; } @@ -139,7 +141,7 @@ siginfo_handler(int signo __unused) /* ARGSUSED */ void -sigalrm_handler(int signo __unused) +sigalarm_handler(int signo __unused) { need_progress = 1; Modified: head/bin/dd/position.c ============================================================================== --- head/bin/dd/position.c Wed Aug 15 19:28:48 2018 (r337864) +++ head/bin/dd/position.c Wed Aug 15 19:46:13 2018 (r337865) @@ -125,6 +125,8 @@ pos_in(void) --cnt; if (need_summary) summary(); + if (need_progress) + progress(); continue; }