From patchwork Thu Oct 23 09:42:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 635 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1761212631; bh=ezoBY803esBQQDcDhccA+1dIlCgWQAvAMnl2lK8WvyU=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=XGyfW/Hz9JwSGjHs0/FWC9n/uO7L3VIuXxDseSm/9NoCitaMcP9J1ZxW+TFTel2xC X96xChBEcjuVDgnlohPRL9Gm1YZA4qOkOI94JrPZ7hk/TjGhMq8oM/CdBaG2QXIpKc ZkRQnrrYcx0GFSag6MXWHLolYZZW7+UZAEKdZAHCafWDSSf4jD5DcMcq4Raj3PcOTb Z1Z2pfQnMbAazoLACKHGrLCVh7ni8XaoxvgT+nhQbJcRyynk3MfgOUqnZ5ULqlPgy7 38AKRYDv7GVd6XudXEeefazJ/udIfetsDoipYiEi9PvG4L4fiXygBuRh7CI7JjwHvW 6qFAq7tb+WT+w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2D4746819C for ; Thu, 23 Oct 2025 03:43:51 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id 4L99IS-3oJMw for ; Thu, 23 Oct 2025 03:43:51 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1761212629; bh=ezoBY803esBQQDcDhccA+1dIlCgWQAvAMnl2lK8WvyU=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=O0DUzjF8ohtYwzUqB1roo+tT6iWoQRwFxN4rg/vtp5SLAG20cf/SgejwhZNmI+9Xe dVCjooeY5V5V01kxvYb/k460FTc56rOaxllitsrBtZrDPjkRPa99hE2dXpHbz+lTWI WV6Ort/Lj8lvc1dH1r/hMpgjlTvxbcvNXFKTSq18bqCshoz/8BrfmGkz/S/+AQOInt AN3sdplk8qUGyshb1Ku1YSn/hua85figamP1m0IbfXTXL0nx7n6TYPHOuDwwYB/jNW uxZqd2pR3JbZxF/hGDGr+cSj0EveLW5Vm+XBOwgD79FAMbZjJCxlAoQJmKlkQAV8xl Tp7Q/QaBPNfJQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id DB55D68228 for ; Thu, 23 Oct 2025 03:43:49 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1761212626; bh=intczG/AjPUPjVb6IAZHcXYFcFnlAJk2At9zR6BAR+E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BiikgPWK3xQUJ/A/y1QiMdpxFspbfhui5NygOeOHqGRBxH27y7AZv1cF64BT78LMd +Lj1bLIPoS0n1w1sVynpE+J6ygNjSybRfKubWX0MRPQm1Bze0nOzCUXlVzA0MbsnWc dcdzKQ3b0YDOwY9Olk0aI3z9EXj8IcDCsTYJWZjh0Kp4VQc3o2tkvOmYMvg4siExNp OGGp1Z+gOhj/RqaiO6DAdojKQf3JlqtEqWD/aKGEoFJtctVqX5vlo9QIOhpjZWHsnV 5ambNWCj7mjhrffHkdcUzTiz1I10b61j0cVO0hEdxZDl8lPZ/mGMYmtOmw3PYXlNXJ C1ABrFLZfh3Ig== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id A82D868228; Thu, 23 Oct 2025 03:43:46 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id pmYvyq-Zpoy3; Thu, 23 Oct 2025 03:43:46 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1761212618; bh=cy4UFeXdCj4T/UKByURgIXnBffIkFolrGL34FNmgWZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nEYOTpgwceKhV89yOv5gu9f2OfcWitUoHSGzBr4Iuj5znXSt5Zy+0rElBuAxayGwa nmZ+6SLzLenQ+E9vddLOFY7R3p08VI00Gqq3bDAfeNFeYzP8rL13BHWJZnv785O+yl lbnbc3FgOfQjEIEhpGxmy9HgFlNWCCzXRn1T9o5JC1U3WNEUF//yavkL+Nz9crviaJ 0yaTBPYm/2pjMmWlbtFIdnvtgpMbRdch9cua9DFc4TF4iote0a2n7tuMqKO8CWFvRO t3ddJGdNLZA1CJDxQldAedUDilObwORDjKctWx8dx6vcDnZWQMSmcuJuZtYcxmNAEg Y67jTczmrQGmQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 9CB536819C; Thu, 23 Oct 2025 03:43:38 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Thu, 23 Oct 2025 03:42:52 -0600 Message-ID: <20251023094308.3406453-4-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251023094308.3406453-1-sjg@u-boot.org> References: <20251023094308.3406453-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: ICBZOYP6TLOR7QLQVDJBCSS4H3H54STP X-Message-ID-Hash: ICBZOYP6TLOR7QLQVDJBCSS4H3H54STP X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 03/11] bootstage: Add some more tests List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass There is already a Python test. Add a few C tests as well, for bootstage itself and for the 'bootstage' command. Add helpers to access the internal state. Be careful to zero records when removing them, since if the record is later reused, bootstage expects the time to be zero. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/bootstage.c | 37 ++++++++ include/bootstage.h | 24 +++++ test/cmd/Makefile | 1 + test/cmd/bootstage.c | 32 +++++++ test/common/Makefile | 2 + test/common/bootstage.c | 199 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 295 insertions(+) create mode 100644 test/cmd/bootstage.c create mode 100644 test/common/bootstage.c diff --git a/common/bootstage.c b/common/bootstage.c index 1fee2edeef3..e0298991fa9 100644 --- a/common/bootstage.c +++ b/common/bootstage.c @@ -212,6 +212,43 @@ uint32_t bootstage_accum(enum bootstage_id id) return duration; } +uint bootstage_get_rec_count(void) +{ + struct bootstage_data *data = gd->bootstage; + + if (!data) + return 0; + + return data->rec_count; +} + +const struct bootstage_record *bootstage_get_rec(uint index) +{ + struct bootstage_data *data = gd->bootstage; + + if (!data || index >= data->rec_count) + return NULL; + + return &data->record[index]; +} + +void bootstage_set_rec_count(uint count) +{ + struct bootstage_data *data = gd->bootstage; + uint i; + + if (!data || count > RECORD_COUNT) + return; + + /* Clear any records beyond the new count */ + for (i = count; i < data->rec_count; i++) { + data->record[i].time_us = 0; + data->record[i].start_us = 0; + } + + data->rec_count = count; +} + /** * Get a record name as a printable string * diff --git a/include/bootstage.h b/include/bootstage.h index 98bd9024da8..adc7b8f0a35 100644 --- a/include/bootstage.h +++ b/include/bootstage.h @@ -354,6 +354,30 @@ uint32_t bootstage_start(enum bootstage_id id, const char *name); */ uint32_t bootstage_accum(enum bootstage_id id); +/** + * bootstage_get_rec_count() - Get the number of bootstage records + * + * Return: number of bootstage records + */ +uint bootstage_get_rec_count(void); + +/** + * bootstage_get_rec() - Get a bootstage record by index + * + * @index: Index of the record to retrieve (numbered from 0) + * Return: pointer to the record, or NULL if @index is out of range + */ +const struct bootstage_record *bootstage_get_rec(uint index); + +/** + * bootstage_set_rec_count() - Set the number of bootstage records + * + * This can be used to restore the record count after testing + * + * @count: New record count (must be <= RECORD_COUNT) + */ +void bootstage_set_rec_count(uint count); + /* Print a report about boot time */ void bootstage_report(void); diff --git a/test/cmd/Makefile b/test/cmd/Makefile index 3fc07f0cacf..9cd8ea3aaf0 100644 --- a/test/cmd/Makefile +++ b/test/cmd/Makefile @@ -15,6 +15,7 @@ obj-$(CONFIG_X86) += cpuid.o msr.o obj-$(CONFIG_CMD_ADDR_FIND) += addr_find.o obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o obj-$(CONFIG_CMD_BDI) += bdinfo.o +obj-$(CONFIG_CMD_BOOTSTAGE) += bootstage.o obj-$(CONFIG_CMD_CHID) += chid.o obj-$(CONFIG_COREBOOT_SYSINFO) += coreboot.o obj-$(CONFIG_CMD_FDT) += fdt.o diff --git a/test/cmd/bootstage.c b/test/cmd/bootstage.c new file mode 100644 index 00000000000..dee4a0671fa --- /dev/null +++ b/test/cmd/bootstage.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Tests for bootstage command + * + * Copyright 2025 Canonical Ltd + */ + +#include +#include +#include + +static int cmd_bootstage_report(struct unit_test_state *uts) +{ + uint count; + + /* Get the current record count */ + count = bootstage_get_rec_count(); + ut_assert(count > 0); + + /* Test the bootstage report command runs successfully */ + ut_assertok(run_command("bootstage report", 0)); + + /* Verify the report contains expected headers and stages */ + ut_assert_nextline("Timer summary in microseconds (%u records):", + count); + ut_assert_nextline(" Mark Elapsed Stage"); + ut_assert_nextline(" 0 0 reset"); + ut_assert_skip_to_line("Accumulated time:"); + + return 0; +} +CMD_TEST(cmd_bootstage_report, UTF_CONSOLE); diff --git a/test/common/Makefile b/test/common/Makefile index 7b5927b5a44..a5df946396a 100644 --- a/test/common/Makefile +++ b/test/common/Makefile @@ -7,6 +7,8 @@ obj-$(CONFIG_$(PHASE_)CMDLINE) += bloblist.o endif endif +obj-$(CONFIG_BOOTSTAGE) += bootstage.o + obj-$(CONFIG_CONSOLE_PAGER) += console.o obj-$(CONFIG_CYCLIC) += cyclic.o obj-$(CONFIG_EVENT_DYNAMIC) += event.o diff --git a/test/common/bootstage.c b/test/common/bootstage.c new file mode 100644 index 00000000000..6e97c3e3a72 --- /dev/null +++ b/test/common/bootstage.c @@ -0,0 +1,199 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Tests for bootstage API + * + * Copyright 2025 Canonical Ltd + */ + +#include +#include +#include +#include +#include + +/* Test bootstage_mark_name() */ +static int test_bootstage_mark(struct unit_test_state *uts) +{ + const struct bootstage_record *rec; + ulong time; + int count; + + /* Get the current count so we know where our record will be */ + count = bootstage_get_rec_count(); + + /* Mark a stage and verify we get a valid timestamp */ + time = bootstage_mark_name(BOOTSTAGE_ID_USER + 50, "test_stage_mark"); + ut_assert(time > 0); + + /* Verify the count increased by 1 */ + ut_asserteq(count + 1, bootstage_get_rec_count()); + + /* Check that the record was added correctly */ + rec = bootstage_get_rec(count); + ut_assertnonnull(rec); + ut_asserteq(BOOTSTAGE_ID_USER + 50, rec->id); + ut_asserteq_str("test_stage_mark", rec->name); + ut_asserteq(time, rec->time_us); + ut_asserteq(0, rec->flags); + + /* Restore the original count */ + bootstage_set_rec_count(count); + + return 0; +} +COMMON_TEST(test_bootstage_mark, 0); + +/* Test bootstage_error_name() */ +static int test_bootstage_error(struct unit_test_state *uts) +{ + const struct bootstage_record *rec; + ulong time; + int count; + + count = bootstage_get_rec_count(); + + /* Mark an error stage and verify we get a valid timestamp */ + time = bootstage_error_name(BOOTSTAGE_ID_USER + 51, "test_error"); + ut_assert(time > 0); + + /* Check the error record */ + rec = bootstage_get_rec(count); + ut_assertnonnull(rec); + ut_asserteq(BOOTSTAGE_ID_USER + 51, rec->id); + ut_asserteq_str("test_error", rec->name); + ut_asserteq(time, rec->time_us); + ut_asserteq(BOOTSTAGEF_ERROR, rec->flags); + + /* Restore the original count */ + bootstage_set_rec_count(count); + + return 0; +} +COMMON_TEST(test_bootstage_error, 0); + +/* Test bootstage_start() and bootstage_accum() */ +static int test_bootstage_accum(struct unit_test_state *uts) +{ + enum bootstage_id id = BOOTSTAGE_ID_USER + 53; + uint start_time, elapsed1, elapsed2; + const struct bootstage_record *rec; + int index, count; + + count = bootstage_get_rec_count(); + + /* Start an accumulator */ + start_time = bootstage_start(id, "test_accum"); + ut_assert(start_time > 0); + + /* Check the accumulator record was created */ + index = count; + rec = bootstage_get_rec(index); + ut_assertnonnull(rec); + ut_asserteq(id, rec->id); + ut_asserteq_str("test_accum", rec->name); + ut_asserteq(start_time, rec->start_us); + + /* Accumulate the time */ + udelay(1); + elapsed1 = bootstage_accum(id); + ut_assert(elapsed1 >= 0); + + /* Check the accumulated time was recorded */ + ut_asserteq(elapsed1, rec->time_us); + + /* Start and accumulate again */ + bootstage_start(id, "test_accum"); + udelay(1); + elapsed2 = bootstage_accum(id); + ut_assert(elapsed2 >= 0); + + /* Check the total time accumulated */ + rec = bootstage_get_rec(index); + ut_asserteq(rec->time_us, elapsed1 + elapsed2); + + /* Restore the original count */ + bootstage_set_rec_count(count); + + return 0; +} +COMMON_TEST(test_bootstage_accum, 0); + +/* Test bootstage_mark_code() */ +static int test_bootstage_mark_code(struct unit_test_state *uts) +{ + const struct bootstage_record *rec; + ulong time; + int count; + + count = bootstage_get_rec_count(); + + /* Mark with file, function, and line number */ + time = bootstage_mark_code("file.c", __func__, 123); + ut_assert(time > 0); + + /* Check the record */ + rec = bootstage_get_rec(count); + ut_assertnonnull(rec); + ut_asserteq(time, rec->time_us); + ut_asserteq_str("file.c,123: test_bootstage_mark_code", rec->name); + + /* Restore the original count */ + bootstage_set_rec_count(count); + + return 0; +} +COMMON_TEST(test_bootstage_mark_code, 0); + +/* Test bootstage_get_rec_count() */ +static int test_bootstage_get_rec_count(struct unit_test_state *uts) +{ + const struct bootstage_record *rec; + int orig, count; + + /* Get initial count */ + orig = bootstage_get_rec_count(); + ut_assert(orig > 0); + + /* Add a new record */ + bootstage_mark_name(BOOTSTAGE_ID_USER + 52, "test_count"); + + /* Verify count increased */ + count = bootstage_get_rec_count(); + ut_asserteq(orig + 1, count); + + /* Verify the record was added at the correct index */ + rec = bootstage_get_rec(orig); + ut_assertnonnull(rec); + ut_asserteq(BOOTSTAGE_ID_USER + 52, rec->id); + ut_asserteq_str("test_count", rec->name); + + /* Restore the original count */ + bootstage_set_rec_count(orig); + + return 0; +} +COMMON_TEST(test_bootstage_get_rec_count, 0); + +/* Test bootstage_get_rec() */ +static int test_bootstage_get_rec(struct unit_test_state *uts) +{ + const struct bootstage_record *rec; + int count; + + /* Get total count */ + count = bootstage_get_rec_count(); + ut_assert(count > 0); + + /* Get first record (should be "reset") */ + rec = bootstage_get_rec(0); + ut_assertnonnull(rec); + ut_asserteq_str("reset", rec->name); + + /* Test out of bounds access */ + ut_assertnull(bootstage_get_rec(count)); + ut_assertnull(bootstage_get_rec(count + 100)); + ut_assertnull(bootstage_get_rec(-1)); + + return 0; +} +COMMON_TEST(test_bootstage_get_rec, 0);