smp_bind_affinity_tc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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-08-10 RV the first version
  9. */
  10. #include <rtthread.h>
  11. #include "utest.h"
  12. /**
  13. * @brief Binding core affinity testcase.
  14. *
  15. * @note Create RT_CPUS_NR threads, thread 0 is bound to core 0, other threads are not bound to specific cores,
  16. * after running for a set number of times, count the number of times each core is run on the corresponding core,
  17. * thread 0 should always be run on core 0, other threads will be run on different cores.
  18. */
  19. /* Number of thread runs */
  20. static int run_num = 100;
  21. #define THREAD_STACK_SIZE UTEST_THR_STACK_SIZE
  22. #define THREAD_PRIORITY 20
  23. static rt_thread_t threads[RT_CPUS_NR];
  24. static struct rt_spinlock lock;
  25. static int thread_inc[RT_CPUS_NR] = {0};
  26. static int thread_tic[RT_CPUS_NR] = {0};
  27. static int finsh_flag = 0;
  28. static int num = 0;
  29. static void thread_entry(void *parameter)
  30. {
  31. int id = 0;
  32. int para = *(int *)parameter;
  33. while (1)
  34. {
  35. thread_tic[para]++;
  36. id = rt_hw_cpu_id();
  37. if (para == id)
  38. {
  39. thread_inc[para]++;
  40. }
  41. if (thread_tic[para] == run_num)
  42. {
  43. if (para == 0)
  44. uassert_int_equal(thread_inc[para], thread_tic[para]);
  45. else
  46. uassert_int_not_equal(thread_inc[para], thread_tic[para]);
  47. finsh_flag ++;
  48. }
  49. rt_thread_delay(5);
  50. }
  51. }
  52. static void thread_bind_affinity_tc(void)
  53. {
  54. static int params[RT_CPUS_NR] = {0};
  55. char thread_name[8];
  56. int i, j;
  57. for (i = 0; i < RT_CPUS_NR; i++)
  58. {
  59. params[i] = i;
  60. }
  61. /* Create RT_CPUS_NR threads Thread 0 is bound to core 0 Other threads are not bound */
  62. for (i = 0; i < RT_CPUS_NR; i++)
  63. {
  64. rt_snprintf(thread_name, sizeof(thread_name), "thread%d", i);
  65. threads[i] = rt_thread_create(thread_name, thread_entry, (int *)&params[i], THREAD_STACK_SIZE, THREAD_PRIORITY, 20);
  66. if (i == 0)
  67. {
  68. rt_thread_control(threads[0], RT_THREAD_CTRL_BIND_CPU, (void *)0);
  69. }
  70. if (threads[i] != RT_NULL)
  71. {
  72. rt_thread_startup(threads[i]);
  73. }
  74. }
  75. while (finsh_flag != RT_CPUS_NR);
  76. /* Displays the number of times a thread was executed on the relevant core */
  77. for (j = 0; j < RT_CPUS_NR; j++)
  78. {
  79. rt_spin_lock(&lock);
  80. rt_kprintf("Total runs[%d], Number of times thread[%d] run on [core%d]: [%4d], always run at core%d ? %s \r\n", run_num, j, j, thread_inc[j], j, (thread_inc[j] == run_num) ? "yes" : "no");
  81. rt_spin_unlock(&lock);
  82. }
  83. }
  84. static rt_err_t utest_tc_init(void)
  85. {
  86. rt_spin_lock_init(&lock);
  87. return RT_EOK;
  88. }
  89. static rt_err_t utest_tc_cleanup(void)
  90. {
  91. for (num = 0; num < RT_CPUS_NR; num++)
  92. {
  93. rt_thread_delete(threads[num]);
  94. }
  95. return RT_EOK;
  96. }
  97. static void testcase(void)
  98. {
  99. UTEST_UNIT_RUN(thread_bind_affinity_tc);
  100. }
  101. UTEST_TC_EXPORT(testcase, "testcases.smp.bind_affinity_tc", utest_tc_init, utest_tc_cleanup, 10);