Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 29 Aug 2018 02:18:13 +0000 (UTC)
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r338364 - stable/11/bin/dd
Message-ID:  <201808290218.w7T2IDFt045248@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Wed Aug 29 02:18:13 2018
New Revision: 338364
URL: https://svnweb.freebsd.org/changeset/base/338364

Log:
  MFC r337505, r337865, r337869: dd status=progress
  
  r337505:
  dd: add status=progress support
  
  This reports the current status on a single line every second, mirroring
  similar functionality in GNU dd, and carefully interacts with SIGINFO.
  
  PR:		229615
  
  r337865:
  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
  
  r337869:
  dd(1): Kill off duplicate progress definition following r337865

Modified:
  stable/11/bin/dd/Makefile
  stable/11/bin/dd/args.c
  stable/11/bin/dd/dd.1
  stable/11/bin/dd/dd.c
  stable/11/bin/dd/dd.h
  stable/11/bin/dd/extern.h
  stable/11/bin/dd/misc.c
  stable/11/bin/dd/position.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/bin/dd/Makefile
==============================================================================
--- stable/11/bin/dd/Makefile	Tue Aug 28 23:56:52 2018	(r338363)
+++ stable/11/bin/dd/Makefile	Wed Aug 29 02:18:13 2018	(r338364)
@@ -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: stable/11/bin/dd/args.c
==============================================================================
--- stable/11/bin/dd/args.c	Tue Aug 28 23:56:52 2018	(r338363)
+++ stable/11/bin/dd/args.c	Wed Aug 29 02:18:13 2018	(r338364)
@@ -303,6 +303,8 @@ f_status(char *arg)
 		ddflags |= C_NOINFO;
 	else if (strcmp(arg, "noxfer") == 0)
 		ddflags |= C_NOXFER;
+	else if (strcmp(arg, "progress") == 0)
+		ddflags |= C_PROGRESS;
 	else
 		errx(1, "unknown status %s", arg);
 }

Modified: stable/11/bin/dd/dd.1
==============================================================================
--- stable/11/bin/dd/dd.1	Tue Aug 28 23:56:52 2018	(r338363)
+++ stable/11/bin/dd/dd.1	Wed Aug 29 02:18:13 2018	(r338364)
@@ -32,7 +32,7 @@
 .\"     @(#)dd.1	8.2 (Berkeley) 1/13/94
 .\" $FreeBSD$
 .\"
-.Dd April 2, 2017
+.Dd August 8, 2018
 .Dt DD 1
 .Os
 .Sh NAME
@@ -164,12 +164,14 @@ bytes per second.
 Where
 .Cm value
 is one of the symbols from the following list.
-.Bl -tag -width "noxfer"
+.Bl -tag -width "progress"
 .It Cm noxfer
 Do not print the transfer statistics as the last line of status output.
 .It Cm none
 Do not print the status output.
 Error messages are shown; informational messages are not.
+.It Cm progress
+Print basic transfer statistics once per second.
 .El
 .It Cm conv Ns = Ns Ar value Ns Op , Ns Ar value ...
 Where

Modified: stable/11/bin/dd/dd.c
==============================================================================
--- stable/11/bin/dd/dd.c	Tue Aug 28 23:56:52 2018	(r338363)
+++ stable/11/bin/dd/dd.c	Wed Aug 29 02:18:13 2018	(r338364)
@@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/conf.h>
 #include <sys/disklabel.h>
 #include <sys/filio.h>
+#include <sys/time.h>
 
 #include <assert.h>
 #include <ctype.h>
@@ -84,15 +85,22 @@ const	u_char *ctab;		/* conversion table */
 char	fill_char;		/* Character to fill with if defined */
 size_t	speed = 0;		/* maximum speed, in bytes per second */
 volatile sig_atomic_t need_summary;
+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();
 
 	(void)signal(SIGINFO, siginfo_handler);
