Skip to content

Commit 2b73636

Browse files
Sebastian Andrzej SiewiorPeter Zijlstra
authored andcommitted
selftests/futex: Use TAP output in futex_priv_hash
Use TAP output for easier automated testing. Suggested-by: André Almeida <[email protected]> Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: André Almeida <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent bd59f61 commit 2b73636

File tree

1 file changed

+62
-86
lines changed

1 file changed

+62
-86
lines changed

tools/testing/selftests/futex/functional/futex_priv_hash.c

Lines changed: 62 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,25 @@ static void futex_hash_slots_set_verify(int slots)
5151

5252
ret = futex_hash_slots_set(slots, 0);
5353
if (ret != 0) {
54-
error("Failed to set slots to %d\n", errno, slots);
55-
exit(1);
54+
ksft_test_result_fail("Failed to set slots to %d: %m\n", slots);
55+
ksft_finished();
5656
}
5757
ret = futex_hash_slots_get();
5858
if (ret != slots) {
59-
error("Set %d slots but PR_FUTEX_HASH_GET_SLOTS returns: %d\n",
60-
errno, slots, ret);
61-
exit(1);
59+
ksft_test_result_fail("Set %d slots but PR_FUTEX_HASH_GET_SLOTS returns: %d, %m\n",
60+
slots, ret);
61+
ksft_finished();
6262
}
63+
ksft_test_result_pass("SET and GET slots %d passed\n", slots);
6364
}
6465

6566
static void futex_hash_slots_set_must_fail(int slots, int immutable)
6667
{
6768
int ret;
6869

6970
ret = futex_hash_slots_set(slots, immutable);
70-
if (ret < 0)
71-
return;
72-
73-
fail("futex_hash_slots_set(%d, %d) expected to fail but succeeded.\n",
74-
slots, immutable);
75-
exit(1);
71+
ksft_test_result(ret < 0, "futex_hash_slots_set(%d, %d)\n",
72+
slots, immutable);
7673
}
7774

7875
static void *thread_return_fn(void *arg)
@@ -97,10 +94,8 @@ static void create_max_threads(void *(*thread_fn)(void *))
9794

9895
for (i = 0; i < MAX_THREADS; i++) {
9996
ret = pthread_create(&threads[i], NULL, thread_fn, NULL);
100-
if (ret) {
101-
error("pthread_create failed\n", errno);
102-
exit(1);
103-
}
97+
if (ret)
98+
ksft_exit_fail_msg("pthread_create failed: %m\n");
10499
}
105100
}
106101

@@ -110,10 +105,8 @@ static void join_max_threads(void)
110105

111106
for (i = 0; i < MAX_THREADS; i++) {
112107
ret = pthread_join(threads[i], NULL);
113-
if (ret) {
114-
error("pthread_join failed for thread %d\n", errno, i);
115-
exit(1);
116-
}
108+
if (ret)
109+
ksft_exit_fail_msg("pthread_join failed for thread %d\n", i);
117110
}
118111
}
119112

@@ -127,6 +120,9 @@ static void usage(char *prog)
127120
VQUIET, VCRITICAL, VINFO);
128121
}
129122

123+
static const char *test_msg_auto_create = "Automatic hash bucket init on thread creation.\n";
124+
static const char *test_msg_auto_inc = "Automatic increase with more than 16 CPUs\n";
125+
130126
int main(int argc, char *argv[])
131127
{
132128
int futex_slots1, futex_slotsn, online_cpus;
@@ -156,56 +152,50 @@ int main(int argc, char *argv[])
156152
}
157153
}
158154

155+
ksft_print_header();
156+
ksft_set_plan(22);
159157

