File: test_threads.c

package info (click to toggle)
dpdk 25.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 127,892 kB
  • sloc: ansic: 2,358,479; python: 16,426; sh: 4,474; makefile: 1,713; awk: 70
file content (283 lines) | stat: -rw-r--r-- 8,239 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (C) 2022 Microsoft Corporation
 */

#include <string.h>

#include <rte_thread.h>
#include <rte_debug.h>
#include <rte_stdatomic.h>

#include "test.h"

RTE_LOG_REGISTER(threads_logtype_test, test.threads, INFO);

static RTE_ATOMIC(uint32_t) thread_id_ready;

static uint32_t
thread_main(void *arg)
{
	if (arg != NULL)
		*(rte_thread_t *)arg = rte_thread_self();

	rte_atomic_store_explicit(&thread_id_ready, 1, rte_memory_order_release);

	while (rte_atomic_load_explicit(&thread_id_ready, rte_memory_order_acquire) == 1)
		;

	return 0;
}

static int
test_thread_create_join(void)
{
	rte_thread_t thread_id;
	rte_thread_t thread_main_id;

	thread_id_ready = 0;
	RTE_TEST_ASSERT(rte_thread_create(&thread_id, NULL, thread_main, &thread_main_id) == 0,
		"Failed to create thread.");

	while (rte_atomic_load_explicit(&thread_id_ready, rte_memory_order_acquire) == 0)
		;

	RTE_TEST_ASSERT(rte_thread_equal(thread_id, thread_main_id) != 0,
		"Unexpected thread id.");

	rte_atomic_store_explicit(&thread_id_ready, 2, rte_memory_order_release);

	RTE_TEST_ASSERT(rte_thread_join(thread_id, NULL) == 0,
		"Failed to join thread.");

	return 0;
}

static int
test_thread_create_detach(void)
{
	rte_thread_t thread_id;
	rte_thread_t thread_main_id;

	thread_id_ready = 0;
	RTE_TEST_ASSERT(rte_thread_create(&thread_id, NULL, thread_main,
		&thread_main_id) == 0, "Failed to create thread.");

	while (rte_atomic_load_explicit(&thread_id_ready, rte_memory_order_acquire) == 0)
		;

	RTE_TEST_ASSERT(rte_thread_equal(thread_id, thread_main_id) != 0,
		"Unexpected thread id.");

	rte_atomic_store_explicit(&thread_id_ready, 2, rte_memory_order_release);

	RTE_TEST_ASSERT(rte_thread_detach(thread_id) == 0,
		"Failed to detach thread.");

	return 0;
}

static int
test_thread_priority(void)
{
	rte_thread_t thread_id;
	enum rte_thread_priority priority;

	thread_id_ready = 0;
	RTE_TEST_ASSERT(rte_thread_create(&thread_id, NULL, thread_main, NULL) == 0,
		"Failed to create thread");

	while (rte_atomic_load_explicit(&thread_id_ready, rte_memory_order_acquire) == 0)
		;

	priority = RTE_THREAD_PRIORITY_NORMAL;
	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
		"Failed to set thread priority");
	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
		"Failed to get thread priority");
	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
		"Priority set mismatches priority get");

	priority = RTE_THREAD_PRIORITY_REALTIME_CRITICAL;
#ifndef RTE_EXEC_ENV_WINDOWS
	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == ENOTSUP,
		"Priority set to critical should fail");
	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
		"Failed to get thread priority");
	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
		"Failed set to critical should have retained normal");
#else
	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
		"Priority set to critical should succeed");
	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
		"Failed to get thread priority");
	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_REALTIME_CRITICAL,
		"Priority set mismatches priority get");
#endif

	priority = RTE_THREAD_PRIORITY_NORMAL;
	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
		"Failed to set thread priority");
	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
		"Failed to get thread priority");
	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
		"Priority set mismatches priority get");

	rte_atomic_store_explicit(&thread_id_ready, 2, rte_memory_order_release);

	return 0;
}

static int
test_thread_affinity(void)
{
	rte_thread_t thread_id;
	rte_cpuset_t cpuset0;
	rte_cpuset_t cpuset1;

	thread_id_ready = 0;
	RTE_TEST_ASSERT(rte_thread_create(&thread_id, NULL, thread_main, NULL) == 0,
		"Failed to create thread");

	while (rte_atomic_load_explicit(&thread_id_ready, rte_memory_order_acquire) == 0)
		;

	RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(thread_id, &cpuset0) == 0,
		"Failed to get thread affinity");
	RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(thread_id, &cpuset1) == 0,
		"Failed to get thread affinity");
	RTE_TEST_ASSERT(memcmp(&cpuset0, &cpuset1, sizeof(rte_cpuset_t)) == 0,
		"Affinity should be stable");

	size_t i;
	for (i = 1; i < CPU_SETSIZE; i++)
		if (CPU_ISSET(i, &cpuset0)) {
			CPU_ZERO(&cpuset0);
			CPU_SET(i, &cpuset0);

			break;
		}
	RTE_TEST_ASSERT(rte_thread_set_affinity_by_id(thread_id, &cpuset0) == 0,
		"Failed to set thread affinity");
	RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(thread_id, &cpuset1) == 0,
		"Failed to get thread affinity");
	RTE_TEST_ASSERT(memcmp(&cpuset0, &cpuset1, sizeof(rte_cpuset_t)) == 0,
		"Affinity should be stable");

	return 0;
}

