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.
36 #include "test_lwext4.h"
38 /**@brief Read-write size*/
39 #define READ_MAX_WRITE_SZIZE 1024 * 8
41 /**@brief File read/write buffer*/
42 static uint8_t rw_buff[READ_MAX_WRITE_SZIZE];
44 /**@brief Block device handle.*/
45 static struct ext4_blockdev *bd;
47 /**@brief Block cache handle.*/
48 static struct ext4_bcache *bc;
50 static char *entry_to_str(uint8_t type)
53 case EXT4_DIRENTRY_UNKNOWN:
55 case EXT4_DIRENTRY_REG_FILE:
57 case EXT4_DIRENTRY_DIR:
59 case EXT4_DIRENTRY_CHRDEV:
61 case EXT4_DIRENTRY_BLKDEV:
63 case EXT4_DIRENTRY_FIFO:
65 case EXT4_DIRENTRY_SOCK:
67 case EXT4_DIRENTRY_SYMLINK:
75 static clock_t get_ms(void) { return tim_get_ms(); }
77 static void printf_io_timings(clock_t diff)
79 const struct ext4_io_stats *stats = io_timings_get(diff);
83 printf("io_timings:\n");
84 printf(" io_read: %.3f%%\n", stats->io_read);
85 printf(" io_write: %.3f%%\n", stats->io_write);
86 printf(" io_cpu: %.3f%%\n", stats->cpu);
89 void test_lwext4_dir_ls(const char *path)
93 const ext4_direntry *de;
95 printf("ls %s\n", path);
97 ext4_dir_open(&d, path);
98 de = ext4_dir_entry_next(&d);
101 memcpy(sss, de->name, de->name_length);
102 sss[de->name_length] = 0;
103 printf(" %s%s\n", entry_to_str(de->inode_type), sss);
104 de = ext4_dir_entry_next(&d);
109 void test_lwext4_mp_stats(void)
111 struct ext4_mount_stats stats;
112 ext4_mount_point_stats("/mp/", &stats);
114 printf("********************\n");
115 printf("ext4_mount_point_stats\n");
116 printf("inodes_count = %" PRIu32 "\n", stats.inodes_count);
117 printf("free_inodes_count = %" PRIu32 "\n", stats.free_inodes_count);
118 printf("blocks_count = %" PRIu32 "\n", (uint32_t)stats.blocks_count);
119 printf("free_blocks_count = %" PRIu32 "\n",
120 (uint32_t)stats.free_blocks_count);
121 printf("block_size = %" PRIu32 "\n", stats.block_size);
122 printf("block_group_count = %" PRIu32 "\n", stats.block_group_count);
123 printf("blocks_per_group= %" PRIu32 "\n", stats.blocks_per_group);
124 printf("inodes_per_group = %" PRIu32 "\n", stats.inodes_per_group);
125 printf("volume_name = %s\n", stats.volume_name);
126 printf("********************\n");
129 void test_lwext4_block_stats(void)
134 printf("********************\n");
135 printf("ext4 blockdev stats\n");
136 printf("bdev->bread_ctr = %" PRIu32 "\n", bd->bread_ctr);
137 printf("bdev->bwrite_ctr = %" PRIu32 "\n", bd->bwrite_ctr);
139 printf("bcache->ref_blocks = %" PRIu32 "\n", bc->ref_blocks);
140 printf("bcache->max_ref_blocks = %" PRIu32 "\n", bc->max_ref_blocks);
141 printf("bcache->lru_ctr = %" PRIu32 "\n", bc->lru_ctr);
146 for (i = 0; i < bc->cnt; ++i) {
147 printf("bcache->refctr[%" PRIu32 "]= %" PRIu32 "\n", i,
152 for (i = 0; i < bc->cnt; ++i) {
153 printf("bcache->lru_id[%" PRIu32 "] = %" PRIu32 "\n", i,
158 for (i = 0; i < bc->cnt; ++i) {
159 printf("bcache->free_delay[%" PRIu32 "] = %d\n", i,
164 for (i = 0; i < bc->cnt; ++i) {
165 printf("bcache->lba[%" PRIu32 "] = %" PRIu32 "\n", i,
166 (uint32_t)bc->lba[i]);
169 printf("********************\n");
172 bool test_lwext4_dir_test(int len)
182 printf("test_lwext4_dir_test: %d\n", len);
186 printf("directory create: /mp/dir1\n");
187 r = ext4_dir_mk("/mp/dir1");
189 printf("ext4_dir_mk: rc = %d\n", r);
193 printf("add files to: /mp/dir1\n");
194 for (i = 0; i < len; ++i) {
195 sprintf(path, "/mp/dir1/f%d", i);
196 r = ext4_fopen(&f, path, "wb");
198 printf("ext4_fopen: rc = %d\n", r);
205 test_lwext4_dir_ls("/mp/dir1");
206 printf("test_lwext4_dir_test: time: %d ms\n", (int)diff);
207 printf("test_lwext4_dir_test: av: %d ms/entry\n", (int)diff / (len + 1));
208 printf_io_timings(diff);
212 static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
215 for (i = 0; i < len; ++i) {
223 bool test_lwext4_file_test(uint32_t rw_szie, uint32_t rw_count)
236 printf("file_test:\n");
237 printf(" rw size: %" PRIu32 "\n", rw_szie);
238 printf(" rw count: %" PRIu32 "\n", rw_count);
240 /*Add hello world file.*/
241 r = ext4_fopen(&f, "/mp/hello.txt", "wb");
242 r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
247 r = ext4_fopen(&f, "/mp/test1", "wb");
249 printf("ext4_fopen ERROR = %d\n", r);
253 printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_szie,
255 for (i = 0; i < rw_count; ++i) {
257 memset(rw_buff, i % 10 + '0', rw_szie);
259 r = ext4_fwrite(&f, rw_buff, rw_szie, &size);
261 if ((r != EOK) || (size != rw_szie))
266 printf(" file_test: rw_count = %" PRIu32 "\n", i);
272 size_bytes = rw_szie * rw_count;
273 size_bytes = (size_bytes * 1000) / 1024;
274 kbps = (size_bytes) / (diff + 1);
275 printf(" write time: %d ms\n", (int)diff);
276 printf(" write speed: %" PRIu32 " KB/s\n", kbps);
277 printf_io_timings(diff);
282 r = ext4_fopen(&f, "/mp/test1", "r+");
284 printf("ext4_fopen ERROR = %d\n", r);
288 printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_szie, rw_count);
290 for (i = 0; i < rw_count; ++i) {
291 r = ext4_fread(&f, rw_buff, rw_szie, &size);
293 if ((r != EOK) || (size != rw_szie))
296 if (verify_buf(rw_buff, rw_szie, i % 10 + '0'))
301 printf(" file_test: rw_count = %" PRIu32 "\n", i);
307 size_bytes = rw_szie * rw_count;
308 size_bytes = (size_bytes * 1000) / 1024;
309 kbps = (size_bytes) / (diff + 1);
310 printf(" read time: %d ms\n", (int)diff);
311 printf(" read speed: %d KB/s\n", (int)kbps);
312 printf_io_timings(diff);
317 void test_lwext4_cleanup(void)
323 printf("\ncleanup:\n");
324 ext4_fremove("/mp/hello.txt");
326 printf("remove /mp/test1\n");
327 ext4_fremove("/mp/test1");
329 printf("remove /mp/dir1\n");
332 ext4_dir_rm("/mp/dir1");
335 printf("cleanup: time: %d ms\n", (int)diff);
336 printf_io_timings(diff);
339 bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
347 printf("test_lwext4_mount: no block device\n");
351 ext4_dmask_set(EXT4_DEBUG_ALL);
353 r = ext4_device_register(bd, bc ? bc : 0, "ext4_fs");
355 printf("ext4_device_register: rc = %d\n", r);
359 r = ext4_mount("ext4_fs", "/mp/");
361 printf("ext4_mount: rc = %d\n", r);
365 ext4_cache_write_back("/mp/", 1);
369 bool test_lwext4_umount(void)
371 ext4_cache_write_back("/mp/", 0);
372 int r = ext4_umount("/mp/");
374 printf("ext4_umount: fail %d", r);