From patchwork Sun Aug 12 16:09:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Yan X-Patchwork-Id: 10563679 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 19B06139A for ; Sun, 12 Aug 2018 16:10:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 01BE729467 for ; Sun, 12 Aug 2018 16:10:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E66A629466; Sun, 12 Aug 2018 16:10:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B1A3529467 for ; Sun, 12 Aug 2018 16:10:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727699AbeHLSs2 (ORCPT ); Sun, 12 Aug 2018 14:48:28 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:53850 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728180AbeHLSs1 (ORCPT ); Sun, 12 Aug 2018 14:48:27 -0400 Received: by mail-wm0-f68.google.com with SMTP id s9-v6so6473899wmh.3 for ; Sun, 12 Aug 2018 09:09:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7syOievGKU1gnJa8oh05y8DUlQgWF5NjJ3xt3wLMcp0=; b=Aqw4vA0xT3WxMfTUxnd2mwPWd8CNO/XKIvCXki9jTDnHbOsEpUGb8lNr5DxnTeREod RtqC2bdKn0+llD37F45FFVZkE3+SCLiR5wyX/knNX9M8hLOei8vErZgtXqz8CuzNhE1W OtoVDdXcF8fJUARQ4SjmDJL1uS57JPBz+gdfk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=7syOievGKU1gnJa8oh05y8DUlQgWF5NjJ3xt3wLMcp0=; b=e/odI49FNjEI3P0KulXdZXbVAy90exRzVZ2CjPsRSwMXk4YK9NVC0zVG6aXj2QOrM0 cGUms0jypV1l03YfOwnc4ux8+piUNpWKNlhuId+guRayRvLk/lVWdf96AVZNIDMcjXeA tr8u1AZOpw4Spb66HpjcEO9oU7nrxLisIKrJFNhepBMWfiicu4uQy6CRrqFSVGSLpUYE ZbsNvYCg9F/yZm0IJR3Xtscjs1UKIjYL4FteTo3U8Auc17xzZXUcXyUCmde53HdVUsUa Hxa4J1B8Tn2XtCIpXP1gdxojTztVxdhRE8VrDL3VW4j9y3ME5/r7VHq91HiVXnCKU5WP /m6A== X-Gm-Message-State: AOUpUlGzHPPXPdedY6z6k0N3kB70HIepSxiTuTmJwjPo14W/MhNKrNIt jEe6yypwVqX39s/2/E9ed0ygLw== X-Google-Smtp-Source: AA+uWPw7s5O072Udhi0wx1PE6Pr4V4AHd6MaUxCYSajv11b1UuYlphjFpTQ/V31jboWyqOhGW9JodQ== X-Received: by 2002:a1c:28c2:: with SMTP id o185-v6mr6159731wmo.40.1534090195777; Sun, 12 Aug 2018 09:09:55 -0700 (PDT) Received: from localhost.localdomain ([45.76.138.171]) by smtp.gmail.com with ESMTPSA id t6-v6sm7437369wmf.8.2018.08.12.09.09.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 12 Aug 2018 09:09:55 -0700 (PDT) From: Leo Yan To: "Rafael J. Wysocki" , "Peter Zijlstra (Intel)" , Daniel Lezcano , Vincent Guittot , Ramesh Thomas , linux-kernel@vger.kernel.org, Linux PM Cc: Leo Yan Subject: [PATCH v1 1/5] cpuidle: menu: Clean up variables usage in menu_select() Date: Mon, 13 Aug 2018 00:09:27 +0800 Message-Id: <1534090171-14464-2-git-send-email-leo.yan@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1534090171-14464-1-git-send-email-leo.yan@linaro.org> References: <1534090171-14464-1-git-send-email-leo.yan@linaro.org> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The usage for two variables 'data->predicted_us' and 'expected_interval' in menu_select() are confused, especially these two variables are assigned with each other: firstly 'data->predicted_us' is assigned to the minimum value between 'data->predicted_us' and 'expected_interval', so it presents the prediction period for taking account different factors and include consideration for expected interval; but later 'data->predicted_us' is assigned back to 'expected_interval' and from then on the function uses 'expected_interval' to select idle state; this results in 'expected_interval' has two different semantics between the top half and the bottom half of the same function. This patch is to clean up the usage of these two variables, we always use 'data->predicted_us' to present the idle duration predictions and it can be used to compare with idle state target residency or tick boundary for choosing idle state; we purely use 'expected_interval' to record the expected interval value, which is mainly for interval interrupt estimation. Signed-off-by: Leo Yan --- drivers/cpuidle/governors/menu.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/cpuidle/governors/menu.c b/drivers/cpuidle/governors/menu.c index 5eb7d6f..b972db1 100644 --- a/drivers/cpuidle/governors/menu.c +++ b/drivers/cpuidle/governors/menu.c @@ -363,7 +363,6 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, latency_req = interactivity_req; select: - expected_interval = data->predicted_us; /* * Find the idle state with the lowest power while satisfying * our constraints. @@ -386,7 +385,7 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, * expected idle duration so that the tick is retained * as long as that target residency is low enough. */ - expected_interval = drv->states[idx].target_residency; + data->predicted_us = drv->states[idx].target_residency; break; } idx = i; @@ -400,7 +399,7 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, * expected idle duration is shorter than the tick period length. */ if (((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) || - expected_interval < TICK_USEC) && !tick_nohz_tick_stopped()) { + data->predicted_us < TICK_USEC) && !tick_nohz_tick_stopped()) { unsigned int delta_next_us = ktime_to_us(delta_next); *stop_tick = false; From patchwork Sun Aug 12 16:09:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Yan X-Patchwork-Id: 10563681 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 16A50139A for ; Sun, 12 Aug 2018 16:10:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 048B729466 for ; Sun, 12 Aug 2018 16:10:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ED43429469; Sun, 12 Aug 2018 16:10:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A55529466 for ; Sun, 12 Aug 2018 16:10:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727564AbeHLSse (ORCPT ); Sun, 12 Aug 2018 14:48:34 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:36165 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727747AbeHLSsb (ORCPT ); Sun, 12 Aug 2018 14:48:31 -0400 Received: by mail-wr1-f66.google.com with SMTP id h9-v6so12251408wro.3 for ; Sun, 12 Aug 2018 09:10:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=V18Yg/vtevZ0nWVd2TxyiDMrqJoQAluOHbayWFvQXps=; b=OSxffkp8NwJyPYpiwF1fIiewerSpTUDVO903chrUq34Ei11KpkKTlPdZubWBo51/Q7 GbrHWJSKaJBzEnXyCV+jI7+Nrezo1178q/h9F6MM14Cop3As8v5NX8aezc3DOGnsm8xo xrYwzUbUVOBGD9k9dvoB0LKsbG5Sxb1gTh22o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=V18Yg/vtevZ0nWVd2TxyiDMrqJoQAluOHbayWFvQXps=; b=lfX7t1LYjsWZE9n9daNi9JpmbcLpXff1cHmWABmi3YHPsJnUa61NZF9w2yENbw36rX h2L/kmCzlPKpv5bsVbIM2zwcZ7yNWgciTDDmdxPe3ooHxDo4ZpV0kxz+mXI46Hl0EDWl WcL91a3Fx8LujEH8OaxEmVspZ5QCSnIllsSWhvcqYLeYYbCNERJ7tvNgChW7U6OYcZv3 3fc7kqr7OdGIfvdYGbD/+wBovF8VXDZEcx6062zNWEQCld7ORgc4jPC5Q88Uom3QiOps AJaUTTjD1Gph/USz66ObOgOwICl4ByV9EAKUdNHUwwR8oy1FwB0uVU60rpPeP489nHLG 19QQ== X-Gm-Message-State: AOUpUlHpeTqBwEoVR71ti4Q9CsRJK09dMjVZu44Lby8qkahp8QYt8Byf IBGj5OMo+Xedmi2hHZIJz3u0PA== X-Google-Smtp-Source: AA+uWPzw35FdQt+zWdjk80+9C6z2ADA9CZMw40hg2gNSVOnmMHebG6LDNWqmFukwn27mOuwUBUdfmA== X-Received: by 2002:a5d:458b:: with SMTP id p11-v6mr8555372wrq.122.1534090199872; Sun, 12 Aug 2018 09:09:59 -0700 (PDT) Received: from localhost.localdomain ([45.76.138.171]) by smtp.gmail.com with ESMTPSA id t6-v6sm7437369wmf.8.2018.08.12.09.09.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 12 Aug 2018 09:09:58 -0700 (PDT) From: Leo Yan To: "Rafael J. Wysocki" , "Peter Zijlstra (Intel)" , Daniel Lezcano , Vincent Guittot , Ramesh Thomas , linux-kernel@vger.kernel.org, Linux PM Cc: Leo Yan Subject: [PATCH v1 2/5] cpuidle: menu: Record tick delta value in struct menu_device Date: Mon, 13 Aug 2018 00:09:28 +0800 Message-Id: <1534090171-14464-3-git-send-email-leo.yan@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1534090171-14464-1-git-send-email-leo.yan@linaro.org> References: <1534090171-14464-1-git-send-email-leo.yan@linaro.org> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Since the tick delta is used in multiple places in menu_select(), it's better to use single one variable to record this value; furthermore, for more readable we can refactor the code to split a separate function to making decision for stopping tick, which also needs to use tick delta value as one metric for consideration. To achieve these purposes, this patch adds a new item 'tick_delta_us' in struct menu_device to record tick delta value. This patch also is a preparation for optimization stopping tick in sequential patches. Signed-off-by: Leo Yan --- drivers/cpuidle/governors/menu.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/cpuidle/governors/menu.c b/drivers/cpuidle/governors/menu.c index b972db1..83618ab 100644 --- a/drivers/cpuidle/governors/menu.c +++ b/drivers/cpuidle/governors/menu.c @@ -124,6 +124,7 @@ struct menu_device { int tick_wakeup; unsigned int next_timer_us; + unsigned int tick_delta_us; unsigned int predicted_us; unsigned int bucket; unsigned int correction_factor[BUCKETS]; @@ -305,6 +306,7 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, /* determine the expected residency time, round up */ data->next_timer_us = ktime_to_us(tick_nohz_get_sleep_length(&delta_next)); + data->tick_delta_us = ktime_to_us(delta_next); get_iowait_load(&nr_iowaiters, &cpu_load); data->bucket = which_bucket(data->next_timer_us, nr_iowaiters); @@ -317,7 +319,7 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, * timer event for the idle state selection. */ if (tick_nohz_tick_stopped()) { - data->predicted_us = ktime_to_us(delta_next); + data->predicted_us = data->tick_delta_us; goto select; } @@ -400,11 +402,11 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, */ if (((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) || data->predicted_us < TICK_USEC) && !tick_nohz_tick_stopped()) { - unsigned int delta_next_us = ktime_to_us(delta_next); *stop_tick = false; - if (idx > 0 && drv->states[idx].target_residency > delta_next_us) { + if (idx > 0 && + drv->states[idx].target_residency > data->tick_delta_us) { /* * The tick is not going to be stopped and the target * residency of the state to be returned is not within @@ -417,7 +419,8 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, continue; idx = i; - if (drv->states[i].target_residency <= delta_next_us) + if (drv->states[i].target_residency <= + data->tick_delta_us) break; } } From patchwork Sun Aug 12 16:09:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Yan X-Patchwork-Id: 10563683 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CF277139A for ; Sun, 12 Aug 2018 16:10:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BF68829466 for ; Sun, 12 Aug 2018 16:10:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B31DE29469; Sun, 12 Aug 2018 16:10:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 44EAF29466 for ; Sun, 12 Aug 2018 16:10:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728307AbeHLSsf (ORCPT ); Sun, 12 Aug 2018 14:48:35 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:43209 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727747AbeHLSsf (ORCPT ); Sun, 12 Aug 2018 14:48:35 -0400 Received: by mail-wr1-f67.google.com with SMTP id b15-v6so12210482wrv.10 for ; Sun, 12 Aug 2018 09:10:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=v4mRTz9lovuOcDxlp6+85Pwdka+3yYASS/8Q6JA5+Fc=; b=WqWRK+EhooOB8zO22jSGNKLlVuQnJLQ/RdvK4jHkVx663qtYtx2pnnwr0ncjiKn4UT E8tsB8HHZY+h5E/9abCtSeoATObCAafU+7IGV8372NNjcD+zy+GnTZL0StXLKcEofsaQ mSgTXhXyW2pnU+NyrCaJL3n3vMei/pkXGIsmE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=v4mRTz9lovuOcDxlp6+85Pwdka+3yYASS/8Q6JA5+Fc=; b=A33tYSQmW2gfSTv/WidN5qOkDrQkvV3t3GjPnsyCBTU6P4oWHXVJoD2i2B5jGFkr9i O1/Br06Rx8liV5D+JmrCzxFYWS923b883euurnYAlXSeRI8N6blTVRX/HQHe6FqaVBxz 5moWsAFZP8dUES9Td1wGc8cEDFLXn1NOTmKpV115QT5/EoGrIRoMUHpZoPiBs85Ye8wI mdjLWt29DQ7Nj1VFNBOb+F/IbJNgGrPPpvZ++Hp3j87slFlC4RykTrCmRlMWgsRmLSMW Wil3Q2PX91zg9hGmuC49TcAlcDHiKPVzPNlc330Mqn9j9T+05jTXjSMjB3k3iCyiSOja Fxyg== X-Gm-Message-State: AOUpUlETWn7VfTSgwOxgG/pEExiz24nsLx+94eaeI8m+WX+y3tkO10Wf +XOxvNA8pbMerqeYWUN66n5IpqTj8LWh0g== X-Google-Smtp-Source: AA+uWPwWAsKnnJ1jdYBxN7Nmjypuk1Jn2ShEhyFcS1RABpYB+XM73Jn1OFvN2KU0+X9WaiJk2G5bnQ== X-Received: by 2002:adf:9261:: with SMTP id 88-v6mr8159181wrj.62.1534090203670; Sun, 12 Aug 2018 09:10:03 -0700 (PDT) Received: from localhost.localdomain ([45.76.138.171]) by smtp.gmail.com with ESMTPSA id t6-v6sm7437369wmf.8.2018.08.12.09.10.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 12 Aug 2018 09:10:02 -0700 (PDT) From: Leo Yan To: "Rafael J. Wysocki" , "Peter Zijlstra (Intel)" , Daniel Lezcano , Vincent Guittot , Ramesh Thomas , linux-kernel@vger.kernel.org, Linux PM Cc: Leo Yan Subject: [PATCH v1 3/5] cpuidle: menu: Provide menu_decide_stopping_tick() Date: Mon, 13 Aug 2018 00:09:29 +0800 Message-Id: <1534090171-14464-4-git-send-email-leo.yan@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1534090171-14464-1-git-send-email-leo.yan@linaro.org> References: <1534090171-14464-1-git-send-email-leo.yan@linaro.org> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch is only for code refactoring and without functional change. It introduces a new function menu_decide_stopping_tick(); we can use this function to focus on making stopping tick decision. With moving out stopping tick decision code, it lets the below piece code is simplized only for the idle state calibration and thus save one indent level in the loop. Signed-off-by: Leo Yan --- drivers/cpuidle/governors/menu.c | 76 ++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 26 deletions(-) diff --git a/drivers/cpuidle/governors/menu.c b/drivers/cpuidle/governors/menu.c index 83618ab..4f02207 100644 --- a/drivers/cpuidle/governors/menu.c +++ b/drivers/cpuidle/governors/menu.c @@ -276,6 +276,37 @@ static unsigned int get_typical_interval(struct menu_device *data) } /** + * menu_decide_stopping_tick - decides if need to stopping tick + * @drv: cpuidle driver containing state data + * @data: menu_device structure pointer + * @idx: the candidate idle state index + */ +static bool menu_decide_stopping_tick(struct cpuidle_driver *drv, + struct menu_device *data, int idx) +{ + /* + * If the tick has been stopped yet, force to stop it afterwards and + * don't give chance to set *stop_tick to false. + */ + if (tick_nohz_tick_stopped()) + return true; + + /* Don't stop the tick if the selected state is a polling one */ + if (drv->states[idx].flags & CPUIDLE_FLAG_POLLING) + return false; + + /* + * Don't stop the tick if the prediction is shorter than the + * tick period length. + */ + if (data->predicted_us < TICK_USEC) + return false; + + /* Otherwise, let's stop the tick at this time. */ + return true; +} + +/** * menu_select - selects the next idle state to enter * @drv: cpuidle driver containing state data * @dev: the CPU @@ -396,33 +427,26 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, if (idx == -1) idx = 0; /* No states enabled. Must use 0. */ - /* - * Don't stop the tick if the selected state is a polling one or if the - * expected idle duration is shorter than the tick period length. - */ - if (((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) || - data->predicted_us < TICK_USEC) && !tick_nohz_tick_stopped()) { - - *stop_tick = false; + *stop_tick = menu_decide_stopping_tick(drv, data, idx); - if (idx > 0 && - drv->states[idx].target_residency > data->tick_delta_us) { - /* - * The tick is not going to be stopped and the target - * residency of the state to be returned is not within - * the time until the next timer event including the - * tick, so try to correct that. - */ - for (i = idx - 1; i >= 0; i--) { - if (drv->states[i].disabled || - dev->states_usage[i].disable) - continue; - - idx = i; - if (drv->states[i].target_residency <= - data->tick_delta_us) - break; - } + /* Calibrate the idle state according to the tick event. */ + if (!*stop_tick && idx > 0 && + drv->states[idx].target_residency > data->tick_delta_us) { + /* + * The tick is not going to be stopped and the target + * residency of the state to be returned is not within + * the time until the next timer event including the + * tick, so try to correct that. + */ + for (i = idx - 1; i >= 0; i--) { + if (drv->states[i].disabled || + dev->states_usage[i].disable) + continue; + + idx = i; + if (drv->states[i].target_residency <= + data->tick_delta_us) + break; } } From patchwork Sun Aug 12 16:09:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Yan X-Patchwork-Id: 10563687 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9216D174A for ; Sun, 12 Aug 2018 16:10:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 81A9C29466 for ; Sun, 12 Aug 2018 16:10:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7641A29469; Sun, 12 Aug 2018 16:10:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 172D429466 for ; Sun, 12 Aug 2018 16:10:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728339AbeHLSsk (ORCPT ); Sun, 12 Aug 2018 14:48:40 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:38793 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728294AbeHLSsj (ORCPT ); Sun, 12 Aug 2018 14:48:39 -0400 Received: by mail-wm0-f67.google.com with SMTP id t25-v6so6451108wmi.3 for ; Sun, 12 Aug 2018 09:10:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=V/KD4PZAGRjRkYZ8ZQmu8anS2A7JIpWJS7JiZdKweu4=; b=aAes6E+f295M09g4lYl2w+zmOWFBPesp3HWKkqOQ5wPP5kvZPb7W5oWINgh1HcevCw oqkiFY51eG1PKuRYLMhZ+6B5AcRNNzTuCtfUB5xloqeK9tSAkUa15MIGFDbIYy9AmU8N AYXGe4AeVpuXtaiZrDMTZjIay1m0kZOJIQcQY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=V/KD4PZAGRjRkYZ8ZQmu8anS2A7JIpWJS7JiZdKweu4=; b=nF1N3NcVyiG4AKSxdG9v1x9BJIlEYE3l8Hy7aJBRAWr+Pi0iPfSewFQLeapk8iI+kQ vhdCgcet6gnhXeIt1T9fobXTKEDZE40ROCWQEwOKaHE9Y0ysyhcT1eOFLVFZS9x4gtwX vhfuhvYmIbtaKJxcJa0xnc3pec84WkRMm2SjKfqE06A0x1V1b7oImcQkHbWEyXswho5r ypPyPzbUE+SUGsKJq6Ca8toFwDtuRb8zjoE5lYS/fka3bUABG8cZokcMsBWMcI/rcN0t wTQiSFdSK08w+CwVrxukcc87KmII1G1xMJyttjmiAxKhIYvgTSZgaPQpIlc+xBliqYdi RmOg== X-Gm-Message-State: AOUpUlEn7cE5FrsnGj6PL+38mmr4CzXBDNJgm+3pzem8TOD3h4ME8Zu7 oXzvFHex9kVekzHzYiA8X3IqaA== X-Google-Smtp-Source: AA+uWPwUjljUvyDSo5RIJEKnpg6GAQDUGnFPU/b15XwILTLs5Oxj4ZvrxXpODptJ+gh11ntgmMPFYA== X-Received: by 2002:a1c:4885:: with SMTP id v127-v6mr5928679wma.161.1534090207747; Sun, 12 Aug 2018 09:10:07 -0700 (PDT) Received: from localhost.localdomain ([45.76.138.171]) by smtp.gmail.com with ESMTPSA id t6-v6sm7437369wmf.8.2018.08.12.09.10.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 12 Aug 2018 09:10:06 -0700 (PDT) From: Leo Yan To: "Rafael J. Wysocki" , "Peter Zijlstra (Intel)" , Daniel Lezcano , Vincent Guittot , Ramesh Thomas , linux-kernel@vger.kernel.org, Linux PM Cc: Leo Yan Subject: [PATCH v1 4/5] cpuidle: menu: Don't stay in shallow state for a long time Date: Mon, 13 Aug 2018 00:09:30 +0800 Message-Id: <1534090171-14464-5-git-send-email-leo.yan@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1534090171-14464-1-git-send-email-leo.yan@linaro.org> References: <1534090171-14464-1-git-send-email-leo.yan@linaro.org> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP To avoid staying in a shallow state for a long time, the menu governor relies on not stopping tick when detects the prediction is shorter than the tick event. This is just luckily to cover most cases but cannot say it is completely safe. For example, if the prediction is 2000us and the TICK_USEC=1000 so it's impossible to meet the condition 'data->predicted_us < TICK_USEC' and this lead to stop the tick for a shallow state; finally the CPU is possible to stay in this shallow state for very long time. This patch checks the candidate idle state isn't deepest one and find if the timer will come after more than 2 times of the maximum target residency, though the governor selects a shallow state according to prediction, due the timer is most reliable waken up source but it will come very late, so the CPU has chance to stay in the shallow state for a long time; the patch doesn't stop the tick for this case so can avoid powernightmares issue. Signed-off-by: Leo Yan --- drivers/cpuidle/governors/menu.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/drivers/cpuidle/governors/menu.c b/drivers/cpuidle/governors/menu.c index 4f02207..566c65c 100644 --- a/drivers/cpuidle/governors/menu.c +++ b/drivers/cpuidle/governors/menu.c @@ -284,6 +284,10 @@ static unsigned int get_typical_interval(struct menu_device *data) static bool menu_decide_stopping_tick(struct cpuidle_driver *drv, struct menu_device *data, int idx) { + int max_target_residency; + + max_target_residency = drv->states[drv->state_count-1].target_residency; + /* * If the tick has been stopped yet, force to stop it afterwards and * don't give chance to set *stop_tick to false. @@ -302,6 +306,23 @@ static bool menu_decide_stopping_tick(struct cpuidle_driver *drv, if (data->predicted_us < TICK_USEC) return false; + /* + * The candidate idle state isn't deepest one, on the other hand + * the most reliable wakeup source is timer (compare against to + * interrupts) says it will come after more than 2 times of maximum + * target residency, this means the CPU has risk to stay in shallow + * state for more than 2 times of maximum target residency. + * + * It's acceptable to stay in the shallow state at this time but we + * need to ensure to wake up the CPU by tick to check if has better + * choice. Finally it can have choice to select deeper state and + * avoid the CPU staying in shallow state for very long time and + * without any wake up event. + */ + if (idx < drv->state_count - 1 && + data->next_timer_us > max_target_residency * 2) + return false; + /* Otherwise, let's stop the tick at this time. */ return true; } From patchwork Sun Aug 12 16:09:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Yan X-Patchwork-Id: 10563685 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 76F59139A for ; Sun, 12 Aug 2018 16:10:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 645D929466 for ; Sun, 12 Aug 2018 16:10:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 58E2A29469; Sun, 12 Aug 2018 16:10:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 04DCC29466 for ; Sun, 12 Aug 2018 16:10:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728363AbeHLSsn (ORCPT ); Sun, 12 Aug 2018 14:48:43 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:54893 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728333AbeHLSsn (ORCPT ); Sun, 12 Aug 2018 14:48:43 -0400 Received: by mail-wm0-f65.google.com with SMTP id c14-v6so6464092wmb.4 for ; Sun, 12 Aug 2018 09:10:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SX0m3tGLxDNEaSJcRwiLoR6t4RrqbIncbJJxqLoIX9o=; b=HdnB8NatpocGEL93w0XRHehbE84HUhV/wb/KWqRlt4/m3a3yrGKkZEratVTpaarNKQ EjqDQIshLQT01QbrZqGG/CJX39qrxEfXaJ9Wk132TKDlcchpBqygOcrQwHp1UquQXCKL 0nIGiLHmbhTbfoy3tE8GrerVdc1/PNgi1XnPw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=SX0m3tGLxDNEaSJcRwiLoR6t4RrqbIncbJJxqLoIX9o=; b=PJrNxjysKDR+nEdpKQHxupV3PPz9sNGrtSvGJr4K5bXmCaMV7vOrnNskarKlx6T0Dj bIQynOhcm5UNeKkNe/d5bzk3c5HmokZdQmVU3r3AU3duzx+phF15ymP8s82NLobdM9kY IYtCpB8wAYd8/to6o1d1C01sBBa5s6kaYg2e1+tch5mlhHNaRMJ+3slQa0xrOGSAB4T8 JDLQNFpoYZN9oWUsqHrqODhQUSPdG/tp4n7CT32pFin0EzK57X4MPPTcwRvUN0VQbtwq 7u+NK2lf3oO/mP9TdX0fU3mZG6Du2/QOLoEtFw1WTmKgrCKl6irH2jH8QwJdMxUbJa+L VZFQ== X-Gm-Message-State: AOUpUlFa4Gwoe1IBX38DktXF0qS0nHgiitSVhabRM1gtlvlNmyt60uYh 5tXT+QwdaB5e0gaDTDNHGx0mbA== X-Google-Smtp-Source: AA+uWPyLiUo5B2rbJHEm0BkcnWYqQfm14cjj4pNytZM+5CkRTB7lO9YCmhOW9IjwzfAUN1H7MzU7Ig== X-Received: by 2002:a1c:1188:: with SMTP id 130-v6mr5942158wmr.138.1534090211534; Sun, 12 Aug 2018 09:10:11 -0700 (PDT) Received: from localhost.localdomain ([45.76.138.171]) by smtp.gmail.com with ESMTPSA id t6-v6sm7437369wmf.8.2018.08.12.09.10.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 12 Aug 2018 09:10:10 -0700 (PDT) From: Leo Yan To: "Rafael J. Wysocki" , "Peter Zijlstra (Intel)" , Daniel Lezcano , Vincent Guittot , Ramesh Thomas , linux-kernel@vger.kernel.org, Linux PM Cc: Leo Yan Subject: [PATCH v1 5/5] cpuidle: menu: Change to compare prediction with tick delta Date: Mon, 13 Aug 2018 00:09:31 +0800 Message-Id: <1534090171-14464-6-git-send-email-leo.yan@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1534090171-14464-1-git-send-email-leo.yan@linaro.org> References: <1534090171-14464-1-git-send-email-leo.yan@linaro.org> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The tick stopping decision is made by comparing the prediction with TICK_USEC, if the prediction is shorter than TICK_USEC then this means the CPU is likely waken up before the tick event so it's pointless to stop tick. In reality when make the decision, though the tick period is fixed to TICK_USEC, but the CPU is randomly entering/exiting idle states so the next tick delta is float and should be in the range [0, TICK_USEC]. This can result in wrong decision for stopping tick, e.g. if the prediction is 3ms idle duration and we compare with TICK_USEC=4000 (HZ=250), this can lead to a wrong conclusion is the tick event will be later than the prediction duration so the governor doesn't stop the tick; but in fact the tick is expired for 1ms, so the tick wakes up the CPU ahead and the CPU cannot stay in idle for 3ms as expected. Alternatively, 'data->tick_delta_us' is for the tick delta value and it's a accurate estimation for tick event coming. This patch changes to compare prediction with tick delta rather than comparing with the static tick interval. Signed-off-by: Leo Yan --- drivers/cpuidle/governors/menu.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/cpuidle/governors/menu.c b/drivers/cpuidle/governors/menu.c index 566c65c..06d5942 100644 --- a/drivers/cpuidle/governors/menu.c +++ b/drivers/cpuidle/governors/menu.c @@ -300,10 +300,11 @@ static bool menu_decide_stopping_tick(struct cpuidle_driver *drv, return false; /* - * Don't stop the tick if the prediction is shorter than the - * tick period length. + * If the prediction is shorter than the next tick event, means + * the CPU will be waken up before the tick event; don't stop + * the tick. */ - if (data->predicted_us < TICK_USEC) + if (data->predicted_us < data->tick_delta_us) return false; /*