Re: md5(1): use mono clock in time trial

2017-09-11 Thread Todd C. Miller
On Mon, 11 Sep 2017 16:19:02 +0200, Christian Weisgerber wrote:

> FreeBSD uses getrusage() to fetch the user time used.  I think that 
> makes more sense.
> 
> https://svnweb.freebsd.org/base/head/sbin/md5/md5.c?revision=307658=mark
> up#l293

Indeed.  There's no point in including system time in the measurement.

 - todd

Index: bin/md5/md5.c
===
RCS file: /cvs/src/bin/md5/md5.c,v
retrieving revision 1.91
diff -u -p -u -r1.91 md5.c
--- bin/md5/md5.c   22 May 2017 16:00:47 -  1.91
+++ bin/md5/md5.c   11 Sep 2017 15:48:29 -
@@ -24,6 +24,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -750,7 +751,8 @@ void
 digest_time(struct hash_list *hl, int times)
 {
struct hash_function *hf;
-   struct timeval start, stop, res;
+   struct rusage start, stop;
+   struct timeval res;
union ANY_CTX context;
u_int i;
u_char data[TEST_BLOCK_LEN];
@@ -769,13 +771,13 @@ digest_time(struct hash_list *hl, int ti
for (i = 0; i < TEST_BLOCK_LEN; i++)
data[i] = (u_char)(i & 0xff);
 
-   gettimeofday(, NULL);
+   getrusage(RUSAGE_SELF, );
hf->init();
for (i = 0; i < count; i++)
hf->update(, data, (size_t)TEST_BLOCK_LEN);
digest_end(hf, , digest, sizeof(digest), hf->base64);
-   gettimeofday(, NULL);
-   timersub(, , );
+   getrusage(RUSAGE_SELF, );
+   timersub(_utime, _utime, );
elapsed = res.tv_sec + res.tv_usec / 100.0;
 
(void)printf("\nDigest = %s\n", digest);



Re: md5(1): use mono clock in time trial

2017-09-11 Thread Theo de Raadt
> > On Sep 11, 2017, at 9:25 AM, Theo de Raadt  
> wrote:
> > 
> >> Scott Cheloha:
> >> 
> >>> Use a monotonic clock for the elapsed time trial.
> >> 
> >> FreeBSD uses getrusage() to fetch the user time used.  I think that 
> 
> >> makes more sense.
> >> 
> >> 
> https://svnweb.freebsd.org/base/head/sbin/md5/md5.c?revision=3D307658=markup#l293
> > 
> > The granularity is weak.  However you can stop such a process, then
> > let it continue, and get a reasonable assessment of the cputime it
> > actually used.  Which is perhaps closer to the stated goal.
> > 
> > Compared to that, CLOCK_MONOTONIC vs CLOCK_REALTIME vs whatever else
> > doesn't seem that valuable a change to me.  I'll note none of these
> > diffs come with a clear problem statement.
> 
> My thinking with the use of CLOCK_MONOTONIC was that it made the measured
> time immune to clock jumps and changes to the system time by root via
> settimeofday(2).
> 
> This gets us closer to what the time trial is trying to measure, even
> if root does something like resetting the system clock in the midst of
> the program's execution, which would make the output totally incorrect
> if we were using gettimeofday(2).

Huh?  Where do you get that?

It seems obvious the intent is performance of algorithm, rather than
"how busy is the machine".

Read naddy's comment again.



Re: md5(1): use mono clock in time trial

2017-09-11 Thread Scott Cheloha
> On Sep 11, 2017, at 9:25 AM, Theo de Raadt  wrote:
> 
>> Scott Cheloha:
>> 
>>> Use a monotonic clock for the elapsed time trial.
>> 
>> FreeBSD uses getrusage() to fetch the user time used.  I think that 
>> makes more sense.
>> 
>> https://svnweb.freebsd.org/base/head/sbin/md5/md5.c?revision=307658=markup#l293
> 
> The granularity is weak.  However you can stop such a process, then
> let it continue, and get a reasonable assessment of the cputime it
> actually used.  Which is perhaps closer to the stated goal.
> 
> Compared to that, CLOCK_MONOTONIC vs CLOCK_REALTIME vs whatever else
> doesn't seem that valuable a change to me.  I'll note none of these
> diffs come with a clear problem statement.

My thinking with the use of CLOCK_MONOTONIC was that it made the measured
time immune to clock jumps and changes to the system time by root via
settimeofday(2).

This gets us closer to what the time trial is trying to measure, even
if root does something like resetting the system clock in the midst of
the program's execution, which would make the output totally incorrect
if we were using gettimeofday(2).

--
Scott Cheloha


Re: md5(1): use mono clock in time trial

2017-09-11 Thread Theo de Raadt
> Scott Cheloha:
> 
> > Use a monotonic clock for the elapsed time trial.
> 
> FreeBSD uses getrusage() to fetch the user time used.  I think that 
> makes more sense.
> 
> https://svnweb.freebsd.org/base/head/sbin/md5/md5.c?revision=307658=markup#l293

The granularity is weak.  However you can stop such a process, then
let it continue, and get a reasonable assessment of the cputime it
actually used.  Which is perhaps closer to the stated goal.

Compared to that, CLOCK_MONOTONIC vs CLOCK_REALTIME vs whatever else
doesn't seem that valuable a change to me.  I'll note none of these
diffs come with a clear problem statement.



Re: md5(1): use mono clock in time trial

2017-09-11 Thread Christian Weisgerber
Scott Cheloha:

> Use a monotonic clock for the elapsed time trial.

FreeBSD uses getrusage() to fetch the user time used.  I think that 
makes more sense.

https://svnweb.freebsd.org/base/head/sbin/md5/md5.c?revision=307658=markup#l293

-- 
Christian "naddy" Weisgerber  na...@mips.inka.de



md5(1): use mono clock in time trial

2017-09-10 Thread Scott Cheloha
Hi,

Use a monotonic clock for the elapsed time trial.

--
Scott Cheloha

Index: bin/md5/md5.c
===
RCS file: /cvs/src/bin/md5/md5.c,v
retrieving revision 1.91
diff -u -p -r1.91 md5.c
--- bin/md5/md5.c   22 May 2017 16:00:47 -  1.91
+++ bin/md5/md5.c   10 Sep 2017 19:29:05 -
@@ -750,7 +750,7 @@ void
 digest_time(struct hash_list *hl, int times)
 {
struct hash_function *hf;
-   struct timeval start, stop, res;
+   struct timespec start, stop, res;
union ANY_CTX context;
u_int i;
u_char data[TEST_BLOCK_LEN];
@@ -769,14 +769,14 @@ digest_time(struct hash_list *hl, int ti
for (i = 0; i < TEST_BLOCK_LEN; i++)
data[i] = (u_char)(i & 0xff);
 
-   gettimeofday(, NULL);
+   clock_gettime(CLOCK_MONOTONIC, );
hf->init();
for (i = 0; i < count; i++)
hf->update(, data, (size_t)TEST_BLOCK_LEN);
digest_end(hf, , digest, sizeof(digest), hf->base64);
-   gettimeofday(, NULL);
-   timersub(, , );
-   elapsed = res.tv_sec + res.tv_usec / 100.0;
+   clock_gettime(CLOCK_MONOTONIC, );
+   timespecsub(, , );
+   elapsed = res.tv_sec + res.tv_nsec / 10.0;
 
(void)printf("\nDigest = %s\n", digest);
(void)printf("Time   = %f seconds\n", elapsed);