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.
This commit is contained in:
Yousef Ahmed 2026-04-06 13:33:03 +02:00
parent 067d766f21
commit a4e4b102e3

View file

@ -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