Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp673663rdh; Thu, 26 Oct 2023 12:21:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG3CukQlD1+PZxJPfSwYn5QYAptiAutamgn11X2J92VR4Uf/noB5B36xUB7832WwNWKzwWN X-Received: by 2002:a25:41d0:0:b0:da0:f622:8466 with SMTP id o199-20020a2541d0000000b00da0f6228466mr547011yba.22.1698348098901; Thu, 26 Oct 2023 12:21:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698348098; cv=none; d=google.com; s=arc-20160816; b=FXvcsDEYoc0fOOl+0jdZ8wZ7QL93rNVcbLrcgolY3no2bZZyzUk8BFOS9X6knBpGf/ 7NU9Wm0WOIvktMohYbkG2v7YQzN6KSd/Za66/J+QRKZBuA52HQpaiyzrPswTV36WgTGQ MNoT0kQRWSntjcuBSP/MLqqbdeC9QRtkXQOGWI7QUurgzy7nrc1Fpy1PrG699gMKjYIo Bx96lTH88zneM3gIM7r10Q+NxgEtS6gpZUUd75hxcl1hGZg7oe+Jbup/oN2plLUpzhbG 1KzHAoF4OpduA6WREPty/uMcEJOz0t8wp4Sczd2nmPsM7wwOu5P1zy61ym4GCiRpeqOS G5hA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=EEwZ5TWFXrbxM66xOVKiq55FHQigiJd60ZSUPiX9Bvs=; fh=qs+3pj00Q72KxiZyDxdQrBEzGPfkm8t4CkCoocYEl8o=; b=e0Z5Ffbclp2CcQTWzauE2H6s56V3h9YGeJoDF4ny0ZmsZg0i2u9ShysBiL+8wGi1GD 0B9c6/USArPjZash842SQXS+D8nOZf45TM0qmLSYVG1GqDeonUf867SvxfZnttQ2om8g ZcBc35ydbnHLLNLeE4iI6l40ZSOsqHCA2p+faD+2UlCv8nVjPAlTYNsSKG1AFETns2m7 YVlCXE3PqTnBBOk8jMZ7OaF3JzqePQrUbN8qu2IEbs9/PDTB/jhv1gGxOIYuejWpr8yr aJBpVMkPpxuu8S9SAuloDYee+RH69qfPEXwBEwtPcAVZ7IGEZocDRTkt//o64yrfsfza Hvmg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id r201-20020a2576d2000000b00d9af2e13e72si56456ybc.582.2023.10.26.12.21.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 12:21:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 46CB0808DB69; Thu, 26 Oct 2023 12:20:49 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229991AbjJZTUc convert rfc822-to-8bit (ORCPT + 99 others); Thu, 26 Oct 2023 15:20:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229649AbjJZTUa (ORCPT ); Thu, 26 Oct 2023 15:20:30 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 679961A7 for ; Thu, 26 Oct 2023 12:20:28 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91384C433C8; Thu, 26 Oct 2023 19:20:24 +0000 (UTC) Date: Thu, 26 Oct 2023 15:20:22 -0400 From: Steven Rostedt To: Mathieu Desnoyers Cc: Peter Zijlstra , LKML , Thomas Gleixner , Ankur Arora , Linus Torvalds , linux-mm@kvack.org, x86@kernel.org, akpm@linux-foundation.org, luto@kernel.org, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, willy@infradead.org, mgorman@suse.de, jon.grimm@amd.com, bharata@amd.com, raghavendra.kt@amd.com, boris.ostrovsky@oracle.com, konrad.wilk@oracle.com, jgross@suse.com, andrew.cooper3@citrix.com, Joel Fernandes , Youssef Esmat , Vineeth Pillai , Suleiman Souhlal , Ingo Molnar , Daniel Bristot de Oliveira Subject: Re: [POC][RFC][PATCH v2] sched: Extended Scheduler Time Slice Message-ID: <20231026152022.668ca0f3@gandalf.local.home> In-Reply-To: References: <20231025235413.597287e1@gandalf.local.home> <20231026105944.GJ33965@noisy.programming.kicks-ass.net> <20231026071413.4ed47b0e@gandalf.local.home> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 26 Oct 2023 12:20:49 -0700 (PDT) On Thu, 26 Oct 2023 14:36:36 -0400 Mathieu Desnoyers wrote: > > static inline unsigned long > > xchg(volatile unsigned *ptr, unsigned new) > > { > > unsigned ret = new; > > > > asm volatile("xchg %b0,%1" > > which has an implicit lock prefix (xchg with a memory operand is a > special-case): > > Quoting Intel manual: > > "If a memory operand is referenced, the processor’s locking protocol is > automatically implemented for the duration of the exchange operation, > regardless of the presence or absence of the LOCK prefix or of the value > of the IOPL. (See the LOCK prefix description in this chapter for more > information on the locking protocol.)" > Bah. I learn something new every day :-p I thought xchg required specifying lock too. This means that cmpxchg is actually faster than xchg! It's been a long time since I had written assembly. What makes this interesting though, is that when I ran my tests originally, when my change was disabled, the xchg() code never executed, and it still showed a significant improvement. That is, even with adding xchg(), it still improved much more. But that's probably because the xchg() happens after releasing the lock and after that it goes into a loop waiting for another thread to make the update, which requires a lock cmpxchg(). Hence, the xchg() slowdown wasn't in a critical path of the test. Anyway, I changed the code to use: static inline unsigned clrbit(volatile unsigned *ptr) { unsigned ret; asm volatile("andb %b1,%0" : "+m" (*(volatile char *)ptr) : "iq" (0x2) : "memory"); ret = *ptr; *ptr = 0; return ret; } I just used andb as that's a locally atomic operation. I could also use bts (as Mathieu suggested to me on IRC). It doesn't matter as this is just example code and is not in production. How this is implemented is not an important part of the algorithm. Just that it can atomically clear the bit it sets without a race with the kernel setting the bit it sets. I could modify the code to put these bits in separate bytes as well. That's just an implementation detail we can work on later. I updated my test to have: static bool no_rseq; static void init_extend_map(void) { int ret; if (no_rseq) return; ret = rseq(&rseq_map, sizeof(rseq_map), 0, 0); perror("rseq"); printf("ret = %d (%zd) %p\n", ret, sizeof(rseq_map), &rseq_map); } static inline unsigned clrbit(volatile unsigned *ptr) { unsigned ret; asm volatile("andb %b1,%0" : "+m" (*(volatile char *)ptr) : "iq" (0x2) : "memory"); ret = *ptr; *ptr = 0; return ret; } static void extend(void) { if (no_rseq) return; rseq_map.cr_flags = 1; } static void unextend(void) { unsigned prev; if (no_rseq) return; prev = clrbit(&rseq_map.cr_flags); if (prev & 2) { tracefs_printf(NULL, "Yield!\n"); sched_yield(); } } where the tests with it disabled do not run the extend or unextend() code (as it makes sense to only perform that code with this feature, as that would be part of the overhead to implement it). Here's the results: With no_rseq = true, with 5 runs, which were basically all the same, but the best run was: Ran for 4260797 times Total wait time: 33.905306 And with no_rseq = false; Most runs were like: Ran for 5378845 times Total wait time: 32.253018 But the worse run was: Ran for 4991411 times Total wait time: 31.745662 But with that lower "wait time" it probably was preempted by a something else during that run, as the lower the "wait time" the better the result. -- Steve