From nobody Sun Feb 8 12:36:52 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 41692EB64DD for ; Tue, 25 Jul 2023 21:25:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231396AbjGYVZq (ORCPT ); Tue, 25 Jul 2023 17:25:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230348AbjGYVZe (ORCPT ); Tue, 25 Jul 2023 17:25:34 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 909551FEF for ; Tue, 25 Jul 2023 14:25:32 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5840614b107so26441487b3.1 for ; Tue, 25 Jul 2023 14:25:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690320332; x=1690925132; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5NNnpa8KMX+/Q6wTyExQfdfF9Al9Wy1b2MTdnf60RVc=; b=I/YWv/3XBvyBpd3KXhvF3Uws0tP4BWDjfQfvY/0iGjKRW641va+kHaQLAJHsu296jy QccDVNf25JKmwt3P1IKYKCZSMNOOh63USSSg0aLkv6TxC3BD0Ygc1LyJMX6RRikuDBPV qN2Nd6W84rVYs5I4DKTglp0iodRy8COZN946ja+2MkXwwPfNtNVh2qXjv6jrdamPC8eu 22HDfvkqD/fmZRh+4tNLKoT8APopThjlxoRu3/5mWFLE2pVyQJZbJmecUll/0/NZ+1jV iSmgyfeIuNtnIo6kaIszEnTyYDAc45Db81quVn2IL0OrxUVW4hG68LsAbrlnI3KvFRCN //uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690320332; x=1690925132; 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=5NNnpa8KMX+/Q6wTyExQfdfF9Al9Wy1b2MTdnf60RVc=; b=jXs+NSsJgl/DQkfyVzWZ7lSauSVX77qRU+T2NFD0JCCkunVWm7BRNEyDg3UM0ENre3 PGjDFf7jCi5UEAcNd4RQmH7JoW4IwFJ63zhz/x0qziJKY/k6t++YD7sqj4Wqs0OKqyZu ip1TJs7fhtvaIl6jPlZpIJaopeU5EFQ9edVJE8PmJQKc5RTKHWvmjBTudKcH5r5UySvq Vi64GBkLne35eCE4y+BkHHkm7rnsSa+LYR+TTYm3lLuH1jFaiHsb4IXPld4krcRSdZPX 7icTKcivrQGJuKlGyC5fdE3eW5my/tw5EaOYjTj1W6xy93gZGLlqKeJFBuneRgKNBCaB yNDw== X-Gm-Message-State: ABy/qLZyM3esSb5+0Lx3lwwDiVN1mFG36d5x3ec63IouEZbLVm4ggofi zEv5A3Je3TT90EIuQMBkJ7X5qPTjIg== X-Google-Smtp-Source: APBJJlF41OM3nMuPki+hkQSHF9/I+ac31Shey9hN6bOwS46KEuuc4tqAAipQvXS0LdJStwV2uqfEDBYcmA== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a05:6902:1803:b0:d0b:c67:de3b with SMTP id cf3-20020a056902180300b00d0b0c67de3bmr1372ybb.13.1690320331881; Tue, 25 Jul 2023 14:25:31 -0700 (PDT) Date: Tue, 25 Jul 2023 21:25:13 +0000 In-Reply-To: <20230725212522.1622716-1-rmoar@google.com> Mime-Version: 1.0 References: <20230725212522.1622716-1-rmoar@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230725212522.1622716-3-rmoar@google.com> Subject: [PATCH v3 2/9] kunit: Add speed attribute From: Rae Moar To: shuah@kernel.org, davidgow@google.com, dlatypov@google.com, brendan.higgins@linux.dev Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, keescook@chromium.org, linux-hardening@vger.kernel.org, jstultz@google.com, tglx@linutronix.de, sboyd@kernel.org, Rae Moar Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add speed attribute to the test attribute API. This attribute will allow users to mark tests with a category of speed. Currently the categories of speed proposed are: normal, slow, and very_slow (outlined in enum kunit_speed). These are outlined in the enum kunit_speed. The assumed default speed for tests is "normal". This indicates that the test takes a relatively trivial amount of time (less than 1 second), regardless of the machine it is running on. Any test slower than this could be marked as "slow" or "very_slow". Add the macro KUNIT_CASE_SLOW to set a test as slow, as this is likely a common use of the attributes API. Add an example of marking a slow test to kunit-example-test.c. Reviewed-by: David Gow Signed-off-by: Rae Moar --- Changes since v2: - No changes. Changes since v1: - Change kunit_attr_list definition to fix compile error Changes since RFC v2: - Fixup comment. Changes since RFC v1: - Remove the "fast" category of speed. include/kunit/test.h | 32 ++++++++++++++++++++++++- lib/kunit/attributes.c | 44 +++++++++++++++++++++++++++++++++- lib/kunit/kunit-example-test.c | 9 +++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 1fc9155988e9..ed5f5000a095 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -63,8 +63,27 @@ enum kunit_status { KUNIT_SKIPPED, }; =20 +/* Attribute struct/enum definitions */ + +/* + * Speed Attribute is stored as an enum and separated into categories of + * speed: very_slowm, slow, and normal. These speeds are relative to + * other KUnit tests. + * + * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL. + */ +enum kunit_speed { + KUNIT_SPEED_UNSET, + KUNIT_SPEED_VERY_SLOW, + KUNIT_SPEED_SLOW, + KUNIT_SPEED_NORMAL, + KUNIT_SPEED_MAX =3D KUNIT_SPEED_NORMAL, +}; + /* Holds attributes for each test case and suite */ -struct kunit_attributes {}; +struct kunit_attributes { + enum kunit_speed speed; +}; =20 /** * struct kunit_case - represents an individual test case. @@ -150,6 +169,17 @@ static inline char *kunit_status_to_ok_not_ok(enum kun= it_status status) { .run_case =3D test_name, .name =3D #test_name, \ .attr =3D attributes } =20 +/** + * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case + * with the slow attribute + * + * @test_name: a reference to a test case function. + */ + +#define KUNIT_CASE_SLOW(test_name) \ + { .run_case =3D test_name, .name =3D #test_name, \ + .attr.speed =3D KUNIT_SPEED_SLOW } + /** * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_= case * diff --git a/lib/kunit/attributes.c b/lib/kunit/attributes.c index 9bda5a5f4030..ffd0d692b334 100644 --- a/lib/kunit/attributes.c +++ b/lib/kunit/attributes.c @@ -40,9 +40,51 @@ struct kunit_attr { enum print_ops print; }; =20 +/* String Lists for enum Attributes */ + +static const char * const speed_str_list[] =3D {"unset", "very_slow", "slo= w", "normal"}; + +/* To String Methods */ + +static const char *attr_enum_to_string(void *attr, const char * const str_= list[], bool *to_free) +{ + long val =3D (long)attr; + + *to_free =3D false; + if (!val) + return NULL; + return str_list[val]; +} + +static const char *attr_speed_to_string(void *attr, bool *to_free) +{ + return attr_enum_to_string(attr, speed_str_list, to_free); +} + +/* Get Attribute Methods */ + +static void *attr_speed_get(void *test_or_suite, bool is_test) +{ + struct kunit_suite *suite =3D is_test ? NULL : test_or_suite; + struct kunit_case *test =3D is_test ? test_or_suite : NULL; + + if (test) + return ((void *) test->attr.speed); + else + return ((void *) suite->attr.speed); +} + /* List of all Test Attributes */ =20 -static struct kunit_attr kunit_attr_list[] =3D {}; +static struct kunit_attr kunit_attr_list[] =3D { + { + .name =3D "speed", + .get_attr =3D attr_speed_get, + .to_string =3D attr_speed_to_string, + .attr_default =3D (void *)KUNIT_SPEED_NORMAL, + .print =3D PRINT_ALWAYS, + }, +}; =20 /* Helper Functions to Access Attributes */ =20 diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index b69b689ea850..01a769f35e1d 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -220,6 +220,14 @@ static void example_params_test(struct kunit *test) KUNIT_EXPECT_EQ(test, param->value % param->value, 0); } =20 +/* + * This test should always pass. Can be used to practice filtering attribu= tes. + */ +static void example_slow_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, 1 + 1, 2); +} + /* * Here we make a list of all the test cases we want to add to the test su= ite * below. @@ -237,6 +245,7 @@ static struct kunit_case example_test_cases[] =3D { KUNIT_CASE(example_all_expect_macros_test), KUNIT_CASE(example_static_stub_test), KUNIT_CASE_PARAM(example_params_test, example_gen_params), + KUNIT_CASE_SLOW(example_slow_test), {} }; =20 --=20 2.41.0.487.g6d72f3e995-goog