test_thread_affinity.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2018 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. #include <kern/atomic.h>
  19. #include <kern/error.h>
  20. #include <kern/log.h>
  21. #include <kern/semaphore.h>
  22. #include <kern/thread.h>
  23. #include <test/test.h>
  24. struct semaphore affinity_sem;
  25. static void
  26. test_affinity_self(void *arg)
  27. {
  28. struct cpumap *prev, *cpumap;
  29. int error;
  30. cpumap = arg;
  31. error = cpumap_create(&prev);
  32. error_check(error, "cpumap_create");
  33. error = thread_get_affinity(thread_self(), prev);
  34. error_check(error, "thread_get_affinity");
  35. if (cpumap_cmp(prev, cpumap) != 0) {
  36. panic("test: unexpected affinity map (get)");
  37. }
  38. cpumap_zero(cpumap);
  39. cpumap_set(cpumap, 0);
  40. error = thread_set_affinity(thread_self(), cpumap);
  41. error_check(error, "thread_set_affinity");
  42. semaphore_timedwait(&affinity_sem, 1);
  43. error = thread_get_affinity(thread_self(), prev);
  44. error_check(error, "thread_get_affinity (2)");
  45. if (cpumap_cmp(prev, cpumap) != 0) {
  46. panic("test: unexpected affinity map (set)");
  47. } else if (!cpumap_test(cpumap, thread_cpu(thread_self()))) {
  48. panic("test: thread not running in expected CPU");
  49. }
  50. cpumap_destroy(prev);
  51. }
  52. static void
  53. test_affinity_suspended(void *arg)
  54. {
  55. struct cpumap *cpumap, *prev;
  56. int error;
  57. prev = arg;
  58. error = cpumap_create(&cpumap);
  59. error_check(error, "cpumap_create");
  60. semaphore_wait(&affinity_sem);
  61. error = thread_get_affinity(thread_self(), cpumap);
  62. error_check(error, "thread_get_affinity");
  63. /* At this point, the parent thread has changed the passed
  64. * CPU map, and thus, they should be equal. */
  65. if (cpumap_cmp(cpumap, prev) != 0) {
  66. panic("test: unexpected affinity map");
  67. } else if (!cpumap_test(cpumap, thread_cpu(thread_self()))) {
  68. panic("test: thread not running in expected CPU");
  69. }
  70. cpumap_destroy(cpumap);
  71. }
  72. static void
  73. test_run(void *arg)
  74. {
  75. struct thread *thread;
  76. struct thread_attr attr;
  77. struct cpumap *cpumap;
  78. int error;
  79. (void)arg;
  80. if (cpu_count() < 2) {
  81. /* Nothing to test on uni-processor systems. */
  82. return;
  83. }
  84. semaphore_init(&affinity_sem, 0, 0xff);
  85. error = cpumap_create(&cpumap);
  86. error_check(error, "cpumap_create");
  87. cpumap_zero(cpumap);
  88. cpumap_set(cpumap, 1);
  89. thread_attr_init(&attr, "test_affinity");
  90. thread_attr_set_cpumap(&attr, cpumap);
  91. error = thread_create(&thread, &attr, test_affinity_self, cpumap);
  92. error_check(error, "thread_create");
  93. thread_join(thread);
  94. cpumap_zero(cpumap);
  95. cpumap_set(cpumap, 1);
  96. thread_attr_init(&attr, "test_affinity(2)");
  97. thread_attr_set_cpumap(&attr, cpumap);
  98. error = thread_create(&thread, &attr, test_affinity_suspended, cpumap);
  99. error_check(error, "thread_create(2)");
  100. while (thread_state(thread) == THREAD_RUNNING) {
  101. cpu_pause();
  102. }
  103. cpumap_zero(cpumap);
  104. cpumap_set(cpumap, 0);
  105. error = thread_set_affinity(thread, cpumap);
  106. semaphore_post(&affinity_sem);
  107. thread_join(thread);
  108. log_info("affinity test done");
  109. }
  110. void __init
  111. test_setup(void)
  112. {
  113. struct thread_attr attr;
  114. int error;
  115. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_run");
  116. thread_attr_set_detached(&attr);
  117. error = thread_create(NULL, &attr, test_run, NULL);
  118. error_check(error, "thread_create");
  119. }