Skip to content

Commit 47669f4

Browse files
committed
Merge tag 'linux_kselftest-kunit-fixes-6.7-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
Pull KUnit fixes from Shuah Khan: "Three fixes to warnings and run-time test behavior. With these fixes, test suite counter will be reset correctly before running tests, kunit will warn if tests are too slow, and eliminate warning when kfree() as an action" * tag 'linux_kselftest-kunit-fixes-6.7-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: kunit: test: Avoid cast warning when adding kfree() as an action kunit: Reset suite counter right before running tests kunit: Warn if tests are slow
2 parents 2594faa + 1bddcf7 commit 47669f4

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/kunit/kunit-test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ static void kunit_log_test(struct kunit *test)
562562
KUNIT_EXPECT_TRUE(test, test->log->append_newlines);
563563

564564
full_log = string_stream_get_string(test->log);
565-
kunit_add_action(test, (kunit_action_t *)kfree, full_log);
565+
kunit_add_action(test, kfree_wrapper, full_log);
566566
KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
567567
strstr(full_log, "put this in log."));
568568
KUNIT_EXPECT_NOT_ERR_OR_NULL(test,

lib/kunit/test.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,45 @@ void kunit_init_test(struct kunit *test, const char *name, struct string_stream
338338
}
339339
EXPORT_SYMBOL_GPL(kunit_init_test);
340340

341+
/* Only warn when a test takes more than twice the threshold */
342+
#define KUNIT_SPEED_WARNING_MULTIPLIER 2
343+
344+
/* Slow tests are defined as taking more than 1s */
345+
#define KUNIT_SPEED_SLOW_THRESHOLD_S 1
346+
347+
#define KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S \
348+
(KUNIT_SPEED_WARNING_MULTIPLIER * KUNIT_SPEED_SLOW_THRESHOLD_S)
349+
350+
#define s_to_timespec64(s) ns_to_timespec64((s) * NSEC_PER_SEC)
351+
352+
static void kunit_run_case_check_speed(struct kunit *test,
353+
struct kunit_case *test_case,
354+
struct timespec64 duration)
355+
{
356+
struct timespec64 slow_thr =
357+
s_to_timespec64(KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S);
358+
enum kunit_speed speed = test_case->attr.speed;
359+
360+
if (timespec64_compare(&duration, &slow_thr) < 0)
361+
return;
362+
363+
if (speed == KUNIT_SPEED_VERY_SLOW || speed == KUNIT_SPEED_SLOW)
364+
return;
365+
366+
kunit_warn(test,
367+
"Test should be marked slow (runtime: %lld.%09lds)",
368+
duration.tv_sec, duration.tv_nsec);
369+
}
370+
341371
/*
342372
* Initializes and runs test case. Does not clean up or do post validations.
343373
*/
344374
static void kunit_run_case_internal(struct kunit *test,
345375
struct kunit_suite *suite,
346376
struct kunit_case *test_case)
347377
{
378+
struct timespec64 start, end;
379+
348380
if (suite->init) {
349381
int ret;
350382

@@ -356,7 +388,13 @@ static void kunit_run_case_internal(struct kunit *test,
356388
}
357389
}
358390

391+
ktime_get_ts64(&start);
392+
359393
test_case->run_case(test);
394+
395+
ktime_get_ts64(&end);
396+
397+
kunit_run_case_check_speed(test, test_case, timespec64_sub(end, start));
360398
}
361399

362400
static void kunit_case_internal_cleanup(struct kunit *test)
@@ -670,6 +708,8 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_
670708
return 0;
671709
}
672710

711+
kunit_suite_counter = 1;
712+
673713
static_branch_inc(&kunit_running);
674714

675715
for (i = 0; i < num_suites; i++) {
@@ -696,8 +736,6 @@ void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
696736

697737
for (i = 0; i < num_suites; i++)
698738
kunit_exit_suite(suites[i]);
699-
700-
kunit_suite_counter = 1;
701739
}
702740
EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
703741

0 commit comments

Comments
 (0)