smp_001_tc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024/9/12 zhujiale the first version
  9. * 2024/10/28 Shell Added more assertions
  10. * 2025/12/3 ChuanN-sudo add standardized utest documentation block
  11. */
  12. /**
  13. * Test Case Name: SMP Call Smoke 001 Test
  14. *
  15. * Test Objectives:
  16. * - Validate rt_smp_call_cpu_mask() with SMP_CALL_WAIT_ALL flag for blocking synchronous calls.
  17. * - Verify callback execution on all targeted CPUs.
  18. * - Ensure callback runs in interrupt-disabled context.
  19. * - Test core APIs: rt_smp_call_cpu_mask().
  20. *
  21. * Test Scenarios:
  22. * - Main thread generates random CPU masks and dispatches callbacks to targeted CPUs.
  23. * - Callback function clears corresponding CPU bit from shared mask variable.
  24. * - Blocking call waits for all targeted CPUs to complete execution.
  25. * - Random CPU selection simulates various multi-core scenarios.
  26. *
  27. * Verification Metrics:
  28. * - rt_smp_call_cpu_mask() must block until all targeted CPUs complete callback execution.
  29. * - Shared mask variable must be zero after function returns, confirming all targets executed.
  30. * - Callback must execute in interrupt-disabled context.
  31. * - All available CPUs must be tested throughout the run.
  32. * - Data integrity maintained across CPUs with spinlock protection.
  33. *
  34. * Dependencies:
  35. * - Hardware requirements: QEMU emulator or any multi-core hardware platform that supports RT-Thread.
  36. * - Software configuration:
  37. * - RT_USING_UTEST must be enabled (select "RT-Thread Utestcases" in menuconfig).
  38. * - RT_UTEST_SMP_CALL_FUNC must be enabled(enable via: RT-Thread Utestcases -> Kernel Components -> Drivers -> SMP-Call Test -> SMP-Call Smoke Test).
  39. * - Environmental Assumptions: System scheduler and SMP services working normally.
  40. *
  41. * Expected Results:
  42. * - Final output: "[ PASSED ] [ result ] testcase (components.drivers.smp_call.smoke_001)"
  43. * - No assertions triggered during test execution.
  44. */
  45. #include <rtdevice.h>
  46. #include <utest.h>
  47. #include <utest_assert.h>
  48. #include <smp_call.h>
  49. #define TEST_COUNT 10000
  50. static int pass_count = 0;
  51. static RT_DEFINE_SPINLOCK(_test_data_lock);
  52. static void _test_smp_cb(void *data)
  53. {
  54. int *maskp;
  55. int oncpu;
  56. if (!rt_hw_interrupt_is_disabled())
  57. {
  58. /* SYNC.004 */
  59. uassert_true(0);
  60. }
  61. rt_spin_lock(&_test_data_lock);
  62. oncpu = rt_hw_cpu_id();
  63. maskp = (int *)data;
  64. *maskp &= ~(1 << oncpu);
  65. rt_spin_unlock(&_test_data_lock);
  66. }
  67. static void _blocking_call(void)
  68. {
  69. volatile int cpu_mask;
  70. rt_ubase_t tested_cpus = 0;
  71. for (int i = 0; i < TEST_COUNT; i++)
  72. {
  73. cpu_mask = rand() % RT_ALL_CPU;
  74. tested_cpus |= cpu_mask;
  75. rt_smp_call_cpu_mask(cpu_mask, _test_smp_cb, (void *)&cpu_mask, SMP_CALL_WAIT_ALL);
  76. if (!cpu_mask)
  77. {
  78. pass_count++;
  79. }
  80. else
  81. {
  82. /* TARG.001, MP.001 */
  83. uassert_true(0);
  84. break;
  85. }
  86. }
  87. LOG_D("pass_count %d", pass_count);
  88. /* TARG.001 */
  89. uassert_true(pass_count == TEST_COUNT);
  90. /* TOP.001, TOP.002 */
  91. uassert_true(tested_cpus == RT_ALL_CPU);
  92. }
  93. static rt_err_t utest_tc_init(void)
  94. {
  95. pass_count = 0;
  96. srand(rt_tick_get());
  97. return RT_EOK;
  98. }
  99. static rt_err_t utest_tc_cleanup(void)
  100. {
  101. return RT_EOK;
  102. }
  103. static void _testcase(void)
  104. {
  105. UTEST_UNIT_RUN(_blocking_call);
  106. }
  107. UTEST_TC_EXPORT(_testcase, "components.drivers.smp_call.smoke_001", utest_tc_init, utest_tc_cleanup, 10);