@@ -51,28 +51,25 @@ static void futex_hash_slots_set_verify(int slots)
51
51
52
52
ret = futex_hash_slots_set (slots , 0 );
53
53
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 ( );
56
56
}
57
57
ret = futex_hash_slots_get ();
58
58
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 ( );
62
62
}
63
+ ksft_test_result_pass ("SET and GET slots %d passed\n" , slots );
63
64
}
64
65
65
66
static void futex_hash_slots_set_must_fail (int slots , int immutable )
66
67
{
67
68
int ret ;
68
69
69
70
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 );
76
73
}
77
74
78
75
static void * thread_return_fn (void * arg )
@@ -97,10 +94,8 @@ static void create_max_threads(void *(*thread_fn)(void *))
97
94
98
95
for (i = 0 ; i < MAX_THREADS ; i ++ ) {
99
96
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" );
104
99
}
105
100
}
106
101
@@ -110,10 +105,8 @@ static void join_max_threads(void)
110
105
111
106
for (i = 0 ; i < MAX_THREADS ; i ++ ) {
112
107
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 );
117
110
}
118
111
}
119
112
@@ -127,6 +120,9 @@ static void usage(char *prog)
127
120
VQUIET , VCRITICAL , VINFO );
128
121
}
129
122
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
+
130
126
int main (int argc , char * argv [])
131
127
{
132
128
int futex_slots1 , futex_slotsn , online_cpus ;
@@ -156,56 +152,50 @@ int main(int argc, char *argv[])
156
152
}
157
153
}
158
154
155
+ ksft_print_header ();
156
+ ksft_set_plan (22 );
159
157
160
158
ret = pthread_mutexattr_init (& mutex_attr_pi );
161
159
ret |= pthread_mutexattr_setprotocol (& mutex_attr_pi , PTHREAD_PRIO_INHERIT );
162
160
ret |= pthread_mutex_init (& global_lock , & mutex_attr_pi );
163
161
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" );
166
163
}
167
-
168
164
/* First thread, expect to be 0, not yet initialized */
169
165
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
+
174
169
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 );
179
172
173
+ ksft_test_result_pass ("Basic get slots and immutable status.\n" );
180
174
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
+
185
178
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
+
190
182
/* First thread, has to initialiaze private hash */
191
183
futex_slots1 = futex_hash_slots_get ();
192
184
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 ) ;
195
187
}
196
188
189
+ ksft_test_result_pass (test_msg_auto_create );
190
+
197
191
online_cpus = sysconf (_SC_NPROCESSORS_ONLN );
198
192
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" );
203
195
204
196
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" );
209
199
210
200
counter = 0 ;
211
201
create_max_threads (thread_lock_fn );
@@ -215,14 +205,17 @@ int main(int argc, char *argv[])
215
205
* The current default size of hash buckets is 16. The auto increase
216
206
* works only if more than 16 CPUs are available.
217
207
*/
208
+ ksft_print_msg ("Online CPUs: %d\n" , online_cpus );
218
209
if (online_cpus > 16 ) {
219
210
futex_slotsn = futex_hash_slots_get ();
220
211
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 );
225
215
}
216
+ ksft_test_result_pass (test_msg_auto_inc );
217
+ } else {
218
+ ksft_test_result_skip (test_msg_auto_inc );
226
219
}
227
220
ret = pthread_mutex_unlock (& global_lock );
228
221
@@ -234,17 +227,12 @@ int main(int argc, char *argv[])
234
227
futex_hash_slots_set_verify (16 );
235
228
236
229
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
+
241
232
futex_hash_slots_set_verify (2 );
242
233
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 );
248
236
counter = 0 ;
249
237
/* Once the user set something, auto reisze must be disabled */
250
238
ret = pthread_barrier_init (& barrier_main , NULL , MAX_THREADS );
@@ -253,10 +241,8 @@ int main(int argc, char *argv[])
253
241
join_max_threads ();
254
242
255
243
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 );
260
246
261
247
futex_hash_slots_set_must_fail (1 << 29 , 0 );
262
248
@@ -266,17 +252,13 @@ int main(int argc, char *argv[])
266
252
*/
267
253
if (use_global_hash ) {
268
254
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" );
273
256
} else {
274
257
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" );
279
259
}
260
+ if (ret != 0 )
261
+ goto out ;
280
262
281
263
futex_hash_slots_set_must_fail (4 , 0 );
282
264
futex_hash_slots_set_must_fail (4 , 1 );
@@ -287,29 +269,23 @@ int main(int argc, char *argv[])
287
269
288
270
ret = pthread_barrier_init (& barrier_main , NULL , MAX_THREADS );
289
271
if (ret != 0 ) {
290
- error ("pthread_barrier_init failed. \n" , errno );
272
+ ksft_exit_fail_msg ("pthread_barrier_init failed: %m \n" );
291
273
return 1 ;
292
274
}
293
275
create_max_threads (thread_lock_fn );
294
276
join_max_threads ();
295
277
296
278
ret = futex_hash_slots_get ();
297
279
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" );
302
281
} 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" );
307
283
}
308
284
309
285
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 ();
314
290
return 0 ;
315
291
}
0 commit comments