This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v2 05/10] Use clock_gettime to implement time.


* Zack Weinberg:

> On Wed, Aug 28, 2019 at 2:16 PM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * Zack Weinberg:
>>
>> > Most ports were using gettimeofday to implement time, or they were
>> > making a direct (v)syscall.  Unconditionally switch to using
>> > clock_gettime instead.  All sysdeps implementations of time are
>> > removed.
>>
>> I'm sorry, but this is clearly not advisable because clock_gettime is
>> almost an order of magnitude slower than time.
>
> Hmm.  How do we reconcile this with the (Linux) kernel maintainers'
> stated intentions to provide only clock_gettime in the future?  We
> could use CLOCK_REALTIME_COARSE when available, I suppose; can you see
> whether that recovers the lost performance, or else send me your
> benchmark code so I can try it?

I see 4.9 ns with CLOCK_REALTIME_COARSE, compared to 2.2 ns for time.
It still looks significantly slower to me.

We also have to be careful not to use clocks that don't stay in the vDSO
on all supported kernel/hypervisor combinations.

Benchmark code is below.

Thanks,
Florian

#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>

int
main (int argc, char **argv)
{
  int count;
  if (argc != 3 || (count = atoi (argv[2])) <= 0)
    err (1, "usage: %s METHOD ITERATIONS", argv[0]);

  if (strcmp (argv[1], "time") == 0)
    for (int i = 0; i < count; ++i)
      time (NULL);
  else if (strcmp (argv[1], "gettimeofday") == 0)
    for (int i = 0; i < count; ++i)
      {
        struct timeval tv;
        gettimeofday (&tv, NULL);
      }
  else if (strcmp (argv[1], "realtime") == 0)
    for (int i = 0; i < count; ++i)
      {
        struct timespec tv;
        clock_gettime (CLOCK_REALTIME, &tv);
      }
  else if (strcmp (argv[1], "realtime_coarse") == 0)
    for (int i = 0; i < count; ++i)
      {
        struct timespec tv;
        clock_gettime (CLOCK_REALTIME_COARSE, &tv);
      }
  else if (strcmp (argv[1], "monotonic") == 0)
    for (int i = 0; i < count; ++i)
      {
        struct timespec tv;
        clock_gettime (CLOCK_MONOTONIC, &tv);
      }
  else if (strcmp (argv[1], "monotonic_coarse") == 0)
    for (int i = 0; i < count; ++i)
      {
        struct timespec tv;
        clock_gettime (CLOCK_MONOTONIC_COARSE, &tv);
      }
  else if (strcmp (argv[1], "monotonic_raw") == 0)
    for (int i = 0; i < count; ++i)
      {
        struct timespec tv;
        clock_gettime (CLOCK_MONOTONIC_RAW, &tv);
      }
  else
    errx (1, "invalid clock: %s", argv[1]);

  return 0;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]