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 1sOMS8-00DoIn-GM for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Jul 2024 19:12:40 +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 1sOMS5-003itx-3V for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Jul 2024 19:12:37 +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 1sOMS4-003itp-7m for pgsql-hackers@lists.postgresql.org; Mon, 01 Jul 2024 19:12:37 +0000 Received: from wfout3-smtp.messagingengine.com ([64.147.123.146]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sOMS1-004Xk1-8j for pgsql-hackers@lists.postgresql.org; Mon, 01 Jul 2024 19:12:35 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.west.internal (Postfix) with ESMTP id 9541F1C00096; Mon, 1 Jul 2024 15:12:28 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 01 Jul 2024 15:12:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1719861148; x= 1719947548; bh=ggxJe8KO6sv67sPy5MBkPu1YBvFYBXiivCzW5sFmKNc=; b=E HdVrTipAWpuV2o0nKCOpIsMjHJyE2OR2gqXKDjH0MY3u1ioUhbi1bE87a4ColzQy eYwxYnzkXdz0ymjYOY8s+bIUpXgmy8NB5OnMZbROki1+17Ff9s9WNHHW3a1SGwfq hleu6y2fjQDOKeHAn1BKerPE9zdic1VJt1yTfZ6D7KJ/xXGA3tqhrjw2nYRGQMbY XnrjT/YQVZuwX9+aRc8WMidBxADaMfQiPNTu9VtguV6CD2qoqw2be2cnnLDjF3lo zu2T4UlOTEjIZal1mnEIo4zl6XwlNrDchspUo0QuAnVJEz5L4sZRy46KoHkHaxJI QYVRuGQtT3pOVuH0orJJw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudefgddufedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffhjeejhffgieeuueekjeek fffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 1 Jul 2024 15:12:27 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1719861145; bh=Yp9CZCNMk5lXSM4wY7bZy0PQbCZC6psUZL6ThIg3fSA=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=osmE2H4gnYLEGyTysy8PMjtSV+9K09iLFVTZlXBt0HhtiVvTB/PLplc3b5+PFkofY CXCtziAw1Q4TayU19LmDp3vESIASqTOIRNkTzVGLrIE7decr7sxT4venKNaOue/qyg KVXWGWbI0eMo8BlrugELZWMv4QMyHF/GlunCAUy1ENA/arYPHGM1x884qpYG+bIr6Z sSLNZxGcGFSdvcSYK3Hwc1yoSEBS7qCyOr5sKzrpqtOjY8b8r3CBs52CHeZIYtEs1k ivqlc5XkrUbnPEXbRYlirtDaDNU2qemJSOMCb/ohFTAB2ZAQ9VHCHQ1fBi/6UgCe7d wkVYDrw2mJqGA== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id B8800529; Mon, 1 Jul 2024 21:12:25 +0200 (CEST) Date: Mon, 1 Jul 2024 21:12:25 +0200 From: Alvaro Herrera To: Andres Freund Cc: Alexander Lakhin , Bharath Rupireddy , Tom Lane , Jeff Davis , pgsql-hackers@lists.postgresql.org, Jaime Casanova Subject: Re: LogwrtResult contended spinlock Message-ID: <202407011912.diw4rf4rgd76@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20240701171703.in2bvoc4f5abyjdd@alap3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello, Thanks for your attention here. On 2024-Jul-01, Andres Freund wrote: > On 2024-07-01 11:10:24 +0200, Alvaro Herrera wrote: > > In the meantime I noticed that pg_attribute_aligned() is not supported > > in every platform/compiler, so for safety sake I think it's better to go > > with what we do for PGAlignedBlock: use a union with a double member. > > That should be 8-byte aligned on x86 as well, unless I misunderstand. > > If a platform wants to support 8 byte atomics, it better provides a way to > make variables 8 bytes aligned. We already rely on that, actually. See use of > pg_attribute_aligned in e.g. src/include/port/atomics/generic-msvc.h Well, pg_atomic_uint64 is a struct. Passing pointers to it is fine, which is what non-platform-specific code does; but because the declaration of the type is in each platform-specific file, it might not work to use it directly in generic code. I didn't actually try, but it seems a bit of a layering violation. (I didn't find any place where the struct is used that way.) If that works, then I think we could simply declare currval as a pg_atomic_uint64 and it'd be prettier. > > + /* > > + * On 32-bit machines, declaring a bare uint64 variable doesn't promise > > + * the alignment we need, so coerce the compiler this way. > > + */ > > + union > > + { > > + uint64 u64; > > + double force_align_d; > > + } currval; > > I wonder if we should just relax the alignment requirement for currval. It's > crucial that the pointer is atomically aligned (atomic ops across pages are > either forbidden or extremely slow), but it's far from obvious that it's > crucial for comparator value to be aligned. I'm pretty sure the Microsoft docs I linked to are saying it must be aligned. > > #ifndef PG_HAVE_ATOMIC_U64_SIMULATION > > AssertPointerAlignment(ptr, 8); > > #endif > > What's the point of this assert, btw? This stuff is already asserted in lower > level routines, so it just seems redundant to have it here? There are in some of them, but not in pg_atomic_compare_exchange_u64_impl. > > - return Max(target_, currval); > > + return Max(target_, currval.u64); > > What does the Max() actually achieve here? Shouldn't it be impossible to reach > this with currval < target_? When two processes are hitting the cmpxchg concurrently, we need to return the highest value that was written, even if it was the other process that did it. The assertions in the calling site are quickly broken if we don't do this. I admit this aspect took me by surprise. > And why does target_ end in an underscore? Heh, you tell me -- I just copied the style of the functions just above. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ "No nos atrevemos a muchas cosas porque son difíciles, pero son difíciles porque no nos atrevemos a hacerlas" (Séneca)