2 * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
35 #include "test_lwext4.h"
37 /**@brief Block device handle.*/
38 static struct ext4_blockdev *bd;
40 /**@brief Block cache handle.*/
41 static struct ext4_bcache *bc;
43 static char *entry_to_str(uint8_t type)
46 case EXT4_DIRENTRY_UNKNOWN:
48 case EXT4_DIRENTRY_REG_FILE:
50 case EXT4_DIRENTRY_DIR:
52 case EXT4_DIRENTRY_CHRDEV:
54 case EXT4_DIRENTRY_BLKDEV:
56 case EXT4_DIRENTRY_FIFO:
58 case EXT4_DIRENTRY_SOCK:
60 case EXT4_DIRENTRY_SYMLINK:
68 static long int get_ms(void) { return tim_get_ms(); }
70 static void printf_io_timings(long int diff)
72 const struct ext4_io_stats *stats = io_timings_get(diff);
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);
82 void test_lwext4_dir_ls(const char *path)
86 const ext4_direntry *de;
88 printf("ls %s\n", path);
90 ext4_dir_open(&d, path);
91 de = ext4_dir_entry_next(&d);
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);
102 void test_lwext4_mp_stats(void)
104 struct ext4_mount_stats stats;
105 ext4_mount_point_stats("/mp/", &stats);
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");
122 void test_lwext4_block_stats(void)
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);
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);
139 for (i = 0; i < bc->cnt; ++i) {
140 printf("bcache->refctr[%" PRIu32 "]= %" PRIu32 "\n", i,
145 for (i = 0; i < bc->cnt; ++i) {
146 printf("bcache->lru_id[%" PRIu32 "] = %" PRIu32 "\n", i,
151 for (i = 0; i < bc->cnt; ++i) {
152 printf("bcache->free_delay[%" PRIu32 "] = %d\n", i,
157 for (i = 0; i < bc->cnt; ++i) {
158 printf("bcache->lba[%" PRIu32 "] = %" PRIu32 "\n", i,
159 (uint32_t)bc->lba[i]);
162 printf("********************\n");
165 bool test_lwext4_dir_test(int len)
175 printf("test_lwext4_dir_test: %d\n", len);
179 printf("directory create: /mp/dir1\n");
180 r = ext4_dir_mk("/mp/dir1");
182 printf("ext4_dir_mk: rc = %d\n", r);
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");
191 printf("ext4_fopen: rc = %d\n", r);
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);
205 static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
208 for (i = 0; i < len; ++i) {
216 bool test_lwext4_file_test(uint8_t *rw_buff, uint32_t rw_size, uint32_t rw_count)
229 printf("file_test:\n");
230 printf(" rw size: %" PRIu32 "\n", rw_size);
231 printf(" rw count: %" PRIu32 "\n", rw_count);
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);
240 r = ext4_fopen(&f, "/mp/test1", "wb");
242 printf("ext4_fopen ERROR = %d\n", r);
246 printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_size,
248 for (i = 0; i < rw_count; ++i) {
250 memset(rw_buff, i % 10 + '0', rw_size);
252 r = ext4_fwrite(&f, rw_buff, rw_size, &size);
254 if ((r != EOK) || (size != rw_size))
259 printf(" file_test: rw_count = %" PRIu32 "\n", i);
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);
275 r = ext4_fopen(&f, "/mp/test1", "r+");
277 printf("ext4_fopen ERROR = %d\n", r);
281 printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_size, rw_count);
283 for (i = 0; i < rw_count; ++i) {
284 r = ext4_fread(&f, rw_buff, rw_size, &size);
286 if ((r != EOK) || (size != rw_size))
289 if (verify_buf(rw_buff, rw_size, i % 10 + '0'))
294 printf(" file_test: rw_count = %" PRIu32 "\n", i);
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);
310 void test_lwext4_cleanup(void)
316 printf("\ncleanup:\n");
317 ext4_fremove("/mp/hello.txt");
319 printf("remove /mp/test1\n");
320 ext4_fremove("/mp/test1");
322 printf("remove /mp/dir1\n");
325 ext4_dir_rm("/mp/dir1");
328 printf("cleanup: time: %d ms\n", (int)diff);
329 printf_io_timings(diff);
332 bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
340 printf("test_lwext4_mount: no block device\n");
344 ext4_dmask_set(DEBUG_ALL);
346 r = ext4_device_register(bd, bc ? bc : 0, "ext4_fs");
348 printf("ext4_device_register: rc = %d\n", r);
352 r = ext4_mount("ext4_fs", "/mp/");
354 printf("ext4_mount: rc = %d\n", r);
358 ext4_cache_write_back("/mp/", 1);
362 bool test_lwext4_umount(void)
364 ext4_cache_write_back("/mp/", 0);
365 int r = ext4_umount("/mp/");
367 printf("ext4_umount: fail %d", r);