static int
test_thread_attributes_affinity(void)
{
	rte_thread_t thread_id;
	rte_thread_attr_t attr;
	rte_cpuset_t cpuset0;
	rte_cpuset_t cpuset1;

	RTE_TEST_ASSERT(rte_thread_attr_init(&attr) == 0,
		"Failed to initialize thread attributes");

	CPU_ZERO(&cpuset0);
	RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(rte_thread_self(), &cpuset0) == 0,
		"Failed to get thread affinity");
	RTE_TEST_ASSERT(rte_thread_attr_set_affinity(&attr, &cpuset0) == 0,
		"Failed to set thread attributes affinity");
	RTE_TEST_ASSERT(rte_thread_attr_get_affinity(&attr, &cpuset1) == 0,
		"Failed to get thread attributes affinity");
	RTE_TEST_ASSERT(memcmp(&cpuset0, &cpuset1, sizeof(rte_cpuset_t)) == 0,
		"Affinity should be stable");

	thread_id_ready = 0;
	RTE_TEST_ASSERT(rte_thread_create(&thread_id, &attr, thread_main, NULL) == 0,
		"Failed to create attributes affinity thread.");

	while (rte_atomic_load_explicit(&thread_id_ready, rte_memory_order_acquire) == 0)
		;

	RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(thread_id, &cpuset1) == 0,
		"Failed to get attributes thread affinity");
	RTE_TEST_ASSERT(memcmp(&cpuset0, &cpuset1, sizeof(rte_cpuset_t)) == 0,
		"Failed to apply affinity attributes");

	rte_atomic_store_explicit(&thread_id_ready, 2, rte_memory_order_release);

	return 0;
}

static int
test_thread_attributes_priority(void)
{
	rte_thread_t thread_id;
	rte_thread_attr_t attr;
	enum rte_thread_priority priority;

	RTE_TEST_ASSERT(rte_thread_attr_init(&attr) == 0,
		"Failed to initialize thread attributes");
	RTE_TEST_ASSERT(rte_thread_attr_set_priority(&attr, RTE_THREAD_PRIORITY_NORMAL) == 0,
		"Failed to set thread attributes priority");

	thread_id_ready = 0;
	RTE_TEST_ASSERT(rte_thread_create(&thread_id, &attr, thread_main, NULL) == 0,
		"Failed to create attributes priority thread.");

	while (rte_atomic_load_explicit(&thread_id_ready, rte_memory_order_acquire) == 0)
		;

	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
		"Failed to get thread priority");
	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
		"Failed to apply priority attributes");

	rte_atomic_store_explicit(&thread_id_ready, 2, rte_memory_order_release);

	return 0;
}

static int
test_thread_control_create_join(void)
{
	rte_thread_t thread_id;
	rte_thread_t thread_main_id;

	thread_id_ready = 0;
	RTE_TEST_ASSERT(rte_thread_create_control(&thread_id, "dpdk-test-thcc",
		thread_main, &thread_main_id) == 0,
		"Failed to create thread.");

	while (rte_atomic_load_explicit(&thread_id_ready, rte_memory_order_acquire) == 0)
		;

	RTE_TEST_ASSERT(rte_thread_equal(thread_id, thread_main_id) != 0,
		"Unexpected thread id.");

	rte_atomic_store_explicit(&thread_id_ready, 2, rte_memory_order_release);

	RTE_TEST_ASSERT(rte_thread_join(thread_id, NULL) == 0,
		"Failed to join thread.");

	return 0;
}

static struct unit_test_suite threads_test_suite = {
	.suite_name = "threads autotest",
	.setup = NULL,
	.teardown = NULL,
	.unit_test_cases = {
		TEST_CASE(test_thread_create_join),
		TEST_CASE(test_thread_create_detach),
		TEST_CASE(test_thread_affinity),
		TEST_CASE(test_thread_priority),
		TEST_CASE(test_thread_attributes_affinity),
		TEST_CASE(test_thread_attributes_priority),
		TEST_CASE(test_thread_control_create_join),
		TEST_CASES_END()
	}
};

static int
test_threads(void)
{
	return unit_test_suite_runner(&threads_test_suite);
}

REGISTER_FAST_TEST(threads_autotest, true, true, test_threads);