1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2022 Microsoft Corporation
3 */
4
5 #include <string.h>
6 #include <pthread.h>
7
8 #include <rte_thread.h>
9 #include <rte_debug.h>
10
11 #include "test.h"
12
13 RTE_LOG_REGISTER(threads_logtype_test, test.threads, INFO);
14
15 static uint32_t thread_id_ready;
16
17 static void *
thread_main(void * arg)18 thread_main(void *arg)
19 {
20 *(rte_thread_t *)arg = rte_thread_self();
21 __atomic_store_n(&thread_id_ready, 1, __ATOMIC_RELEASE);
22
23 return NULL;
24 }
25
26 static int
test_thread_affinity(void)27 test_thread_affinity(void)
28 {
29 pthread_t id;
30 rte_thread_t thread_id;
31 rte_cpuset_t cpuset0;
32 rte_cpuset_t cpuset1;
33
34 RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
35 "Failed to create thread");
36
37 while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
38 ;
39
40 RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(thread_id, &cpuset0) == 0,
41 "Failed to get thread affinity");
42 RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(thread_id, &cpuset1) == 0,
43 "Failed to get thread affinity");
44 RTE_TEST_ASSERT(memcmp(&cpuset0, &cpuset1, sizeof(rte_cpuset_t)) == 0,
45 "Affinity should be stable");
46
47 size_t i;
48 for (i = 1; i < CPU_SETSIZE; i++)
49 if (CPU_ISSET(i, &cpuset0)) {
50 CPU_ZERO(&cpuset0);
51 CPU_SET(i, &cpuset0);
52
53 break;
54 }
55 RTE_TEST_ASSERT(rte_thread_set_affinity_by_id(thread_id, &cpuset0) == 0,
56 "Failed to set thread affinity");
57 RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(thread_id, &cpuset1) == 0,
58 "Failed to get thread affinity");
59 RTE_TEST_ASSERT(memcmp(&cpuset0, &cpuset1, sizeof(rte_cpuset_t)) == 0,
60 "Affinity should be stable");
61
62 return 0;
63 }
64
65 static struct unit_test_suite threads_test_suite = {
66 .suite_name = "threads autotest",
67 .setup = NULL,
68 .teardown = NULL,
69 .unit_test_cases = {
70 TEST_CASE(test_thread_affinity),
71 TEST_CASES_END()
72 }
73 };
74
75 static int
test_threads(void)76 test_threads(void)
77 {
78 return unit_test_suite_runner(&threads_test_suite);
79 }
80
81 REGISTER_TEST_COMMAND(threads_autotest, test_threads);
82