Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp264139rwd; Wed, 31 May 2023 23:07:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6BT+DBWWBr+JKL5PR1/rH5cXoxK6NR7tz8UmK5WgzE0yrSJ7lEmPKRxGlHVW30dIOxUcbU X-Received: by 2002:a05:6358:8810:b0:125:7b6f:1ef with SMTP id hv16-20020a056358881000b001257b6f01efmr2139918rwb.27.1685599645810; Wed, 31 May 2023 23:07:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685599645; cv=none; d=google.com; s=arc-20160816; b=G+u+x112frwOlVnS0sXFuYoCH3HJ4HVOF/ED52qDzu5Gl3lLXz2B7kh95z9ktad5fk 4/GSzthITresIAiIVd3nHJsEqS7NiKOGx2iszG+6B5rg2hQlI9D5AOW7sRowzq9aS+Q7 zwigwRzA+BCHnvxesdbzvFbj/0ONBXZPfxkZ6bbw9R3cHHprf17HWUq2ANPRMYIRKvha lw1dMFZX1ZzeYCLya/47GNxtO1kLcA/Ld80QREfk8AqQ3C5oN9G7QcNN32ulD/wUNPct brnDNsxboB+d6cFiShiR6R596vqAIAFqQhgQctYuTZj9XmviekPWRc8jqVL/ipQLEAF5 ME1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=H64rb2yYp8MOyz/roqnSkHpqb1clNb2Uq7VZzIq9jAQ=; b=w1pTYvICqEJp9wv7WTeWrr2KCYHLeXMbOed7QeaHOVuMucNURuj1hIyGF7JoC2nOTw ObuULvamxW3hYv6zhn5ZUYH0Rx0VbsDqs6IePmqpQG2UKGwronyYLfWBH7PE7IKItkEb /lSVTYAmwItA3sZfX8IGU4Ix5/pQ3ROCaMdx8YR0wd7wP8dR0fyxOiNHKaBUm9E8h1Z5 nYs/56G4lKwdIJ9VJyuwTbiTuTW3NhWJgd7gDL4pgYTwKAFHS6juanhPgYF/eh7gxqpt nfw0q+kIv5wqK2hB+nnyUgQmdrwOInpzO6XTkHuNm/NX0RzbJ2WLUNuW+cXIzPFaI9KU gmog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=gPfRmIMy; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d21-20020a17090ad99500b002535adfa0e4si579786pjv.65.2023.05.31.23.07.10; Wed, 31 May 2023 23:07:25 -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=@google.com header.s=20221208 header.b=gPfRmIMy; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231366AbjFAF7C (ORCPT + 99 others); Thu, 1 Jun 2023 01:59:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231276AbjFAF7A (ORCPT ); Thu, 1 Jun 2023 01:59:00 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D60F3C9 for ; Wed, 31 May 2023 22:58:57 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-64d413b27a1so196322b3a.2 for ; Wed, 31 May 2023 22:58:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685599137; x=1688191137; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=H64rb2yYp8MOyz/roqnSkHpqb1clNb2Uq7VZzIq9jAQ=; b=gPfRmIMyXNJ7efaEUgu5j8YwEfzpoLjve3VskawV/jLJlUvCL7giRctbP6w37UsKY3 C/V+PiD8OLEMTg31u46QJe9l2AZjPLKZNa8nG06AY8lxJFYgN6mCN78/pyj8sqyJbF84 EjFtaFOkb0W3aDPM5hbTvDMwpZst3KzWKqrfJ5lwqZl6bkAimUAh69XFvquuaw3fYGeR KVpFijdNeJmyZ7ZgZax0dzUs7GltsTPLZTi3vxB0aYMGNGQ2M3lagRPptcPjwzZf9ZNx G8HxCgjZKmBaNLkAv2d+TViyNtH8efgwLm7no7gNyri+/d4akl6/JblCKHzFEpOm2bnf INYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685599137; x=1688191137; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=H64rb2yYp8MOyz/roqnSkHpqb1clNb2Uq7VZzIq9jAQ=; b=RZmFdrI5qUosrAgLJBB2SUaSF6r4vczLFqXRDQZoUXzWWSru6zGWkr2n2G/B98OQL0 EXf+yG/cNFI92hqleLmjGz61cmgf0tJWwJfRgMXWQtwJ4UEIw5SRa6HiIqEktsR3YvSf IJ/JMtJRoYLPXUJxTXNW+WpPTPQyKwv+8NCv6W9+/Q+dM5SQFTRnBLXd4zjSooJpIioW eYZZl1rf9q/1+7ZUZYk3UFmfauvvYDFeBAgf7eNd8aiAL95ABD819SDjhLgEw64oFFGB 1Y3bJ5dES5V5QVaB2WMkWiCtMh9ucbydN0OJBb4+MxCWpNrJvOgCxEGa7LBJ2k0pma2r rsog== X-Gm-Message-State: AC+VfDyi3cQRA6bkIHDhjCs/UF7yGLAKma4k7biqk7ElTLNGx4gI7XCM ewOAH512gwg9kgkJjihI9xtDyBgAhMp0KN8CgoS8bAhPPOE+lBx1UvqPY6uUJhQ9SsmhsB/hnVu Gsm+T0utRvyp2p8Do+M3DeDbuEpJmFDvqOOmlMsNMqlPq09s31DVqMs6xoNe0z78KXcYS/p4= X-Received: from jstultz-noogler2.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:600]) (user=jstultz job=sendgmr) by 2002:a05:6a00:2e24:b0:64d:2cb0:c60c with SMTP id fc36-20020a056a002e2400b0064d2cb0c60cmr2986111pfb.5.1685599137166; Wed, 31 May 2023 22:58:57 -0700 (PDT) Date: Thu, 1 Jun 2023 05:58:04 +0000 In-Reply-To: <20230601055846.2349566-1-jstultz@google.com> Mime-Version: 1.0 References: <20230601055846.2349566-1-jstultz@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230601055846.2349566-2-jstultz@google.com> Subject: [PATCH v4 01/13] sched: Unify runtime accounting across classes From: John Stultz To: LKML Cc: Peter Zijlstra , Joel Fernandes , Qais Yousef , Ingo Molnar , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Youssef Esmat , Mel Gorman , Daniel Bristot de Oliveira , Will Deacon , Waiman Long , Boqun Feng , "Paul E . McKenney" , kernel-team@android.com, "Connor O'Brien" , John Stultz Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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 From: Peter Zijlstra All classes use sched_entity::exec_start to track runtime and have copies of the exact same code around to compute runtime. Collapse all that. Cc: Joel Fernandes Cc: Qais Yousef Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Juri Lelli Cc: Vincent Guittot Cc: Dietmar Eggemann Cc: Valentin Schneider Cc: Steven Rostedt Cc: Ben Segall Cc: Zimuzo Ezeozue Cc: Youssef Esmat Cc: Mel Gorman Cc: Daniel Bristot de Oliveira Cc: Will Deacon Cc: Waiman Long Cc: Boqun Feng Cc: "Paul E . McKenney" Cc: kernel-team@android.com Signed-off-by: Peter Zijlstra (Intel) [fix conflicts, fold in update_current_exec_runtime] Signed-off-by: Connor O'Brien [jstultz: rebased, resovling minor conflicts] Signed-off-by: John Stultz --- NOTE: This patch is a general cleanup and if no one objects could be merged at this point. If needed, I'll resend separately if it isn't picked up on its own. --- include/linux/sched.h | 2 +- kernel/sched/deadline.c | 13 +++------- kernel/sched/fair.c | 56 ++++++++++++++++++++++++++++++---------- kernel/sched/rt.c | 13 +++------- kernel/sched/sched.h | 12 ++------- kernel/sched/stop_task.c | 13 +--------- 6 files changed, 52 insertions(+), 57 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index eed5d65b8d1f..37dd571a1246 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -521,7 +521,7 @@ struct sched_statistics { u64 block_max; s64 sum_block_runtime; - u64 exec_max; + s64 exec_max; u64 slice_max; u64 nr_migrations_cold; diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index 5a9a4b81c972..f6f746d52410 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -1308,9 +1308,8 @@ static void update_curr_dl(struct rq *rq) { struct task_struct *curr = rq->curr; struct sched_dl_entity *dl_se = &curr->dl; - u64 delta_exec, scaled_delta_exec; + s64 delta_exec, scaled_delta_exec; int cpu = cpu_of(rq); - u64 now; if (!dl_task(curr) || !on_dl_rq(dl_se)) return; @@ -1323,21 +1322,15 @@ static void update_curr_dl(struct rq *rq) * natural solution, but the full ramifications of this * approach need further study. */ - now = rq_clock_task(rq); - delta_exec = now - curr->se.exec_start; - if (unlikely((s64)delta_exec <= 0)) { + delta_exec = update_curr_common(rq); + if (unlikely(delta_exec <= 0)) { if (unlikely(dl_se->dl_yielded)) goto throttle; return; } - schedstat_set(curr->stats.exec_max, - max(curr->stats.exec_max, delta_exec)); - trace_sched_stat_runtime(curr, delta_exec, 0); - update_current_exec_runtime(curr, now, delta_exec); - if (dl_entity_is_special(dl_se)) return; diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 373ff5f55884..bf9e8f29398e 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -891,23 +891,17 @@ static void update_tg_load_avg(struct cfs_rq *cfs_rq) } #endif /* CONFIG_SMP */ -/* - * Update the current task's runtime statistics. - */ -static void update_curr(struct cfs_rq *cfs_rq) +static s64 update_curr_se(struct rq *rq, struct sched_entity *curr) { - struct sched_entity *curr = cfs_rq->curr; - u64 now = rq_clock_task(rq_of(cfs_rq)); - u64 delta_exec; - - if (unlikely(!curr)) - return; + u64 now = rq_clock_task(rq); + s64 delta_exec; delta_exec = now - curr->exec_start; - if (unlikely((s64)delta_exec <= 0)) - return; + if (unlikely(delta_exec <= 0)) + return delta_exec; curr->exec_start = now; + curr->sum_exec_runtime += delta_exec; if (schedstat_enabled()) { struct sched_statistics *stats; @@ -917,9 +911,43 @@ static void update_curr(struct cfs_rq *cfs_rq) max(delta_exec, stats->exec_max)); } - curr->sum_exec_runtime += delta_exec; - schedstat_add(cfs_rq->exec_clock, delta_exec); + return delta_exec; +} + +/* + * Used by other classes to account runtime. + */ +s64 update_curr_common(struct rq *rq) +{ + struct task_struct *curr = rq->curr; + s64 delta_exec; + delta_exec = update_curr_se(rq, &curr->se); + if (unlikely(delta_exec <= 0)) + return delta_exec; + + account_group_exec_runtime(curr, delta_exec); + cgroup_account_cputime(curr, delta_exec); + + return delta_exec; +} + +/* + * Update the current task's runtime statistics. + */ +static void update_curr(struct cfs_rq *cfs_rq) +{ + struct sched_entity *curr = cfs_rq->curr; + s64 delta_exec; + + if (unlikely(!curr)) + return; + + delta_exec = update_curr_se(rq_of(cfs_rq), curr); + if (unlikely(delta_exec <= 0)) + return; + + schedstat_add(cfs_rq->exec_clock, delta_exec); curr->vruntime += calc_delta_fair(delta_exec, curr); update_min_vruntime(cfs_rq); diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c index 00e0e5074115..0d0b276c447d 100644 --- a/kernel/sched/rt.c +++ b/kernel/sched/rt.c @@ -1046,24 +1046,17 @@ static void update_curr_rt(struct rq *rq) { struct task_struct *curr = rq->curr; struct sched_rt_entity *rt_se = &curr->rt; - u64 delta_exec; - u64 now; + s64 delta_exec; if (curr->sched_class != &rt_sched_class) return; - now = rq_clock_task(rq); - delta_exec = now - curr->se.exec_start; - if (unlikely((s64)delta_exec <= 0)) + delta_exec = update_curr_common(rq); + if (unlikely(delta_exec < 0)) return; - schedstat_set(curr->stats.exec_max, - max(curr->stats.exec_max, delta_exec)); - trace_sched_stat_runtime(curr, delta_exec, 0); - update_current_exec_runtime(curr, now, delta_exec); - if (!rt_bandwidth_enabled()) return; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index ec7b3e0a2b20..4a1ef64449b2 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -2166,6 +2166,8 @@ struct affinity_context { unsigned int flags; }; +extern s64 update_curr_common(struct rq *rq); + struct sched_class { #ifdef CONFIG_UCLAMP_TASK @@ -3242,16 +3244,6 @@ extern int sched_dynamic_mode(const char *str); extern void sched_dynamic_update(int mode); #endif -static inline void update_current_exec_runtime(struct task_struct *curr, - u64 now, u64 delta_exec) -{ - curr->se.sum_exec_runtime += delta_exec; - account_group_exec_runtime(curr, delta_exec); - - curr->se.exec_start = now; - cgroup_account_cputime(curr, delta_exec); -} - #ifdef CONFIG_SCHED_MM_CID #define SCHED_MM_CID_PERIOD_NS (100ULL * 1000000) /* 100ms */ diff --git a/kernel/sched/stop_task.c b/kernel/sched/stop_task.c index 85590599b4d6..7595494ceb6d 100644 --- a/kernel/sched/stop_task.c +++ b/kernel/sched/stop_task.c @@ -70,18 +70,7 @@ static void yield_task_stop(struct rq *rq) static void put_prev_task_stop(struct rq *rq, struct task_struct *prev) { - struct task_struct *curr = rq->curr; - u64 now, delta_exec; - - now = rq_clock_task(rq); - delta_exec = now - curr->se.exec_start; - if (unlikely((s64)delta_exec < 0)) - delta_exec = 0; - - schedstat_set(curr->stats.exec_max, - max(curr->stats.exec_max, delta_exec)); - - update_current_exec_runtime(curr, now, delta_exec); + update_curr_common(rq); } /* -- 2.41.0.rc0.172.g3f132b7071-goog