160158
ret = pthread_mutexattr_init(&mutex_attr_pi);
161159
ret |= pthread_mutexattr_setprotocol(&mutex_attr_pi, PTHREAD_PRIO_INHERIT);
162160
ret |= pthread_mutex_init(&global_lock, &mutex_attr_pi);
163161
if (ret != 0) {
164-
fail("Failed to initialize pthread mutex.\n");
165-
return 1;
162+
ksft_exit_fail_msg("Failed to initialize pthread mutex.\n");
166163
}
167-
168164
/* First thread, expect to be 0, not yet initialized */
169165
ret = futex_hash_slots_get();
170-
if (ret != 0) {
171-
error("futex_hash_slots_get() failed: %d\n", errno, ret);
172-
return 1;
173-
}
166+
if (ret != 0)
167+
ksft_exit_fail_msg("futex_hash_slots_get() failed: %d, %m\n", ret);
168+
174169
ret = futex_hash_immutable_get();
175-
if (ret != 0) {
176-
error("futex_hash_immutable_get() failed: %d\n", errno, ret);
177-
return 1;
178-
}
170+
if (ret != 0)
171+
ksft_exit_fail_msg("futex_hash_immutable_get() failed: %d, %m\n", ret);
179172

173+
ksft_test_result_pass("Basic get slots and immutable status.\n");
180174
ret = pthread_create(&threads[0], NULL, thread_return_fn, NULL);
181-
if (ret != 0) {
182-
error("pthread_create() failed: %d\n", errno, ret);
183-
return 1;
184-
}
175+
if (ret != 0)
176+
ksft_exit_fail_msg("pthread_create() failed: %d, %m\n", ret);
177+
185178
ret = pthread_join(threads[0], NULL);
186-
if (ret != 0) {
187-
error("pthread_join() failed: %d\n", errno, ret);
188-
return 1;
189-
}
179+
if (ret != 0)
180+
ksft_exit_fail_msg("pthread_join() failed: %d, %m\n", ret);
181+
190182
/* First thread, has to initialiaze private hash */
191183
futex_slots1 = futex_hash_slots_get();
192184
if (futex_slots1 <= 0) {
193-
fail("Expected > 0 hash buckets, got: %d\n", futex_slots1);
194-
return 1;
185+
ksft_print_msg("Current hash buckets: %d\n", futex_slots1);
186+
ksft_exit_fail_msg(test_msg_auto_create);
195187
}
196188

189+
ksft_test_result_pass(test_msg_auto_create);
190+
197191
online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
198192
ret = pthread_barrier_init(&barrier_main, NULL, MAX_THREADS + 1);
199-
if (ret != 0) {
200-
error("pthread_barrier_init failed.\n", errno);
201-
return 1;
202-
}
193+
if (ret != 0)
194+
ksft_exit_fail_msg("pthread_barrier_init failed: %m.\n");
203195

204196
ret = pthread_mutex_lock(&global_lock);
205-
if (ret != 0) {
206-
error("pthread_mutex_lock failed.\n", errno);
207-
return 1;
208-
}
197+
if (ret != 0)
198+
ksft_exit_fail_msg("pthread_mutex_lock failed: %m.\n");
209199

210200
counter = 0;
211201
create_max_threads(thread_lock_fn);
@@ -215,14 +205,17 @@ int main(int argc, char *argv[])
215205
* The current default size of hash buckets is 16. The auto increase
216206
* works only if more than 16 CPUs are available.
217207
*/
208+
ksft_print_msg("Online CPUs: %d\n", online_cpus);
218209
if (online_cpus > 16) {
219210
futex_slotsn = futex_hash_slots_get();
220211
if (futex_slotsn < 0 || futex_slots1 == futex_slotsn) {
221-
fail("Expected increase of hash buckets but got: %d -> %d\n",
222-
futex_slots1, futex_slotsn);
223-
info("Online CPUs: %d\n", online_cpus);
224-
return 1;
212+
ksft_print_msg("Expected increase of hash buckets but got: %d -> %d\n",
213+
futex_slots1, futex_slotsn);
214+
ksft_exit_fail_msg(test_msg_auto_inc);
225215
}
216+
ksft_test_result_pass(test_msg_auto_inc);
217+
} else {
218+
ksft_test_result_skip(test_msg_auto_inc);
226219
}
227220
ret = pthread_mutex_unlock(&global_lock);
228221