+	if (ddflags & C_PROGRESS) {
+		(void)signal(SIGALRM, sigalarm_handler);
+		setitimer(ITIMER_REAL, &itv, NULL);
+	}
 	(void)signal(SIGINT, terminate);
 
 	atexit(summary);
@@ -420,9 +428,10 @@ dd_in(void)
 
 		in.dbp += in.dbrcnt;
 		(*cfunc)();
-		if (need_summary) {
+		if (need_summary)
 			summary();
-		}
+		if (need_progress)
+			progress();
 	}
 }
 

Modified: stable/11/bin/dd/dd.h
==============================================================================
--- stable/11/bin/dd/dd.h	Tue Aug 28 23:56:52 2018	(r338363)
+++ stable/11/bin/dd/dd.h	Wed Aug 29 02:18:13 2018	(r338364)
@@ -99,5 +99,6 @@ typedef struct {
 #define	C_STATUS	0x08000000
 #define	C_NOXFER	0x10000000
 #define	C_NOINFO	0x20000000
+#define	C_PROGRESS	0x40000000
 
 #define	C_PARITY	(C_PAREVEN | C_PARODD | C_PARNONE | C_PARSET)

Modified: stable/11/bin/dd/extern.h
==============================================================================
--- stable/11/bin/dd/extern.h	Tue Aug 28 23:56:52 2018	(r338363)
+++ stable/11/bin/dd/extern.h	Wed Aug 29 02:18:13 2018	(r338364)
@@ -43,7 +43,9 @@ void jcl(char **);
 void pos_in(void);
 void pos_out(void);
 double secs_elapsed(void);
+void progress(void);
 void summary(void);
+void sigalarm_handler(int);
 void siginfo_handler(int);
 void terminate(int);
 void unblock(void);
@@ -64,3 +66,4 @@ extern const u_char a2ibm_32V[], a2ibm_POSIX[];
 extern u_char casetab[];
 extern char fill_char;
 extern volatile sig_atomic_t need_summary;
+extern volatile sig_atomic_t need_progress;

Modified: stable/11/bin/dd/misc.c
==============================================================================
--- stable/11/bin/dd/misc.c	Tue Aug 28 23:56:52 2018	(r338363)
+++ stable/11/bin/dd/misc.c	Wed Aug 29 02:18:13 2018	(r338364)
@@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$");
 #include <err.h>
 #include <errno.h>
 #include <inttypes.h>
+#include <libutil.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -81,6 +82,9 @@ summary(void)
 	if (ddflags & C_NOINFO)
 		return;
 
+	if (ddflags & C_PROGRESS)
+		fprintf(stderr, "\n");
+
 	secs = secs_elapsed();
 
 	(void)fprintf(stderr,
@@ -100,12 +104,45 @@ summary(void)
 	need_summary = 0;
 }
 
+void
+progress(void)
+{
+	static int outlen;
+	char si[4 + 1 + 2 + 1];		/* 123 <space> <suffix> NUL */
+	char iec[4 + 1 + 2 + 1];	/* 123 <space> <suffix> NUL */
+	char persec[4 + 1 + 2 + 1];	/* 123 <space> <suffix> NUL */
+	char *buf;
+	double secs;
+
+	secs = secs_elapsed();
+	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;
+}
+
 /* ARGSUSED */
 void
 siginfo_handler(int signo __unused)
 {
 
 	need_summary = 1;
+}
+
+/* ARGSUSED */
+void
+sigalarm_handler(int signo __unused)
+{
+
+	need_progress = 1;
 }
 
 /* ARGSUSED */

Modified: stable/11/bin/dd/position.c
==============================================================================
--- stable/11/bin/dd/position.c	Tue Aug 28 23:56:52 2018	(r338363)
+++ stable/11/bin/dd/position.c	Wed Aug 29 02:18:13 2018	(r338364)
@@ -123,6 +123,8 @@ pos_in(void)
 				--cnt;
 			if (need_summary)
 				summary();
+			if (need_progress)
+				progress();
 			continue;
 		}
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201808290218.w7T2IDFt045248>