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.
29 #include "../common/test_lwext4.h"
38 /**@brief Block device handle.*/
39 static struct ext4_blockdev *bd;
41 /**@brief Block cache handle.*/
42 static struct ext4_bcache *bc;
44 static char *entry_to_str(uint8_t type)
49 case EXT4_DE_REG_FILE:
69 static long int get_ms(void) { return tim_get_ms(); }
71 static void printf_io_timings(long int diff)
73 const struct ext4_io_stats *stats = io_timings_get(diff);
77 printf("io_timings:\n");
78 printf(" io_read: %.3f%%\n", (double)stats->io_read);
79 printf(" io_write: %.3f%%\n", (double)stats->io_write);
80 printf(" io_cpu: %.3f%%\n", (double)stats->cpu);
83 void test_lwext4_dir_ls(const char *path)
87 const ext4_direntry *de;
89 printf("ls %s\n", path);
91 ext4_dir_open(&d, path);
92 de = ext4_dir_entry_next(&d);
95 memcpy(sss, de->name, de->name_length);
96 sss[de->name_length] = 0;
97 printf(" %s%s\n", entry_to_str(de->inode_type), sss);
98 de = ext4_dir_entry_next(&d);
103 void test_lwext4_mp_stats(void)
105 struct ext4_mount_stats stats;
106 ext4_mount_point_stats("/mp/", &stats);
108 printf("********************\n");
109 printf("ext4_mount_point_stats\n");
110 printf("inodes_count = %" PRIu32 "\n", stats.inodes_count);
111 printf("free_inodes_count = %" PRIu32 "\n", stats.free_inodes_count);
112 printf("blocks_count = %" PRIu32 "\n", (uint32_t)stats.blocks_count);
113 printf("free_blocks_count = %" PRIu32 "\n",
114 (uint32_t)stats.free_blocks_count);
115 printf("block_size = %" PRIu32 "\n", stats.block_size);
116 printf("block_group_count = %" PRIu32 "\n", stats.block_group_count);
117 printf("blocks_per_group= %" PRIu32 "\n", stats.blocks_per_group);
118 printf("inodes_per_group = %" PRIu32 "\n", stats.inodes_per_group);
119 printf("volume_name = %s\n", stats.volume_name);
120 printf("********************\n");
123 void test_lwext4_block_stats(void)
128 printf("********************\n");
129 printf("ext4 blockdev stats\n");
130 printf("bdev->bread_ctr = %" PRIu32 "\n", bd->bdif->bread_ctr);
131 printf("bdev->bwrite_ctr = %" PRIu32 "\n", bd->bdif->bwrite_ctr);
133 printf("bcache->ref_blocks = %" PRIu32 "\n", bd->bc->ref_blocks);
134 printf("bcache->max_ref_blocks = %" PRIu32 "\n", bd->bc->max_ref_blocks);
135 printf("bcache->lru_ctr = %" PRIu32 "\n", bd->bc->lru_ctr);
139 printf("********************\n");
142 bool test_lwext4_dir_test(int len)
152 printf("test_lwext4_dir_test: %d\n", len);
156 printf("directory create: /mp/dir1\n");
157 r = ext4_dir_mk("/mp/dir1");
159 printf("ext4_dir_mk: rc = %d\n", r);
163 printf("add files to: /mp/dir1\n");
164 for (i = 0; i < len; ++i) {
165 sprintf(path, "/mp/dir1/f%d", i);
166 r = ext4_fopen(&f, path, "wb");
168 printf("ext4_fopen: rc = %d\n", r);
175 test_lwext4_dir_ls("/mp/dir1");
176 printf("test_lwext4_dir_test: time: %d ms\n", (int)diff);
177 printf("test_lwext4_dir_test: av: %d ms/entry\n", (int)diff / (len + 1));
178 printf_io_timings(diff);
182 static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
185 for (i = 0; i < len; ++i) {
193 bool test_lwext4_file_test(uint8_t *rw_buff, uint32_t rw_size, uint32_t rw_count)
206 printf("file_test:\n");
207 printf(" rw size: %" PRIu32 "\n", rw_size);
208 printf(" rw count: %" PRIu32 "\n", rw_count);
210 /*Add hello world file.*/
211 r = ext4_fopen(&f, "/mp/hello.txt", "wb");
212 r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
217 r = ext4_fopen(&f, "/mp/test1", "wb");
219 printf("ext4_fopen ERROR = %d\n", r);
223 printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_size,
225 for (i = 0; i < rw_count; ++i) {
227 memset(rw_buff, i % 10 + '0', rw_size);
229 r = ext4_fwrite(&f, rw_buff, rw_size, &size);
231 if ((r != EOK) || (size != rw_size))
236 printf(" file_test: rw_count = %" PRIu32 "\n", i);
242 size_bytes = rw_size * rw_count;
243 size_bytes = (size_bytes * 1000) / 1024;
244 kbps = (size_bytes) / (diff + 1);
245 printf(" write time: %d ms\n", (int)diff);
246 printf(" write speed: %" PRIu32 " KB/s\n", kbps);
247 printf_io_timings(diff);
252 r = ext4_fopen(&f, "/mp/test1", "r+");
254 printf("ext4_fopen ERROR = %d\n", r);
258 printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_size, rw_count);
260 for (i = 0; i < rw_count; ++i) {
261 r = ext4_fread(&f, rw_buff, rw_size, &size);
263 if ((r != EOK) || (size != rw_size))
266 if (verify_buf(rw_buff, rw_size, i % 10 + '0'))
271 printf(" file_test: rw_count = %" PRIu32 "\n", i);
277 size_bytes = rw_size * rw_count;
278 size_bytes = (size_bytes * 1000) / 1024;
279 kbps = (size_bytes) / (diff + 1);
280 printf(" read time: %d ms\n", (int)diff);
281 printf(" read speed: %d KB/s\n", (int)kbps);
282 printf_io_timings(diff);
287 void test_lwext4_cleanup(void)
294 printf("\ncleanup:\n");
295 r = ext4_fremove("/mp/hello.txt");
296 if (r != EOK && r != ENOENT) {
297 printf("ext4_fremove error: rc = %d\n", r);
300 printf("remove /mp/test1\n");
301 r = ext4_fremove("/mp/test1");
302 if (r != EOK && r != ENOENT) {
303 printf("ext4_fremove error: rc = %d\n", r);
306 printf("remove /mp/dir1\n");
309 r = ext4_dir_rm("/mp/dir1");
310 if (r != EOK && r != ENOENT) {
311 printf("ext4_fremove ext4_dir_rm: rc = %d\n", r);
315 printf("cleanup: time: %d ms\n", (int)diff);
316 printf_io_timings(diff);
319 bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
327 printf("test_lwext4_mount: no block device\n");
331 ext4_dmask_set(DEBUG_ALL);
333 r = ext4_device_register(bd, "ext4_fs");
335 printf("ext4_device_register: rc = %d\n", r);
339 r = ext4_mount("ext4_fs", "/mp/", false);
341 printf("ext4_mount: rc = %d\n", r);
345 r = ext4_recover("/mp/");
346 if (r != EOK && r != ENOTSUP) {
347 printf("ext4_recover: rc = %d\n", r);
351 r = ext4_journal_start("/mp/");
353 printf("ext4_journal_start: rc = %d\n", r);
357 ext4_cache_write_back("/mp/", 1);
361 bool test_lwext4_umount(void)
365 ext4_cache_write_back("/mp/", 0);
367 r = ext4_journal_stop("/mp/");
369 printf("ext4_journal_stop: fail %d", r);
373 r = ext4_umount("/mp/");
375 printf("ext4_umount: fail %d", r);