On Wed, Nov 22 2017, Scott Cheloha <[email protected]> wrote:
> Whoops, ignore that last patch, it lacked the
> static changes in apps_posix.c
This looks good to me. I'm tempted to commit the apps_posix.c part
first: it seems to me that app_tminterval() could be reused in s_time.c,
leading to simpler code instead of inlining clock_gettime calls.
> --
> 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 22 Nov 2017 16:39:42 -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;
> + 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;
> + struct timeval elapsed;
> + 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 22 Nov 2017 16:39:42 -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;
>
--
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF DDCC 0DFA 74AE 1524 E7EE