Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Sep 2019 20:20:21 +0000 (UTC)
From:      Dimitry Andric <dim@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r352818 - head/usr.bin/top
Message-ID:  <201909272020.x8RKKL8d083022@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: dim
Date: Fri Sep 27 20:20:21 2019
New Revision: 352818
URL: https://svnweb.freebsd.org/changeset/base/352818

Log:
  Make fractional delays for top(1) work for interactive mode.
  
  In r334906, the -s option was changed to allow fractional times, but
  this only functioned correctly for batch mode.  In interactive mode, any
  delay below 1.0 would get floored to zero.  This would put top(1) into a
  tight loop, which could be difficult to interrupt.
  
  Fix this by storing the -s option value (after validation) into a struct
  timeval, and using that struct consistently for delaying with select(2).
  
  Next up is to allow interactive entry of a fractional delay value.
  
  MFC after:	3 days

Modified:
  head/usr.bin/top/top.1
  head/usr.bin/top/top.c

Modified: head/usr.bin/top/top.1
==============================================================================
--- head/usr.bin/top/top.1	Fri Sep 27 20:09:43 2019	(r352817)
+++ head/usr.bin/top/top.1	Fri Sep 27 20:20:21 2019	(r352818)
@@ -147,7 +147,7 @@ no information is available about the percentage of ti
 .It Fl s Ar time
 Set the delay between screen updates to
 .Ar time
-seconds.
+seconds, which may be fractional.
 The default delay between updates is 1 second.
 .It Fl o Ar field
 Sort the process display area on the specified field.

Modified: head/usr.bin/top/top.c
==============================================================================
--- head/usr.bin/top/top.c	Fri Sep 27 20:09:43 2019	(r352817)
+++ head/usr.bin/top/top.c	Fri Sep 27 20:20:21 2019	(r352818)
@@ -233,7 +233,7 @@ main(int argc, const char *argv[])
     static char tempbuf2[50];
 	sigset_t old_sigmask, new_sigmask;
     int topn = Infinity;
-    double delay = 2;
+    struct timeval delay = { 2, 0 };
     int displays = 0;		/* indicates unspecified */
     int sel_ret = 0;
     time_t curr_time;
@@ -377,21 +377,27 @@ main(int argc, const char *argv[])
 		break;
 	      }
 
-		  case 's':
-			delay = strtod(optarg, &nptr);
-			if (nptr == optarg) {
-				warnx("warning: invalid delay");
-				delay = 2;
-				warnings++;
-			}
-			if (delay < 0) {
-				warnx("warning: seconds delay should be positive -- using default");
-				delay = 2;
-				warnings++;
-			}
+	      case 's':
+	      {
+		  double delay_d = strtod(optarg, &nptr);
+		  if (nptr == optarg)
+		  {
+		      warnx("warning: invalid delay");
+		      warnings++;
+		  }
+		  else if (delay_d <= 0)
+		  {
+		      warnx("warning: seconds delay should be positive -- using default");
+		      warnings++;
+		  }
+		  else
+		  {
+		      delay.tv_sec = delay_d;
+		      delay.tv_usec = (delay_d - delay.tv_sec) * 1e6;
+		  }
+		  break;
+	      }
 
-		break;
-
 	      case 'q':		/* be quick about it */
 			errno = 0;
 			i = setpriority(PRIO_PROCESS, 0, PRIO_MIN);
@@ -704,7 +710,8 @@ restart:
 	    no_command = true;
 	    if (!interactive)
 	    {
-		usleep(delay * 1e6);
+		timeout = delay;
+		select(0, NULL, NULL, NULL, &timeout);
 		if (leaveflag) {
 		    end_screen();
 		    exit(0);
@@ -718,8 +725,7 @@ restart:
 		/* set up arguments for select with timeout */
 		FD_ZERO(&readfds);
 		FD_SET(0, &readfds);		/* for standard input */
-		timeout.tv_sec  = delay;
-		timeout.tv_usec = 0;
+		timeout = delay;
 
 		if (leaveflag) {
 		    end_screen();
@@ -880,12 +886,10 @@ restart:
 
 			    case CMD_delay:	/* new seconds delay */
 				new_message(MT_standout, "Seconds to delay: ");
-				if ((i = readline(tempbuf1, 8, true)) > -1)
+				if ((i = readline(tempbuf1, 8, true)) > 0)
 				{
-				    if ((delay = i) == 0)
-				    {
-					delay = 1;
-				    }
+				    delay.tv_sec = i;
+				    delay.tv_usec = 0;
 				}
 				clear_message();
 				break;



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