@@ -234,17 +227,12 @@ int main(int argc, char *argv[])
234227
futex_hash_slots_set_verify(16);
235228

236229
ret = futex_hash_slots_set(15, 0);
237-
if (ret >= 0) {
238-
fail("Expected to fail with 15 slots but succeeded: %d.\n", ret);
239-
return 1;
240-
}
230+
ksft_test_result(ret < 0, "Use 15 slots\n");
231+
241232
futex_hash_slots_set_verify(2);
242233
join_max_threads();
243-
if (counter != MAX_THREADS) {
244-
fail("Expected thread counter at %d but is %d\n",
245-
MAX_THREADS, counter);
246-
return 1;
247-
}
234+
ksft_test_result(counter == MAX_THREADS, "Created of waited for %d of %d threads\n",
235+
counter, MAX_THREADS);
248236
counter = 0;
249237
/* Once the user set something, auto reisze must be disabled */
250238
ret = pthread_barrier_init(&barrier_main, NULL, MAX_THREADS);
@@ -253,10 +241,8 @@ int main(int argc, char *argv[])
253241
join_max_threads();
254242

255243
ret = futex_hash_slots_get();
256-
if (ret != 2) {
257-
printf("Expected 2 slots, no auto-resize, got %d\n", ret);
258-
return 1;
259-
}
244+
ksft_test_result(ret == 2, "No more auto-resize after manaul setting, got %d\n",
245+
ret);
260246

261247
futex_hash_slots_set_must_fail(1 << 29, 0);
262248

@@ -266,17 +252,13 @@ int main(int argc, char *argv[])
266252
*/
267253
if (use_global_hash) {
268254
ret = futex_hash_slots_set(0, 0);
269-
if (ret != 0) {
270-
printf("Can't request global hash: %m\n");
271-
return 1;
272-
}
255+
ksft_test_result(ret == 0, "Global hash request\n");
273256
} else {
274257
ret = futex_hash_slots_set(4, 1);
275-
if (ret != 0) {
276-
printf("Immutable resize to 4 failed: %m\n");
277-
return 1;
278-
}
258+
ksft_test_result(ret == 0, "Immutable resize to 4\n");
279259
}
260+
if (ret != 0)
261+
goto out;
280262

281263
futex_hash_slots_set_must_fail(4, 0);
282264
futex_hash_slots_set_must_fail(4, 1);
@@ -287,29 +269,23 @@ int main(int argc, char *argv[])
287269

288270
ret = pthread_barrier_init(&barrier_main, NULL, MAX_THREADS);
289271
if (ret != 0) {
290-
error("pthread_barrier_init failed.\n", errno);
272+
ksft_exit_fail_msg("pthread_barrier_init failed: %m\n");
291273
return 1;
292274
}
293275
create_max_threads(thread_lock_fn);
294276
join_max_threads();
295277

296278
ret = futex_hash_slots_get();
297279
if (use_global_hash) {
298-
if (ret != 0) {
299-
error("Expected global hash, got %d\n", errno, ret);
300-
return 1;
301-
}
280+
ksft_test_result(ret == 0, "Continue to use global hash\n");
302281
} else {
303-
if (ret != 4) {
304-
error("Expected 4 slots, no auto-resize, got %d\n", errno, ret);
305-
return 1;
306-
}
282+
ksft_test_result(ret == 4, "Continue to use the 4 hash buckets\n");
307283
}
308284

309285
ret = futex_hash_immutable_get();
310-
if (ret != 1) {
311-
fail("Expected immutable private hash, got %d\n", ret);
312-
return 1;
313-
}
286+
ksft_test_result(ret == 1, "Hash reports to be immutable\n");
287+
288+
out:
289+
ksft_finished();
314290
return 0;
315291
}

0 commit comments

Comments
 (0)