]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: clock: make global_now_ns a pointer as well
authorAurelien DARRAGON <adarragon@haproxy.com>
Thu, 26 Jun 2025 10:58:07 +0000 (12:58 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Tue, 29 Jul 2025 16:04:15 +0000 (18:04 +0200)
Similar to previous commit but for global_now_ns

include/haproxy/clock.h
src/clock.c

index 264363e27b0c18c3cec69fbd69b10c3066838ae6..664e53968980c11a1c3226c6338bf7e84632ec4e 100644 (file)
@@ -28,7 +28,7 @@
 extern struct timeval              start_date;    /* the process's start date in wall-clock time */
 extern struct timeval              ready_date;    /* date when the process was considered ready */
 extern ullong                      start_time_ns; /* the process's start date in internal monotonic time (ns) */
-extern volatile ullong             global_now_ns; /* common monotonic date between all threads, in ns (wraps every 585 yr) */
+extern volatile ullong             *global_now_ns;/* common monotonic date between all threads, in ns (wraps every 585 yr) */
 
 extern THREAD_LOCAL ullong         now_ns;        /* internal monotonic date derived from real clock, in ns (wraps every 585 yr) */
 extern THREAD_LOCAL struct timeval date;          /* the real current date (wall-clock time) */
index f0ef35cc88987ff5eb77146d1c8124872c5c798b..16270aac332acf298afd8fd1678d7dba7e2b2bf1 100644 (file)
@@ -29,7 +29,8 @@
 struct timeval                   start_date;      /* the process's start date in wall-clock time */
 struct timeval                   ready_date;      /* date when the process was considered ready */
 ullong                           start_time_ns;   /* the process's start date in internal monotonic time (ns) */
-volatile ullong                  global_now_ns;   /* common monotonic date between all threads, in ns (wraps every 585 yr) */
+volatile ullong                  _global_now_ns;   /* locally stored common monotonic date between all threads, in ns (wraps every 585 yr) */
+volatile ullong                        *global_now_ns;    /* common monotonic date, may point to _global_now_ns or shared memory */
 volatile uint                    _global_now_ms;   /* locally stored common monotonic date in milliseconds (may wrap) */
 volatile uint                  *global_now_ms;    /* common monotonic date in milliseconds (may wrap), may point to _global_now_ms or shared memory */
 
@@ -239,7 +240,7 @@ void clock_update_local_date(int max_wait, int interrupted)
                        now_ns += ms_to_ns(max_wait);
 
                /* consider the most recent known date */
-               now_ns = MAX(now_ns, HA_ATOMIC_LOAD(&global_now_ns));
+               now_ns = MAX(now_ns, HA_ATOMIC_LOAD(global_now_ns));
 
                /* this event is rare, but it requires proper handling because if
                 * we just left now_ns where it was, the date will not be updated
@@ -270,7 +271,7 @@ void clock_update_global_date()
         * realistic regarding the global date, which only moves forward,
         * otherwise catch up.
         */
-       old_now_ns = _HA_ATOMIC_LOAD(&global_now_ns);
+       old_now_ns = _HA_ATOMIC_LOAD(global_now_ns);
        old_now_ms = _HA_ATOMIC_LOAD(global_now_ms);
 
        do {
@@ -300,7 +301,7 @@ void clock_update_global_date()
                /* let's try to update the global_now_ns (both in nanoseconds
                 * and ms forms) or loop again.
                 */
-       } while ((!_HA_ATOMIC_CAS(&global_now_ns, &old_now_ns, now_ns) ||
+       } while ((!_HA_ATOMIC_CAS(global_now_ns, &old_now_ns, now_ns) ||
                  (now_ms  != old_now_ms && !_HA_ATOMIC_CAS(global_now_ms, &old_now_ms, now_ms))) &&
                 __ha_cpu_relax());
 
@@ -323,10 +324,10 @@ void clock_init_process_date(void)
        th_ctx->prev_mono_time = th_ctx->curr_mono_time = before_poll_mono_ns;
        gettimeofday(&date, NULL);
        after_poll = before_poll = date;
-       global_now_ns = th_ctx->curr_mono_time;
-       if (!global_now_ns) // CLOCK_MONOTONIC not supported
-               global_now_ns = tv_to_ns(&date);
-       now_ns = global_now_ns;
+       _global_now_ns = th_ctx->curr_mono_time;
+       if (!_global_now_ns) // CLOCK_MONOTONIC not supported
+               _global_now_ns = tv_to_ns(&date);
+       now_ns = _global_now_ns;
 
        _global_now_ms = ns_to_ms(now_ns);
 
@@ -337,8 +338,8 @@ void clock_init_process_date(void)
         * match and continue from this shifted date.
         */
        now_offset = sec_to_ns((uint)((uint)(-_global_now_ms) / 1000U - BOOT_TIME_WRAP_SEC));
-       global_now_ns += now_offset;
-       now_ns = global_now_ns;
+       _global_now_ns += now_offset;
+       now_ns = _global_now_ns;
        now_ms = ns_to_ms(now_ns);
        /* correct for TICK_ETNERITY (0) */
        if (now_ms == TICK_ETERNITY)
@@ -347,6 +348,8 @@ void clock_init_process_date(void)
 
        /* for now global_now_ms points to the process-local _global_now_ms */
        global_now_ms = &_global_now_ms;
+       /* same goes for global_ns_ns */
+       global_now_ns = &_global_now_ns;
 
        th_ctx->idle_pct = 100;
        clock_update_date(0, 1);
@@ -369,7 +372,7 @@ void clock_init_thread_date(void)
        gettimeofday(&date, NULL);
        after_poll = before_poll = date;
 
-       now_ns = _HA_ATOMIC_LOAD(&global_now_ns);
+       now_ns = _HA_ATOMIC_LOAD(global_now_ns);
        th_ctx->idle_pct = 100;
        th_ctx->prev_cpu_time  = now_cpu_time();
        th_ctx->prev_mono_time = now_mono_time();