From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 8B83A3858D28 for ; Wed, 10 Nov 2021 20:53:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8B83A3858D28 Received: by mail-pf1-x42c.google.com with SMTP id x64so3722492pfd.6 for ; Wed, 10 Nov 2021 12:53:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=d9jOCmGBev5TTfPpWDHOkg7MDS4KRS4ehMXmnuWOzgU=; b=oDYln0Rlnfp8cekuS/2QU1cCJVsnlDIa0dk46hM5Hk3/EkWKxQVHHzyVuQbuy48fCw gRs2EThL1f/5Vr4vwJf3ztjASE9RhTkTDIsZKlfCL4FixfBsPdzT4XmdIMzY5tZqfbYF c3nwcDXldRx1dgFbIIpYD7Df0maXEqjLuUlw70intFaxFx+nmcEre0t5zrCAoEFGFo22 r+SVXVmi2qYgISqMkol1SYcVXgUEH1g231Gh45Qj2lfgxAt0rdGmXKyGgJm1kzOdrnCJ 2FSJR0jERzcxnO69EijUoEIQo3R4TQnxwx4tHkmfT5WAxnv/OrG1ANcRap9muMdrlF0i XRrg== X-Gm-Message-State: AOAM532o83hwRcH5PzKptUMkWtf4MQ0zYDanh9qyLdndJDQ/yXAkou3G C1t8HlLc5dkpl4rfvCzlEWF3OvUePzVIcARvhVE= X-Google-Smtp-Source: ABdhPJxv0XKqr4YkaaH15L6/RNrSgdptK45YeW+I56yYnjObVWcagwCSqWDyTvv25X9GPIdSjnUaQfHtz2H6vIDoXY8= X-Received: by 2002:a63:87c1:: with SMTP id i184mr1165824pge.75.1636577624657; Wed, 10 Nov 2021 12:53:44 -0800 (PST) MIME-Version: 1.0 References: <20211110184153.2269857-1-hjl.tools@gmail.com> <20211110202345.GG4930@li-24c3614c-2adc-11b2-a85c-85f334518bdb.ibm.com> In-Reply-To: <20211110202345.GG4930@li-24c3614c-2adc-11b2-a85c-85f334518bdb.ibm.com> From: "H.J. Lu" Date: Wed, 10 Nov 2021 12:53:08 -0800 Message-ID: Subject: Re: [PATCH v5 0/3] Optimize CAS [BZ #28537] To: "Paul A. Clarke" Cc: GNU C Library , Florian Weimer , Oleh Derevenko , Arjan van de Ven , Andreas Schwab , Noah Goldstein Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3023.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Nov 2021 20:53:47 -0000 On Wed, Nov 10, 2021 at 12:23 PM Paul A. Clarke wrote: > > On Wed, Nov 10, 2021 at 10:41:50AM -0800, H.J. Lu wrote: > > Changes in v5: > > > > 1. Put back __glibc_unlikely in __lll_trylock and lll_cond_trylock. > > 2. Remove an atomic load in a CAS usage which has been already optimized. > > 3. Add an empty statement with a semicolon to a goto label for older > > compiler versions. > > 4. Simplify CAS optimization. > > > > CAS instruction is expensive. From the x86 CPU's point of view, getting > > a cache line for writing is more expensive than reading. See Appendix > > A.2 Spinlock in: > > > > https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/xeon-lock-scaling-analysis-paper.pdf > > > > The full compare and swap will grab the cache line exclusive and cause > > excessive cache line bouncing. > > > > Optimize CAS in low level locks and pthread_mutex_lock.c: > > > > 1. Do an atomic load and skip CAS if compare may fail to reduce cache > > line bouncing on contended locks. > > 2. Replace atomic_compare_and_exchange_bool_acq with > > atomic_compare_and_exchange_val_acq to avoid the extra load. > > > > This is the first patch set to optimize CAS. I will submit the rest > > CAS optimizations in glibc after this patch set has been accepted. > > > > With all CAS optimizations applied, on a machine with 112 cores, > > "make check -j28" under heavy load took > > > > 3093.18user 1644.12system 22:26.05elapsed 351%CPU > > > > vs without CAS optimizations > > > > 3746.07user 1614.93system 22:02.91elapsed 405%CPU > > I read that as about 2% slower with your changes. Is that the desired result? > The machine was under heavy load. My reading is with CAS optimization, it takes less CPU cycles and reduces CPU utilization by 13%. It saves power and gives CPU cycles to other tasks. -- H.J.