From patchwork Fri Aug 23 20:05:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joshua Hahn joshua.hahn6@gmail.com X-Patchwork-Id: 822960 Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 521911A0AEA; Fri, 23 Aug 2024 20:13:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724444001; cv=none; b=nGcuibytJznGqrWWHWjtZo8jO8cS7Ow9ptR9igiMJRu3tJLnl8ImNCaRs9BH5YRmmw0Z+vfY3JtvwsFCorpCMrDoMtP0nxBQWMsTyabXLLQ9QhPmBZnjUcQ45AnetYqlNJMdAMPDMQN0bz4EwhoSSzypuVYw+c/lQLK+86i55GM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724444001; c=relaxed/simple; bh=UL/XckKo3kn7th1uM+7y69Pt86ODI3pVVR/tWeCFLEc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UyCY2ADwSixgjZm9yXGEobTILTP4KgkPsMGWeUXeSui4obghySI19laoyfjDEAMHCPyp3zoqsAlA/j2p8O3gSPgc2dcD2gEA4/hxSniARZXRzcqmNFjTdW9xo32164V/R7hMPL1xn4A+kuZrdShwDjwBMg38K5VDeWlgghIEwHA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.219.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yb1-f180.google.com with SMTP id 3f1490d57ef6-e1659e9a982so2690741276.1; Fri, 23 Aug 2024 13:13:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724443999; x=1725048799; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nWwDIy8I8h3qLuacU8FLKoOjmAz8N3kjG+Ycu/vPy88=; b=l86DybrcIV5J6nEwrRv3mMxn4e4+dSak3u25s4+5VvSoVNPa4JYa99egimnQ//tAHL vF1bKKXTRnjzrhpRyZBwfHEX5GFAykXK9dY2P0JNwQQZXaFTtJayMa9jVSPQvwb1sd6y rlx7fgXg7uOTt/V7QfjenCyI1tK9MB3LS13vgH0Nd/ECIKVRHT1bOn8Y7pMrSpSmj9CL Q6ERNN282XnaJggOzLk146FMEgBe5XgGB3RzqyVjrMbPw/iOuJ1cIiznD5ut3gJS2Cpn QC8bXpjj33EUoJw4Pyc27Y+BVFjf99X0SUDeen9vSsLD9b9qPbQgfaip/JMEQ8KT6fLB ojjw== X-Forwarded-Encrypted: i=1; AJvYcCU28+UkV3S2sfs/JYr6V2ikrl7H+jScMiBvGK8WE0EylT7zc+Iczw3kPKMc+TQS+n/orQtVD+UhGRqDMGT8@vger.kernel.org, AJvYcCXFg2h882R+7nI4IqKHQE4Jjkl8oN6HwCgCkNM3D00DwvSGIxmOB8faunoDCjgVro5CSdBS2HsSMHH/IxLeajDZ@vger.kernel.org, AJvYcCXnjfyvY3msG1GJ0ut8Q5+B9DdsOEm7iVJt0deQjrPw0n8faeRa4dJL9eaCLIWSriKHnOZZ48cs@vger.kernel.org X-Gm-Message-State: AOJu0YwKAOfPVoNbNZya2orkWhMWAlua++s+Gyd+JQThFLW+aN2lTQjb BKFM+Odw0oQdegOXqfiDVCwcvmU+Vu86hPzRVWheAVJm8deDmBqo X-Google-Smtp-Source: AGHT+IGGQcXqQBnVapgoipWzzWG7/XIjvm84064Irp0z/7OKPnrFAtLNiyo8JHuDyQL335N3YTTz7g== X-Received: by 2002:a05:6902:1895:b0:e0b:e28c:b4f4 with SMTP id 3f1490d57ef6-e17a8666314mr4182818276.38.1724443999227; Fri, 23 Aug 2024 13:13:19 -0700 (PDT) Received: from localhost (fwdproxy-frc-007.fbsv.net. [2a03:2880:21ff:7::face:b00c]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e178e4637e1sm788386276.15.2024.08.23.13.13.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Aug 2024 13:13:18 -0700 (PDT) From: Joshua Hahn joshua.hahn6@gmail.com To: tj@kernel.org Cc: lizefan.x@bytedance.com, hannes@cmpxchg.org, mkoutny@suse.com, shuah@kernel.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 1/2] Tracking cgroup-level niced CPU time Date: Fri, 23 Aug 2024 13:05:17 -0700 Message-ID: <20240823201317.156379-2-joshua.hahn6@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240823201317.156379-1-joshua.hahn6@gmail.com> References: <20240823201317.156379-1-joshua.hahn6@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Joshua Hahn Cgroup-level CPU statistics currently include time spent on user/system processes, but do not include niced CPU time (despite already being tracked). This patch exposes niced CPU time to the userspace, allowing users to get a better understanding of their hardware limits and can facilitate better load-balancing. A new field 'ntime' is added to struct cgroup_base_stat as opposed to struct task_cputime to minimize footprint. --- include/linux/cgroup-defs.h | 1 + kernel/cgroup/rstat.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index ae04035b6cbe..a2fcb3db6c52 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -315,6 +315,7 @@ struct cgroup_base_stat { #ifdef CONFIG_SCHED_CORE u64 forceidle_sum; #endif + u64 ntime; }; /* diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index a06b45272411..a77ba9a83bab 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -444,6 +444,7 @@ static void cgroup_base_stat_add(struct cgroup_base_stat *dst_bstat, #ifdef CONFIG_SCHED_CORE dst_bstat->forceidle_sum += src_bstat->forceidle_sum; #endif + dst_bstat->ntime += src_bstat->ntime; } static void cgroup_base_stat_sub(struct cgroup_base_stat *dst_bstat, @@ -455,6 +456,7 @@ static void cgroup_base_stat_sub(struct cgroup_base_stat *dst_bstat, #ifdef CONFIG_SCHED_CORE dst_bstat->forceidle_sum -= src_bstat->forceidle_sum; #endif + dst_bstat->ntime -= src_bstat->ntime; } static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu) @@ -535,7 +537,10 @@ void __cgroup_account_cputime_field(struct cgroup *cgrp, switch (index) { case CPUTIME_USER: + rstatc->bstat.cputime.utime += delta_exec; + break; case CPUTIME_NICE: + rstatc->bstat.ntime += delta_exec; rstatc->bstat.cputime.utime += delta_exec; break; case CPUTIME_SYSTEM: @@ -591,6 +596,7 @@ static void root_cgroup_cputime(struct cgroup_base_stat *bstat) #ifdef CONFIG_SCHED_CORE bstat->forceidle_sum += cpustat[CPUTIME_FORCEIDLE]; #endif + bstat->ntime += cpustat[CPUTIME_NICE]; } } @@ -608,13 +614,14 @@ static void cgroup_force_idle_show(struct seq_file *seq, struct cgroup_base_stat void cgroup_base_stat_cputime_show(struct seq_file *seq) { struct cgroup *cgrp = seq_css(seq)->cgroup; - u64 usage, utime, stime; + u64 usage, utime, stime, ntime; if (cgroup_parent(cgrp)) { cgroup_rstat_flush_hold(cgrp); usage = cgrp->bstat.cputime.sum_exec_runtime; cputime_adjust(&cgrp->bstat.cputime, &cgrp->prev_cputime, &utime, &stime); + ntime = cgrp->bstat.ntime; cgroup_rstat_flush_release(cgrp); } else { /* cgrp->bstat of root is not actually used, reuse it */ @@ -622,16 +629,19 @@ void cgroup_base_stat_cputime_show(struct seq_file *seq) usage = cgrp->bstat.cputime.sum_exec_runtime; utime = cgrp->bstat.cputime.utime; stime = cgrp->bstat.cputime.stime; + ntime = cgrp->bstat.ntime; } do_div(usage, NSEC_PER_USEC); do_div(utime, NSEC_PER_USEC); do_div(stime, NSEC_PER_USEC); + do_div(ntime, NSEC_PER_USEC); seq_printf(seq, "usage_usec %llu\n" "user_usec %llu\n" - "system_usec %llu\n", - usage, utime, stime); + "system_usec %llu\n" + "nice_usec %llu\n", + usage, utime, stime, ntime); cgroup_force_idle_show(seq, &cgrp->bstat); } From patchwork Fri Aug 23 20:05:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joshua Hahn joshua.hahn6@gmail.com X-Patchwork-Id: 821954 Received: from mail-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D35F1A2542; Fri, 23 Aug 2024 20:13:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724444003; cv=none; b=u5ecDM/jJqJDxDIubOGYDe5V3eDLp7vauvOp5I8fHvm3f38kHXSj2w1/EPzaKnyLkqbVkv+LN3M+lGdEEPVrFBRe0Hfq0PCpluMoscXAN4NHVv4RdsD+18acxIF6Fhsmz3SBC7N0OdODDUVLCHU2/5NITwEw/Tl2Ww8jFYDaNCQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724444003; c=relaxed/simple; bh=0ZWXXcdMHQD7yQhOFHjj1ApmLTmEwbfaGpySJmAc0GU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Cm01Dq5iZQwrN1NeWH+8CwmCO9zrpeTiC75rAk05ysK9fdVGBxjnskirtStbKZDQdRgOGPO7pKCt6HbdKqmcK537dKhn+KmMCG+ZsFhCnUn+lqz2DyqSblNlpgbZcvFqhoPyc2/TlPO6Ikm8aH6pG8P0cGOHwF0kf6DbTjMczIo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.219.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yb1-f169.google.com with SMTP id 3f1490d57ef6-e162df8bab4so2255228276.0; Fri, 23 Aug 2024 13:13:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724444000; x=1725048800; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=E28ANgGeyclkwdvrEsGewBjyWcjmIbB2+4duJTvxOC4=; b=B73mokvCa7dirUsYGe1xTyo2uWv5Q8iylEv/ERrjLXGJD9AKlGf3vslzc2GyR9TnzW QrhQdqSJy/Agy9nP4NayJuTVvKa+rx57KF4izdVfn1imzNzcL74xS6LtdsQqyuAHOgVO hGnpw3tX9/zQLVY5swU81RUhYNia6nue7oMAfLQTnqKpTJKplyXtdUt7WlPoRopYLXyY Mb53lBui0XNsxZtadonsRfIL+2RPPl4E1tDopY3fAQziUcU4rBbaB81qN5ZzFRbzscdw l5bDfUhwqjIvrogEPp2eZ4IxdyYVW8u0FlMH4Dk1taWJq2fx+j4XVWtU/q05oh//xRj+ dyfw== X-Forwarded-Encrypted: i=1; AJvYcCVHjwygxIiZPF565fZedLTF+xgEfnzc8RvPJHwNjQkQxW9tKLapiWNNK8ssVC/CczGj3wgu6v5AuM9nNNZfk99y@vger.kernel.org, AJvYcCWInrvk2Y5pusyhKO0mMzkLpChzebzH05Jan20AB0gJHquIDLN7oh/AMd2GGVtesnJJXb6a6q/1@vger.kernel.org, AJvYcCXN1+BqxABsKNZpl888l4XEsNfjz4VejN/0nQl2p5HR7XkUMNJIUL/R9tuSfgjCy1f6pOS0Di2lnqs7Fhgk@vger.kernel.org X-Gm-Message-State: AOJu0YwDkIa3PHSBJr/9okfcGW6Ja66zm3ryL1l7olTrwAnvU3Em+j4G EFExodHB1wk+EL5BCDeAKKaSEUyrVuRPMrgPZ58sqr10rK0pzKqC X-Google-Smtp-Source: AGHT+IGRQdVgD+Z0XXzEDRPlmEXjVlDzfPqGLxw1CLnmBdevSGwx+4jtlizhbK6M3KfDcg/kNNj2ww== X-Received: by 2002:a05:6902:2610:b0:e0b:2b6c:3cc with SMTP id 3f1490d57ef6-e17a868a7a4mr3678281276.51.1724444000416; Fri, 23 Aug 2024 13:13:20 -0700 (PDT) Received: from localhost (fwdproxy-frc-020.fbsv.net. [2a03:2880:21ff:14::face:b00c]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e178e56a5f7sm812073276.44.2024.08.23.13.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Aug 2024 13:13:19 -0700 (PDT) From: Joshua Hahn joshua.hahn6@gmail.com To: tj@kernel.org Cc: lizefan.x@bytedance.com, hannes@cmpxchg.org, mkoutny@suse.com, shuah@kernel.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 2/2] Selftests for niced CPU statistics Date: Fri, 23 Aug 2024 13:05:18 -0700 Message-ID: <20240823201317.156379-3-joshua.hahn6@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240823201317.156379-1-joshua.hahn6@gmail.com> References: <20240823201317.156379-1-joshua.hahn6@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Joshua Hahn Creates a cgroup with a single nice CPU hog process running. fork() is called to generate the nice process because un-nicing is not possible (see man nice(3)). If fork() was not used to generate the CPU hog, we would run the rest of the cgroup selftest suite as a nice process. --- tools/testing/selftests/cgroup/test_cpu.c | 72 +++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/selftests/cgroup/test_cpu.c index dad2ed82f3ef..cd5550391f49 100644 --- a/tools/testing/selftests/cgroup/test_cpu.c +++ b/tools/testing/selftests/cgroup/test_cpu.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "../kselftest.h" #include "cgroup_util.h" @@ -229,6 +230,76 @@ static int test_cpucg_stats(const char *root) return ret; } +/* + * Creates a nice process that consumes CPU and checks that the elapsed + * usertime in the cgroup is close to the expected time. + */ +static int test_cpucg_nice(const char *root) +{ + int ret = KSFT_FAIL; + int status; + long user_usec, nice_usec; + long usage_seconds = 2; + long expected_nice_usec = usage_seconds * USEC_PER_SEC; + char *cpucg; + pid_t pid; + + cpucg = cg_name(root, "cpucg_test"); + if (!cpucg) + goto cleanup; + + if (cg_create(cpucg)) + goto cleanup; + + user_usec = cg_read_key_long(cpucg, "cpu.stat", "user_usec"); + nice_usec = cg_read_key_long(cpucg, "cpu.stat", "nice_usec"); + if (user_usec != 0 || nice_usec != 0) + goto cleanup; + + /* + * We fork here to create a new process that can be niced without + * polluting the nice value of other selftests + */ + pid = fork(); + if (pid < 0) { + goto cleanup; + } else if (pid == 0) { + struct cpu_hog_func_param param = { + .nprocs = 1, + .ts = { + .tv_sec = usage_seconds, + .tv_nsec = 0, + }, + .clock_type = CPU_HOG_CLOCK_PROCESS, + }; + + /* Try to keep niced CPU usage as constrained to hog_cpu as possible */ + nice(1); + cg_run(cpucg, hog_cpus_timed, (void *)¶m); + exit(0); + } else { + waitpid(pid, &status, 0); + if (!WIFEXITED(status)) + goto cleanup; + + user_usec = cg_read_key_long(cpucg, "cpu.stat", "user_usec"); + nice_usec = cg_read_key_long(cpucg, "cpu.stat", "nice_usec"); + if (nice_usec > user_usec || user_usec <= 0) + goto cleanup; + + if (!values_close(nice_usec, expected_nice_usec, 1)) + goto cleanup; + + ret = KSFT_PASS; + } + +cleanup: + cg_destroy(cpucg); + free(cpucg); + + return ret; +} + static int run_cpucg_weight_test( const char *root, @@ -686,6 +757,7 @@ struct cpucg_test { } tests[] = { T(test_cpucg_subtree_control), T(test_cpucg_stats), + T(test_cpucg_nice), T(test_cpucg_weight_overprovisioned), T(test_cpucg_weight_underprovisioned), T(test_cpucg_nested_weight_overprovisioned),