From a4e4b102e3997f4a96fd12df6b5348905d1fc804 Mon Sep 17 00:00:00 2001 From: Yousef Ahmed Date: Mon, 6 Apr 2026 13:33:03 +0200 Subject: [PATCH] Align per peer lock to cache line boundary in ngx_http_upstream_rr_peer_s Before: ngx_uint_t down; /* 144 8 */ unsigned route:1; /* 152 4 */ unsigned zombie:1; /* 152 4 */ ngx_atomic_t lock; /* 160 8 */ <-- CL2, offset 32 ngx_uint_t refs; /* 168 8 */ lock shares cacheline 2 with down, slow_start, start_time, route, zombie. Workers spinning on lock invalidate these fields for the lock holder. down/slow_start/start_time are read without the lock in the health check and weight calculation paths. After: /* --- cacheline 3 boundary (192 bytes) --- */ ngx_atomic_t lock; /* 192 8 */ <-- CL3, offset 0 ngx_uint_t refs; /* 200 8 */ ngx_http_upstream_host_t *host; /* 208 8 */ lock starts cacheline 3. refs and host are only accessed while holding the lock, so sharing is harmless. This roughly costs 48 bytes padding per upstream peer in the shared zone. --- src/http/ngx_http_upstream_round_robin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/http/ngx_http_upstream_round_robin.h b/src/http/ngx_http_upstream_round_robin.h index 95c7ae190..28308dbc0 100644 --- a/src/http/ngx_http_upstream_round_robin.h +++ b/src/http/ngx_http_upstream_round_robin.h @@ -80,7 +80,7 @@ struct ngx_http_upstream_rr_peer_s { #if (NGX_HTTP_UPSTREAM_ZONE) unsigned zombie:1; - ngx_atomic_t lock; + ngx_atomic_t lock __attribute__((aligned(64))); ngx_uint_t refs; ngx_http_upstream_host_t *host; #endif