Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1924079rwr; Fri, 21 Apr 2023 01:21:54 -0700 (PDT) X-Google-Smtp-Source: AKy350aknFz41+/tqkENzIdxKYiMmvp27XPOjQaM8rMXIC7yk2PO0Z7VSw8uIeBdlFQCKEkuyj++ X-Received: by 2002:a05:6a20:9388:b0:f2:ad27:f98a with SMTP id x8-20020a056a20938800b000f2ad27f98amr222506pzh.14.1682065314278; Fri, 21 Apr 2023 01:21:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682065314; cv=none; d=google.com; s=arc-20160816; b=UUtSXAHtTbsbPbERAyp6DcizGfJdDHzFQ/MzcciljM6cZrS6nzqml5QkLWe2hgFpjX rdKM+S5+XNtJdAGqvKWXopIZKc4j2nfFTSBDtxZaaUWEPwbPCEaBIkB8QmPH21+o5q7g OuNWnmSjtUjq7dRdcVVMZUI6HQGZAmWrGW7jzzJ4YJKtdeTafRfYBK0NUU9xPgf/tKnS B9KtpBM6huDtMRjUHefxgWefwM7Mfz0pzBD7uRDnvBYlLiehF/wKeEhUDsplqdIXm5SI LD1SNYWKY/s5kamwVKIdiMgTzs/CtiOwI6f0UiNMc/dZNMEKgScJA9tdDVKdmsU+mV5B u3bw== 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:date:subject:cc:to:from :dkim-signature; bh=XIAD6upNjw+zDBn3KFz+DmtxA4CL5mcgbXE3yNZPih0=; b=I8uurJkhe3/FAg12TMnpMnD6ymzZA3W3eMOGqAQiiZGX8kB8UXaeqAyL6PrDjtyl9C Yrs96hkyasTQLLklMxBHvCrTD9u7vF9A8ntL94cpYW850vA+GPBD0rs7/L6Rl0VmFhpi o00LM9q9UuQOGSitah899ZDuJtXnfDmjsfCAE8bbJWClgJGxEhaXzCjaEvSLU42omQwf 9a2g0eVjOMe0tKoXRQrKru7DaGQpjBT+bRnT8sFLEr95lz9MGD6xvtqaQZG/kH/N3Bfc cM+n9dXj63LinstczF6sMyk47qb2jCAXxTe3MX5cLs4vLrbeEAjm9Sd2sdohd/c6FFwN FPCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZWBgi+b1; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t191-20020a6381c8000000b004fc2def26adsi1272595pgd.197.2023.04.21.01.21.42; Fri, 21 Apr 2023 01:21:54 -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=@intel.com header.s=Intel header.b=ZWBgi+b1; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230512AbjDUIMY (ORCPT + 99 others); Fri, 21 Apr 2023 04:12:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230424AbjDUIMT (ORCPT ); Fri, 21 Apr 2023 04:12:19 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F33D286BC for ; Fri, 21 Apr 2023 01:12:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1682064731; x=1713600731; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8UHeIwHoNHSaGGLlq77jKVSJJ0dt1Yd7Oj21caRg448=; b=ZWBgi+b1BWPoJGOZzDEf90LNVVGaiAiySVLI3S6sNF/gprSdJSNE4ZBQ YOySnocQWutQD4ACdMGOmpYAu9iRTAvkEfynNpLsRkbZ1hQEGVG8CET7W YiSiwmBqjss0LBaH606xPUvtCRnnHUYgao3OCr3ms3pxaMuTG/rqs7s/w b2aatONChwzSxZ+nFJhkCUnaEgbZL8Sj3QLYeXlyHYk14bRg5fKg0LS+s mJmK2B3wBbZ1TZ2Gquuh8n1tXgPsV/QC40bN7xMaDw19zBdLozdPLC9fF g0j23OdW5JMnqPwHsMRpFk6vqsw3rK1bDylteXQMmexe7r4LrCs1k6hkz A==; X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="326269502" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="326269502" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:12:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="722683002" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="722683002" Received: from chenyu-dev.sh.intel.com ([10.239.158.170]) by orsmga008.jf.intel.com with ESMTP; 21 Apr 2023 01:12:04 -0700 From: Chen Yu To: Peter Zijlstra , Vincent Guittot , Ingo Molnar , Juri Lelli Cc: Mel Gorman , Tim Chen , Dietmar Eggemann , Steven Rostedt , Ben Segall , K Prateek Nayak , Abel Wu , Yicong Yang , "Gautham R . Shenoy" , Honglei Wang , Len Brown , Chen Yu , Tianchen Ding , Joel Fernandes , Josh Don , Hillf Danton , kernel test robot , Arjan Van De Ven , Aaron Lu , linux-kernel@vger.kernel.org, Chen Yu Subject: [PATCH v7 1/2] sched/fair: Record the average duration of a task Date: Sat, 22 Apr 2023 00:07:59 +0800 Message-Id: <5bf131940d2fa1d25bddc745892ccd407beebcd6.1682060436.git.yu.c.chen@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DATE_IN_FUTURE_06_12, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_MED,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 Record the average duration of a task, as there is a requirement to leverage this information for better task placement. At first thought the (p->se.sum_exec_runtime / p->nvcsw) can be used to measure the task duration. However, the history long past was factored too heavily in such a formula. Ideally, the old activity should decay and not affect the current status too much. Although something based on PELT can be used, se.util_avg might not be appropriate to describe the task duration: Task p1 and task p2 are doing frequent ping-pong scheduling on one CPU, both p1 and p2 have a short duration, but the util_avg can be up to 50%, which is inconsistent with task duration. It was found that there was once a similar feature to track the duration of a task: commit ad4b78bbcbab ("sched: Add new wakeup preemption mode: WAKEUP_RUNNING") Unfortunately, it was reverted because it was an experiment. Pick the patch up again, by recording the average duration when a task voluntarily switches out. For example, suppose on CPU1, task p1 and p2 run alternatively: --------------------> time | p1 runs 1ms | p2 preempt p1 | p1 switch in, runs 0.5ms and blocks | ^ ^ ^ |_____________| |_____________________________________| ^ | p1 dequeued p1's duration in one section is (1 + 0.5)ms. Because if p2 does not preempt p1, p1 can run 1.5ms. This reflects the nature of a task: how long it wishes to run at most. Suggested-by: Tim Chen Suggested-by: Vincent Guittot Tested-by: K Prateek Nayak Signed-off-by: Chen Yu --- include/linux/sched.h | 3 +++ kernel/sched/core.c | 2 ++ kernel/sched/debug.c | 1 + kernel/sched/fair.c | 13 +++++++++++++ 4 files changed, 19 insertions(+) diff --git a/include/linux/sched.h b/include/linux/sched.h index 6d654eb4cabd..f94e6aa159b0 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -558,6 +558,9 @@ struct sched_entity { u64 prev_sum_exec_runtime; u64 nr_migrations; + u64 prev_sleep_sum_runtime; + /* average duration of a task */ + u64 dur_avg; #ifdef CONFIG_FAIR_GROUP_SCHED int depth; diff --git a/kernel/sched/core.c b/kernel/sched/core.c index d861db8aa7ab..59a6c0414a19 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4446,6 +4446,8 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p) p->se.prev_sum_exec_runtime = 0; p->se.nr_migrations = 0; p->se.vruntime = 0; + p->se.dur_avg = 0; + p->se.prev_sleep_sum_runtime = 0; INIT_LIST_HEAD(&p->se.group_node); #ifdef CONFIG_FAIR_GROUP_SCHED diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c index 1637b65ba07a..8d64fba16cfe 100644 --- a/kernel/sched/debug.c +++ b/kernel/sched/debug.c @@ -1024,6 +1024,7 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns, __PS("nr_involuntary_switches", p->nivcsw); P(se.load.weight); + P(se.dur_avg); #ifdef CONFIG_SMP P(se.avg.load_sum); P(se.avg.runnable_sum); diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index f5da01a6b35a..4af5799b90fc 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -6283,6 +6283,18 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) static void set_next_buddy(struct sched_entity *se); +static inline void dur_avg_update(struct task_struct *p, bool task_sleep) +{ + u64 dur; + + if (!task_sleep) + return; + + dur = p->se.sum_exec_runtime - p->se.prev_sleep_sum_runtime; + p->se.prev_sleep_sum_runtime = p->se.sum_exec_runtime; + update_avg(&p->se.dur_avg, dur); +} + /* * The dequeue_task method is called before nr_running is * decreased. We remove the task from the rbtree and @@ -6355,6 +6367,7 @@ static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) dequeue_throttle: util_est_update(&rq->cfs, p, task_sleep); + dur_avg_update(p, task_sleep); hrtick_update(rq); } -- 2.25.1