On Sat, Nov 18, 2017 at 05:27:14PM +0100, Jeremie Courreges-Anglas wrote:
> On Sat, Nov 11 2017, Scott Cheloha <[email protected]> wrote:
> > [...]
> 
> I doubt that timersub/timespecsub are a big problem to add to -portable,
> they're just macros.  clock_gettime and getrusage seem to already be
> used in libressl(-portable), there's autoconf glue for the former at
> least.

Sounds good to me.

> > Thoughts and feedback?
> 
> More comment inline.
> 
> > [...]
> >
> >  double
> > -app_tminterval(int stop, int usertime)
> > +real_interval(int new)
> 
> I suggest you keep app_tminterval() as the entry point here.  As
> mentioned by the comment, non-POSIX systems need to implement the same
> interface, and indeed apps/openssl/apps_win.c in libressl-portable also
> provides app_tminterval().

Okay, retained.

> > [...]
> >
> > +   static struct timespec elapsed, now, start;
> 
> Only "start" need to be static here.
> 
> > [...]
> >
> > +double
> > +user_interval(int new)
> > +{
> > +   static struct timeval elapsed, start;
> > +   static struct rusage now;
> 
> Same here.

Fixed and fixed.

> > [...]
> >
> > -   if (usertime == 0 && !mr)
> > +   if (usertime == 0 && !mr) {
> >             BIO_printf(bio_err, "You have chosen to measure elapsed time 
> > instead of user CPU time.\n");
> > +           interval_function = real_interval;
> 
> It seems a bit overengineered to use a function pointer for this.  No
> need to change this code if you just keep the app_tminterval()
> interface.

Yep.  Keeping the interface eliminates the need for changes in
speed.c and apps.h.

Also, you need <sys/time.h> to use timersub, timespecsub, etc, so
I added that to s_time.c and apps_posix.c.

--
Scott Cheloha

Index: usr.bin/openssl/apps_posix.c
===================================================================
RCS file: /cvs/src/usr.bin/openssl/apps_posix.c,v
retrieving revision 1.2
diff -u -p -r1.2 apps_posix.c
--- usr.bin/openssl/apps_posix.c        13 Sep 2015 12:41:01 -0000      1.2
+++ usr.bin/openssl/apps_posix.c        21 Nov 2017 23:48:12 -0000
@@ -116,31 +116,48 @@
  * Functions that need to be overridden by non-POSIX operating systems.
  */
 
-#include <sys/times.h>
+#include <sys/resource.h>
+#include <sys/time.h>
 
-#include <unistd.h>
+#include <time.h>
 
 #include "apps.h"
 
-double
-app_tminterval(int stop, int usertime)
+static double
+real_interval(int stop)
 {
-       double ret = 0;
-       struct tms rus;
-       clock_t now = times(&rus);
-       static clock_t tmstart;
-
-       if (usertime)
-               now = rus.tms_utime;
-
-       if (stop == TM_START)
-               tmstart = now;
-       else {
-               long int tck = sysconf(_SC_CLK_TCK);
-               ret = (now - tmstart) / (double) tck;
+       static struct timespec start;
+       static struct timespec elapsed, now;
+
+       clock_gettime(CLOCK_MONOTONIC, &now);
+       if (stop) {
+               timespecsub(&now, &start, &elapsed);
+               return elapsed.tv_sec + elapsed.tv_nsec / 1000000000.0;
        }
+       start = now;
+       return 0.0;
+}
 
-       return (ret);
+static double
+user_interval(int stop)
+{
+       static struct timeval start;
+       static struct timeval elapsed;
+       static struct rusage now;
+
+       getrusage(RUSAGE_SELF, &now);
+       if (stop) {
+               timersub(&now.ru_utime, &start, &elapsed);
+               return elapsed.tv_sec + elapsed.tv_usec / 1000000.0;
+       }
+       start = now.ru_utime;
+       return 0.0;
+}
+
+double
+app_tminterval(int stop, int usertime)
+{
+       return (usertime) ? user_interval(stop) : real_interval(stop);
 }
 
 int
Index: usr.bin/openssl/s_time.c
===================================================================
RCS file: /cvs/src/usr.bin/openssl/s_time.c,v
retrieving revision 1.18
diff -u -p -r1.18 s_time.c
--- usr.bin/openssl/s_time.c    2 Nov 2017 00:31:49 -0000       1.18
+++ usr.bin/openssl/s_time.c    21 Nov 2017 23:48:12 -0000
@@ -63,11 +63,13 @@
 
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/time.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 #include <poll.h>
 
@@ -248,7 +250,7 @@ s_time_main(int argc, char **argv)
        double totalTime = 0.0;
        int nConn = 0;
        SSL *scon = NULL;
-       time_t finishtime;
+       struct timespec finishtime, now;
        int ret = 1;
        char buf[1024 * 8];
        int ver;
@@ -330,10 +332,12 @@ s_time_main(int argc, char **argv)
        /* Loop and time how long it takes to make connections */
 
        bytes_read = 0;
-       finishtime = time(NULL) + s_time_config.maxtime;
+       clock_gettime(CLOCK_MONOTONIC, &finishtime);
+       finishtime.tv_sec += s_time_config.maxtime;
        tm_Time_F(START);
        for (;;) {
-               if (finishtime < time(NULL))
+               clock_gettime(CLOCK_MONOTONIC, &now);
+               if (timespeccmp(&finishtime, &now, <))
                        break;
                if ((scon = doConnection(NULL)) == NULL)
                        goto end;
@@ -383,7 +387,7 @@ s_time_main(int argc, char **argv)
            nConn, totalTime, ((double) nConn / totalTime), bytes_read);
        printf("%d connections in %lld real seconds, %ld bytes read per 
connection\n",
            nConn,
-           (long long)(time(NULL) - finishtime + s_time_config.maxtime),
+           (long long)(now.tv_sec - finishtime.tv_sec + s_time_config.maxtime),
            bytes_read / nConn);
 
        /*
@@ -422,14 +426,16 @@ next:
        nConn = 0;
        totalTime = 0.0;
 
-       finishtime = time(NULL) + s_time_config.maxtime;
+       clock_gettime(CLOCK_MONOTONIC, &finishtime);
+       finishtime.tv_sec += s_time_config.maxtime;
 
        printf("starting\n");
        bytes_read = 0;
        tm_Time_F(START);
 
        for (;;) {
-               if (finishtime < time(NULL))
+               clock_gettime(CLOCK_MONOTONIC, &now);
+               if (timespeccmp(&finishtime, &now, <))
                        break;
                if ((doConnection(scon)) == NULL)
                        goto end;
@@ -475,7 +481,7 @@ next:
        printf("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes 
read %ld\n", nConn, totalTime, ((double) nConn / totalTime), bytes_read);
        printf("%d connections in %lld real seconds, %ld bytes read per 
connection\n",
            nConn,
-           (long long)(time(NULL) - finishtime + s_time_config.maxtime),
+           (long long)(now.tv_sec - finishtime.tv_sec + s_time_config.maxtime),
            bytes_read / nConn);
 
        ret = 0;

Reply via email to