]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/splat/splat-condvar.c
1fe306cb27dec2c8af819ac98e56477d8fccf74b
[mirror_spl-debian.git] / module / splat / splat-condvar.c
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Condition Variable Tests.
25 \*****************************************************************************/
26
27 #include <linux/kthread.h>
28 #include <sys/condvar.h>
29 #include "splat-internal.h"
30
31 #define SPLAT_CONDVAR_NAME "condvar"
32 #define SPLAT_CONDVAR_DESC "Kernel Condition Variable Tests"
33
34 #define SPLAT_CONDVAR_TEST1_ID 0x0501
35 #define SPLAT_CONDVAR_TEST1_NAME "signal1"
36 #define SPLAT_CONDVAR_TEST1_DESC "Wake a single thread, cv_wait()/cv_signal()"
37
38 #define SPLAT_CONDVAR_TEST2_ID 0x0502
39 #define SPLAT_CONDVAR_TEST2_NAME "broadcast1"
40 #define SPLAT_CONDVAR_TEST2_DESC "Wake all threads, cv_wait()/cv_broadcast()"
41
42 #define SPLAT_CONDVAR_TEST3_ID 0x0503
43 #define SPLAT_CONDVAR_TEST3_NAME "signal2"
44 #define SPLAT_CONDVAR_TEST3_DESC "Wake a single thread, cv_wait_timeout()/cv_signal()"
45
46 #define SPLAT_CONDVAR_TEST4_ID 0x0504
47 #define SPLAT_CONDVAR_TEST4_NAME "broadcast2"
48 #define SPLAT_CONDVAR_TEST4_DESC "Wake all threads, cv_wait_timeout()/cv_broadcast()"
49
50 #define SPLAT_CONDVAR_TEST5_ID 0x0505
51 #define SPLAT_CONDVAR_TEST5_NAME "timeout"
52 #define SPLAT_CONDVAR_TEST5_DESC "Timeout thread, cv_wait_timeout()"
53
54 #define SPLAT_CONDVAR_TEST_MAGIC 0x115599DDUL
55 #define SPLAT_CONDVAR_TEST_NAME "condvar"
56 #define SPLAT_CONDVAR_TEST_COUNT 8
57
58 typedef struct condvar_priv {
59 unsigned long cv_magic;
60 struct file *cv_file;
61 kcondvar_t cv_condvar;
62 kmutex_t cv_mtx;
63 } condvar_priv_t;
64
65 typedef struct condvar_thr {
66 const char *ct_name;
67 condvar_priv_t *ct_cvp;
68 struct task_struct *ct_thread;
69 int ct_rc;
70 } condvar_thr_t;
71
72 int
73 splat_condvar_test12_thread(void *arg)
74 {
75 condvar_thr_t *ct = (condvar_thr_t *)arg;
76 condvar_priv_t *cv = ct->ct_cvp;
77
78 ASSERT(cv->cv_magic == SPLAT_CONDVAR_TEST_MAGIC);
79
80 mutex_enter(&cv->cv_mtx);
81 splat_vprint(cv->cv_file, ct->ct_name,
82 "%s thread sleeping with %d waiters\n",
83 ct->ct_thread->comm, atomic_read(&cv->cv_condvar.cv_waiters));
84 cv_wait(&cv->cv_condvar, &cv->cv_mtx);
85 splat_vprint(cv->cv_file, ct->ct_name,
86 "%s thread woken %d waiters remain\n",
87 ct->ct_thread->comm, atomic_read(&cv->cv_condvar.cv_waiters));
88 mutex_exit(&cv->cv_mtx);
89
90 return 0;
91 }
92
93 static int
94 splat_condvar_test1(struct file *file, void *arg)
95 {
96 int i, count = 0, rc = 0;
97 condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT];
98 condvar_priv_t cv;
99
100 cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC;
101 cv.cv_file = file;
102 mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL);
103 cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL);
104
105 /* Create some threads, the exact number isn't important just as
106 * long as we know how many we managed to create and should expect. */
107 for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
108 ct[i].ct_cvp = &cv;
109 ct[i].ct_name = SPLAT_CONDVAR_TEST1_NAME;
110 ct[i].ct_rc = 0;
111 ct[i].ct_thread = kthread_create(splat_condvar_test12_thread,
112 &ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i);
113
114 if (!IS_ERR(ct[i].ct_thread)) {
115 wake_up_process(ct[i].ct_thread);
116 count++;
117 }
118 }
119
120 /* Wait until all threads are waiting on the condition variable */
121 while (atomic_read(&cv.cv_condvar.cv_waiters) != count)
122 schedule();
123
124 /* Wake a single thread at a time, wait until it exits */
125 for (i = 1; i <= count; i++) {
126 cv_signal(&cv.cv_condvar);
127
128 while (atomic_read(&cv.cv_condvar.cv_waiters) > (count - i))
129 schedule();
130
131 /* Correct behavior 1 thread woken */
132 if (atomic_read(&cv.cv_condvar.cv_waiters) == (count - i))
133 continue;
134
135 splat_vprint(file, SPLAT_CONDVAR_TEST1_NAME, "Attempted to "
136 "wake %d thread but work %d threads woke\n",
137 1, count - atomic_read(&cv.cv_condvar.cv_waiters));
138 rc = -EINVAL;
139 break;
140 }
141
142 if (!rc)
143 splat_vprint(file, SPLAT_CONDVAR_TEST1_NAME, "Correctly woke "
144 "%d sleeping threads %d at a time\n", count, 1);
145
146 /* Wait until that last nutex is dropped */
147 while (mutex_owner(&cv.cv_mtx))
148 schedule();
149
150 /* Wake everything for the failure case */
151 cv_broadcast(&cv.cv_condvar);
152 cv_destroy(&cv.cv_condvar);
153 mutex_destroy(&cv.cv_mtx);
154
155 return rc;
156 }
157
158 static int
159 splat_condvar_test2(struct file *file, void *arg)
160 {
161 int i, count = 0, rc = 0;
162 condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT];
163 condvar_priv_t cv;
164
165 cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC;
166 cv.cv_file = file;
167 mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL);
168 cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL);
169
170 /* Create some threads, the exact number isn't important just as
171 * long as we know how many we managed to create and should expect. */
172 for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
173 ct[i].ct_cvp = &cv;
174 ct[i].ct_name = SPLAT_CONDVAR_TEST2_NAME;
175 ct[i].ct_rc = 0;
176 ct[i].ct_thread = kthread_create(splat_condvar_test12_thread,
177 &ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i);
178
179 if (!IS_ERR(ct[i].ct_thread)) {
180 wake_up_process(ct[i].ct_thread);
181 count++;
182 }
183 }
184
185 /* Wait until all threads are waiting on the condition variable */
186 while (atomic_read(&cv.cv_condvar.cv_waiters) != count)
187 schedule();
188
189 /* Wake all threads waiting on the condition variable */
190 cv_broadcast(&cv.cv_condvar);
191
192 /* Wait until all threads have exited */
193 while ((atomic_read(&cv.cv_condvar.cv_waiters) > 0) || mutex_owner(&cv.cv_mtx))
194 schedule();
195
196 splat_vprint(file, SPLAT_CONDVAR_TEST2_NAME, "Correctly woke all "
197 "%d sleeping threads at once\n", count);
198
199 /* Wake everything for the failure case */
200 cv_destroy(&cv.cv_condvar);
201 mutex_destroy(&cv.cv_mtx);
202
203 return rc;
204 }
205
206 int
207 splat_condvar_test34_thread(void *arg)
208 {
209 condvar_thr_t *ct = (condvar_thr_t *)arg;
210 condvar_priv_t *cv = ct->ct_cvp;
211 clock_t rc;
212
213 ASSERT(cv->cv_magic == SPLAT_CONDVAR_TEST_MAGIC);
214
215 mutex_enter(&cv->cv_mtx);
216 splat_vprint(cv->cv_file, ct->ct_name,
217 "%s thread sleeping with %d waiters\n",
218 ct->ct_thread->comm, atomic_read(&cv->cv_condvar.cv_waiters));
219
220 /* Sleep no longer than 3 seconds, for this test we should
221 * actually never sleep that long without being woken up. */
222 rc = cv_timedwait(&cv->cv_condvar, &cv->cv_mtx, lbolt + HZ * 3);
223 if (rc == -1) {
224 ct->ct_rc = -ETIMEDOUT;
225 splat_vprint(cv->cv_file, ct->ct_name, "%s thread timed out, "
226 "should have been woken\n", ct->ct_thread->comm);
227 } else {
228 splat_vprint(cv->cv_file, ct->ct_name,
229 "%s thread woken %d waiters remain\n",
230 ct->ct_thread->comm,
231 atomic_read(&cv->cv_condvar.cv_waiters));
232 }
233
234 mutex_exit(&cv->cv_mtx);
235
236 return 0;
237 }
238
239 static int
240 splat_condvar_test3(struct file *file, void *arg)
241 {
242 int i, count = 0, rc = 0;
243 condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT];
244 condvar_priv_t cv;
245
246 cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC;
247 cv.cv_file = file;
248 mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL);
249 cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL);
250
251 /* Create some threads, the exact number isn't important just as
252 * long as we know how many we managed to create and should expect. */
253 for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
254 ct[i].ct_cvp = &cv;
255 ct[i].ct_name = SPLAT_CONDVAR_TEST3_NAME;
256 ct[i].ct_rc = 0;
257 ct[i].ct_thread = kthread_create(splat_condvar_test34_thread,
258 &ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i);
259
260 if (!IS_ERR(ct[i].ct_thread)) {
261 wake_up_process(ct[i].ct_thread);
262 count++;
263 }
264 }
265
266 /* Wait until all threads are waiting on the condition variable */
267 while (atomic_read(&cv.cv_condvar.cv_waiters) != count)
268 schedule();
269
270 /* Wake a single thread at a time, wait until it exits */
271 for (i = 1; i <= count; i++) {
272 cv_signal(&cv.cv_condvar);
273
274 while (atomic_read(&cv.cv_condvar.cv_waiters) > (count - i))
275 schedule();
276
277 /* Correct behavior 1 thread woken */
278 if (atomic_read(&cv.cv_condvar.cv_waiters) == (count - i))
279 continue;
280
281 splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Attempted to "
282 "wake %d thread but work %d threads woke\n",
283 1, count - atomic_read(&cv.cv_condvar.cv_waiters));
284 rc = -EINVAL;
285 break;
286 }
287
288 /* Validate no waiting thread timed out early */
289 for (i = 0; i < count; i++)
290 if (ct[i].ct_rc)
291 rc = ct[i].ct_rc;
292
293 if (!rc)
294 splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Correctly woke "
295 "%d sleeping threads %d at a time\n", count, 1);
296
297 /* Wait until that last nutex is dropped */
298 while (mutex_owner(&cv.cv_mtx))
299 schedule();
300
301 /* Wake everything for the failure case */
302 cv_broadcast(&cv.cv_condvar);
303 cv_destroy(&cv.cv_condvar);
304 mutex_destroy(&cv.cv_mtx);
305
306 return rc;
307 }
308
309 static int
310 splat_condvar_test4(struct file *file, void *arg)
311 {
312 int i, count = 0, rc = 0;
313 condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT];
314 condvar_priv_t cv;
315
316 cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC;
317 cv.cv_file = file;
318 mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL);
319 cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL);
320
321 /* Create some threads, the exact number isn't important just as
322 * long as we know how many we managed to create and should expect. */
323 for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
324 ct[i].ct_cvp = &cv;
325 ct[i].ct_name = SPLAT_CONDVAR_TEST3_NAME;
326 ct[i].ct_rc = 0;
327 ct[i].ct_thread = kthread_create(splat_condvar_test34_thread,
328 &ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i);
329
330 if (!IS_ERR(ct[i].ct_thread)) {
331 wake_up_process(ct[i].ct_thread);
332 count++;
333 }
334 }
335
336 /* Wait until all threads are waiting on the condition variable */
337 while (atomic_read(&cv.cv_condvar.cv_waiters) != count)
338 schedule();
339
340 /* Wake a single thread at a time, wait until it exits */
341 for (i = 1; i <= count; i++) {
342 cv_signal(&cv.cv_condvar);
343
344 while (atomic_read(&cv.cv_condvar.cv_waiters) > (count - i))
345 schedule();
346
347 /* Correct behavior 1 thread woken */
348 if (atomic_read(&cv.cv_condvar.cv_waiters) == (count - i))
349 continue;
350
351 splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Attempted to "
352 "wake %d thread but work %d threads woke\n",
353 1, count - atomic_read(&cv.cv_condvar.cv_waiters));
354 rc = -EINVAL;
355 break;
356 }
357
358 /* Validate no waiting thread timed out early */
359 for (i = 0; i < count; i++)
360 if (ct[i].ct_rc)
361 rc = ct[i].ct_rc;
362
363 if (!rc)
364 splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Correctly woke "
365 "%d sleeping threads %d at a time\n", count, 1);
366
367 /* Wait until that last nutex is dropped */
368 while (mutex_owner(&cv.cv_mtx))
369 schedule();
370
371 /* Wake everything for the failure case */
372 cv_broadcast(&cv.cv_condvar);
373 cv_destroy(&cv.cv_condvar);
374 mutex_destroy(&cv.cv_mtx);
375
376 return rc;
377 }
378
379 static int
380 splat_condvar_test5(struct file *file, void *arg)
381 {
382 kcondvar_t condvar;
383 kmutex_t mtx;
384 clock_t time_left, time_before, time_after, time_delta;
385 int64_t whole_delta;
386 int32_t remain_delta;
387 int rc = 0;
388
389 mutex_init(&mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL);
390 cv_init(&condvar, NULL, CV_DEFAULT, NULL);
391
392 splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME, "Thread going to sleep for "
393 "%d second and expecting to be woken by timeout\n", 1);
394
395 /* Allow a 1 second timeout, plenty long to validate correctness. */
396 time_before = lbolt;
397 mutex_enter(&mtx);
398 time_left = cv_timedwait(&condvar, &mtx, lbolt + HZ);
399 mutex_exit(&mtx);
400 time_after = lbolt;
401 time_delta = time_after - time_before; /* XXX - Handle jiffie wrap */
402 whole_delta = time_delta;
403 remain_delta = do_div(whole_delta, HZ);
404
405 if (time_left == -1) {
406 if (time_delta >= HZ) {
407 splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME,
408 "Thread correctly timed out and was asleep "
409 "for %d.%d seconds (%d second min)\n",
410 (int)whole_delta, remain_delta, 1);
411 } else {
412 splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME,
413 "Thread correctly timed out but was only "
414 "asleep for %d.%d seconds (%d second "
415 "min)\n", (int)whole_delta, remain_delta, 1);
416 rc = -ETIMEDOUT;
417 }
418 } else {
419 splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME,
420 "Thread exited after only %d.%d seconds, it "
421 "did not hit the %d second timeout\n",
422 (int)whole_delta, remain_delta, 1);
423 rc = -ETIMEDOUT;
424 }
425
426 cv_destroy(&condvar);
427 mutex_destroy(&mtx);
428
429 return rc;
430 }
431
432 splat_subsystem_t *
433 splat_condvar_init(void)
434 {
435 splat_subsystem_t *sub;
436
437 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
438 if (sub == NULL)
439 return NULL;
440
441 memset(sub, 0, sizeof(*sub));
442 strncpy(sub->desc.name, SPLAT_CONDVAR_NAME, SPLAT_NAME_SIZE);
443 strncpy(sub->desc.desc, SPLAT_CONDVAR_DESC, SPLAT_DESC_SIZE);
444 INIT_LIST_HEAD(&sub->subsystem_list);
445 INIT_LIST_HEAD(&sub->test_list);
446 spin_lock_init(&sub->test_lock);
447 sub->desc.id = SPLAT_SUBSYSTEM_CONDVAR;
448
449 SPLAT_TEST_INIT(sub, SPLAT_CONDVAR_TEST1_NAME, SPLAT_CONDVAR_TEST1_DESC,
450 SPLAT_CONDVAR_TEST1_ID, splat_condvar_test1);
451 SPLAT_TEST_INIT(sub, SPLAT_CONDVAR_TEST2_NAME, SPLAT_CONDVAR_TEST2_DESC,
452 SPLAT_CONDVAR_TEST2_ID, splat_condvar_test2);
453 SPLAT_TEST_INIT(sub, SPLAT_CONDVAR_TEST3_NAME, SPLAT_CONDVAR_TEST3_DESC,
454 SPLAT_CONDVAR_TEST3_ID, splat_condvar_test3);
455 SPLAT_TEST_INIT(sub, SPLAT_CONDVAR_TEST4_NAME, SPLAT_CONDVAR_TEST4_DESC,
456 SPLAT_CONDVAR_TEST4_ID, splat_condvar_test4);
457 SPLAT_TEST_INIT(sub, SPLAT_CONDVAR_TEST5_NAME, SPLAT_CONDVAR_TEST5_DESC,
458 SPLAT_CONDVAR_TEST5_ID, splat_condvar_test5);
459
460 return sub;
461 }
462
463 void
464 splat_condvar_fini(splat_subsystem_t *sub)
465 {
466 ASSERT(sub);
467 SPLAT_TEST_FINI(sub, SPLAT_CONDVAR_TEST5_ID);
468 SPLAT_TEST_FINI(sub, SPLAT_CONDVAR_TEST4_ID);
469 SPLAT_TEST_FINI(sub, SPLAT_CONDVAR_TEST3_ID);
470 SPLAT_TEST_FINI(sub, SPLAT_CONDVAR_TEST2_ID);
471 SPLAT_TEST_FINI(sub, SPLAT_CONDVAR_TEST1_ID);
472
473 kfree(sub);
474 }
475
476 int
477 splat_condvar_id(void) {
478 return SPLAT_SUBSYSTEM_CONDVAR;
479 }