Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp2228312rwb; Sat, 29 Jul 2023 02:34:37 -0700 (PDT) X-Google-Smtp-Source: APBJJlGauOPcaI5Mx2Hcwib58ILghlYFq6uYNGnSZOBAD+wQpDhKJFkC261Xk3KO0wNEKPiViiuT X-Received: by 2002:aa7:c543:0:b0:521:d75d:ef69 with SMTP id s3-20020aa7c543000000b00521d75def69mr3807264edr.31.1690623277315; Sat, 29 Jul 2023 02:34:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690623277; cv=none; d=google.com; s=arc-20160816; b=kEJMPvHc2LnkYIkmtWRouYW/MlvqPiXFcm9X5Pq8onwTM2kXNnLpZTLf06CFrBu+Dz vCFMeHkUACFjldGYvQi05i8DZeYSUAfvspDO7AeYkHy30kUY0+y/MhUcwOup0MvJymLn VS1DQGyYkM3JYGLO2x3DBreIuZmIpVJm2pMrl4HtyqU6eDaevj3cgWIHGQouv20Hplp+ TXU3rshjmxQNkAbD+KyadUBZ501KkUiDRGwAnzH8/P3aniQUj/51zAbxQOEhHW2TCWbV gJ2ETyzhPlRuQzLqjJ9Naakz5GlQCQ2bezEwU2W7w1CTVZNXoYcl5AUfiVK7idgtXGjQ skKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=g6UojXCOkyCdhNj31kgSFEVj8BuEt/lnkxAfMMWCe9s=; fh=syReuckUlyH7oAqUHfLi+Tpy+5mTO07IT97c4EyMeYk=; b=jvRAeBbfih4ipxuTWT+3tNsoZoVJW1MOGe75XMKS8hOGiXSmbjVWTQLmlnbR4044JZ ubYbh4rxEYGEL/FX84vHcanohWG5mCtct+vGbAXX58LV3BsIBk5O8Y6UO4HsQ654/Q73 qF9DI2omW/E8B8AmjGdKuH5J4u84kF482rUuPNKoTj7hYHsmpgFoC7S1TfcYcJ6EzvDw m/A7A2S27YkPonNlQhF9jERGEsFtpYyBYOdheeIK3ISDaVHCur6j3Rq3uMRJH2CAaBx6 szLIKtQYWeHFeY31ArO2rlPFllMmXL2B1GQYIXwSnKIQWEOsn+l30YnBeJMYEJtDMr8a c5xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=pFuOcdSB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j26-20020a50ed1a000000b0052220448d66si1050123eds.283.2023.07.29.02.34.10; Sat, 29 Jul 2023 02:34:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=pFuOcdSB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230317AbjG2JDC (ORCPT + 99 others); Sat, 29 Jul 2023 05:03:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229602AbjG2JDA (ORCPT ); Sat, 29 Jul 2023 05:03:00 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70E013C33; Sat, 29 Jul 2023 02:02:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=g6UojXCOkyCdhNj31kgSFEVj8BuEt/lnkxAfMMWCe9s=; b=pFuOcdSBnIPxpL8/P8SR0fNDxG nl507WgPU+OmBiRrT7g+mhjrFyCPcijN0CMf4CpDKsWBW3yc7tVijPvu3AWJzb2547fJEMFUuEGpV fmnCxjUqLrXOcR3aKzYgzs3GaRbJKk3xXN16tRs5u4BTeih4xd5jLPdaE1qNUzTEB/29AUCeVF6SM wGD2nVRjhS6iIOo+DXwpYGYavUVKLxHlUAeNYSIL0WD4pdlx8xOur8szeHQBBYboXtRQdmiozBTeZ DudGOTV7odYI3sFW6yawsLFTjgbEl4YC+O+vVcdQkbx4KtRDnAedZwFmaxA0PU+jodN5cT3gpk0rg m7xYiZsQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1qPfqh-009wW8-Om; Sat, 29 Jul 2023 09:02:56 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id C04A63002CE; Sat, 29 Jul 2023 11:02:55 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id A87DF2027C618; Sat, 29 Jul 2023 11:02:55 +0200 (CEST) Date: Sat, 29 Jul 2023 11:02:55 +0200 From: Peter Zijlstra To: "Rafael J. Wysocki" Cc: Linux PM , LKML , Anna-Maria Behnsen , Frederic Weisbecker , Kajetan Puchalski , Thomas Gleixner Subject: Re: [PATCH v1] cpuidle: teo: Update idle duration estimate when choosing shallower state Message-ID: <20230729090255.GD3945851@hirez.programming.kicks-ass.net> References: <4506480.LvFx2qVVIh@kreacher> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 27, 2023 at 10:12:56PM +0200, Rafael J. Wysocki wrote: > On Thu, Jul 27, 2023 at 10:05 PM Rafael J. Wysocki wrote: > > > > From: Rafael J. Wysocki > > > > The TEO governor takes CPU utilization into account by refining idle state > > selection when the utilization is above a certain threshold. The idle state > > selection is then refined by choosing an idle state shallower than the > > previously selected one. > > > > However, when this is done, the idle duration estimate needs to be updated > > so as to prevent the scheduler tick from being stopped while the candidate > > idle state is shallow, which may lead to excessive energy usage if the CPU > > is not interrupted quickly enough going forward. Moreover, in case the > > scheduler tick has been stopped already and the new idle duration estimate > > is too small, the replacement candidate state cannot be used. > > > > Modify the relevant code to take the above observations into account. > > > > Fixes: 9ce0f7c4bc64 ("cpuidle: teo: Introduce util-awareness") > > Signed-off-by: Rafael J. Wysocki > > --- > > > > @Peter: This doesn't attempt to fix the tick stopping problem, it just makes > > the current behavior consistent. > > > > @Anna-Maria: This is likely to basically prevent the tick from being stopped > > at all if the CPU utilization is above a certain threshold. I'm wondering if > > your results will be affected by it and in what way. > > > > --- > > drivers/cpuidle/governors/teo.c | 33 ++++++++++++++++++++++++++------- > > 1 file changed, 26 insertions(+), 7 deletions(-) > > > > Index: linux-pm/drivers/cpuidle/governors/teo.c > > =================================================================== > > --- linux-pm.orig/drivers/cpuidle/governors/teo.c > > +++ linux-pm/drivers/cpuidle/governors/teo.c > > @@ -397,13 +397,22 @@ static int teo_select(struct cpuidle_dri > > * the shallowest non-polling state and exit. > > */ > > if (drv->state_count < 3 && cpu_data->utilized) { > > - for (i = 0; i < drv->state_count; ++i) { > > - if (!dev->states_usage[i].disable && > > - !(drv->states[i].flags & CPUIDLE_FLAG_POLLING)) { > > - idx = i; > > + /* > > + * If state 0 is enabled and it is not a polling one, select it > > + * right away and update the idle duration estimate accordingly, > > + * unless the scheduler tick has been stopped. > > + */ > > + if (!idx && !(drv->states[0].flags & CPUIDLE_FLAG_POLLING)) { > > + s64 span_ns = teo_middle_of_bin(0, drv); > > + > > + if (teo_time_ok(span_ns)) { > > + duration_ns = span_ns; > > goto end; > > } > > } > > + /* Assume that state 1 is not a polling one and select it. */ > > Well, I should also check if it is not disabled. Will send a v2 tomorrow. > > > + idx = 1; > > + goto end; > > } > > > > /* > > @@ -539,10 +548,20 @@ static int teo_select(struct cpuidle_dri > > > > /* > > * If the CPU is being utilized over the threshold, choose a shallower > > - * non-polling state to improve latency > > + * non-polling state to improve latency, unless the scheduler tick has > > + * been stopped already and the shallower state's target residency is > > + * not sufficiently large. > > */ > > - if (cpu_data->utilized) > > - idx = teo_find_shallower_state(drv, dev, idx, duration_ns, true); > > + if (cpu_data->utilized) { > > + s64 span_ns; > > + > > + i = teo_find_shallower_state(drv, dev, idx, duration_ns, true); > > + span_ns = teo_middle_of_bin(i, drv); > > + if (teo_time_ok(span_ns)) { > > + idx = i; > > + duration_ns = span_ns; > > + } > > + } So I'm not a huge fan of that utilized thing to begin with.. that feels like a hack. I think my patch 3 would achieve much the same, because if busy, you'll have short idles, which will drive the hit+intercept to favour low states, and voila. I didn't take it out -- yet -- because I haven't had much time to evaluate it. Simply lowering one state at a random busy threshold is duct-tape if ever I saw some.