Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | // SPDX-License-Identifier: GPL-2.0+ /* * Tests for bootstage API * * Copyright 2025 Canonical Ltd */ #include <bootstage.h> #include <linux/delay.h> #include <test/common.h> #include <test/test.h> #include <test/ut.h> /* 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); ut_asserteq(time, bootstage_get_time(BOOTSTAGE_ID_USER + 50)); /* 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); ut_asserteq(rec->time_us, bootstage_get_time(id)); /* 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); |