Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rAGBf-005hx6-U0 for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Dec 2023 21:09:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rAGBe-001GHS-4S for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Dec 2023 21:09:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rAGBd-001GHK-Q1 for pgsql-hackers@lists.postgresql.org; Mon, 04 Dec 2023 21:09:05 +0000 Received: from mail-il1-x134.google.com ([2607:f8b0:4864:20::134]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rAGBa-00A6w6-3F for pgsql-hackers@postgresql.org; Mon, 04 Dec 2023 21:09:05 +0000 Received: by mail-il1-x134.google.com with SMTP id e9e14a558f8ab-35d718382b7so4873715ab.1 for ; Mon, 04 Dec 2023 13:09:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701724140; x=1702328940; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=DMz0fB19QbECBQv/hU6BOydzDjPvLdmr1emiK+o1K7s=; b=LhRSBWhr+7fXdhjoFB/Nz0Q2eTIWOvGppAUWxFoq6k7RMX0S8EtHcQTBof6KOwHnl2 a+N8KWc3tdN8JvN+CHHqzkog+8F3znWPj+sYPqu7yljS0TOXGkR5VLC+bUMQ3PAcXjsj EfHnQmAIS2nhe+gs/61ngSn8jlpThXHK4xzoQj8xOBIBNb3Yjo8/piwfdCMhihlr7YnT EW9GMPKNOjcdYcJMxf07qZSMplf53tPm+M4il2LWjHMKoxLUgg70upUIuzjiNPbywIDQ 5GTGEDbCavNLgNiRUfqvPfPrxkGz8mjGPxCN8k9sKReZh9UoblrRAhin8l+5m1egLvA9 kW0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701724140; x=1702328940; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=DMz0fB19QbECBQv/hU6BOydzDjPvLdmr1emiK+o1K7s=; b=hO8ErnsnP7Nvaf/wsruAyV+w2tkr0NvG6slAbA7KeHYNMp3bSw1CNHoaQk5MqFLsrD evocCRe9lOTvlYE6l6UbQzMYRdXlNOISed68vRiEN+rYpnPASSXbI/jPcqJs346l7zLV te5q9ne4Bl863XS7aLOzZp8iVq6n9s3LPKf3kuLcJLNOlCKVxbjR8F7VetzwccBfkSx8 xaDMEH1xqcdh09e766t0fDCEiu2fJmdvA2u3ulCJ2h+a7PG2AHM82q6kKqw6WfKLUbE4 SY+bti83pAiiSkCrLJtt8xGYW+h2nTyTdip/obl8/WzufgVnCElQ5ZmlvMqvuh99Am6C aAbw== X-Gm-Message-State: AOJu0YyacUWMvR/YunYdeoQK4v5Ktmpg0SZft+YIamW695zVwVQPJ1hV 7SBMC1ZLKSYRNSs92FaRFyTetLcokbY= X-Google-Smtp-Source: AGHT+IHTM6YmXOEmgXtutIdvHcshahH17UIBRHUBTS7123VvdeXPbE2JAPi4GNUh+1VM+dTEdjr0VQ== X-Received: by 2002:a05:6e02:260a:b0:35d:6f9f:5743 with SMTP id by10-20020a056e02260a00b0035d6f9f5743mr3401918ilb.57.1701724139971; Mon, 04 Dec 2023 13:08:59 -0800 (PST) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id g19-20020a0566380bd300b0045a04a88b0csm2730926jad.86.2023.12.04.13.08.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 13:08:59 -0800 (PST) Date: Mon, 4 Dec 2023 15:08:57 -0600 From: Nathan Bossart To: Andres Freund Cc: pgsql-hackers@postgresql.org Subject: Re: optimize atomic exchanges Message-ID: <20231204210857.GA2203877@nathanxps13> References: <20231129212905.GA1258737@nathanxps13> <20231201031815.GA1519459@nathanxps13> <20231201035627.7xke2qvec6p5lihy@awork3.anarazel.de> <20231201043522.GB1577008@nathanxps13> <20231204181805.GA2148732@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="y0ulUmNC+osPPQO6" Content-Disposition: inline In-Reply-To: <20231204181805.GA2148732@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --y0ulUmNC+osPPQO6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Dec 04, 2023 at 12:18:05PM -0600, Nathan Bossart wrote: > Barring objections or additional feedback, I think I'm inclined to press > forward with this one and commit it in the next week or two. I'm currently > planning to keep the inline assembly, but I'm considering removing the > configuration checks for __atomic_exchange_n() if the availability of > __atomic_compare_exchange_n() seems like a reliable indicator of its > presence. Thoughts? Concretely, like this. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --y0ulUmNC+osPPQO6 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-Optimize-pg_atomic_exchange_u-32-64.patch" From c5f5502dc9fed267011d0f3cd34522d8b7ed4ed2 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 4 Dec 2023 15:05:18 -0600 Subject: [PATCH v2 1/1] Optimize pg_atomic_exchange_u[32|64]. Reviewed-by: Andres Freund Discussion: https://postgr.es/m/20231129212905.GA1258737%40nathanxps13 --- src/include/port/atomics/arch-x86.h | 28 ++++++++++++++++++++ src/include/port/atomics/generic-gcc.h | 32 +++++++++++++++++++++++ src/include/port/atomics/generic-msvc.h | 18 +++++++++++++ src/include/port/atomics/generic-sunpro.h | 14 ++++++++++ 4 files changed, 92 insertions(+) diff --git a/src/include/port/atomics/arch-x86.h b/src/include/port/atomics/arch-x86.h index bb84b9bad8..290f3e0c58 100644 --- a/src/include/port/atomics/arch-x86.h +++ b/src/include/port/atomics/arch-x86.h @@ -184,6 +184,20 @@ pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, return (bool) ret; } +#define PG_HAVE_ATOMIC_EXCHANGE_U32 +static inline uint32 +pg_atomic_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 newval) +{ + uint32 res; + __asm__ __volatile__( + " lock \n" + " xchgl %0,%1 \n" +: "=q" (res), "=m" (ptr->value) +: "0"(newval), "m" (ptr->value) +: "memory"); + return res; +} + #define PG_HAVE_ATOMIC_FETCH_ADD_U32 static inline uint32 pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_) @@ -221,6 +235,20 @@ pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, return (bool) ret; } +#define PG_HAVE_ATOMIC_EXCHANGE_U64 +static inline uint64 +pg_atomic_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 newval) +{ + uint64 res; + __asm__ __volatile__( + " lock \n" + " xchgq %0,%1 \n" +: "=q" (res), "=m" (ptr->value) +: "0"(newval), "m" (ptr->value) +: "memory"); + return res; +} + #define PG_HAVE_ATOMIC_FETCH_ADD_U64 static inline uint64 pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_) diff --git a/src/include/port/atomics/generic-gcc.h b/src/include/port/atomics/generic-gcc.h index da04e9f0dc..1a45394923 100644 --- a/src/include/port/atomics/generic-gcc.h +++ b/src/include/port/atomics/generic-gcc.h @@ -176,6 +176,22 @@ pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, } #endif +/* + * __sync_lock_test_and_set() only supports setting the value to 1 on some + * platforms, so we only provide an __atomic implementation for + * pg_atomic_exchange. We assume the availability of 32-bit + * __atomic_compare_exchange_n() implies the availability of 32-bit + * __atomic_exchange_n(). + */ +#if !defined(PG_HAVE_ATOMIC_EXCHANGE_U32) && defined(HAVE_GCC__ATOMIC_INT32_CAS) +#define PG_HAVE_ATOMIC_EXCHANGE_U32 +static inline uint32 +pg_atomic_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 newval) +{ + return __atomic_exchange_n(&ptr->value, newval, __ATOMIC_SEQ_CST); +} +#endif + /* if we have 32-bit __sync_val_compare_and_swap, assume we have these too: */ #if !defined(PG_HAVE_ATOMIC_FETCH_ADD_U32) && defined(HAVE_GCC__SYNC_INT32_CAS) @@ -243,6 +259,22 @@ pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, } #endif +/* + * __sync_lock_test_and_set() only supports setting the value to 1 on some + * platforms, so we only provide an __atomic implementation for + * pg_atomic_exchange. We assume the availability of 64-bit + * __atomic_compare_exchange_n() implies the availability of 64-bit + * __atomic_exchange_n(). + */ +#if !defined(PG_HAVE_ATOMIC_EXCHANGE_U64) && defined(HAVE_GCC__ATOMIC_INT64_CAS) +#define PG_HAVE_ATOMIC_EXCHANGE_U64 +static inline uint64 +pg_atomic_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 newval) +{ + return __atomic_exchange_n(&ptr->value, newval, __ATOMIC_SEQ_CST); +} +#endif + /* if we have 64-bit __sync_val_compare_and_swap, assume we have these too: */ #if !defined(PG_HAVE_ATOMIC_FETCH_ADD_U64) && defined(HAVE_GCC__SYNC_INT64_CAS) diff --git a/src/include/port/atomics/generic-msvc.h b/src/include/port/atomics/generic-msvc.h index 8835f4ceea..7566d6f937 100644 --- a/src/include/port/atomics/generic-msvc.h +++ b/src/include/port/atomics/generic-msvc.h @@ -58,6 +58,13 @@ pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, return ret; } +#define PG_HAVE_ATOMIC_EXCHANGE_U32 +static inline uint32 +pg_atomic_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 newval) +{ + return InterlockedExchange(&ptr->value, newval); +} + #define PG_HAVE_ATOMIC_FETCH_ADD_U32 static inline uint32 pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_) @@ -88,6 +95,16 @@ pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, /* Only implemented on 64bit builds */ #ifdef _WIN64 + +#pragma intrinsic(_InterlockedExchange64) + +#define PG_HAVE_ATOMIC_EXCHANGE_U64 +static inline uint64 +pg_atomic_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 newval) +{ + return _InterlockedExchange64(&ptr->value, newval); +} + #pragma intrinsic(_InterlockedExchangeAdd64) #define PG_HAVE_ATOMIC_FETCH_ADD_U64 @@ -96,6 +113,7 @@ pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_) { return _InterlockedExchangeAdd64(&ptr->value, add_); } + #endif /* _WIN64 */ #endif /* HAVE_ATOMICS */ diff --git a/src/include/port/atomics/generic-sunpro.h b/src/include/port/atomics/generic-sunpro.h index 30f7d8b536..5d41b73de0 100644 --- a/src/include/port/atomics/generic-sunpro.h +++ b/src/include/port/atomics/generic-sunpro.h @@ -87,6 +87,13 @@ pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, return ret; } +#define PG_HAVE_ATOMIC_EXCHANGE_U32 +static inline uint32 +pg_atomic_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 newval) +{ + return atomic_swap_32(&ptr->value, newval); +} + #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64 static inline bool pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, @@ -101,6 +108,13 @@ pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, return ret; } +#define PG_HAVE_ATOMIC_EXCHANGE_U64 +static inline uint64 +pg_atomic_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 newval) +{ + return atomic_swap_64(&ptr->value, newval); +} + #endif /* HAVE_ATOMIC_H */ #endif /* defined(HAVE_ATOMICS) */ -- 2.25.1 --y0ulUmNC+osPPQO6--