Skip to content

Commit 14966c8

Browse files
committed
MINOR: clock: make global_now_ns a pointer as well
Similar to previous commit but for global_now_ns
1 parent 4a20b38 commit 14966c8

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

include/haproxy/clock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
extern struct timeval start_date; /* the process's start date in wall-clock time */
2929
extern struct timeval ready_date; /* date when the process was considered ready */
3030
extern ullong start_time_ns; /* the process's start date in internal monotonic time (ns) */
31-
extern volatile ullong global_now_ns; /* common monotonic date between all threads, in ns (wraps every 585 yr) */
31+
extern volatile ullong *global_now_ns;/* common monotonic date between all threads, in ns (wraps every 585 yr) */
3232

3333
extern THREAD_LOCAL ullong now_ns; /* internal monotonic date derived from real clock, in ns (wraps every 585 yr) */
3434
extern THREAD_LOCAL struct timeval date; /* the real current date (wall-clock time) */

src/clock.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
struct timeval start_date; /* the process's start date in wall-clock time */
3030
struct timeval ready_date; /* date when the process was considered ready */
3131
ullong start_time_ns; /* the process's start date in internal monotonic time (ns) */
32-
volatile ullong global_now_ns; /* common monotonic date between all threads, in ns (wraps every 585 yr) */
32+
volatile ullong _global_now_ns; /* locally stored common monotonic date between all threads, in ns (wraps every 585 yr) */
33+
volatile ullong *global_now_ns; /* common monotonic date, may point to _global_now_ns or shared memory */
3334
volatile uint _global_now_ms; /* locally stored common monotonic date in milliseconds (may wrap) */
3435
volatile uint *global_now_ms; /* common monotonic date in milliseconds (may wrap), may point to _global_now_ms or shared memory */
3536

@@ -239,7 +240,7 @@ void clock_update_local_date(int max_wait, int interrupted)
239240
now_ns += ms_to_ns(max_wait);
240241

241242
/* consider the most recent known date */
242-
now_ns = MAX(now_ns, HA_ATOMIC_LOAD(&global_now_ns));
243+
now_ns = MAX(now_ns, HA_ATOMIC_LOAD(global_now_ns));
243244

244245
/* this event is rare, but it requires proper handling because if
245246
* we just left now_ns where it was, the date will not be updated
@@ -270,7 +271,7 @@ void clock_update_global_date()
270271
* realistic regarding the global date, which only moves forward,
271272
* otherwise catch up.
272273
*/
273-
old_now_ns = _HA_ATOMIC_LOAD(&global_now_ns);
274+
old_now_ns = _HA_ATOMIC_LOAD(global_now_ns);
274275
old_now_ms = _HA_ATOMIC_LOAD(global_now_ms);
275276

276277
do {
@@ -300,7 +301,7 @@ void clock_update_global_date()
300301
/* let's try to update the global_now_ns (both in nanoseconds
301302
* and ms forms) or loop again.
302303
*/
303-
} while ((!_HA_ATOMIC_CAS(&global_now_ns, &old_now_ns, now_ns) ||
304+
} while ((!_HA_ATOMIC_CAS(global_now_ns, &old_now_ns, now_ns) ||
304305
(now_ms != old_now_ms && !_HA_ATOMIC_CAS(global_now_ms, &old_now_ms, now_ms))) &&
305306
__ha_cpu_relax());
306307

@@ -323,10 +324,10 @@ void clock_init_process_date(void)
323324
th_ctx->prev_mono_time = th_ctx->curr_mono_time = before_poll_mono_ns;
324325
gettimeofday(&date, NULL);
325326
after_poll = before_poll = date;
326-
global_now_ns = th_ctx->curr_mono_time;
327-
if (!global_now_ns) // CLOCK_MONOTONIC not supported
328-
global_now_ns = tv_to_ns(&date);
329-
now_ns = global_now_ns;
327+
_global_now_ns = th_ctx->curr_mono_time;
328+
if (!_global_now_ns) // CLOCK_MONOTONIC not supported
329+
_global_now_ns = tv_to_ns(&date);
330+
now_ns = _global_now_ns;
330331

331332
_global_now_ms = ns_to_ms(now_ns);
332333

@@ -337,8 +338,8 @@ void clock_init_process_date(void)
337338
* match and continue from this shifted date.
338339
*/
339340
now_offset = sec_to_ns((uint)((uint)(-_global_now_ms) / 1000U - BOOT_TIME_WRAP_SEC));
340-
global_now_ns += now_offset;
341-
now_ns = global_now_ns;
341+
_global_now_ns += now_offset;
342+
now_ns = _global_now_ns;
342343
now_ms = ns_to_ms(now_ns);
343344
/* correct for TICK_ETNERITY (0) */
344345
if (now_ms == TICK_ETERNITY)
@@ -347,6 +348,8 @@ void clock_init_process_date(void)
347348

348349
/* for now global_now_ms points to the process-local _global_now_ms */
349350
global_now_ms = &_global_now_ms;
351+
/* same goes for global_ns_ns */
352+
global_now_ns = &_global_now_ns;
350353

351354
th_ctx->idle_pct = 100;
352355
clock_update_date(0, 1);
@@ -369,7 +372,7 @@ void clock_init_thread_date(void)
369372
gettimeofday(&date, NULL);
370373
after_poll = before_poll = date;
371374

372-
now_ns = _HA_ATOMIC_LOAD(&global_now_ns);
375+
now_ns = _HA_ATOMIC_LOAD(global_now_ns);
373376
th_ctx->idle_pct = 100;
374377
th_ctx->prev_cpu_time = now_cpu_time();
375378
th_ctx->prev_mono_time = now_mono_time();

0 commit comments

Comments
 (0)