1 | /* |
---|
2 | * Copyright (c) 2012, 2018 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include "tmacros.h" |
---|
20 | |
---|
21 | #include <sys/stat.h> |
---|
22 | #include <assert.h> |
---|
23 | #include <errno.h> |
---|
24 | #include <fcntl.h> |
---|
25 | #include <string.h> |
---|
26 | #include <unistd.h> |
---|
27 | |
---|
28 | #include <rtems/bdbuf.h> |
---|
29 | |
---|
30 | const char rtems_test_name[] = "BLOCK 13"; |
---|
31 | |
---|
32 | #define BLOCK_COUNT 11 |
---|
33 | #define READ_COUNT 23 |
---|
34 | |
---|
35 | #define DISK_PATH "/disk" |
---|
36 | |
---|
37 | static int block_access_counts [BLOCK_COUNT]; |
---|
38 | |
---|
39 | #define RESET_CACHE (-1) |
---|
40 | |
---|
41 | static const int action_sequence [READ_COUNT] = { |
---|
42 | 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
---|
43 | RESET_CACHE, |
---|
44 | 10, |
---|
45 | RESET_CACHE, |
---|
46 | 9, |
---|
47 | RESET_CACHE, |
---|
48 | 8, |
---|
49 | RESET_CACHE, |
---|
50 | 7, 8, |
---|
51 | RESET_CACHE, |
---|
52 | 6, 7, 9 |
---|
53 | }; |
---|
54 | |
---|
55 | #define UNUSED_LINE { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } |
---|
56 | |
---|
57 | static const int expected_block_access_counts [READ_COUNT] [BLOCK_COUNT] = { |
---|
58 | { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
---|
59 | { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, |
---|
60 | { 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0 }, |
---|
61 | { 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0 }, |
---|
62 | { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, |
---|
63 | { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, |
---|
64 | { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, |
---|
65 | { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
---|
66 | { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
---|
67 | { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
---|
68 | UNUSED_LINE, |
---|
69 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, |
---|
70 | UNUSED_LINE, |
---|
71 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }, |
---|
72 | UNUSED_LINE, |
---|
73 | { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, |
---|
74 | UNUSED_LINE, |
---|
75 | { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, |
---|
76 | { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 }, |
---|
77 | UNUSED_LINE, |
---|
78 | { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, |
---|
79 | { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 }, |
---|
80 | { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 } |
---|
81 | }; |
---|
82 | |
---|
83 | #define NO_TRIGGER RTEMS_DISK_READ_AHEAD_NO_TRIGGER |
---|
84 | |
---|
85 | #define TRIGGER_AFTER_RESET RTEMS_DISK_READ_AHEAD_NO_TRIGGER |
---|
86 | |
---|
87 | static const rtems_blkdev_bnum trigger [READ_COUNT] = { |
---|
88 | 1, 3, 5, 5, 8, 8, 8, NO_TRIGGER, NO_TRIGGER, NO_TRIGGER, |
---|
89 | TRIGGER_AFTER_RESET, |
---|
90 | 11, |
---|
91 | TRIGGER_AFTER_RESET, |
---|
92 | 10, |
---|
93 | TRIGGER_AFTER_RESET, |
---|
94 | 9, |
---|
95 | TRIGGER_AFTER_RESET, |
---|
96 | 8, NO_TRIGGER, |
---|
97 | TRIGGER_AFTER_RESET, |
---|
98 | 7, 9, NO_TRIGGER |
---|
99 | }; |
---|
100 | |
---|
101 | #define NOT_CHANGED_BY_RESET(i) (i) |
---|
102 | |
---|
103 | static const rtems_blkdev_bnum next [READ_COUNT] = { |
---|
104 | 2, 4, 7, 7, 10, 10, 10, 10, 10, 10, |
---|
105 | NOT_CHANGED_BY_RESET(10), |
---|
106 | 12, |
---|
107 | NOT_CHANGED_BY_RESET(12), |
---|
108 | 11, |
---|
109 | NOT_CHANGED_BY_RESET(11), |
---|
110 | 10, |
---|
111 | NOT_CHANGED_BY_RESET(10), |
---|
112 | 9, 9, |
---|
113 | NOT_CHANGED_BY_RESET(9), |
---|
114 | 8, 11, 11 |
---|
115 | }; |
---|
116 | |
---|
117 | static int test_disk_ioctl(rtems_disk_device *dd, uint32_t req, void *arg) |
---|
118 | { |
---|
119 | int rv = 0; |
---|
120 | |
---|
121 | if (req == RTEMS_BLKIO_REQUEST) { |
---|
122 | rtems_blkdev_request *breq = arg; |
---|
123 | rtems_blkdev_sg_buffer *sg = breq->bufs; |
---|
124 | uint32_t i; |
---|
125 | |
---|
126 | rtems_test_assert(breq->req == RTEMS_BLKDEV_REQ_READ); |
---|
127 | |
---|
128 | for (i = 0; i < breq->bufnum; ++i) { |
---|
129 | rtems_blkdev_bnum block = sg [i].block; |
---|
130 | |
---|
131 | rtems_test_assert(block < BLOCK_COUNT); |
---|
132 | |
---|
133 | ++block_access_counts [block]; |
---|
134 | } |
---|
135 | |
---|
136 | rtems_blkdev_request_done(breq, RTEMS_SUCCESSFUL); |
---|
137 | } else { |
---|
138 | rv = rtems_blkdev_ioctl(dd, req, arg); |
---|
139 | } |
---|
140 | |
---|
141 | return rv; |
---|
142 | } |
---|
143 | |
---|
144 | static void test_read_ahead(rtems_disk_device *dd) |
---|
145 | { |
---|
146 | int i; |
---|
147 | |
---|
148 | for (i = 0; i < READ_COUNT; ++i) { |
---|
149 | int action = action_sequence [i]; |
---|
150 | |
---|
151 | if (action != RESET_CACHE) { |
---|
152 | rtems_blkdev_bnum block = (rtems_blkdev_bnum) action; |
---|
153 | rtems_status_code sc; |
---|
154 | rtems_bdbuf_buffer *bd; |
---|
155 | |
---|
156 | printf("%i ", action); |
---|
157 | |
---|
158 | sc = rtems_bdbuf_read(dd, block, &bd); |
---|
159 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
160 | |
---|
161 | sc = rtems_bdbuf_release(bd); |
---|
162 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
163 | |
---|
164 | rtems_test_assert( |
---|
165 | memcmp( |
---|
166 | block_access_counts, |
---|
167 | expected_block_access_counts [i], |
---|
168 | sizeof(block_access_counts) |
---|
169 | ) == 0 |
---|
170 | ); |
---|
171 | } else { |
---|
172 | printf("\nreset\n"); |
---|
173 | |
---|
174 | rtems_bdbuf_purge_dev(dd); |
---|
175 | memset(&block_access_counts, 0, sizeof(block_access_counts)); |
---|
176 | } |
---|
177 | |
---|
178 | rtems_test_assert(trigger [i] == dd->read_ahead.trigger); |
---|
179 | rtems_test_assert(next [i] == dd->read_ahead.next); |
---|
180 | } |
---|
181 | |
---|
182 | printf("\n"); |
---|
183 | } |
---|
184 | |
---|
185 | static void test(void) |
---|
186 | { |
---|
187 | rtems_status_code sc; |
---|
188 | rtems_disk_device *dd; |
---|
189 | int fd; |
---|
190 | int rv; |
---|
191 | |
---|
192 | sc = rtems_blkdev_create( |
---|
193 | DISK_PATH, |
---|
194 | 1, |
---|
195 | BLOCK_COUNT, |
---|
196 | test_disk_ioctl, |
---|
197 | NULL |
---|
198 | ); |
---|
199 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
200 | |
---|
201 | fd = open(DISK_PATH, O_RDWR); |
---|
202 | rtems_test_assert(fd >= 0); |
---|
203 | |
---|
204 | rv = rtems_disk_fd_get_disk_device(fd, &dd); |
---|
205 | rtems_test_assert(rv == 0); |
---|
206 | |
---|
207 | rv = close(fd); |
---|
208 | rtems_test_assert(rv == 0); |
---|
209 | |
---|
210 | test_read_ahead(dd); |
---|
211 | |
---|
212 | rv = unlink(DISK_PATH); |
---|
213 | rtems_test_assert(rv == 0); |
---|
214 | } |
---|
215 | |
---|
216 | static void Init(rtems_task_argument arg) |
---|
217 | { |
---|
218 | TEST_BEGIN(); |
---|
219 | |
---|
220 | test(); |
---|
221 | |
---|
222 | TEST_END(); |
---|
223 | |
---|
224 | rtems_test_exit(0); |
---|
225 | } |
---|
226 | |
---|
227 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
228 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
229 | #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK |
---|
230 | |
---|
231 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4 |
---|
232 | |
---|
233 | #define CONFIGURE_BDBUF_BUFFER_MIN_SIZE 1 |
---|
234 | #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE 1 |
---|
235 | #define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE BLOCK_COUNT |
---|
236 | #define CONFIGURE_BDBUF_MAX_READ_AHEAD_BLOCKS 3 |
---|
237 | #define CONFIGURE_BDBUF_READ_AHEAD_TASK_PRIORITY 1 |
---|
238 | |
---|
239 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
240 | |
---|
241 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
242 | |
---|
243 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
244 | |
---|
245 | #define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES |
---|
246 | #define CONFIGURE_INIT_TASK_PRIORITY 2 |
---|
247 | |
---|
248 | #define CONFIGURE_INIT |
---|
249 | |
---|
250 | #include <rtems/confdefs.h> |
---|