From patchwork Thu Jul 27 20:05:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rafael J. Wysocki" X-Patchwork-Id: 127142 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b8e4:0:b0:3e9:de7a:15be with SMTP id v4csp1374777vqw; Thu, 27 Jul 2023 13:25:45 -0700 (PDT) X-Google-Smtp-Source: APBJJlGWx8sDQ5WVNVBtdQL//l7D+qQO6kFnduLKlUxkZBzmivOralXEaFYSyflDqn1Fpx07ded+ X-Received: by 2002:a17:90a:ac09:b0:263:6114:f0f9 with SMTP id o9-20020a17090aac0900b002636114f0f9mr336025pjq.42.1690489544800; Thu, 27 Jul 2023 13:25:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690489544; cv=none; d=google.com; s=arc-20160816; b=nQKlTHbDyPds6usti6IbKpI8Oi5ob1hVEP0XfYNSXDvnd6/Dl3z2Cnj8m+OMFThZd7 KOWSeKOjqNk6gcSbF+5mhZWMFYpmyVZjiy3EBDQRAb5X39KhKE63wXg9BFCQC/HDYGmH barG8e51uzeXGpoOcP3XqsgH9uzypByT8A4Zp0clKqwh/hNNrsCNVj9NSO/mNKh0y9/k 0g/4Ffu86n2HJa0C97X87+OTeDvpAK+Ig0k06Rz3mzvbxPL6Pj/EVP4okzPEtT4E3ByK W2RvIVEqeoPZBnpnqQIUCYzQbVaWLSNMpvAYt95ba4QNytSPUDCD+gJAAQFtLPv05xoZ YOWA== 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 :message-id:date:subject:cc:to:from; bh=j8120/bi8YMktJRRE+fxWRLX3mODqy/VEouEmAOGUQM=; fh=vcb1WTwJPUMTZX6OxkyO+5cKBEoTRHpgQZ5gmsle4eE=; b=tA8Ztbz7EQdAf3HiX6dWxQhRqp+G5YYXKgR0Sm9VO85KUP5KHK38Nqe/oK39OMi1sy SRTWx4G9mRSKUYLfIBuZJbRM4H6jy4LEE6UaHn5249FJkytDIpL+AtZly/SVPS7rwqZP 9wzQNe07Ez5TEwLpIj+87h+JCPulnuyZSQ++v4NNaBVd4Dg/M9OJfubU/3SfNPGWHaDo T5QrVp4ZnOMwv/rgvsZ6poWJpNjVZmXVxGomck244H8bONglDwDrOOHxpBxtpXWX/Bbv FerbJUsBqm+iHJwnnD0d7H3nOvdjtn9jiMmWp27e8O0Yljg9RpNV+nY/y9RaeojrjF4u ECYw== ARC-Authentication-Results: i=1; mx.google.com; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z14-20020a1709027e8e00b001bb96a30e21si1792964pla.70.2023.07.27.13.25.31; Thu, 27 Jul 2023 13:25:44 -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; 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 S232220AbjG0UFi (ORCPT + 99 others); Thu, 27 Jul 2023 16:05:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230468AbjG0UFh (ORCPT ); Thu, 27 Jul 2023 16:05:37 -0400 Received: from cloudserver094114.home.pl (cloudserver094114.home.pl [79.96.170.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0133213F; Thu, 27 Jul 2023 13:05:35 -0700 (PDT) Received: from localhost (127.0.0.1) (HELO v370.home.net.pl) by /usr/run/smtp (/usr/run/postfix/private/idea_relay_lmtp) via UNIX with SMTP (IdeaSmtpServer 5.2.0) id 31a11507767cd30a; Thu, 27 Jul 2023 22:05:33 +0200 Authentication-Results: v370.home.net.pl; spf=softfail (domain owner discourages use of this host) smtp.mailfrom=rjwysocki.net (client-ip=195.136.19.94; helo=[195.136.19.94]; envelope-from=rjw@rjwysocki.net; receiver=) Received: from kreacher.localnet (unknown [195.136.19.94]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by v370.home.net.pl (Postfix) with ESMTPSA id 290E7661E19; Thu, 27 Jul 2023 22:05:33 +0200 (CEST) From: "Rafael J. Wysocki" To: Linux PM Cc: LKML , Peter Zijlstra , Anna-Maria Behnsen , Frederic Weisbecker , Kajetan Puchalski Subject: [PATCH v1] cpuidle: teo: Update idle duration estimate when choosing shallower state Date: Thu, 27 Jul 2023 22:05:33 +0200 Message-ID: <4506480.LvFx2qVVIh@kreacher> MIME-Version: 1.0 X-CLIENT-IP: 195.136.19.94 X-CLIENT-HOSTNAME: 195.136.19.94 X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedviedrieeggdduudekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecujffqoffgrffnpdggtffipffknecuuegrihhlohhuthemucduhedtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufffkfgggfgtsehtufertddttdejnecuhfhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqnecuggftrfgrthhtvghrnhepffffffekgfehheffleetieevfeefvefhleetjedvvdeijeejledvieehueevueffnecukfhppeduleehrddufeeirdduledrleegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepudelhedrudefiedrudelrdelgedphhgvlhhopehkrhgvrggthhgvrhdrlhhotggrlhhnvghtpdhmrghilhhfrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqedpnhgspghrtghpthhtohepiedprhgtphhtthhopehlihhnuhigqdhpmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehpvghtvghriiesihhnfhhrrgguvggrugdrohhrghdprhgtphhtthhopegrnhhnrgdqmhgrrhhirgeslhhinhhuthhrohhnihigrdguvgdprhgtphhtthhopehfrhgvuggv rhhitgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepkhgrjhgvthgrnhdrphhutghhrghlshhkihesrghrmhdrtghomh X-DCC--Metrics: v370.home.net.pl 1024; Body=6 Fuz1=6 Fuz2=6 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772606765364141449 X-GMAIL-MSGID: 1772606765364141449 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. */ + 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; + } + } end: /*