1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup test_bdbuf |
---|
5 | * |
---|
6 | * @brief Bdbuf test. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2009, 2018 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Dornierstr. 4 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #ifdef HAVE_CONFIG_H |
---|
24 | #include "config.h" |
---|
25 | #endif |
---|
26 | |
---|
27 | #include <sys/stat.h> |
---|
28 | #include <errno.h> |
---|
29 | #include <fcntl.h> |
---|
30 | #include <stdarg.h> |
---|
31 | |
---|
32 | #include <rtems.h> |
---|
33 | #include <rtems/bspIo.h> |
---|
34 | #include <rtems/bdbuf.h> |
---|
35 | #include <rtems/blkdev.h> |
---|
36 | |
---|
37 | #include <tmacros.h> |
---|
38 | |
---|
39 | const char rtems_test_name[] = "BLOCK 5"; |
---|
40 | |
---|
41 | /* forward declarations to avoid warnings */ |
---|
42 | static rtems_task Init(rtems_task_argument argument); |
---|
43 | |
---|
44 | #define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL) |
---|
45 | |
---|
46 | #define PRIORITY_INIT 1 |
---|
47 | |
---|
48 | #define PRIORITY_HIGH 2 |
---|
49 | |
---|
50 | #define PRIORITY_LOW_ALT 3 |
---|
51 | |
---|
52 | #define PRIORITY_SWAPOUT 4 |
---|
53 | |
---|
54 | #define PRIORITY_LOW 5 |
---|
55 | |
---|
56 | #define PRIORITY_RESUME 6 |
---|
57 | |
---|
58 | #define BLOCK_SIZE_A 1 |
---|
59 | |
---|
60 | #define BLOCK_COUNT_A 2 |
---|
61 | |
---|
62 | #define BLOCK_SIZE_B 2 |
---|
63 | |
---|
64 | #define BLOCK_COUNT_B 1 |
---|
65 | |
---|
66 | /* In case of trouble change this to 1 or 2 for more output */ |
---|
67 | static unsigned output_level = 0; |
---|
68 | |
---|
69 | static rtems_disk_device *dd_a; |
---|
70 | |
---|
71 | static rtems_disk_device *dd_b; |
---|
72 | |
---|
73 | static rtems_id task_id_init; |
---|
74 | |
---|
75 | static rtems_id task_id_low; |
---|
76 | |
---|
77 | static rtems_id task_id_high; |
---|
78 | |
---|
79 | static rtems_id task_id_resume; |
---|
80 | |
---|
81 | static volatile bool finish_low; |
---|
82 | |
---|
83 | static volatile bool finish_high; |
---|
84 | |
---|
85 | static volatile enum resume_style { |
---|
86 | RESUME_IMMEDIATE, |
---|
87 | RESUME_FINISH |
---|
88 | } resume; |
---|
89 | |
---|
90 | static volatile enum trig_style { |
---|
91 | SUSP = 0, |
---|
92 | CONT |
---|
93 | } trig; |
---|
94 | |
---|
95 | static volatile enum get_type { |
---|
96 | GET = 0, |
---|
97 | READ |
---|
98 | } trig_get, low_get, high_get; |
---|
99 | |
---|
100 | static volatile enum blk_kind { |
---|
101 | BLK_A0 = 0, |
---|
102 | BLK_A1, |
---|
103 | BLK_B0 |
---|
104 | } trig_blk, low_blk, high_blk; |
---|
105 | |
---|
106 | static volatile enum rel_type { |
---|
107 | REL = 0, |
---|
108 | REL_MOD, |
---|
109 | SYNC |
---|
110 | } trig_rel, low_rel, high_rel; |
---|
111 | |
---|
112 | static const char trig_style_desc [] = { |
---|
113 | 'S', |
---|
114 | 'C' |
---|
115 | }; |
---|
116 | |
---|
117 | static const char get_type_desc [] = { |
---|
118 | 'G', |
---|
119 | 'R' |
---|
120 | }; |
---|
121 | |
---|
122 | static const char *blk_kind_desc [] = { |
---|
123 | "A0", |
---|
124 | "A1", |
---|
125 | "B0" |
---|
126 | }; |
---|
127 | |
---|
128 | static const char rel_type_desc [] = { |
---|
129 | 'R', |
---|
130 | 'M', |
---|
131 | 'S' |
---|
132 | }; |
---|
133 | |
---|
134 | static void print(unsigned level, const char *fmt, ...) |
---|
135 | { |
---|
136 | if (level <= output_level) { |
---|
137 | va_list ap; |
---|
138 | |
---|
139 | va_start(ap, fmt); |
---|
140 | vprintk(fmt, ap); |
---|
141 | va_end(ap); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | static void set_task_prio(rtems_id task, rtems_task_priority prio) |
---|
146 | { |
---|
147 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
148 | rtems_task_priority cur = 0; |
---|
149 | |
---|
150 | sc = rtems_task_set_priority(task, prio, &cur); |
---|
151 | ASSERT_SC(sc); |
---|
152 | } |
---|
153 | |
---|
154 | static rtems_bdbuf_buffer *get(enum get_type type, enum blk_kind kind) |
---|
155 | { |
---|
156 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
157 | rtems_bdbuf_buffer *bd = NULL; |
---|
158 | rtems_blkdev_bnum blk_index = 0; |
---|
159 | rtems_status_code (*get_bd)( |
---|
160 | rtems_disk_device *, |
---|
161 | rtems_blkdev_bnum, |
---|
162 | rtems_bdbuf_buffer ** |
---|
163 | ) = NULL; |
---|
164 | rtems_disk_device *dd = NULL; |
---|
165 | size_t bds_per_group = 0; |
---|
166 | |
---|
167 | switch (kind) { |
---|
168 | case BLK_A0: |
---|
169 | dd = dd_a; |
---|
170 | blk_index = 0; |
---|
171 | bds_per_group = 2; |
---|
172 | break; |
---|
173 | case BLK_A1: |
---|
174 | dd = dd_a; |
---|
175 | blk_index = 1; |
---|
176 | bds_per_group = 2; |
---|
177 | break; |
---|
178 | case BLK_B0: |
---|
179 | dd = dd_b; |
---|
180 | blk_index = 0; |
---|
181 | bds_per_group = 1; |
---|
182 | break; |
---|
183 | default: |
---|
184 | rtems_test_assert(false); |
---|
185 | break; |
---|
186 | } |
---|
187 | |
---|
188 | switch (type) { |
---|
189 | case GET: |
---|
190 | get_bd = rtems_bdbuf_get; |
---|
191 | break; |
---|
192 | case READ: |
---|
193 | get_bd = rtems_bdbuf_read; |
---|
194 | break; |
---|
195 | default: |
---|
196 | rtems_test_assert(false); |
---|
197 | break; |
---|
198 | } |
---|
199 | |
---|
200 | sc = (*get_bd)(dd, blk_index, &bd); |
---|
201 | rtems_test_assert( |
---|
202 | sc == RTEMS_SUCCESSFUL |
---|
203 | && bd->dd == dd |
---|
204 | && bd->block == blk_index |
---|
205 | && bd->group->bds_per_group == bds_per_group |
---|
206 | ); |
---|
207 | |
---|
208 | return bd; |
---|
209 | } |
---|
210 | |
---|
211 | static void rel(rtems_bdbuf_buffer *bd, enum rel_type type) |
---|
212 | { |
---|
213 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
214 | rtems_status_code (*rel_bd)(rtems_bdbuf_buffer *) = NULL; |
---|
215 | |
---|
216 | switch (type) { |
---|
217 | case REL: |
---|
218 | rel_bd = rtems_bdbuf_release; |
---|
219 | break; |
---|
220 | case REL_MOD: |
---|
221 | rel_bd = rtems_bdbuf_release_modified; |
---|
222 | break; |
---|
223 | case SYNC: |
---|
224 | rel_bd = rtems_bdbuf_sync; |
---|
225 | break; |
---|
226 | default: |
---|
227 | rtems_test_assert(false); |
---|
228 | break; |
---|
229 | } |
---|
230 | |
---|
231 | sc = (*rel_bd)(bd); |
---|
232 | ASSERT_SC(sc); |
---|
233 | } |
---|
234 | |
---|
235 | static void task_low(rtems_task_argument arg) |
---|
236 | { |
---|
237 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
238 | |
---|
239 | while (true) { |
---|
240 | print(2, "LG\n"); |
---|
241 | rtems_bdbuf_buffer *bd = get(low_get, low_blk); |
---|
242 | |
---|
243 | print(2, "LR\n"); |
---|
244 | rel(bd, low_rel); |
---|
245 | |
---|
246 | finish_low = true; |
---|
247 | |
---|
248 | sc = rtems_task_suspend(RTEMS_SELF); |
---|
249 | ASSERT_SC(sc); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | static void task_high(rtems_task_argument arg) |
---|
254 | { |
---|
255 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
256 | |
---|
257 | while (true) { |
---|
258 | print(2, "HG\n"); |
---|
259 | rtems_bdbuf_buffer *bd = get(high_get, high_blk); |
---|
260 | |
---|
261 | print(2, "HR\n"); |
---|
262 | rel(bd, high_rel); |
---|
263 | |
---|
264 | finish_high = true; |
---|
265 | |
---|
266 | sc = rtems_task_suspend(RTEMS_SELF); |
---|
267 | ASSERT_SC(sc); |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | static void task_resume(rtems_task_argument arg) |
---|
272 | { |
---|
273 | while (true) { |
---|
274 | bool do_resume = false; |
---|
275 | |
---|
276 | switch (resume) { |
---|
277 | case RESUME_IMMEDIATE: |
---|
278 | print(2, "RI\n"); |
---|
279 | do_resume = true; |
---|
280 | break; |
---|
281 | case RESUME_FINISH: |
---|
282 | print(2, "RF\n"); |
---|
283 | do_resume = finish_low && finish_high; |
---|
284 | break; |
---|
285 | default: |
---|
286 | rtems_test_assert(false); |
---|
287 | break; |
---|
288 | } |
---|
289 | |
---|
290 | if (do_resume) { |
---|
291 | rtems_task_resume(task_id_init); |
---|
292 | } |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | static void execute_test(unsigned i) |
---|
297 | { |
---|
298 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
299 | rtems_bdbuf_buffer *bd = NULL; |
---|
300 | bool suspend = false; |
---|
301 | |
---|
302 | print( |
---|
303 | 1, |
---|
304 | "[%05u]T(%c,%c,%s,%c)L(%c,%s,%c)H(%c,%s,%c)\n", |
---|
305 | i, |
---|
306 | trig_style_desc [trig], |
---|
307 | get_type_desc [trig_get], |
---|
308 | blk_kind_desc [trig_blk], |
---|
309 | rel_type_desc [trig_rel], |
---|
310 | get_type_desc [low_get], |
---|
311 | blk_kind_desc [low_blk], |
---|
312 | rel_type_desc [low_rel], |
---|
313 | get_type_desc [high_get], |
---|
314 | blk_kind_desc [high_blk], |
---|
315 | rel_type_desc [high_rel] |
---|
316 | ); |
---|
317 | |
---|
318 | set_task_prio(task_id_low, PRIORITY_LOW); |
---|
319 | |
---|
320 | finish_low = false; |
---|
321 | finish_high = false; |
---|
322 | |
---|
323 | sc = rtems_task_resume(task_id_low); |
---|
324 | ASSERT_SC(sc); |
---|
325 | |
---|
326 | sc = rtems_task_resume(task_id_high); |
---|
327 | ASSERT_SC(sc); |
---|
328 | |
---|
329 | sc = rtems_bdbuf_get(dd_b, 0, &bd); |
---|
330 | rtems_test_assert(sc == RTEMS_SUCCESSFUL && bd->dd == dd_b && bd->block == 0); |
---|
331 | |
---|
332 | sc = rtems_bdbuf_release(bd); |
---|
333 | ASSERT_SC(sc); |
---|
334 | |
---|
335 | switch (trig) { |
---|
336 | case SUSP: |
---|
337 | suspend = true; |
---|
338 | break; |
---|
339 | case CONT: |
---|
340 | suspend = false; |
---|
341 | break; |
---|
342 | default: |
---|
343 | rtems_test_assert(false); |
---|
344 | break; |
---|
345 | } |
---|
346 | |
---|
347 | print(2, "TG\n"); |
---|
348 | bd = get(trig_get, trig_blk); |
---|
349 | |
---|
350 | if (suspend) { |
---|
351 | print(2, "S\n"); |
---|
352 | resume = RESUME_IMMEDIATE; |
---|
353 | sc = rtems_task_suspend(RTEMS_SELF); |
---|
354 | ASSERT_SC(sc); |
---|
355 | } |
---|
356 | resume = RESUME_FINISH; |
---|
357 | |
---|
358 | print(2, "TR\n"); |
---|
359 | rel(bd, trig_rel); |
---|
360 | |
---|
361 | sc = rtems_task_suspend(RTEMS_SELF); |
---|
362 | ASSERT_SC(sc); |
---|
363 | |
---|
364 | print(2, "F\n"); |
---|
365 | |
---|
366 | sc = rtems_bdbuf_syncdev(dd_a); |
---|
367 | ASSERT_SC(sc); |
---|
368 | |
---|
369 | sc = rtems_bdbuf_syncdev(dd_b); |
---|
370 | ASSERT_SC(sc); |
---|
371 | } |
---|
372 | |
---|
373 | static int disk_ioctl(rtems_disk_device *dd, uint32_t req, void *argp) |
---|
374 | { |
---|
375 | if (req == RTEMS_BLKIO_REQUEST) { |
---|
376 | rtems_blkdev_request *r = argp; |
---|
377 | |
---|
378 | set_task_prio(task_id_low, PRIORITY_LOW_ALT); |
---|
379 | |
---|
380 | switch (r->req) { |
---|
381 | case RTEMS_BLKDEV_REQ_READ: |
---|
382 | case RTEMS_BLKDEV_REQ_WRITE: |
---|
383 | rtems_blkdev_request_done(r, RTEMS_SUCCESSFUL); |
---|
384 | return 0; |
---|
385 | default: |
---|
386 | errno = EINVAL; |
---|
387 | return -1; |
---|
388 | } |
---|
389 | } else { |
---|
390 | return rtems_blkdev_ioctl(dd, req, argp); |
---|
391 | } |
---|
392 | } |
---|
393 | |
---|
394 | static void disk_register( |
---|
395 | const char *dev, |
---|
396 | uint32_t block_size, |
---|
397 | rtems_blkdev_bnum block_count, |
---|
398 | rtems_disk_device **dd_ptr |
---|
399 | ) |
---|
400 | { |
---|
401 | rtems_status_code sc; |
---|
402 | int rv; |
---|
403 | int fd; |
---|
404 | |
---|
405 | sc = rtems_blkdev_create( |
---|
406 | dev, |
---|
407 | block_size, |
---|
408 | block_count, |
---|
409 | disk_ioctl, |
---|
410 | NULL |
---|
411 | ); |
---|
412 | ASSERT_SC(sc); |
---|
413 | |
---|
414 | fd = open(dev, O_RDWR); |
---|
415 | rtems_test_assert(fd >= 0); |
---|
416 | |
---|
417 | rv = rtems_disk_fd_get_disk_device(fd, dd_ptr); |
---|
418 | rtems_test_assert(rv == 0); |
---|
419 | |
---|
420 | rv = close(fd); |
---|
421 | rtems_test_assert(rv == 0); |
---|
422 | } |
---|
423 | |
---|
424 | static rtems_task Init(rtems_task_argument argument) |
---|
425 | { |
---|
426 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
427 | unsigned i = 0; |
---|
428 | |
---|
429 | TEST_BEGIN(); |
---|
430 | |
---|
431 | task_id_init = rtems_task_self(); |
---|
432 | |
---|
433 | disk_register("dd_b", BLOCK_SIZE_A, BLOCK_COUNT_A, &dd_a); |
---|
434 | |
---|
435 | disk_register("dd_a", BLOCK_SIZE_B, BLOCK_COUNT_B, &dd_b); |
---|
436 | |
---|
437 | sc = rtems_task_create( |
---|
438 | rtems_build_name(' ', 'L', 'O', 'W'), |
---|
439 | PRIORITY_LOW, |
---|
440 | 0, |
---|
441 | RTEMS_DEFAULT_MODES, |
---|
442 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
443 | &task_id_low |
---|
444 | ); |
---|
445 | ASSERT_SC(sc); |
---|
446 | |
---|
447 | sc = rtems_task_start(task_id_low, task_low, 0); |
---|
448 | ASSERT_SC(sc); |
---|
449 | |
---|
450 | sc = rtems_task_create( |
---|
451 | rtems_build_name('H', 'I', 'G', 'H'), |
---|
452 | PRIORITY_HIGH, |
---|
453 | 0, |
---|
454 | RTEMS_DEFAULT_MODES, |
---|
455 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
456 | &task_id_high |
---|
457 | ); |
---|
458 | ASSERT_SC(sc); |
---|
459 | |
---|
460 | sc = rtems_task_start(task_id_high, task_high, 0); |
---|
461 | ASSERT_SC(sc); |
---|
462 | |
---|
463 | sc = rtems_task_create( |
---|
464 | rtems_build_name('R', 'E', 'S', 'U'), |
---|
465 | PRIORITY_RESUME, |
---|
466 | 0, |
---|
467 | RTEMS_DEFAULT_MODES, |
---|
468 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
469 | &task_id_resume |
---|
470 | ); |
---|
471 | ASSERT_SC(sc); |
---|
472 | |
---|
473 | sc = rtems_task_start(task_id_resume, task_resume, 0); |
---|
474 | ASSERT_SC(sc); |
---|
475 | |
---|
476 | sc = rtems_task_suspend(task_id_low); |
---|
477 | ASSERT_SC(sc); |
---|
478 | |
---|
479 | sc = rtems_task_suspend(task_id_high); |
---|
480 | ASSERT_SC(sc); |
---|
481 | |
---|
482 | for (trig = SUSP; trig <= CONT; ++trig) { |
---|
483 | for (trig_get = GET; trig_get <= READ; ++trig_get) { |
---|
484 | for (low_get = GET; low_get <= READ; ++low_get) { |
---|
485 | for (high_get = GET; high_get <= READ; ++high_get) { |
---|
486 | for (trig_blk = BLK_A0; trig_blk <= BLK_B0; ++trig_blk) { |
---|
487 | for (low_blk = BLK_A0; low_blk <= BLK_B0; ++low_blk) { |
---|
488 | for (high_blk = BLK_A0; high_blk <= BLK_B0; ++high_blk) { |
---|
489 | for (trig_rel = REL; trig_rel <= SYNC; ++trig_rel) { |
---|
490 | for (low_rel = REL; low_rel <= SYNC; ++low_rel) { |
---|
491 | for (high_rel = REL; high_rel <= SYNC; ++high_rel) { |
---|
492 | execute_test(i); |
---|
493 | ++i; |
---|
494 | } |
---|
495 | } |
---|
496 | } |
---|
497 | } |
---|
498 | } |
---|
499 | } |
---|
500 | } |
---|
501 | } |
---|
502 | } |
---|
503 | } |
---|
504 | |
---|
505 | TEST_END(); |
---|
506 | |
---|
507 | exit(0); |
---|
508 | } |
---|
509 | |
---|
510 | #define CONFIGURE_INIT |
---|
511 | |
---|
512 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
513 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
514 | #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK |
---|
515 | |
---|
516 | #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 5 |
---|
517 | |
---|
518 | #define CONFIGURE_MAXIMUM_TASKS 4 |
---|
519 | |
---|
520 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
521 | |
---|
522 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
523 | |
---|
524 | #define CONFIGURE_INIT_TASK_PRIORITY PRIORITY_INIT |
---|
525 | #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_DEFAULT_ATTRIBUTES |
---|
526 | #define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES |
---|
527 | |
---|
528 | #define CONFIGURE_SWAPOUT_TASK_PRIORITY PRIORITY_SWAPOUT |
---|
529 | |
---|
530 | #define CONFIGURE_BDBUF_BUFFER_MIN_SIZE BLOCK_SIZE_A |
---|
531 | #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE BLOCK_SIZE_B |
---|
532 | #define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE BLOCK_SIZE_B |
---|
533 | |
---|
534 | #include <rtems/confdefs.h> |
---|