Remove obsolete chibios block devices
[lwext4.git] / blockdev / test_lwext4.c
1 /*
2  * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * - The name of the author may not be used to endorse or promote products
15  *   derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <ext4.h>
30
31 #include <stdio.h>
32 #include <inttypes.h>
33 #include <string.h>
34
35 #include "test_lwext4.h"
36
37 /**@brief   Block device handle.*/
38 static struct ext4_blockdev *bd;
39
40 /**@brief   Block cache handle.*/
41 static struct ext4_bcache *bc;
42
43 static char *entry_to_str(uint8_t type)
44 {
45         switch (type) {
46         case EXT4_DIRENTRY_UNKNOWN:
47                 return "[unk] ";
48         case EXT4_DIRENTRY_REG_FILE:
49                 return "[fil] ";
50         case EXT4_DIRENTRY_DIR:
51                 return "[dir] ";
52         case EXT4_DIRENTRY_CHRDEV:
53                 return "[cha] ";
54         case EXT4_DIRENTRY_BLKDEV:
55                 return "[blk] ";
56         case EXT4_DIRENTRY_FIFO:
57                 return "[fif] ";
58         case EXT4_DIRENTRY_SOCK:
59                 return "[soc] ";
60         case EXT4_DIRENTRY_SYMLINK:
61                 return "[sym] ";
62         default:
63                 break;
64         }
65         return "[???]";
66 }
67
68 static long int get_ms(void) { return tim_get_ms(); }
69
70 static void printf_io_timings(long int diff)
71 {
72         const struct ext4_io_stats *stats = io_timings_get(diff);
73         if (!stats)
74                 return;
75
76         printf("io_timings:\n");
77         printf("  io_read: %.3f%%\n", (double)stats->io_read);
78         printf("  io_write: %.3f%%\n", (double)stats->io_write);
79         printf("  io_cpu: %.3f%%\n", (double)stats->cpu);
80 }
81
82 void test_lwext4_dir_ls(const char *path)
83 {
84         char sss[255];
85         ext4_dir d;
86         const ext4_direntry *de;
87
88         printf("ls %s\n", path);
89
90         ext4_dir_open(&d, path);
91         de = ext4_dir_entry_next(&d);
92
93         while (de) {
94                 memcpy(sss, de->name, de->name_length);
95                 sss[de->name_length] = 0;
96                 printf("  %s%s\n", entry_to_str(de->inode_type), sss);
97                 de = ext4_dir_entry_next(&d);
98         }
99         ext4_dir_close(&d);
100 }
101
102 void test_lwext4_mp_stats(void)
103 {
104         struct ext4_mount_stats stats;
105         ext4_mount_point_stats("/mp/", &stats);
106
107         printf("********************\n");
108         printf("ext4_mount_point_stats\n");
109         printf("inodes_count = %" PRIu32 "\n", stats.inodes_count);
110         printf("free_inodes_count = %" PRIu32 "\n", stats.free_inodes_count);
111         printf("blocks_count = %" PRIu32 "\n", (uint32_t)stats.blocks_count);
112         printf("free_blocks_count = %" PRIu32 "\n",
113                (uint32_t)stats.free_blocks_count);
114         printf("block_size = %" PRIu32 "\n", stats.block_size);
115         printf("block_group_count = %" PRIu32 "\n", stats.block_group_count);
116         printf("blocks_per_group= %" PRIu32 "\n", stats.blocks_per_group);
117         printf("inodes_per_group = %" PRIu32 "\n", stats.inodes_per_group);
118         printf("volume_name = %s\n", stats.volume_name);
119         printf("********************\n");
120 }
121
122 void test_lwext4_block_stats(void)
123 {
124         if (!bd)
125                 return;
126
127         printf("********************\n");
128         printf("ext4 blockdev stats\n");
129         printf("bdev->bread_ctr = %" PRIu32 "\n", bd->bread_ctr);
130         printf("bdev->bwrite_ctr = %" PRIu32 "\n", bd->bwrite_ctr);
131
132         printf("bcache->ref_blocks = %" PRIu32 "\n", bc->ref_blocks);
133         printf("bcache->max_ref_blocks = %" PRIu32 "\n", bc->max_ref_blocks);
134         printf("bcache->lru_ctr = %" PRIu32 "\n", bc->lru_ctr);
135
136         printf("\n");
137
138         uint32_t i;
139         for (i = 0; i < bc->cnt; ++i) {
140                 printf("bcache->refctr[%" PRIu32 "]= %" PRIu32 "\n", i,
141                        bc->refctr[i]);
142         }
143
144         printf("\n");
145         for (i = 0; i < bc->cnt; ++i) {
146                 printf("bcache->lru_id[%" PRIu32 "] = %" PRIu32 "\n", i,
147                        bc->lru_id[i]);
148         }
149
150         printf("\n");
151         for (i = 0; i < bc->cnt; ++i) {
152                 printf("bcache->free_delay[%" PRIu32 "] = %d\n", i,
153                        bc->free_delay[i]);
154         }
155
156         printf("\n");
157         for (i = 0; i < bc->cnt; ++i) {
158                 printf("bcache->lba[%" PRIu32 "] = %" PRIu32 "\n", i,
159                        (uint32_t)bc->lba[i]);
160         }
161
162         printf("********************\n");
163 }
164
165 bool test_lwext4_dir_test(int len)
166 {
167         ext4_file f;
168         int r;
169         int i;
170         char path[64];
171         long int diff;
172         long int stop;
173         long int start;
174
175         printf("test_lwext4_dir_test: %d\n", len);
176         io_timings_clear();
177         start = get_ms();
178
179         printf("directory create: /mp/dir1\n");
180         r = ext4_dir_mk("/mp/dir1");
181         if (r != EOK) {
182                 printf("ext4_dir_mk: rc = %d\n", r);
183                 return false;
184         }
185
186         printf("add files to: /mp/dir1\n");
187         for (i = 0; i < len; ++i) {
188                 sprintf(path, "/mp/dir1/f%d", i);
189                 r = ext4_fopen(&f, path, "wb");
190                 if (r != EOK) {
191                         printf("ext4_fopen: rc = %d\n", r);
192                         return false;
193                 }
194         }
195
196         stop = get_ms();
197         diff = stop - start;
198         test_lwext4_dir_ls("/mp/dir1");
199         printf("test_lwext4_dir_test: time: %d ms\n", (int)diff);
200         printf("test_lwext4_dir_test: av: %d ms/entry\n", (int)diff / (len + 1));
201         printf_io_timings(diff);
202         return true;
203 }
204
205 static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
206 {
207         size_t i;
208         for (i = 0; i < len; ++i) {
209                 if (b[i] != c)
210                         return c - b[i];
211         }
212
213         return 0;
214 }
215
216 bool test_lwext4_file_test(uint8_t *rw_buff, uint32_t rw_size, uint32_t rw_count)
217 {
218         int r;
219         size_t size;
220         uint32_t i;
221         long int start;
222         long int stop;
223         long int diff;
224         uint32_t kbps;
225         uint64_t size_bytes;
226
227         ext4_file f;
228
229         printf("file_test:\n");
230         printf("  rw size: %" PRIu32 "\n", rw_size);
231         printf("  rw count: %" PRIu32 "\n", rw_count);
232
233         /*Add hello world file.*/
234         r = ext4_fopen(&f, "/mp/hello.txt", "wb");
235         r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
236         r = ext4_fclose(&f);
237
238         io_timings_clear();
239         start = get_ms();
240         r = ext4_fopen(&f, "/mp/test1", "wb");
241         if (r != EOK) {
242                 printf("ext4_fopen ERROR = %d\n", r);
243                 return false;
244         }
245
246         printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_size,
247                rw_count);
248         for (i = 0; i < rw_count; ++i) {
249
250                 memset(rw_buff, i % 10 + '0', rw_size);
251
252                 r = ext4_fwrite(&f, rw_buff, rw_size, &size);
253
254                 if ((r != EOK) || (size != rw_size))
255                         break;
256         }
257
258         if (i != rw_count) {
259                 printf("  file_test: rw_count = %" PRIu32 "\n", i);
260                 return false;
261         }
262
263         stop = get_ms();
264         diff = stop - start;
265         size_bytes = rw_size * rw_count;
266         size_bytes = (size_bytes * 1000) / 1024;
267         kbps = (size_bytes) / (diff + 1);
268         printf("  write time: %d ms\n", (int)diff);
269         printf("  write speed: %" PRIu32 " KB/s\n", kbps);
270         printf_io_timings(diff);
271         r = ext4_fclose(&f);
272
273         io_timings_clear();
274         start = get_ms();
275         r = ext4_fopen(&f, "/mp/test1", "r+");
276         if (r != EOK) {
277                 printf("ext4_fopen ERROR = %d\n", r);
278                 return false;
279         }
280
281         printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_size, rw_count);
282
283         for (i = 0; i < rw_count; ++i) {
284                 r = ext4_fread(&f, rw_buff, rw_size, &size);
285
286                 if ((r != EOK) || (size != rw_size))
287                         break;
288
289                 if (verify_buf(rw_buff, rw_size, i % 10 + '0'))
290                         break;
291         }
292
293         if (i != rw_count) {
294                 printf("  file_test: rw_count = %" PRIu32 "\n", i);
295                 return false;
296         }
297
298         stop = get_ms();
299         diff = stop - start;
300         size_bytes = rw_size * rw_count;
301         size_bytes = (size_bytes * 1000) / 1024;
302         kbps = (size_bytes) / (diff + 1);
303         printf("  read time: %d ms\n", (int)diff);
304         printf("  read speed: %d KB/s\n", (int)kbps);
305         printf_io_timings(diff);
306
307         r = ext4_fclose(&f);
308         return true;
309 }
310 void test_lwext4_cleanup(void)
311 {
312         long int start;
313         long int stop;
314         long int diff;
315
316         printf("\ncleanup:\n");
317         ext4_fremove("/mp/hello.txt");
318
319         printf("remove /mp/test1\n");
320         ext4_fremove("/mp/test1");
321
322         printf("remove /mp/dir1\n");
323         io_timings_clear();
324         start = get_ms();
325         ext4_dir_rm("/mp/dir1");
326         stop = get_ms();
327         diff = stop - start;
328         printf("cleanup: time: %d ms\n", (int)diff);
329         printf_io_timings(diff);
330 }
331
332 bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
333 {
334         int r;
335
336         bc = bcache;
337         bd = bdev;
338
339         if (!bd) {
340                 printf("test_lwext4_mount: no block device\n");
341                 return false;
342         }
343
344         ext4_dmask_set(DEBUG_ALL);
345
346         r = ext4_device_register(bd, bc ? bc : 0, "ext4_fs");
347         if (r != EOK) {
348                 printf("ext4_device_register: rc = %d\n", r);
349                 return false;
350         }
351
352         r = ext4_mount("ext4_fs", "/mp/");
353         if (r != EOK) {
354                 printf("ext4_mount: rc = %d\n", r);
355                 return false;
356         }
357
358         ext4_cache_write_back("/mp/", 1);
359         return true;
360 }
361
362 bool test_lwext4_umount(void)
363 {
364         ext4_cache_write_back("/mp/", 0);
365         int r = ext4_umount("/mp/");
366         if (r != EOK) {
367                 printf("ext4_umount: fail %d", r);
368                 return false;
369         }
370         return true;
371 }