From nobody Sun May 10 21:17:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88F0DC433EF for ; Fri, 22 Apr 2022 17:37:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234491AbiDVRkV (ORCPT ); Fri, 22 Apr 2022 13:40:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234623AbiDVRkM (ORCPT ); Fri, 22 Apr 2022 13:40:12 -0400 Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECDA2102CE5; Fri, 22 Apr 2022 10:37:07 -0700 (PDT) Received: by mail-qt1-x82d.google.com with SMTP id hf18so6028930qtb.0; Fri, 22 Apr 2022 10:37:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rYC9LwSf9hKtGt3gL+sG23+M8BOF30E6rBmugyKTlUE=; b=VuV2FLTMGQw6Y+kTZhRwh9YDTOVZOZ/JjSPmrQ5Y4KWg/cKqojH3EIznYBo1dofHjH +N3AgGOEkzJ/8vciWhqCJEL6PO/7TaNrSTr2oAMkG6IUgpsQN+9Z7E3G3Pv1xCExtFG5 1KWnmZlfCzf16tPzoMBs2qH0uLjOUDDz/Jf4JR+QAf9Rw/bWeCDXEGDNUreYSEVF1CLF HC6ytcqUsiQamJq2RpUFrWdoDeCqDN0aWZkaCLKgqhgCRdgRmA+N3oPoT115lkBPTQL0 rsL0shQpSWYbGcqZpMEnh8O+7NrDykUjJZdbhkvQhYVLgBNZhbWSPkEVW/tLK/2GP+Md DJyg== X-Gm-Message-State: AOAM530CSKepuE9Vr+ipDFET4B67ho/tThgP4A6zDu7AvWrWqQFBF6QY QNt4i0qvokN8txrqvVxdKcQ= X-Google-Smtp-Source: ABdhPJxV05PtSC4F9XnIhE3tcIEZz63GnTMA8Kee2UaPIk1OJaBtsC1fQSWWCviUoGpDgEs3wOxYoQ== X-Received: by 2002:ac8:5795:0:b0:2f1:f9cd:4076 with SMTP id v21-20020ac85795000000b002f1f9cd4076mr4002801qta.549.1650648865242; Fri, 22 Apr 2022 10:34:25 -0700 (PDT) Received: from localhost (fwdproxy-ash-116.fbsv.net. [2a03:2880:20ff:74::face:b00c]) by smtp.gmail.com with ESMTPSA id o21-20020a05620a0d5500b0069c59fae1a5sm1134798qkl.96.2022.04.22.10.34.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 10:34:25 -0700 (PDT) From: David Vernet To: tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org Cc: cgroups@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 1/4] cgroup: Add new test_cpu.c test suite in cgroup selftests Date: Fri, 22 Apr 2022 10:33:49 -0700 Message-Id: <20220422173349.3394844-2-void@manifault.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220422173349.3394844-1-void@manifault.com> References: <20220422173349.3394844-1-void@manifault.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The cgroup selftests suite currently contains tests that validate various aspects of cgroup, such as validating the expected behavior for memory controllers, the expected behavior of cgroup.procs, etc. There are no tests that validate the expected behavior of the cgroup cpu controller. This patch therefore adds a new test_cpu.c file that will contain cpu controller testcases. The file currently only contains a single testcase that validates creating nested cgroups with cgroup.subtree_control including cpu. Future patches will add more sophisticated testcases that validate functional aspects of the cpu controller. Signed-off-by: David Vernet --- tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 2 + tools/testing/selftests/cgroup/test_cpu.c | 110 ++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_cpu.c diff --git a/tools/testing/selftests/cgroup/.gitignore b/tools/testing/self= tests/cgroup/.gitignore index be9643ef6285..306ee1b01e72 100644 --- a/tools/testing/selftests/cgroup/.gitignore +++ b/tools/testing/selftests/cgroup/.gitignore @@ -4,3 +4,4 @@ test_core test_freezer test_kmem test_kill +test_cpu diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selfte= sts/cgroup/Makefile index 745fe25fa0b9..478217cc1371 100644 --- a/tools/testing/selftests/cgroup/Makefile +++ b/tools/testing/selftests/cgroup/Makefile @@ -10,6 +10,7 @@ TEST_GEN_PROGS +=3D test_kmem TEST_GEN_PROGS +=3D test_core TEST_GEN_PROGS +=3D test_freezer TEST_GEN_PROGS +=3D test_kill +TEST_GEN_PROGS +=3D test_cpu =20 LOCAL_HDRS +=3D $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidf= d.h =20 @@ -20,3 +21,4 @@ $(OUTPUT)/test_kmem: cgroup_util.c $(OUTPUT)/test_core: cgroup_util.c $(OUTPUT)/test_freezer: cgroup_util.c $(OUTPUT)/test_kill: cgroup_util.c +$(OUTPUT)/test_cpu: cgroup_util.c diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/self= tests/cgroup/test_cpu.c new file mode 100644 index 000000000000..a724bff00d07 --- /dev/null +++ b/tools/testing/selftests/cgroup/test_cpu.c @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include + +#include "../kselftest.h" +#include "cgroup_util.h" + +/* + * This test creates two nested cgroups with and without enabling + * the cpu controller. + */ +static int test_cpucg_subtree_control(const char *root) +{ + char *parent =3D NULL, *child =3D NULL, *parent2 =3D NULL, *child2 =3D NU= LL; + int ret =3D KSFT_FAIL; + + // Create two nested cgroups with the cpu controller enabled. + parent =3D cg_name(root, "cpucg_test_0"); + if (!parent) + goto cleanup; + + if (cg_create(parent)) + goto cleanup; + + if (cg_write(parent, "cgroup.subtree_control", "+cpu")) + goto cleanup; + + child =3D cg_name(parent, "cpucg_test_child"); + if (!child) + goto cleanup; + + if (cg_create(child)) + goto cleanup; + + if (cg_read_strstr(child, "cgroup.controllers", "cpu")) + goto cleanup; + + // Create two nested cgroups without enabling the cpu controller. + parent2 =3D cg_name(root, "cpucg_test_1"); + if (!parent2) + goto cleanup; + + if (cg_create(parent2)) + goto cleanup; + + child2 =3D cg_name(parent2, "cpucg_test_child"); + if (!child2) + goto cleanup; + + if (cg_create(child2)) + goto cleanup; + + if (!cg_read_strstr(child2, "cgroup.controllers", "cpu")) + goto cleanup; + + ret =3D KSFT_PASS; + +cleanup: + cg_destroy(child); + free(child); + cg_destroy(child2); + free(child2); + cg_destroy(parent); + free(parent); + cg_destroy(parent2); + free(parent2); + + return ret; +} + +#define T(x) { x, #x } +struct cpucg_test { + int (*fn)(const char *root); + const char *name; +} tests[] =3D { + T(test_cpucg_subtree_control), +}; +#undef T + +int main(int argc, char *argv[]) +{ + char root[PATH_MAX]; + int i, ret =3D EXIT_SUCCESS; + + if (cg_find_unified_root(root, sizeof(root))) + ksft_exit_skip("cgroup v2 isn't mounted\n"); + + if (cg_read_strstr(root, "cgroup.subtree_control", "cpu")) + if (cg_write(root, "cgroup.subtree_control", "+cpu")) + ksft_exit_skip("Failed to set cpu controller\n"); + + for (i =3D 0; i < ARRAY_SIZE(tests); i++) { + switch (tests[i].fn(root)) { + case KSFT_PASS: + ksft_test_result_pass("%s\n", tests[i].name); + break; + case KSFT_SKIP: + ksft_test_result_skip("%s\n", tests[i].name); + break; + default: + ret =3D EXIT_FAILURE; + ksft_test_result_fail("%s\n", tests[i].name); + break; + } + } + + return ret; +} --=20 2.30.2 From nobody Sun May 10 21:17:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C28EC433F5 for ; Fri, 22 Apr 2022 17:36:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232138AbiDVRjG (ORCPT ); Fri, 22 Apr 2022 13:39:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234009AbiDVRjA (ORCPT ); Fri, 22 Apr 2022 13:39:00 -0400 Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CE08D5548; Fri, 22 Apr 2022 10:35:51 -0700 (PDT) Received: by mail-qk1-f175.google.com with SMTP id d198so6283454qkc.12; Fri, 22 Apr 2022 10:35:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0DSWbnn0fhVndljQbawmD94d+TjvuiqLmSnohT7FsPA=; b=HZHwFORWyaCg5tUTlXM3nOsJbcia5kew7VHjz5ht4rIAlGQUa/p7z4+SNKU6Z3t4rr 0PVeoXIcsJXIGFC3EAAeB19fwMChnLK2V4gs27ciFHE4qZk46CDoXkufgfkUOxudQgk4 s631MSb9FHCp+pNEJab4/SOh1K2DEWR/vEvgJeKcdDMuo7LHZpARNpCI+N1JpeANjG+/ VuxzP4CYXn1P8Q37KjXzpElgoVNFTzkzrcPdW35Ly4Um99WPa4dHM4ZE5EjB4wZI0cIq 1cDK0s3DQ9Dtq12LI7WdnjbR+RZFhQGvsjwsN/z65KQiuVcZv2XaTTdzO9EK7bfHWhK0 dGwA== X-Gm-Message-State: AOAM530u5RMH1ox3J69aRbIxS/tEqm/c1M50qgQhjL22M1SnX906Khn9 0uf20iBiP8uERc6NT+53hyk= X-Google-Smtp-Source: ABdhPJwprj8tGc/FLZJ7x9LSgD1YITitei6i6hU3Csv/3S7k2n2Y7Niaj5cPexfM/6JKvYjqB0EHmg== X-Received: by 2002:a37:aa92:0:b0:69c:89c4:e6ee with SMTP id t140-20020a37aa92000000b0069c89c4e6eemr3390371qke.22.1650648868855; Fri, 22 Apr 2022 10:34:28 -0700 (PDT) Received: from localhost (fwdproxy-ash-021.fbsv.net. [2a03:2880:20ff:15::face:b00c]) by smtp.gmail.com with ESMTPSA id d22-20020a05622a101600b002f1f139d83bsm1445521qte.84.2022.04.22.10.34.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 10:34:28 -0700 (PDT) From: David Vernet To: tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org Cc: cgroups@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 2/4] cgroup: Add test_cpucg_stats() testcase to cgroup cpu selftests Date: Fri, 22 Apr 2022 10:33:51 -0700 Message-Id: <20220422173349.3394844-3-void@manifault.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220422173349.3394844-1-void@manifault.com> References: <20220422173349.3394844-1-void@manifault.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" test_cpu.c includes testcases that validate the cgroup cpu controller. This patch adds a new testcase called test_cpucg_stats() that verifies the expected behavior of the cpu.stat interface. In doing so, we define a new hog_cpus_timed() function which takes a cpu_hog_func_param struct that configures how many CPUs it uses, and how long it runs. Future patches will also spawn threads that hog CPUs, so this function will eventually serve those use-cases as well. Signed-off-by: David Vernet --- tools/testing/selftests/cgroup/cgroup_util.h | 3 + tools/testing/selftests/cgroup/test_cpu.c | 128 +++++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/s= elftests/cgroup/cgroup_util.h index 4f66d10626d2..1df13dc8b8aa 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.h +++ b/tools/testing/selftests/cgroup/cgroup_util.h @@ -8,6 +8,9 @@ =20 #define MB(x) (x << 20) =20 +#define USEC_PER_SEC 1000000L +#define NSEC_PER_SEC 1000000000L + /* * Checks if two given values differ by less than err% of their sum. */ diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/self= tests/cgroup/test_cpu.c index a724bff00d07..3bd61964a262 100644 --- a/tools/testing/selftests/cgroup/test_cpu.c +++ b/tools/testing/selftests/cgroup/test_cpu.c @@ -2,11 +2,19 @@ =20 #define _GNU_SOURCE #include +#include +#include #include +#include =20 #include "../kselftest.h" #include "cgroup_util.h" =20 +struct cpu_hog_func_param { + int nprocs; + struct timespec ts; +}; + /* * This test creates two nested cgroups with and without enabling * the cpu controller. @@ -70,12 +78,132 @@ static int test_cpucg_subtree_control(const char *root) return ret; } =20 +static void *hog_cpu_thread_func(void *arg) +{ + while (1) + ; + + return NULL; +} + +static struct timespec +timespec_sub(const struct timespec *lhs, const struct timespec *rhs) +{ + struct timespec zero =3D { + .tv_sec =3D 0, + .tv_nsec =3D 0, + }; + struct timespec ret; + + if (lhs->tv_sec < rhs->tv_sec) + return zero; + + ret.tv_sec =3D lhs->tv_sec - rhs->tv_sec; + + if (lhs->tv_nsec < rhs->tv_nsec) { + if (ret.tv_sec =3D=3D 0) + return zero; + + ret.tv_sec--; + ret.tv_nsec =3D NSEC_PER_SEC - rhs->tv_nsec + lhs->tv_nsec; + } else + ret.tv_nsec =3D lhs->tv_nsec - rhs->tv_nsec; + + return ret; +} + +static int hog_cpus_timed(const char *cgroup, void *arg) +{ + const struct cpu_hog_func_param *param =3D + (struct cpu_hog_func_param *)arg; + struct timespec ts_run =3D param->ts; + struct timespec ts_remaining =3D ts_run; + int i, ret; + + for (i =3D 0; i < param->nprocs; i++) { + pthread_t tid; + + ret =3D pthread_create(&tid, NULL, &hog_cpu_thread_func, NULL); + if (ret !=3D 0) + return ret; + } + + while (ts_remaining.tv_sec > 0 || ts_remaining.tv_nsec > 0) { + struct timespec ts_total; + + ret =3D nanosleep(&ts_remaining, NULL); + if (ret && errno !=3D EINTR) + return ret; + + ret =3D clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_total); + if (ret !=3D 0) + return ret; + + ts_remaining =3D timespec_sub(&ts_run, &ts_total); + } + + return 0; +} + +/* + * Creates a cpu cgroup, burns a CPU for a few quanta, and verifies that + * cpu.stat shows the expected output. + */ +static int test_cpucg_stats(const char *root) +{ + int ret =3D KSFT_FAIL; + long usage_usec, user_usec, system_usec; + long usage_seconds =3D 2; + long expected_usage_usec =3D usage_seconds * USEC_PER_SEC; + char *cpucg; + + cpucg =3D cg_name(root, "cpucg_test"); + if (!cpucg) + goto cleanup; + + if (cg_create(cpucg)) + goto cleanup; + + usage_usec =3D cg_read_key_long(cpucg, "cpu.stat", "usage_usec"); + user_usec =3D cg_read_key_long(cpucg, "cpu.stat", "user_usec"); + system_usec =3D cg_read_key_long(cpucg, "cpu.stat", "system_usec"); + if (usage_usec !=3D 0 || user_usec !=3D 0 || system_usec !=3D 0) + goto cleanup; + + struct cpu_hog_func_param param =3D { + .nprocs =3D 1, + .ts =3D { + .tv_sec =3D usage_seconds, + .tv_nsec =3D 0, + }, + }; + if (cg_run(cpucg, hog_cpus_timed, (void *)¶m)) + goto cleanup; + + usage_usec =3D cg_read_key_long(cpucg, "cpu.stat", "usage_usec"); + user_usec =3D cg_read_key_long(cpucg, "cpu.stat", "user_usec"); + if (user_usec <=3D 0) + goto cleanup; + + if (!values_close(usage_usec, expected_usage_usec, 1)) + goto cleanup; + + ret =3D KSFT_PASS; + +cleanup: + cg_destroy(cpucg); + free(cpucg); + + return ret; +} + #define T(x) { x, #x } struct cpucg_test { int (*fn)(const char *root); const char *name; } tests[] =3D { T(test_cpucg_subtree_control), + T(test_cpucg_stats), }; #undef T =20 --=20 2.30.2 From nobody Sun May 10 21:17:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CEF56C433EF for ; Fri, 22 Apr 2022 17:36:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233588AbiDVRjU (ORCPT ); Fri, 22 Apr 2022 13:39:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229753AbiDVRjT (ORCPT ); Fri, 22 Apr 2022 13:39:19 -0400 Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4058EBB918; Fri, 22 Apr 2022 10:36:16 -0700 (PDT) Received: by mail-qk1-f175.google.com with SMTP id s4so6347874qkh.0; Fri, 22 Apr 2022 10:36:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jd8Of3UQ/tKXQyCAU+TY/4BZIiO1B8v+E8NTO3SRV0g=; b=67bewfq4JSiGMNkH3BxEaaEXNeRZuT29zbFLi9nBMlL2JaBNpVuy9Jbvda99CJAftg ImEs19az89q9OJ3Dt1/kDy7/8Zp/Ehz/ntv+WDZAh+aLqAK3XIj92MaEhKdK8Zhl+R01 XKhgvsYqWL16Iz+5+n/at02WDnMAxurhKUeHpwD64c2an6zYcW6a+1UXQ7nhnqNhSg1A tTWA7Sa2u0jX89UkC4izO5QsZLlOsNjFeyn78k1mSj34JK+W8lr0v528rwy6WXrXr2/4 5xro1gMm8tdO1ud3H3VIUIYF/+R/Ei2DRAbmAtnnxXxpToUzr9+YlFv6SKre85unQbNW cdng== X-Gm-Message-State: AOAM532NU0K5E64U3q2TDPbijDRtiq4+4/HC/VeNqqclQ3YTfRk+q+fU l5t29EcgqtvssKwKi1+plX06mDa1hOgGSQ== X-Google-Smtp-Source: ABdhPJyTT4O6pc8bwgHjfFEarWkws8V3+iMpizSW91UzeSI3fpGVsB8It4W5bxvliPVaK4ljqNmCDw== X-Received: by 2002:a05:620a:d4e:b0:69b:f2e5:b8cd with SMTP id o14-20020a05620a0d4e00b0069bf2e5b8cdmr3390200qkl.535.1650648870591; Fri, 22 Apr 2022 10:34:30 -0700 (PDT) Received: from localhost (fwdproxy-ash-009.fbsv.net. [2a03:2880:20ff:9::face:b00c]) by smtp.gmail.com with ESMTPSA id h5-20020a05622a170500b002f33ec7a932sm1632660qtk.79.2022.04.22.10.34.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 10:34:30 -0700 (PDT) From: David Vernet To: tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org Cc: cgroups@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 3/4] cgroup: Add test_cpucg_weight_overprovisioned() testcase Date: Fri, 22 Apr 2022 10:33:52 -0700 Message-Id: <20220422173349.3394844-4-void@manifault.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220422173349.3394844-1-void@manifault.com> References: <20220422173349.3394844-1-void@manifault.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" test_cpu.c includes testcases that validate the cgroup cpu controller. This patch adds a new testcase called test_cpucg_weight_overprovisioned() that verifies the expected behavior of creating multiple processes with different cpu.weight, on a system that is overprovisioned. So as to avoid code duplication, this patch also updates cpu_hog_func_param to take a new hog_clock_type enum which informs how time is counted in hog_cpus_timed() (either process time or wall clock time). Signed-off-by: David Vernet --- tools/testing/selftests/cgroup/cgroup_util.c | 12 ++ tools/testing/selftests/cgroup/cgroup_util.h | 1 + tools/testing/selftests/cgroup/test_cpu.c | 135 ++++++++++++++++++- 3 files changed, 145 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/s= elftests/cgroup/cgroup_util.c index 0cf7e90c0052..b690fdc8b4cd 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.c +++ b/tools/testing/selftests/cgroup/cgroup_util.c @@ -190,6 +190,18 @@ int cg_write(const char *cgroup, const char *control, = char *buf) return -1; } =20 +int cg_write_numeric(const char *cgroup, const char *control, long value) +{ + char buf[64]; + int ret; + + ret =3D sprintf(buf, "%lu", value); + if (ret < 0) + return ret; + + return cg_write(cgroup, control, buf); +} + int cg_find_unified_root(char *root, size_t len) { char buf[10 * PAGE_SIZE]; diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/s= elftests/cgroup/cgroup_util.h index 1df13dc8b8aa..0f79156697cf 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.h +++ b/tools/testing/selftests/cgroup/cgroup_util.h @@ -35,6 +35,7 @@ extern long cg_read_long(const char *cgroup, const char *= control); long cg_read_key_long(const char *cgroup, const char *control, const char = *key); extern long cg_read_lc(const char *cgroup, const char *control); extern int cg_write(const char *cgroup, const char *control, char *buf); +int cg_write_numeric(const char *cgroup, const char *control, long value); extern int cg_run(const char *cgroup, int (*fn)(const char *cgroup, void *arg), void *arg); diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/self= tests/cgroup/test_cpu.c index 3bd61964a262..8d901c06c79d 100644 --- a/tools/testing/selftests/cgroup/test_cpu.c +++ b/tools/testing/selftests/cgroup/test_cpu.c @@ -2,6 +2,8 @@ =20 #define _GNU_SOURCE #include +#include +#include #include #include #include @@ -10,9 +12,17 @@ #include "../kselftest.h" #include "cgroup_util.h" =20 +enum hog_clock_type { + // Count elapsed time using the CLOCK_PROCESS_CPUTIME_ID clock. + CPU_HOG_CLOCK_PROCESS, + // Count elapsed time using system wallclock time. + CPU_HOG_CLOCK_WALL, +}; + struct cpu_hog_func_param { int nprocs; struct timespec ts; + enum hog_clock_type clock_type; }; =20 /* @@ -118,8 +128,13 @@ static int hog_cpus_timed(const char *cgroup, void *ar= g) (struct cpu_hog_func_param *)arg; struct timespec ts_run =3D param->ts; struct timespec ts_remaining =3D ts_run; + struct timespec ts_start; int i, ret; =20 + ret =3D clock_gettime(CLOCK_MONOTONIC, &ts_start); + if (ret !=3D 0) + return ret; + for (i =3D 0; i < param->nprocs; i++) { pthread_t tid; =20 @@ -135,9 +150,19 @@ static int hog_cpus_timed(const char *cgroup, void *ar= g) if (ret && errno !=3D EINTR) return ret; =20 - ret =3D clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_total); - if (ret !=3D 0) - return ret; + if (param->clock_type =3D=3D CPU_HOG_CLOCK_PROCESS) { + ret =3D clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_total); + if (ret !=3D 0) + return ret; + } else { + struct timespec ts_current; + + ret =3D clock_gettime(CLOCK_MONOTONIC, &ts_current); + if (ret !=3D 0) + return ret; + + ts_total =3D timespec_sub(&ts_current, &ts_start); + } =20 ts_remaining =3D timespec_sub(&ts_run, &ts_total); } @@ -176,6 +201,7 @@ static int test_cpucg_stats(const char *root) .tv_sec =3D usage_seconds, .tv_nsec =3D 0, }, + .clock_type =3D CPU_HOG_CLOCK_PROCESS, }; if (cg_run(cpucg, hog_cpus_timed, (void *)¶m)) goto cleanup; @@ -197,6 +223,108 @@ static int test_cpucg_stats(const char *root) return ret; } =20 +/* + * First, this test creates the following hierarchy: + * A + * A/B cpu.weight =3D 50 + * A/C cpu.weight =3D 100 + * A/D cpu.weight =3D 150 + * + * A separate process is then created for each child cgroup which spawns as + * many threads as there are cores, and hogs each CPU as much as possible + * for some time interval. + * + * Once all of the children have exited, we verify that each child cgroup + * was given proportional runtime as informed by their cpu.weight. + */ +static int test_cpucg_weight_overprovisioned(const char *root) +{ + struct child { + char *cgroup; + pid_t pid; + long usage; + }; + int ret =3D KSFT_FAIL, i; + char *parent =3D NULL; + struct child children[3] =3D {NULL}; + long usage_seconds =3D 10; + + parent =3D cg_name(root, "cpucg_test_0"); + if (!parent) + goto cleanup; + + if (cg_create(parent)) + goto cleanup; + + if (cg_write(parent, "cgroup.subtree_control", "+cpu")) + goto cleanup; + + for (i =3D 0; i < ARRAY_SIZE(children); i++) { + children[i].cgroup =3D cg_name_indexed(parent, "cpucg_child", i); + if (!children[i].cgroup) + goto cleanup; + + if (cg_create(children[i].cgroup)) + goto cleanup; + + if (cg_write_numeric(children[i].cgroup, "cpu.weight", + 50 * (i + 1))) + goto cleanup; + } + + for (i =3D 0; i < ARRAY_SIZE(children); i++) { + struct cpu_hog_func_param param =3D { + .nprocs =3D get_nprocs(), + .ts =3D { + .tv_sec =3D usage_seconds, + .tv_nsec =3D 0, + }, + .clock_type =3D CPU_HOG_CLOCK_WALL, + }; + pid_t pid =3D cg_run_nowait(children[i].cgroup, hog_cpus_timed, + (void *)¶m); + if (pid <=3D 0) + goto cleanup; + children[i].pid =3D pid; + } + + for (i =3D 0; i < ARRAY_SIZE(children); i++) { + int retcode; + + waitpid(children[i].pid, &retcode, 0); + if (!WIFEXITED(retcode)) + goto cleanup; + if (WEXITSTATUS(retcode)) + goto cleanup; + } + + for (i =3D 0; i < ARRAY_SIZE(children); i++) + children[i].usage =3D cg_read_key_long(children[i].cgroup, + "cpu.stat", "usage_usec"); + + for (i =3D 0; i < ARRAY_SIZE(children) - 1; i++) { + long delta; + + if (children[i + 1].usage <=3D children[i].usage) + goto cleanup; + + delta =3D children[i + 1].usage - children[i].usage; + if (!values_close(delta, children[0].usage, 35)) + goto cleanup; + } + + ret =3D KSFT_PASS; +cleanup: + for (i =3D 0; i < ARRAY_SIZE(children); i++) { + cg_destroy(children[i].cgroup); + free(children[i].cgroup); + } + cg_destroy(parent); + free(parent); + + return ret; +} + #define T(x) { x, #x } struct cpucg_test { int (*fn)(const char *root); @@ -204,6 +332,7 @@ struct cpucg_test { } tests[] =3D { T(test_cpucg_subtree_control), T(test_cpucg_stats), + T(test_cpucg_weight_overprovisioned), }; #undef T =20 --=20 2.30.2 From nobody Sun May 10 21:17:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BDEDC433F5 for ; Fri, 22 Apr 2022 17:36:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234009AbiDVRjb (ORCPT ); Fri, 22 Apr 2022 13:39:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233918AbiDVRjV (ORCPT ); Fri, 22 Apr 2022 13:39:21 -0400 Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B92CD64FA; Fri, 22 Apr 2022 10:36:17 -0700 (PDT) Received: by mail-qt1-f175.google.com with SMTP id x24so5993037qtq.11; Fri, 22 Apr 2022 10:36:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mXE/v8JHTyVxf9dgekZsoHje3QpqCFbIJjMNc4DKNeY=; b=koBlbJoht58beorrsGvuuphDI0lniCciWuEnL+dMo+Zu6QF1Q2qbIDXYkqdJup/Btg atVIzFvRvNdYhC+Je6UATIMNm8B5gD0dQ4IZf4A3NlNCFGtBbmXrOiNL8MmAUTQLtske 1sxsKX806nghIVHFloKpjKeC+x5TD/627jSVn4Z/tuonR7SCsX5rS+F5ScVGo9YXLIiD UeEh/te3DaBqtCy66l4a/Ln48h42A3I0Yd8Uvlpgq5W8GRJYP9Wx7PPS65zpaXrXvdyZ FXHmWCrj+DyYFFMw83aXRv2NFmUvinoz95sPKjuhWmoXpSe63lkgunyYIqmPvnnLdBMD WY0g== X-Gm-Message-State: AOAM5329bzGLVzvsCD5q8nA0IqOjAI8LmP1kWKjsakpqckTSHCn9rtvT SejdJo5ly1Exk+N4e/xvvJM= X-Google-Smtp-Source: ABdhPJzCi1TK9u470losckNRhCcKUe7haryJuWzbkADBmQtwP1jOaK9pihePFSoS/nmvY+ThNG1g6g== X-Received: by 2002:ac8:6998:0:b0:2f1:ea20:96ae with SMTP id o24-20020ac86998000000b002f1ea2096aemr4090123qtq.89.1650648872334; Fri, 22 Apr 2022 10:34:32 -0700 (PDT) Received: from localhost (fwdproxy-ash-119.fbsv.net. [2a03:2880:20ff:77::face:b00c]) by smtp.gmail.com with ESMTPSA id c4-20020a37b304000000b0069ec13803adsm1173538qkf.7.2022.04.22.10.34.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 10:34:32 -0700 (PDT) From: David Vernet To: tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org Cc: cgroups@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 4/4] cgroup: Add test_cpucg_weight_underprovisioned() testcase Date: Fri, 22 Apr 2022 10:33:53 -0700 Message-Id: <20220422173349.3394844-5-void@manifault.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220422173349.3394844-1-void@manifault.com> References: <20220422173349.3394844-1-void@manifault.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" test_cpu.c includes testcases that validate the cgroup cpu controller. This patch adds a new testcase called test_cpucg_weight_underprovisioned() that verifies that processes with different cpu.weight that are all running on an underprovisioned system, still get roughly the same amount of cpu time. Because test_cpucg_weight_underprovisioned() is very similar to test_cpucg_weight_overprovisioned(), this patch also pulls the common logic into a separate helper function that is invoked from both testcases, and which uses function pointers to invoke the unique portions of the testcases. Signed-off-by: David Vernet --- tools/testing/selftests/cgroup/test_cpu.c | 155 ++++++++++++++++------ 1 file changed, 117 insertions(+), 38 deletions(-) diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/self= tests/cgroup/test_cpu.c index 8d901c06c79d..64f9ce91c992 100644 --- a/tools/testing/selftests/cgroup/test_cpu.c +++ b/tools/testing/selftests/cgroup/test_cpu.c @@ -19,6 +19,12 @@ enum hog_clock_type { CPU_HOG_CLOCK_WALL, }; =20 +struct cpu_hogger { + char *cgroup; + pid_t pid; + long usage; +}; + struct cpu_hog_func_param { int nprocs; struct timespec ts; @@ -223,31 +229,15 @@ static int test_cpucg_stats(const char *root) return ret; } =20 -/* - * First, this test creates the following hierarchy: - * A - * A/B cpu.weight =3D 50 - * A/C cpu.weight =3D 100 - * A/D cpu.weight =3D 150 - * - * A separate process is then created for each child cgroup which spawns as - * many threads as there are cores, and hogs each CPU as much as possible - * for some time interval. - * - * Once all of the children have exited, we verify that each child cgroup - * was given proportional runtime as informed by their cpu.weight. - */ -static int test_cpucg_weight_overprovisioned(const char *root) +static int +run_cpucg_weight_test( + const char *root, + pid_t (*spawn_child)(const struct cpu_hogger *child), + int (*validate)(const struct cpu_hogger *children, int num_children)) { - struct child { - char *cgroup; - pid_t pid; - long usage; - }; int ret =3D KSFT_FAIL, i; char *parent =3D NULL; - struct child children[3] =3D {NULL}; - long usage_seconds =3D 10; + struct cpu_hogger children[3] =3D {NULL}; =20 parent =3D cg_name(root, "cpucg_test_0"); if (!parent) @@ -273,16 +263,7 @@ static int test_cpucg_weight_overprovisioned(const cha= r *root) } =20 for (i =3D 0; i < ARRAY_SIZE(children); i++) { - struct cpu_hog_func_param param =3D { - .nprocs =3D get_nprocs(), - .ts =3D { - .tv_sec =3D usage_seconds, - .tv_nsec =3D 0, - }, - .clock_type =3D CPU_HOG_CLOCK_WALL, - }; - pid_t pid =3D cg_run_nowait(children[i].cgroup, hog_cpus_timed, - (void *)¶m); + pid_t pid =3D spawn_child(&children[i]); if (pid <=3D 0) goto cleanup; children[i].pid =3D pid; @@ -302,7 +283,46 @@ static int test_cpucg_weight_overprovisioned(const cha= r *root) children[i].usage =3D cg_read_key_long(children[i].cgroup, "cpu.stat", "usage_usec"); =20 - for (i =3D 0; i < ARRAY_SIZE(children) - 1; i++) { + if (validate(children, ARRAY_SIZE(children))) + goto cleanup; + + ret =3D KSFT_PASS; +cleanup: + for (i =3D 0; i < ARRAY_SIZE(children); i++) { + cg_destroy(children[i].cgroup); + free(children[i].cgroup); + } + cg_destroy(parent); + free(parent); + + return ret; +} + +static pid_t weight_hog_ncpus(const struct cpu_hogger *child, int ncpus) +{ + long usage_seconds =3D 10; + struct cpu_hog_func_param param =3D { + .nprocs =3D ncpus, + .ts =3D { + .tv_sec =3D usage_seconds, + .tv_nsec =3D 0, + }, + .clock_type =3D CPU_HOG_CLOCK_WALL, + }; + return cg_run_nowait(child->cgroup, hog_cpus_timed, (void *)¶m); +} + +static pid_t weight_hog_all_cpus(const struct cpu_hogger *child) +{ + return weight_hog_ncpus(child, get_nprocs()); +} + +static int +overprovision_validate(const struct cpu_hogger *children, int num_children) +{ + int ret =3D KSFT_FAIL, i; + + for (i =3D 0; i < num_children - 1; i++) { long delta; =20 if (children[i + 1].usage <=3D children[i].usage) @@ -315,16 +335,74 @@ static int test_cpucg_weight_overprovisioned(const ch= ar *root) =20 ret =3D KSFT_PASS; cleanup: - for (i =3D 0; i < ARRAY_SIZE(children); i++) { - cg_destroy(children[i].cgroup); - free(children[i].cgroup); + return ret; +} + +/* + * First, this test creates the following hierarchy: + * A + * A/B cpu.weight =3D 50 + * A/C cpu.weight =3D 100 + * A/D cpu.weight =3D 150 + * + * A separate process is then created for each child cgroup which spawns as + * many threads as there are cores, and hogs each CPU as much as possible + * for some time interval. + * + * Once all of the children have exited, we verify that each child cgroup + * was given proportional runtime as informed by their cpu.weight. + */ +static int test_cpucg_weight_overprovisioned(const char *root) +{ + return run_cpucg_weight_test(root, weight_hog_all_cpus, + overprovision_validate); +} + +static pid_t weight_hog_one_cpu(const struct cpu_hogger *child) +{ + return weight_hog_ncpus(child, 1); +} + +static int +underprovision_validate(const struct cpu_hogger *children, int num_childre= n) +{ + int ret =3D KSFT_FAIL, i; + + for (i =3D 0; i < num_children - 1; i++) { + if (!values_close(children[i + 1].usage, children[0].usage, 15)) + goto cleanup; } - cg_destroy(parent); - free(parent); =20 + ret =3D KSFT_PASS; +cleanup: return ret; } =20 +/* + * First, this test creates the following hierarchy: + * A + * A/B cpu.weight =3D 50 + * A/C cpu.weight =3D 100 + * A/D cpu.weight =3D 150 + * + * A separate process is then created for each child cgroup which spawns a + * single thread that hogs a CPU. The testcase is only run on systems that + * have at least one core per-thread in the child processes. + * + * Once all of the children have exited, we verify that each child cgroup + * had roughly the same runtime despite having different cpu.weight. + */ +static int test_cpucg_weight_underprovisioned(const char *root) +{ + // Only run the test if there are enough cores to avoid overprovisioning + // the system. + if (get_nprocs() < 4) + return KSFT_SKIP; + + return run_cpucg_weight_test(root, weight_hog_one_cpu, + underprovision_validate); +} + #define T(x) { x, #x } struct cpucg_test { int (*fn)(const char *root); @@ -333,6 +411,7 @@ struct cpucg_test { T(test_cpucg_subtree_control), T(test_cpucg_stats), T(test_cpucg_weight_overprovisioned), + T(test_cpucg_weight_underprovisioned), }; #undef T =20 --=20 2.30.2