test_thread_affinity.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 test_affinity_sem;
  25. static void
  26. test_affinity_self (void *arg)
  27. {
  28. struct cpumap *prev, *cpumap = arg;
  29. int error = cpumap_create (&prev);
  30. error_check (error, "cpumap_create");
  31. error = thread_get_affinity (thread_self (), prev);
  32. error_check (error, "thread_get_affinity");
  33. if (cpumap_cmp (prev, cpumap) != 0)
  34. panic ("test: unexpected affinity map (get)");
  35. cpumap_zero (cpumap);
  36. cpumap_set (cpumap, 0);
  37. error = thread_set_affinity (thread_self (), cpumap);
  38. error_check (error, "thread_set_affinity");
  39. thread_delay (1, false);
  40. error = thread_get_affinity (thread_self (), prev);
  41. error_check (error, "thread_get_affinity (2)");
  42. if (cpumap_cmp (prev, cpumap) != 0)
  43. panic ("test: unexpected affinity map (set)");
  44. else if (!cpumap_test (cpumap, thread_cpu (thread_self ())))
  45. panic ("test: thread not running in expected CPU");
  46. cpumap_destroy (prev);
  47. }
  48. static void
  49. test_affinity_suspended (void *arg)
  50. {
  51. struct cpumap *cpumap, *prev = arg;
  52. int error = cpumap_create (&cpumap);
  53. error_check (error, "cpumap_create");
  54. semaphore_wait (&test_affinity_sem);
  55. error = thread_get_affinity (thread_self (), cpumap);
  56. error_check (error, "thread_get_affinity");
  57. /* At this point, the parent thread has changed the passed
  58. * CPU map, and thus, they should be equal. */
  59. if (cpumap_cmp (cpumap, prev) != 0)
  60. panic ("test: unexpected affinity map");
  61. else if (!cpumap_test (cpumap, thread_cpu (thread_self ())))
  62. panic ("test: thread not running in expected CPU");
  63. cpumap_destroy (cpumap);
  64. }
  65. TEST_DEFERRED (thread_affinity)
  66. {
  67. if (cpu_count () < 2)
  68. { // Nothing to test on uni-processor systems.
  69. log_err ("test (thread_affinity): not enough processors to test");
  70. return (TEST_SKIPPED);
  71. }
  72. semaphore_init (&test_affinity_sem, 0, 0xff);
  73. struct cpumap *cpumap;
  74. int error = cpumap_create (&cpumap);
  75. error_check (error, "cpumap_create");
  76. cpumap_zero (cpumap);
  77. cpumap_set (cpumap, 1);
  78. struct thread_attr attr;
  79. thread_attr_init (&attr, "test_affinity/0");
  80. thread_attr_set_cpumap (&attr, cpumap);
  81. struct thread *thread;
  82. error = thread_create (&thread, &attr, test_affinity_self, cpumap);
  83. error_check (error, "thread_create");
  84. thread_join (thread);
  85. cpumap_zero (cpumap);
  86. cpumap_set (cpumap, 1);
  87. thread_attr_init (&attr, "test_affinity/1");
  88. thread_attr_set_cpumap (&attr, cpumap);
  89. error = thread_create (&thread, &attr, test_affinity_suspended, cpumap);
  90. error_check (error, "thread_create(2)");
  91. test_thread_wait_state (thread, THREAD_RUNNING);
  92. cpumap_zero (cpumap);
  93. cpumap_set (cpumap, 0);
  94. error = thread_set_affinity (thread, cpumap);
  95. semaphore_post (&test_affinity_sem);
  96. thread_join (thread);
  97. return (TEST_OK);
  98. }