Duncan Coutts pushed to branch wip/dcoutts/posix-time at Glasgow Haskell Compiler / GHC

Commits:

27 changed files:

Changes:

  • .gitlab/generate-ci/gen_ci.hs
    ... ... @@ -476,8 +476,6 @@ opsysVariables AArch64 (Darwin {}) = mconcat
    476 476
     opsysVariables Amd64 (Darwin {}) = mconcat
    
    477 477
       [ "NIX_SYSTEM" =: "x86_64-darwin"
    
    478 478
       , "MACOSX_DEPLOYMENT_TARGET" =: "11.0"
    
    479
    -    -- Only Sierra and onwards supports clock_gettime. See #12858
    
    480
    -  , "ac_cv_func_clock_gettime" =: "no"
    
    481 479
         -- Only newer OS Xs support utimensat. See #17895
    
    482 480
       , "ac_cv_func_utimensat" =: "no"
    
    483 481
         -- Only newer OS Xs support futimens. See #22938
    

  • .gitlab/jobs.yaml
    ... ... @@ -3232,7 +3232,6 @@
    3232 3232
           "TEST_ENV": "x86_64-darwin-validate",
    
    3233 3233
           "TEST_WAYS": "",
    
    3234 3234
           "XZ_OPT": "-9",
    
    3235
    -      "ac_cv_func_clock_gettime": "no",
    
    3236 3235
           "ac_cv_func_futimens": "no",
    
    3237 3236
           "ac_cv_func_utimensat": "no"
    
    3238 3237
         }
    
    ... ... @@ -3299,7 +3298,6 @@
    3299 3298
           "TEST_ENV": "x86_64-darwin-validate",
    
    3300 3299
           "TEST_WAYS": "",
    
    3301 3300
           "XZ_OPT": "-9",
    
    3302
    -      "ac_cv_func_clock_gettime": "no",
    
    3303 3301
           "ac_cv_func_futimens": "no",
    
    3304 3302
           "ac_cv_func_utimensat": "no"
    
    3305 3303
         }
    
    ... ... @@ -8902,7 +8900,6 @@
    8902 8900
           "TEST_ENV": "x86_64-darwin-release",
    
    8903 8901
           "TEST_WAYS": "",
    
    8904 8902
           "XZ_OPT": "-9",
    
    8905
    -      "ac_cv_func_clock_gettime": "no",
    
    8906 8903
           "ac_cv_func_futimens": "no",
    
    8907 8904
           "ac_cv_func_utimensat": "no"
    
    8908 8905
         }
    
    ... ... @@ -8970,7 +8967,6 @@
    8970 8967
           "TEST_ENV": "x86_64-darwin-release",
    
    8971 8968
           "TEST_WAYS": "",
    
    8972 8969
           "XZ_OPT": "-9",
    
    8973
    -      "ac_cv_func_clock_gettime": "no",
    
    8974 8970
           "ac_cv_func_futimens": "no",
    
    8975 8971
           "ac_cv_func_utimensat": "no"
    
    8976 8972
         }
    
    ... ... @@ -11086,7 +11082,6 @@
    11086 11082
           "RUNTEST_ARGS": "",
    
    11087 11083
           "TEST_ENV": "x86_64-darwin-validate",
    
    11088 11084
           "TEST_WAYS": "",
    
    11089
    -      "ac_cv_func_clock_gettime": "no",
    
    11090 11085
           "ac_cv_func_futimens": "no",
    
    11091 11086
           "ac_cv_func_utimensat": "no"
    
    11092 11087
         }
    
    ... ... @@ -11152,7 +11147,6 @@
    11152 11147
           "RUNTEST_ARGS": "",
    
    11153 11148
           "TEST_ENV": "x86_64-darwin-validate",
    
    11154 11149
           "TEST_WAYS": "",
    
    11155
    -      "ac_cv_func_clock_gettime": "no",
    
    11156 11150
           "ac_cv_func_futimens": "no",
    
    11157 11151
           "ac_cv_func_utimensat": "no"
    
    11158 11152
         }
    

  • libraries/ghc-internal/include/HsBase.h
    ... ... @@ -98,16 +98,6 @@
    98 98
     #elif HAVE_STDINT_H
    
    99 99
     # include <stdint.h>
    
    100 100
     #endif
    
    101
    -#if defined(HAVE_CLOCK_GETTIME)
    
    102
    -# if defined(_POSIX_MONOTONIC_CLOCK)
    
    103
    -#  define CLOCK_ID CLOCK_MONOTONIC
    
    104
    -# else
    
    105
    -#  define CLOCK_ID CLOCK_REALTIME
    
    106
    -# endif
    
    107
    -#elif defined(darwin_HOST_OS)
    
    108
    -# include <mach/mach.h>
    
    109
    -# include <mach/mach_time.h>
    
    110
    -#endif
    
    111 101
     
    
    112 102
     #if !defined(_WIN32)
    
    113 103
     # if HAVE_SYS_RESOURCE_H
    

  • rts/GetTime.c
    1
    +/* -----------------------------------------------------------------------------
    
    2
    + *
    
    3
    + * (c) The GHC Team, 1995-2009
    
    4
    + *
    
    5
    + * Interface to the RTS time
    
    6
    + *
    
    7
    + * ---------------------------------------------------------------------------*/
    
    8
    +
    
    9
    +#include "Rts.h"
    
    10
    +#include "GetTime.h"
    
    11
    +
    
    12
    +
    
    13
    +/* Exported alias for the internal getMonotonicTime().
    
    14
    + * Used in GHC.Internal.Clock
    
    15
    + */
    
    16
    +StgWord64 getMonotonicNSec(void)
    
    17
    +{
    
    18
    +    return TimeToNS(getMonotonicTime());
    
    19
    +}
    
    20
    +
    
    21
    +/* Exported alias for the internal getMonotonicTime().
    
    22
    + * Used in ghc-internal/cbits/inputReady.c
    
    23
    + */
    
    24
    +Time getProcessElapsedTime(void)
    
    25
    +{
    
    26
    +    return getMonotonicTime();
    
    27
    +}

  • rts/GetTime.h
    ... ... @@ -4,24 +4,33 @@
    4 4
      *
    
    5 5
      * Machine-independent interface to time measurement
    
    6 6
      *
    
    7
    + * See also the public functions includes/rts/GetTime.h
    
    7 8
      * ---------------------------------------------------------------------------*/
    
    8 9
     
    
    9 10
     #pragma once
    
    10 11
     
    
    11 12
     #include "BeginPrivate.h"
    
    12 13
     
    
    13
    -void initializeTimer       (void);
    
    14
    -
    
    15
    -Time getProcessCPUTime     (void);
    
    16
    -Time getCurrentThreadCPUTime (void);
    
    17
    -void getProcessTimes       (Time *user, Time *elapsed);
    
    14
    +/* Get the current time in a monotonic clock.
    
    15
    + *
    
    16
    + * The time is represented as nanoseconds since an arbitrary epoch.
    
    17
    + *
    
    18
    + * This is a high precision clock (on most platforms), but not necessarily
    
    19
    + * nanosecond precision.
    
    20
    + */
    
    21
    +Time getMonotonicTime(void);
    
    18 22
     
    
    19
    -/* Get the current date and time.
    
    20
    -   Uses seconds since the Unix epoch, plus nanoseconds
    
    23
    +/* Get the current wall clock time.
    
    24
    + *
    
    25
    + * The time is represented as nanoseconds since the Unix epoch
    
    26
    + * (1970-01-01 00:00).
    
    27
    + *
    
    28
    + * This is a high precision clock (on most platforms), but not necessarily
    
    29
    + * nanosecond precision.
    
    21 30
      */
    
    22
    -void  getUnixEpochTime      (StgWord64 *sec, StgWord32 *nsec);
    
    31
    +Time getWallClockTime(void);
    
    23 32
     
    
    24
    -// Not strictly timing, but related
    
    25
    -W_    getPageFaults         (void);
    
    33
    +Time getProcessCPUTime(void);
    
    34
    +Time getCurrentThreadCPUTime(void);
    
    26 35
     
    
    27 36
     #include "EndPrivate.h"

  • rts/posix/Clock.h → rts/OSStats.h
    1 1
     /* -----------------------------------------------------------------------------
    
    2 2
      *
    
    3
    - * (c) The GHC Team, 2012
    
    3
    + * (c) The GHC Team 2005
    
    4 4
      *
    
    5
    - * Posix monotonic clock
    
    5
    + * Machine-independent interface to process statistics
    
    6 6
      *
    
    7 7
      * ---------------------------------------------------------------------------*/
    
    8 8
     
    
    9 9
     #pragma once
    
    10 10
     
    
    11
    -#if defined(HAVE_UNISTD_H)
    
    12
    -# include <unistd.h>
    
    13
    -#endif
    
    11
    +#include "BeginPrivate.h"
    
    14 12
     
    
    15
    -#include <time.h>
    
    16
    -
    
    17
    -#if defined(HAVE_SYS_TIME_H)
    
    18
    -# include <sys/time.h>
    
    19
    -#endif
    
    13
    +/* Count of the number of "major" page faults (since process start).
    
    14
    + *
    
    15
    + * In practice, always 0 for Win32 and wasm.
    
    16
    + */
    
    17
    +long getPageFaults(void);
    
    20 18
     
    
    21
    -#if defined(HAVE_CLOCK_GETTIME)
    
    22
    -# if defined(_POSIX_MONOTONIC_CLOCK)
    
    23
    -#  define CLOCK_ID CLOCK_MONOTONIC
    
    24
    -# else
    
    25
    -#  define CLOCK_ID CLOCK_REALTIME
    
    26
    -# endif
    
    27
    -#elif defined(darwin_HOST_OS)
    
    28
    -# include <mach/mach.h>
    
    29
    -# include <mach/mach_time.h>
    
    30
    -#endif
    19
    +#include "EndPrivate.h"

  • rts/RtsStartup.c
    ... ... @@ -360,9 +360,6 @@ hs_init_ghc(int *argc, char **argv[], RtsConfig rts_config)
    360 360
         /* Initialise the stats department, phase 0 */
    
    361 361
         initStats0();
    
    362 362
     
    
    363
    -    /* Initialize system timer before starting to collect stats */
    
    364
    -    initializeTimer();
    
    365
    -
    
    366 363
         /* Next we do is grab the start time...just in case we're
    
    367 364
          * collecting timing statistics.
    
    368 365
          */
    

  • rts/RtsUtils.c
    ... ... @@ -31,10 +31,6 @@ extern char *ctime_r(const time_t *, char *);
    31 31
     #include <fcntl.h>
    
    32 32
     #endif
    
    33 33
     
    
    34
    -#if defined(HAVE_GETTIMEOFDAY)
    
    35
    -#include <sys/time.h>
    
    36
    -#endif
    
    37
    -
    
    38 34
     #include <stdlib.h>
    
    39 35
     #include <string.h>
    
    40 36
     #include <stdarg.h>
    
    ... ... @@ -207,31 +203,6 @@ exitHeapOverflow(void)
    207 203
         stg_exit(EXIT_HEAPOVERFLOW);
    
    208 204
     }
    
    209 205
     
    
    210
    -/* -----------------------------------------------------------------------------
    
    211
    -   Sleep for the given period of time.
    
    212
    -   -------------------------------------------------------------------------- */
    
    213
    -
    
    214
    -/* Returns -1 on failure but handles EINTR internally. On Windows this will
    
    215
    - * only have millisecond precision. */
    
    216
    -int rtsSleep(Time t)
    
    217
    -{
    
    218
    -#if defined(_WIN32)
    
    219
    -    // N.B. we can't use nanosleep on Windows as it would incur a pthreads
    
    220
    -    // dependency. See #18272.
    
    221
    -    Sleep(TimeToMS(t));
    
    222
    -    return 0;
    
    223
    -#else
    
    224
    -    struct timespec req;
    
    225
    -    req.tv_sec = TimeToSeconds(t);
    
    226
    -    req.tv_nsec = TimeToNS(t - req.tv_sec * TIME_RESOLUTION);
    
    227
    -    int ret;
    
    228
    -    do {
    
    229
    -        ret = nanosleep(&req, &req);
    
    230
    -    } while (ret == -1 && errno == EINTR);
    
    231
    -    return ret;
    
    232
    -#endif /* _WIN32 */
    
    233
    -}
    
    234
    -
    
    235 206
     /* -----------------------------------------------------------------------------
    
    236 207
        Get the current time as a string.  Used in profiling reports.
    
    237 208
        -------------------------------------------------------------------------- */
    

  • rts/Stats.c
    ... ... @@ -15,6 +15,7 @@
    15 15
     #include "Stats.h"
    
    16 16
     #include "Profiling.h"
    
    17 17
     #include "GetTime.h"
    
    18
    +#include "OSStats.h"
    
    18 19
     #include "sm/Storage.h"
    
    19 20
     #include "sm/GCThread.h"
    
    20 21
     #include "sm/BlockAlloc.h"
    
    ... ... @@ -79,7 +80,13 @@ static void statsClose( void );
    79 80
     
    
    80 81
     Time stat_getElapsedTime(void)
    
    81 82
     {
    
    82
    -    return getProcessElapsedTime() - start_init_elapsed;
    
    83
    +    return getMonotonicTime() - start_init_elapsed;
    
    84
    +}
    
    85
    +
    
    86
    +static void getProcessTimes(Time *user, Time *elapsed)
    
    87
    +{
    
    88
    +    *user    = getProcessCPUTime();
    
    89
    +    *elapsed = getMonotonicTime();
    
    83 90
     }
    
    84 91
     
    
    85 92
     /* ---------------------------------------------------------------------------
    
    ... ... @@ -294,7 +301,7 @@ stat_endExit(void)
    294 301
     void
    
    295 302
     stat_startGCSync (gc_thread *gct)
    
    296 303
     {
    
    297
    -    gct->gc_sync_start_elapsed = getProcessElapsedTime();
    
    304
    +    gct->gc_sync_start_elapsed = getMonotonicTime();
    
    298 305
     }
    
    299 306
     
    
    300 307
     void
    
    ... ... @@ -302,7 +309,7 @@ stat_startNonmovingGc (void)
    302 309
     {
    
    303 310
         ACQUIRE_LOCK(&stats_mutex);
    
    304 311
         start_nonmoving_gc_cpu = getCurrentThreadCPUTime();
    
    305
    -    start_nonmoving_gc_elapsed = getProcessElapsedTime();
    
    312
    +    start_nonmoving_gc_elapsed = getMonotonicTime();
    
    306 313
         RELEASE_LOCK(&stats_mutex);
    
    307 314
     }
    
    308 315
     
    
    ... ... @@ -310,7 +317,7 @@ void
    310 317
     stat_endNonmovingGc (void)
    
    311 318
     {
    
    312 319
         Time cpu = getCurrentThreadCPUTime();
    
    313
    -    Time elapsed = getProcessElapsedTime();
    
    320
    +    Time elapsed = getMonotonicTime();
    
    314 321
     
    
    315 322
         ACQUIRE_LOCK(&stats_mutex);
    
    316 323
         stats.gc.nonmoving_gc_elapsed_ns = elapsed - start_nonmoving_gc_elapsed;
    
    ... ... @@ -329,7 +336,7 @@ void
    329 336
     stat_startNonmovingGcSync (void)
    
    330 337
     {
    
    331 338
         ACQUIRE_LOCK(&stats_mutex);
    
    332
    -    start_nonmoving_gc_sync_elapsed = getProcessElapsedTime();
    
    339
    +    start_nonmoving_gc_sync_elapsed = getMonotonicTime();
    
    333 340
         RELEASE_LOCK(&stats_mutex);
    
    334 341
         traceConcSyncBegin();
    
    335 342
     }
    
    ... ... @@ -337,7 +344,7 @@ stat_startNonmovingGcSync (void)
    337 344
     void
    
    338 345
     stat_endNonmovingGcSync (void)
    
    339 346
     {
    
    340
    -    Time end_elapsed = getProcessElapsedTime();
    
    347
    +    Time end_elapsed = getMonotonicTime();
    
    341 348
         ACQUIRE_LOCK(&stats_mutex);
    
    342 349
         stats.gc.nonmoving_gc_sync_elapsed_ns = end_elapsed - start_nonmoving_gc_sync_elapsed;
    
    343 350
         stats.nonmoving_gc_sync_elapsed_ns +=  stats.gc.nonmoving_gc_sync_elapsed_ns;
    
    ... ... @@ -435,7 +442,7 @@ stat_startGC (Capability *cap, gc_thread *gct)
    435 442
             gct->gc_start_cpu = getCurrentThreadCPUTime();
    
    436 443
         }
    
    437 444
     
    
    438
    -    gct->gc_start_elapsed = getProcessElapsedTime();
    
    445
    +    gct->gc_start_elapsed = getMonotonicTime();
    
    439 446
     #if !defined(THREADED_RTS)
    
    440 447
         // Non-threaded RTS has no sync phase. Initializing in this way makes the
    
    441 448
         // calculated statistics correctly read zero.
    

  • rts/configure.ac
    ... ... @@ -202,8 +202,11 @@ fi
    202 202
     
    
    203 203
     FP_MUSTTAIL
    
    204 204
     
    
    205
    -dnl ** check for librt
    
    206
    -AC_CHECK_FUNCS(clock_gettime timer_settime)
    
    205
    +dnl ** We now require clock_gettime for our monotonic clock.
    
    206
    +if test "$ghc_host_os" != "mingw32" ; then
    
    207
    +  AC_CHECK_FUNCS([clock_gettime], [],
    
    208
    +    AC_MSG_ERROR([The POSIX.1-2008 function 'clock_gettime' is required but is not available]))
    
    209
    +fi
    
    207 210
     
    
    208 211
     dnl ** check for Apple's "interesting" long double compatibility scheme
    
    209 212
     AC_MSG_CHECKING(for printf\$LDBLStub)
    

  • rts/eventlog/EventLog.c
    ... ... @@ -812,10 +812,6 @@ void postCapsetVecEvent (EventTypeNum tag,
    812 812
     
    
    813 813
     void postWallClockTime (EventCapsetID capset)
    
    814 814
     {
    
    815
    -    StgWord64 ts;
    
    816
    -    StgWord64 sec;
    
    817
    -    StgWord32 nsec;
    
    818
    -
    
    819 815
         ACQUIRE_LOCK_ALWAYS(&eventBufMutex);
    
    820 816
     
    
    821 817
         /* The EVENT_WALL_CLOCK_TIME event is intended to allow programs
    
    ... ... @@ -834,8 +830,12 @@ void postWallClockTime (EventCapsetID capset)
    834 830
            difference we just call them very close together.
    
    835 831
          */
    
    836 832
     
    
    837
    -    getUnixEpochTime(&sec, &nsec);  /* Get the wall clock time */
    
    838
    -    ts = time_ns();                 /* Get the eventlog timestamp */
    
    833
    +    Time now = getWallClockTime();  /* Get the wall clock time */
    
    834
    +    StgWord64 ts = time_ns();       /* Get the eventlog timestamp */
    
    835
    +
    
    836
    +    StgWord64 sec  = now / TIME_RESOLUTION;
    
    837
    +    StgWord32 nsec = now % TIME_RESOLUTION;
    
    838
    +
    
    839 839
         ensureRoomForEvent(&eventBuf, EVENT_WALL_CLOCK_TIME);
    
    840 840
     
    
    841 841
         /* Normally we'd call postEventHeader(), but that generates its own
    

  • rts/include/rts/GetTime.h
    ... ... @@ -13,4 +13,24 @@
    13 13
     
    
    14 14
     #pragma once
    
    15 15
     
    
    16
    -StgWord64 getMonotonicNSec (void);
    16
    +/* The monotonic time source used within the RTS.
    
    17
    + *
    
    18
    + * Note that this uses StgWord64, rather than Time which is int64_t .
    
    19
    + */
    
    20
    +StgWord64 getMonotonicNSec(void);
    
    21
    +
    
    22
    +/* The monotonic time source used within the RTS.
    
    23
    + *
    
    24
    + * There is no good reason (except history) that we have both this and
    
    25
    + * getMonotonicNSec.
    
    26
    + */
    
    27
    +Time getProcessElapsedTime(void);
    
    28
    +
    
    29
    +
    
    30
    +/* It might be nice to tidy this up and export monotonic and wall clock time
    
    31
    + * and precision and bind them in ghc-internal. They should probably all
    
    32
    + * use int64_t / CInt64.
    
    33
    + *
    
    34
    + * The internal functions are getMonotonicTime() and getWallClockTime().
    
    35
    + * These would be better candidates for export.
    
    36
    + */

  • rts/include/rts/Time.h
    ... ... @@ -41,5 +41,3 @@ INLINE_HEADER Time fsecondsToTime (double t)
    41 41
     {
    
    42 42
         return (Time)(t * TIME_RESOLUTION);
    
    43 43
     }
    44
    -
    
    45
    -Time getProcessElapsedTime (void);

  • rts/posix/GetTime.c
    ... ... @@ -6,252 +6,60 @@
    6 6
      *
    
    7 7
      * ---------------------------------------------------------------------------*/
    
    8 8
     
    
    9
    -// Not POSIX, due to use of ru_majflt in getPageFaults()
    
    10
    -// #include "rts/PosixSource.h"
    
    11
    -
    
    9
    +#include "rts/PosixSource.h"
    
    12 10
     #include "Rts.h"
    
    13 11
     #include "GetTime.h"
    
    14
    -#include "Clock.h"
    
    15 12
     
    
    16
    -#if defined(HAVE_SYS_RESOURCE_H)
    
    17
    -# include <sys/resource.h>
    
    18
    -#endif
    
    19
    -
    
    20
    -#if defined(HAVE_SYS_TIMES_H)
    
    21
    -# include <sys/times.h>
    
    22
    -#endif
    
    23
    -
    
    24
    -#if ! (defined(HAVE_GETRUSAGE) || defined(HAVE_TIMES))
    
    25
    -#error No implementation for getProcessCPUTime() available.
    
    26
    -#endif
    
    27
    -
    
    28
    -#if defined(darwin_HOST_OS)
    
    29
    -#include <mach/mach_time.h>
    
    30
    -#include <mach/mach_init.h>
    
    31
    -#include <mach/thread_act.h>
    
    32
    -#include <mach/mach_port.h>
    
    33
    -#endif
    
    13
    +#include <unistd.h>
    
    14
    +#include <time.h>
    
    34 15
     
    
    35 16
     #if defined(solaris2_HOST_OS)
    
    36 17
     #include <sys/time.h>
    
    37 18
     #endif
    
    38 19
     
    
    39
    -#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_GETRUSAGE)
    
    40
    -// we'll implement getProcessCPUTime() and getProcessElapsedTime()
    
    41
    -// separately, using getrusage() and gettimeofday() respectively
    
    42 20
     
    
    43
    -#if defined(darwin_HOST_OS)
    
    44
    -static uint64_t timer_scaling_factor_numer = 0;
    
    45
    -static uint64_t timer_scaling_factor_denom = 0;
    
    21
    +#if !defined(HAVE_CLOCK_GETTIME)
    
    22
    +#error We reqire clock_gettime. This should have been checked in configure.ac
    
    23
    +#elif !defined(_POSIX_MONOTONIC_CLOCK)
    
    24
    +#warning _POSIX_MONOTONIC_CLOCK not defined, which is likely to be a problem.
    
    46 25
     #endif
    
    47 26
     
    
    48
    -void initializeTimer(void)
    
    49
    -{
    
    50
    -#if defined(darwin_HOST_OS)
    
    51
    -    mach_timebase_info_data_t info;
    
    52
    -    (void) mach_timebase_info(&info);
    
    53
    -    timer_scaling_factor_numer = (uint64_t)info.numer;
    
    54
    -    timer_scaling_factor_denom = (uint64_t)info.denom;
    
    55
    -#endif
    
    56
    -}
    
    57
    -
    
    58
    -#if defined(HAVE_CLOCK_GETTIME)
    
    59 27
     static Time getClockTime(clockid_t clock)
    
    60 28
     {
    
    61 29
         struct timespec ts;
    
    62 30
         int res = clock_gettime(clock, &ts);
    
    63
    -    if (res == 0) {
    
    31
    +    if (RTS_LIKELY(res == 0)) {
    
    64 32
             return SecondsToTime(ts.tv_sec) + NSToTime(ts.tv_nsec);
    
    65 33
         } else {
    
    66 34
             sysErrorBelch("clock_gettime");
    
    67 35
             stg_exit(EXIT_FAILURE);
    
    68 36
         }
    
    69 37
     }
    
    70
    -#endif
    
    71 38
     
    
    72 39
     Time getCurrentThreadCPUTime(void)
    
    73 40
     {
    
    74
    -    // N.B. Since macOS Catalina, Darwin supports clock_gettime but does not
    
    75
    -    // support clock_getcpuclockid. Hence we prefer to use the Darwin-specific
    
    76
    -    // path on Darwin, even if clock_gettime is available.
    
    77
    -#if defined(darwin_HOST_OS)
    
    78
    -    thread_basic_info_data_t info = { };
    
    79
    -    mach_msg_type_number_t info_count = THREAD_BASIC_INFO_COUNT;
    
    80
    -    kern_return_t kern_err = thread_info(mach_thread_self(), THREAD_BASIC_INFO,
    
    81
    -                                         (thread_info_t) &info, &info_count);
    
    82
    -    if (kern_err == KERN_SUCCESS) {
    
    83
    -        return SecondsToTime(info.user_time.seconds) + USToTime(info.user_time.microseconds);
    
    84
    -    } else {
    
    85
    -        sysErrorBelch("getThreadCPUTime");
    
    86
    -        stg_exit(EXIT_FAILURE);
    
    87
    -    }
    
    88
    -#elif defined(solaris2_HOST_OS)
    
    41
    +#if defined(solaris2_HOST_OS)
    
    89 42
         // On OpenSolaris derivatives, the constant CLOCK_THREAD_CPUTIME_ID is
    
    90 43
         // defined in a system header but it isn't actually usable. clock_gettime(2)
    
    91 44
         // always returns EINVAL. Use solaris-specific gethrvtime(3) as an
    
    92 45
         // alternative.
    
    93 46
         return NSToTime(gethrvtime());
    
    94
    -#elif defined(HAVE_CLOCK_GETTIME)        &&  \
    
    95
    -       defined(CLOCK_PROCESS_CPUTIME_ID) &&  \
    
    96
    -       defined(HAVE_SYSCONF)
    
    97
    -    static bool have_checked_usability = false;
    
    98
    -    // The RELAXED operation is fine here as it's okay if we do the check below
    
    99
    -    // more than once.
    
    100
    -    if (!RELAXED_LOAD(&have_checked_usability)) {
    
    101
    -        // The Linux clock_getres(2) manpage claims that some early versions of
    
    102
    -        // Linux will return values which are uninterpretable in the presence
    
    103
    -        // of migration across CPUs. They claim that clock_getcpuclockid(0)
    
    104
    -        // will return ENOENT in this case. Check this.
    
    105
    -        clockid_t clkid;
    
    106
    -        if (clock_getcpuclockid(0, &clkid)) {
    
    107
    -            sysErrorBelch("getCurrentThreadCPUTime: no supported");
    
    108
    -            stg_exit(EXIT_FAILURE);
    
    109
    -        }
    
    110
    -        RELAXED_STORE(&have_checked_usability, true);
    
    111
    -    }
    
    112
    -    return getClockTime(CLOCK_THREAD_CPUTIME_ID);
    
    113 47
     #else
    
    114
    -#error I know of no means to find the CPU time of current thread on this platform.
    
    115
    -#endif
    
    116
    -}
    
    117
    -
    
    118
    -Time getProcessCPUTime(void)
    
    119
    -{
    
    120
    -#if !defined(BE_CONSERVATIVE)            &&  \
    
    121
    -       defined(HAVE_CLOCK_GETTIME)       &&  \
    
    122
    -       defined(_SC_CPUTIME)              &&  \
    
    123
    -       defined(CLOCK_PROCESS_CPUTIME_ID) &&  \
    
    124
    -       defined(HAVE_SYSCONF)
    
    125
    -    static int checked_sysconf = 0;
    
    126
    -    static int sysconf_result = 0;
    
    127
    -
    
    128
    -    if (!checked_sysconf) {
    
    129
    -        sysconf_result = sysconf(_SC_CPUTIME);
    
    130
    -        checked_sysconf = 1;
    
    131
    -    }
    
    132
    -    if (sysconf_result != -1) {
    
    133
    -        return getClockTime(CLOCK_PROCESS_CPUTIME_ID);
    
    134
    -    }
    
    135
    -#endif
    
    136
    -
    
    137
    -    // fallback to getrusage
    
    138
    -    {
    
    139
    -        struct rusage t;
    
    140
    -        getrusage(RUSAGE_SELF, &t);
    
    141
    -        return SecondsToTime(t.ru_utime.tv_sec + t.ru_stime.tv_sec)
    
    142
    -                + USToTime(t.ru_utime.tv_usec + t.ru_stime.tv_usec);
    
    143
    -    }
    
    144
    -}
    
    145
    -
    
    146
    -StgWord64 getMonotonicNSec(void)
    
    147
    -{
    
    148
    -#if defined(HAVE_CLOCK_GETTIME)
    
    149
    -    return getClockTime(CLOCK_ID);
    
    150
    -
    
    151
    -#elif defined(darwin_HOST_OS)
    
    152
    -
    
    153
    -    uint64_t time = mach_absolute_time();
    
    154
    -    return (time * timer_scaling_factor_numer) / timer_scaling_factor_denom;
    
    155
    -
    
    156
    -#else // use gettimeofday()
    
    157
    -
    
    158
    -    struct timeval tv;
    
    159
    -
    
    160
    -    if (gettimeofday(&tv, (struct timezone *) NULL) != 0) {
    
    161
    -        debugBlech("getMonotonicNSec: gettimeofday failed: %s", strerror(errno));
    
    162
    -    };
    
    163
    -    return (StgWord64)tv.tv_sec * 1000000000 +
    
    164
    -           (StgWord64)tv.tv_usec * 1000;
    
    165
    -
    
    48
    +    return getClockTime(CLOCK_THREAD_CPUTIME_ID);
    
    166 49
     #endif
    
    167 50
     }
    
    168 51
     
    
    169
    -Time getProcessElapsedTime(void)
    
    170
    -{
    
    171
    -    return NSToTime(getMonotonicNSec());
    
    172
    -}
    
    173
    -
    
    174
    -void getProcessTimes(Time *user, Time *elapsed)
    
    175
    -{
    
    176
    -    *user    = getProcessCPUTime();
    
    177
    -    *elapsed = getProcessElapsedTime();
    
    178
    -}
    
    179
    -
    
    180
    -#elif defined(HAVE_TIMES)
    
    181
    -
    
    182
    -// we'll use the old times() API.
    
    183
    -
    
    184 52
     Time getProcessCPUTime(void)
    
    185 53
     {
    
    186
    -    Time user, elapsed;
    
    187
    -    getProcessTimes(&user,&elapsed);
    
    188
    -    return user;
    
    189
    -}
    
    190
    -
    
    191
    -Time getProcessElapsedTime(void)
    
    192
    -{
    
    193
    -    Time user, elapsed;
    
    194
    -    getProcessTimes(&user,&elapsed);
    
    195
    -    return elapsed;
    
    196
    -}
    
    197
    -
    
    198
    -void getProcessTimes(Time *user, Time *elapsed)
    
    199
    -{
    
    200
    -    static uint32_t ClockFreq = 0;
    
    201
    -
    
    202
    -    if (ClockFreq == 0) {
    
    203
    -#if defined(HAVE_SYSCONF)
    
    204
    -        long ticks;
    
    205
    -        ticks = sysconf(_SC_CLK_TCK);
    
    206
    -        if ( ticks == -1 ) {
    
    207
    -            sysErrorBelch("sysconf");
    
    208
    -            stg_exit(EXIT_FAILURE);
    
    209
    -        }
    
    210
    -        ClockFreq = ticks;
    
    211
    -#elif defined(CLK_TCK)          /* defined by POSIX */
    
    212
    -        ClockFreq = CLK_TCK;
    
    213
    -#elif defined(HZ)
    
    214
    -        ClockFreq = HZ;
    
    215
    -#elif defined(CLOCKS_PER_SEC)
    
    216
    -        ClockFreq = CLOCKS_PER_SEC;
    
    217
    -#else
    
    218
    -        errorBelch("can't get clock resolution");
    
    219
    -        stg_exit(EXIT_FAILURE);
    
    220
    -#endif
    
    221
    -    }
    
    222
    -
    
    223
    -    struct tms t;
    
    224
    -    clock_t r = times(&t);
    
    225
    -    *user = SecondsToTime(t.tms_utime) / ClockFreq;
    
    226
    -    *elapsed = SecondsToTime(r) / ClockFreq;
    
    54
    +    return getClockTime(CLOCK_PROCESS_CPUTIME_ID);
    
    227 55
     }
    
    228 56
     
    
    229
    -#endif // HAVE_TIMES
    
    230
    -
    
    231
    -void getUnixEpochTime(StgWord64 *sec, StgWord32 *nsec)
    
    57
    +Time getMonotonicTime(void)
    
    232 58
     {
    
    233
    -#if defined(HAVE_GETTIMEOFDAY)
    
    234
    -    struct timeval tv;
    
    235
    -    gettimeofday(&tv, (struct timezone *) NULL);
    
    236
    -    *sec  = tv.tv_sec;
    
    237
    -    *nsec = tv.tv_usec * 1000;
    
    238
    -#else
    
    239
    -    /* Sigh, fall back to second resolution. */
    
    240
    -    time_t t;
    
    241
    -    time(&t);
    
    242
    -    *sec  = t;
    
    243
    -    *nsec = 0;
    
    244
    -#endif
    
    59
    +    return getClockTime(CLOCK_MONOTONIC);
    
    245 60
     }
    
    246 61
     
    
    247
    -W_
    
    248
    -getPageFaults(void)
    
    62
    +Time getWallClockTime(void)
    
    249 63
     {
    
    250
    -#if !defined(HAVE_GETRUSAGE) || defined(haiku_HOST_OS)
    
    251
    -    return 0;
    
    252
    -#else
    
    253
    -    struct rusage t;
    
    254
    -    getrusage(RUSAGE_SELF, &t);
    
    255
    -    return(t.ru_majflt);
    
    256
    -#endif
    
    64
    +    return getClockTime(CLOCK_REALTIME);
    
    257 65
     }

  • rts/posix/OSStats.c
    1
    +/* -----------------------------------------------------------------------------
    
    2
    + *
    
    3
    + * (c) The GHC Team 2005
    
    4
    + *
    
    5
    + * Machine-dependent process statistics functions
    
    6
    + *
    
    7
    + * ---------------------------------------------------------------------------*/
    
    8
    +
    
    9
    +// Not POSIX, due to use of ru_majflt in getPageFaults()
    
    10
    +// #include "rts/PosixSource.h"
    
    11
    +
    
    12
    +#include "Rts.h"
    
    13
    +#include "OSStats.h"
    
    14
    +
    
    15
    +#include <unistd.h>
    
    16
    +
    
    17
    +#if defined(HAVE_SYS_RESOURCE_H)
    
    18
    +# include <sys/resource.h>
    
    19
    +#endif
    
    20
    +
    
    21
    +long getPageFaults(void)
    
    22
    +{
    
    23
    +#if !defined(HAVE_GETRUSAGE) || defined(haiku_HOST_OS)
    
    24
    +    return 0;
    
    25
    +#else
    
    26
    +    struct rusage t;
    
    27
    +    getrusage(RUSAGE_SELF, &t);
    
    28
    +    return(t.ru_majflt);
    
    29
    +#endif
    
    30
    +}

  • rts/posix/Poll.c
    ... ... @@ -370,7 +370,7 @@ void pollCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
    370 370
         ASSERT(iomgr->aiop_poll_table == iomgr->full_poll_table+1);
    
    371 371
     
    
    372 372
         if (!isEmptyTimeoutQueue(iomgr->timeout_queue)) {
    
    373
    -        Time now = getProcessElapsedTime();
    
    373
    +        Time now = getMonotonicTime();
    
    374 374
             processTimeoutCompletions(iomgr, now);
    
    375 375
         }
    
    376 376
     
    
    ... ... @@ -440,7 +440,7 @@ bool awaitCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
    440 440
             ASSERT(!isEmptyTimeoutQueue(iomgr->timeout_queue) ||
    
    441 441
                    !isEmptyClosureTable(&iomgr->aiop_table));
    
    442 442
     
    
    443
    -        Time now = getProcessElapsedTime();
    
    443
    +        Time now = getMonotonicTime();
    
    444 444
             processTimeoutCompletions(iomgr, now);
    
    445 445
     
    
    446 446
             /* If we didn't wake any threads due to expiring timeouts, then we need
    

  • rts/posix/Select.c
    ... ... @@ -34,8 +34,6 @@
    34 34
     #include <errno.h>
    
    35 35
     #include <string.h>
    
    36 36
     
    
    37
    -#include "Clock.h"
    
    38
    -
    
    39 37
     #if defined(IOMGR_ENABLED_SELECT)
    
    40 38
     
    
    41 39
     // The target time for a threadDelay is stored in a one-word quantity
    
    ... ... @@ -93,7 +91,7 @@ void interruptIOManagerSelect(CapIOManager *iomgr)
    93 91
      */
    
    94 92
     static LowResTime getLowResTimeOfDay(void)
    
    95 93
     {
    
    96
    -    return TimeToLowResTimeRoundDown(getProcessElapsedTime());
    
    94
    +    return TimeToLowResTimeRoundDown(getMonotonicTime());
    
    97 95
     }
    
    98 96
     
    
    99 97
     /*
    
    ... ... @@ -102,7 +100,7 @@ static LowResTime getLowResTimeOfDay(void)
    102 100
     LowResTime getDelayTarget (HsInt us)
    
    103 101
     {
    
    104 102
         Time elapsed;
    
    105
    -    elapsed = getProcessElapsedTime();
    
    103
    +    elapsed = getMonotonicTime();
    
    106 104
     
    
    107 105
         // If the desired target would be larger than the maximum Time,
    
    108 106
         // default to the maximum Time. (#7087)
    

  • rts/posix/SelectBis.c
    ... ... @@ -355,7 +355,7 @@ static bool processIOCompletions(CapIOManager *iomgr, int ncompletions)
    355 355
     void pollCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr)
    
    356 356
     {
    
    357 357
         if (!isEmptyTimeoutQueue(iomgr->timeout_queue)) {
    
    358
    -        Time now = getProcessElapsedTime();
    
    358
    +        Time now = getMonotonicTime();
    
    359 359
             processTimeoutCompletions(iomgr, now);
    
    360 360
         }
    
    361 361
     
    
    ... ... @@ -406,7 +406,7 @@ bool awaitCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr)
    406 406
             ASSERT(!isEmptyTimeoutQueue(iomgr->timeout_queue) ||
    
    407 407
                    !isEmptyClosureTable(&iomgr->aiop_table));
    
    408 408
     
    
    409
    -        Time now = getProcessElapsedTime();
    
    409
    +        Time now = getMonotonicTime();
    
    410 410
             processTimeoutCompletions(iomgr, now);
    
    411 411
     
    
    412 412
             /* If we didn't wake any threads due to expiring timeouts, then we need
    

  • rts/posix/Ticker.c
    ... ... @@ -64,9 +64,11 @@
    64 64
     #include "RtsUtils.h"
    
    65 65
     #include "Proftimer.h"
    
    66 66
     #include "Schedule.h"
    
    67
    -#include "posix/Clock.h"
    
    68 67
     #include "posix/FdWakeup.h"
    
    69 68
     
    
    69
    +#include <unistd.h>
    
    70
    +#include <time.h>
    
    71
    +
    
    70 72
     #if defined(HAVE_DECL_PPOLL) && HAVE_DECL_PPOLL == 1
    
    71 73
     /* We prefer the ppoll() function if available since it allows sanely waiting
    
    72 74
      * on a single fd with precise timeouts (nanosecond precision). It is not in
    
    ... ... @@ -85,11 +87,6 @@
    85 87
     #include <sys/select.h>
    
    86 88
     #endif
    
    87 89
     
    
    88
    -#include <time.h>
    
    89
    -#if HAVE_SYS_TIME_H
    
    90
    -# include <sys/time.h>
    
    91
    -#endif
    
    92
    -
    
    93 90
     #if defined(HAVE_SIGNAL_H)
    
    94 91
     # include <signal.h>
    
    95 92
     #endif
    
    ... ... @@ -100,7 +97,6 @@
    100 97
     #if defined(HAVE_PTHREAD_NP_H)
    
    101 98
     #include <pthread_np.h>
    
    102 99
     #endif
    
    103
    -#include <unistd.h>
    
    104 100
     #include <fcntl.h>
    
    105 101
     
    
    106 102
     
    

  • rts/posix/Timeout.c
    ... ... @@ -30,7 +30,7 @@
    30 30
     
    
    31 31
     bool syncDelayTimeout(CapIOManager *iomgr, StgTSO *tso, HsInt us_delay)
    
    32 32
     {
    
    33
    -    Time now = getProcessElapsedTime();
    
    33
    +    Time now = getMonotonicTime();
    
    34 34
         Time target;
    
    35 35
     
    
    36 36
         /* If the desired target would be larger than the maximum Time,
    

  • rts/rts.cabal
    ... ... @@ -413,6 +413,7 @@ library
    413 413
                      Disassembler.c
    
    414 414
                      FileLock.c
    
    415 415
                      ForeignExports.c
    
    416
    +                 GetTime.c
    
    416 417
                      Globals.c
    
    417 418
                      Hash.c
    
    418 419
                      Heap.c
    
    ... ... @@ -553,6 +554,7 @@ library
    553 554
                         win32/GetTime.c
    
    554 555
                         win32/MIOManager.c
    
    555 556
                         win32/OSMem.c
    
    557
    +                    win32/OSStats.c
    
    556 558
                         win32/OSThreads.c
    
    557 559
                         win32/ThrIOManager.c
    
    558 560
                         win32/Ticker.c
    
    ... ... @@ -564,6 +566,7 @@ library
    564 566
             c-sources: wasm/StgRun.c
    
    565 567
                        wasm/GetTime.c
    
    566 568
                        wasm/OSMem.c
    
    569
    +                   wasm/OSStats.c
    
    567 570
                        wasm/OSThreads.c
    
    568 571
                        wasm/JSFFI.c
    
    569 572
                        wasm/JSFFIGlobals.c
    
    ... ... @@ -579,6 +582,7 @@ library
    579 582
                         posix/GetTime.c
    
    580 583
                         posix/Ticker.c
    
    581 584
                         posix/OSMem.c
    
    585
    +                    posix/OSStats.c
    
    582 586
                         posix/OSThreads.c
    
    583 587
                         posix/FdWakeup.c
    
    584 588
                         posix/MIO.c
    

  • rts/sm/GC.c
    ... ... @@ -1518,7 +1518,7 @@ waitForGcThreads (Capability *cap, bool idle_cap[])
    1518 1518
         uint32_t i, cur_n_gc_entered;
    
    1519 1519
         Time t0, t1, t2;
    
    1520 1520
     
    
    1521
    -    t0 = t1 = t2 = getProcessElapsedTime();
    
    1521
    +    t0 = t1 = t2 = getMonotonicTime();
    
    1522 1522
     
    
    1523 1523
         for(i = 0; i < getNumCapabilities(); ++i) {
    
    1524 1524
             if (i == me || idle_cap[i]) {
    
    ... ... @@ -1542,7 +1542,7 @@ waitForGcThreads (Capability *cap, bool idle_cap[])
    1542 1542
             // this 1ms timeout is not well justified. It's the shortest timeout we
    
    1543 1543
             // can use on windows. It seems to work well for me.
    
    1544 1544
             timedWaitCondition(&gc_entry_arrived_cv, &gc_entry_mutex, USToTime(1000));
    
    1545
    -        t2 = getProcessElapsedTime();
    
    1545
    +        t2 = getMonotonicTime();
    
    1546 1546
             if (RtsFlags.GcFlags.longGCSync != 0 &&
    
    1547 1547
                 t2 - t1 > RtsFlags.GcFlags.longGCSync) {
    
    1548 1548
                 // best not to hold the mutex while we call a hook function
    

  • rts/wasm/GetTime.c
    ... ... @@ -6,19 +6,13 @@
    6 6
      *
    
    7 7
      * ---------------------------------------------------------------------------*/
    
    8 8
     
    
    9
    -// Not POSIX, due to use of ru_majflt in getPageFaults()
    
    10
    -// #include "rts/PosixSource.h"
    
    11
    -
    
    9
    +#include "rts/PosixSource.h"
    
    12 10
     #include "Rts.h"
    
    13 11
     #include "GetTime.h"
    
    14 12
     
    
    15 13
     #include <time.h>
    
    16 14
     #include <sys/time.h>
    
    17 15
     
    
    18
    -void initializeTimer(void)
    
    19
    -{
    
    20
    -}
    
    21
    -
    
    22 16
     static Time getClockTime(clockid_t clock)
    
    23 17
     {
    
    24 18
         struct timespec ts;
    
    ... ... @@ -41,32 +35,14 @@ Time getProcessCPUTime(void)
    41 35
         return getClockTime(CLOCK_MONOTONIC);
    
    42 36
     }
    
    43 37
     
    
    44
    -StgWord64 getMonotonicNSec(void)
    
    38
    +Time getMonotonicTime(void)
    
    45 39
     {
    
    46 40
         return getClockTime(CLOCK_MONOTONIC);
    
    47 41
     }
    
    48 42
     
    
    49
    -Time getProcessElapsedTime(void)
    
    50
    -{
    
    51
    -    return NSToTime(getMonotonicNSec());
    
    52
    -}
    
    53
    -
    
    54
    -void getProcessTimes(Time *user, Time *elapsed)
    
    55
    -{
    
    56
    -    *user    = getProcessCPUTime();
    
    57
    -    *elapsed = getProcessElapsedTime();
    
    58
    -}
    
    59
    -
    
    60
    -void getUnixEpochTime(StgWord64 *sec, StgWord32 *nsec)
    
    43
    +Time getWallClockTime(void)
    
    61 44
     {
    
    62 45
         struct timeval tv;
    
    63 46
         gettimeofday(&tv, (struct timezone *) NULL);
    
    64
    -    *sec  = tv.tv_sec;
    
    65
    -    *nsec = tv.tv_usec * 1000;
    
    66
    -}
    
    67
    -
    
    68
    -W_
    
    69
    -getPageFaults(void)
    
    70
    -{
    
    71
    -  return 0;
    
    47
    +    return SecondsToTime(tv.tv_sec) + USToTime(tv.tv_usec);
    
    72 48
     }

  • rts/wasm/OSStats.c
    1
    +/* -----------------------------------------------------------------------------
    
    2
    + *
    
    3
    + * (c) The GHC Team 2005
    
    4
    + *
    
    5
    + * Machine-dependent process statistics functions
    
    6
    + *
    
    7
    + * ---------------------------------------------------------------------------*/
    
    8
    +
    
    9
    +#include "Rts.h"
    
    10
    +#include "OSStats.h"
    
    11
    +
    
    12
    +long getPageFaults(void)
    
    13
    +{
    
    14
    +    return 0;
    
    15
    +}

  • rts/win32/GetTime.c
    ... ... @@ -15,8 +15,7 @@
    15 15
     
    
    16 16
     /* Convert FILETIMEs into secs */
    
    17 17
     
    
    18
    -static inline Time
    
    19
    -fileTimeToRtsTime(FILETIME ft)
    
    18
    +static inline Time fileTimeToRtsTime(FILETIME ft)
    
    20 19
     {
    
    21 20
         Time t;
    
    22 21
         t = ((Time)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
    
    ... ... @@ -25,15 +24,7 @@ fileTimeToRtsTime(FILETIME ft)
    25 24
         return t;
    
    26 25
     }
    
    27 26
     
    
    28
    -void
    
    29
    -getProcessTimes(Time *user, Time *elapsed)
    
    30
    -{
    
    31
    -    *user    = getProcessCPUTime();
    
    32
    -    *elapsed = getProcessElapsedTime();
    
    33
    -}
    
    34
    -
    
    35
    -Time
    
    36
    -getCurrentThreadCPUTime(void)
    
    27
    +Time getCurrentThreadCPUTime(void)
    
    37 28
     {
    
    38 29
         FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
    
    39 30
     
    
    ... ... @@ -46,8 +37,7 @@ getCurrentThreadCPUTime(void)
    46 37
         return fileTimeToRtsTime(userTime);
    
    47 38
     }
    
    48 39
     
    
    49
    -Time
    
    50
    -getProcessCPUTime(void)
    
    40
    +Time getProcessCPUTime(void)
    
    51 41
     {
    
    52 42
         FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
    
    53 43
     
    
    ... ... @@ -59,108 +49,57 @@ getProcessCPUTime(void)
    59 49
         return fileTimeToRtsTime(userTime);
    
    60 50
     }
    
    61 51
     
    
    62
    -// Number of ticks per second used by the QueryPerformanceFrequency
    
    63
    -// implementation, represented by a 64-bit union type.
    
    64
    -static LARGE_INTEGER qpc_frequency = {.QuadPart = 0};
    
    65
    -
    
    66
    -// Initialize qpc_frequency. This function should be called before any call to
    
    67
    -// getMonotonicNSec.  If QPC is not supported on this system, qpc_frequency is
    
    68
    -// set to 0.
    
    69
    -void initializeTimer(void)
    
    52
    +Time getMonotonicTime(void)
    
    70 53
     {
    
    71
    -    BOOL qpc_supported = QueryPerformanceFrequency(&qpc_frequency);
    
    72
    -    if (!qpc_supported)
    
    73
    -    {
    
    74
    -        qpc_frequency.QuadPart = 0;
    
    54
    +    // https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
    
    55
    +    // The advice there is use QueryPerformanceCounter, and that this is now
    
    56
    +    // always supported in Windows XP and later, and reliable from Vista.
    
    57
    +
    
    58
    +    // Number of ticks per second, provided by QueryPerformanceFrequency
    
    59
    +    static Time ticks_per_sec = 0;
    
    60
    +
    
    61
    +    // Initialize ticks_per_sec on the first call. It should not matter if two
    
    62
    +    // threads race to do this. The result should be the same.
    
    63
    +    if (RTS_UNLIKELY(RELAXED_LOAD_ALWAYS(&ticks_per_sec) == 0)) {
    
    64
    +        LARGE_INTEGER qpc_frequency; // a 64-bit union type
    
    65
    +        if (QueryPerformanceFrequency(&qpc_frequency)) {
    
    66
    +            RELAXED_STORE_ALWAYS(&ticks_per_sec, qpc_frequency.QuadPart);
    
    67
    +        } else {
    
    68
    +            // This should be impossible now.
    
    69
    +            sysErrorBelch("getMonotonicTime: high res timestamps not available");
    
    70
    +            stg_exit(EXIT_FAILURE);
    
    71
    +        }
    
    75 72
         }
    
    76
    -}
    
    77
    -
    
    78
    -HsWord64
    
    79
    -getMonotonicNSec(void)
    
    80
    -{
    
    81
    -    if (qpc_frequency.QuadPart)
    
    82
    -    {
    
    83
    -        // system_time is a 64-bit union type used to represent the
    
    84
    -        // tick count returned by QueryPerformanceCounter
    
    85
    -        LARGE_INTEGER system_time;
    
    86
    -
    
    87
    -        // get the tick count.
    
    88
    -        QueryPerformanceCounter(&system_time);
    
    89
    -
    
    90
    -        // compute elapsed seconds as double
    
    91
    -        double secs = (double)system_time.QuadPart /
    
    92
    -                      (double)qpc_frequency.QuadPart;
    
    93 73
     
    
    94
    -        // return elapsed time in nanoseconds
    
    95
    -        return (HsWord64)(secs * 1e9);
    
    96
    -    }
    
    97
    -    else // fallback to GetTickCount
    
    98
    -    {
    
    99
    -        // TODO: Remove this code path, it cannot be taken because
    
    100
    -        // `QueryPerformanceFrequency` cannot fail on Windows >= XP
    
    101
    -        // and GHC no longer supports Windows <= XP.
    
    102
    -        // See https://gitlab.haskell.org/ghc/ghc/issues/14233
    
    103
    -
    
    104
    -        // NOTE: GetTickCount is a 32-bit millisecond value, so it wraps around
    
    105
    -        // every 49 days.
    
    106
    -        DWORD count = GetTickCount();
    
    107
    -
    
    108
    -        // getTickCount is in milliseconds, so multiply it by 1000000 to get
    
    109
    -        // nanoseconds.
    
    110
    -        return (HsWord64)count * 1000000;
    
    111
    -    }
    
    112
    -}
    
    74
    +    // Get the tick count.
    
    75
    +    LARGE_INTEGER tick_count;
    
    76
    +    QueryPerformanceCounter(&tick_count);
    
    113 77
     
    
    114
    -Time
    
    115
    -getProcessElapsedTime(void)
    
    116
    -{
    
    117
    -    return NSToTime(getMonotonicNSec());
    
    78
    +    // Compute elapsed time in nanoseconds. If avoiding float, we must multiply
    
    79
    +    // by nanoseconds first (1e9) and then divide by ticks per sec. The
    
    80
    +    // intermediate result can overflow a 64bit integer. So we use 128bit.
    
    81
    +    return ((__uint128_t)tick_count.QuadPart * (__uint128_t)1000000000ull)
    
    82
    +         / (__uint128_t)ticks_per_sec;
    
    118 83
     }
    
    119 84
     
    
    120
    -void
    
    121
    -getUnixEpochTime(StgWord64 *sec, StgWord32 *nsec)
    
    85
    +Time getWallClockTime(void)
    
    122 86
     {
    
    123
    -    /* Windows has a bunch of time APIs but none that directly give
    
    124
    -       us unix epoch time, so we have to do a little dance. */
    
    125
    -
    
    126
    -    SYSTEMTIME systime;
    
    127
    -    FILETIME filetime;
    
    128
    -    ULARGE_INTEGER unixtime;
    
    129
    -
    
    130
    -    /* Windows SYSTEMTIME is a big struct with fields for
    
    131
    -       year, month, day, hour, minute, second, millisecond. */
    
    132
    -    GetSystemTime(&systime);
    
    133 87
         /* Windows FILETIME timestamps use an epoch-based time,
    
    134 88
            using a 64bit unsigned word. The time is measured in
    
    135 89
            units of 100 nanoseconds since an epoch of 1601. */
    
    136
    -    SystemTimeToFileTime(&systime, &filetime);
    
    90
    +    FILETIME filetime;
    
    91
    +    GetSystemTimePreciseAsFileTime(&filetime);
    
    137 92
     
    
    138 93
         /* FILETIME isn't directly a 64bit word, but a struct with
    
    139 94
            a pair of 32bit words, so we have to convert via a
    
    140 95
            ULARGE_INTEGER struct which is a handy union type */
    
    96
    +    ULARGE_INTEGER unixtime;
    
    141 97
         unixtime.LowPart  = filetime.dwLowDateTime;
    
    142 98
         unixtime.HighPart = filetime.dwHighDateTime;
    
    143 99
     
    
    144 100
         /* We have to do an epoch conversion, since FILETIME uses 1601
    
    145 101
            while we want unix epoch of 1970. In case you were wondering,
    
    146 102
            there were 11,644,473,600 seconds between 1601 and 1970, then
    
    147
    -       multiply by 10^7 for units of 100 nanoseconds. */
    
    148
    -    unixtime.QuadPart = unixtime.QuadPart - 116444736000000000ull;
    
    149
    -
    
    150
    -    /* For the seconds part we use integer division by 10^7 */
    
    151
    -    *sec  = unixtime.QuadPart / 10000000ull;
    
    152
    -
    
    153
    -    /* The remainder from integer division by 10^7 gives us
    
    154
    -       the sub-second component in units of 100 nanoseconds.
    
    155
    -       So for nanoseconds we just multiply by 100.
    
    156
    -       Note that nanoseconds always fits in a 32bit word */
    
    157
    -    *nsec = ((unsigned long)(unixtime.QuadPart % 10000000ull)) * 100ul;
    
    158
    -}
    
    159
    -
    
    160
    -W_
    
    161
    -getPageFaults(void)
    
    162
    -{
    
    163
    -  /* ToDo (on NT): better, get this via the performance data
    
    164
    -     that's stored in the registry. */
    
    165
    -    return 0;
    
    103
    +       multiply by 100 for units of 100 nanoseconds. */
    
    104
    +    return ((Time)unixtime.QuadPart - 116444736000000000ull) * 100ull;
    
    166 105
     }

  • rts/win32/OSStats.c
    1
    +/* -----------------------------------------------------------------------------
    
    2
    + *
    
    3
    + * (c) The GHC Team 2005
    
    4
    + *
    
    5
    + * Machine-dependent process statistics functions
    
    6
    + *
    
    7
    + * ---------------------------------------------------------------------------*/
    
    8
    +
    
    9
    +#include "Rts.h"
    
    10
    +#include "OSStats.h"
    
    11
    +
    
    12
    +long getPageFaults(void)
    
    13
    +{
    
    14
    +  /* ToDo (on NT): better, get this via the performance data
    
    15
    +     that's stored in the registry. */
    
    16
    +    return 0;
    
    17
    +}

  • testsuite/tests/rts/TimeoutQueue.c
    ... ... @@ -227,7 +227,6 @@ int main_bench (bool showtiming)
    227 227
     
    
    228 228
         StgTimeoutQueue *root = EMPTY;
    
    229 229
         Time before, after;
    
    230
    -    initializeTimer();
    
    231 230
     
    
    232 231
         /* Init */
    
    233 232
         StgTimeoutQueue *elems = calloc(N, sizeof(StgTimeoutQueue));