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);
80 printf("io_timings:\n");
81 printf(" io_read: %.3f%%\n", stats->io_read);
82 printf(" io_write: %.3f%%\n", stats->io_write);
83 printf(" io_cpu: %.3f%%\n", stats->cpu);
86 void test_lwext4_dir_ls(const char *path)
90 const ext4_direntry *de;
92 printf("ls %s\n", path);
94 ext4_dir_open(&d, path);
95 de = ext4_dir_entry_next(&d);
98 memcpy(sss, de->name, de->name_length);
99 sss[de->name_length] = 0;
100 printf(" %s%s\n", entry_to_str(de->inode_type), sss);
101 de = ext4_dir_entry_next(&d);
106 void test_lwext4_mp_stats(void)
108 struct ext4_mount_stats stats;
109 ext4_mount_point_stats("/mp/", &stats);
111 printf("********************\n");
112 printf("ext4_mount_point_stats\n");
113 printf("inodes_count = %" PRIu32 "\n", stats.inodes_count);
114 printf("free_inodes_count = %" PRIu32 "\n", stats.free_inodes_count);
115 printf("blocks_count = %" PRIu32 "\n", (uint32_t)stats.blocks_count);
116 printf("free_blocks_count = %" PRIu32 "\n",
117 (uint32_t)stats.free_blocks_count);
118 printf("block_size = %" PRIu32 "\n", stats.block_size);
119 printf("block_group_count = %" PRIu32 "\n", stats.block_group_count);
120 printf("blocks_per_group= %" PRIu32 "\n", stats.blocks_per_group);
121 printf("inodes_per_group = %" PRIu32 "\n", stats.inodes_per_group);
122 printf("volume_name = %s\n", stats.volume_name);
123 printf("********************\n");
126 void test_lwext4_block_stats(void)
131 printf("********************\n");
132 printf("ext4 blockdev stats\n");
133 printf("bdev->bread_ctr = %" PRIu32 "\n", bd->bread_ctr);
134 printf("bdev->bwrite_ctr = %" PRIu32 "\n", bd->bwrite_ctr);
136 printf("bcache->ref_blocks = %" PRIu32 "\n", bc->ref_blocks);
137 printf("bcache->max_ref_blocks = %" PRIu32 "\n", bc->max_ref_blocks);
138 printf("bcache->lru_ctr = %" PRIu32 "\n", bc->lru_ctr);
143 for (i = 0; i < bc->cnt; ++i) {
144 printf("bcache->refctr[%" PRIu32 "]= %" PRIu32 "\n", i,
149 for (i = 0; i < bc->cnt; ++i) {
150 printf("bcache->lru_id[%" PRIu32 "] = %" PRIu32 "\n", i,
155 for (i = 0; i < bc->cnt; ++i) {
156 printf("bcache->free_delay[%" PRIu32 "] = %d\n", i,
161 for (i = 0; i < bc->cnt; ++i) {
162 printf("bcache->lba[%" PRIu32 "] = %" PRIu32 "\n", i,
163 (uint32_t)bc->lba[i]);
166 printf("********************\n");
169 bool test_lwext4_dir_test(int len)
179 printf("test_lwext4_dir_test: %d\n", len);
183 printf("directory create: /mp/dir1\n");
184 r = ext4_dir_mk("/mp/dir1");
186 printf("ext4_dir_mk: rc = %d\n", r);
190 printf("add files to: /mp/dir1\n");
191 for (i = 0; i < len; ++i) {
192 sprintf(path, "/mp/dir1/f%d", i);
193 r = ext4_fopen(&f, path, "wb");
195 printf("ext4_fopen: rc = %d\n", r);
202 test_lwext4_dir_ls("/mp/dir1");
203 printf("test_lwext4_dir_test: time: %d ms\n", (int)diff);
204 printf("test_lwext4_dir_test: av: %d ms/entry\n", (int)diff / len);
205 printf_io_timings(diff);
209 static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
212 for (i = 0; i < len; ++i) {
220 bool test_lwext4_file_test(uint32_t rw_szie, uint32_t rw_count)
233 printf("file_test:\n");
234 printf(" rw size: %" PRIu32 "\n", rw_szie);
235 printf(" rw count: %" PRIu32 "\n", rw_count);
237 /*Add hello world file.*/
238 r = ext4_fopen(&f, "/mp/hello.txt", "wb");
239 r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
244 r = ext4_fopen(&f, "/mp/test1", "wb");
246 printf("ext4_fopen ERROR = %d\n", r);
250 printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_szie,
252 for (i = 0; i < rw_count; ++i) {
254 memset(rw_buff, i % 10 + '0', rw_szie);
256 r = ext4_fwrite(&f, rw_buff, rw_szie, &size);
258 if ((r != EOK) || (size != rw_szie))
263 printf(" file_test: rw_count = %" PRIu32 "\n", i);
269 size_bytes = rw_szie * rw_count;
270 size_bytes = (size_bytes * 1000) / 1024;
271 kbps = (size_bytes) / (diff + 1);
272 printf(" write time: %d ms\n", (int)diff);
273 printf(" write speed: %" PRIu32 " KB/s\n", kbps);
274 printf_io_timings(diff);
279 r = ext4_fopen(&f, "/mp/test1", "r+");
281 printf("ext4_fopen ERROR = %d\n", r);
285 printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_szie, rw_count);
287 for (i = 0; i < rw_count; ++i) {
288 r = ext4_fread(&f, rw_buff, rw_szie, &size);
290 if ((r != EOK) || (size != rw_szie))
293 if (verify_buf(rw_buff, rw_szie, i % 10 + '0'))
298 printf(" file_test: rw_count = %" PRIu32 "\n", i);
304 size_bytes = rw_szie * rw_count;
305 size_bytes = (size_bytes * 1000) / 1024;
306 kbps = (size_bytes) / (diff + 1);
307 printf(" read time: %d ms\n", (int)diff);
308 printf(" read speed: %d KB/s\n", (int)kbps);
309 printf_io_timings(diff);
314 void test_lwext4_cleanup(void)
320 printf("\ncleanup:\n");
321 ext4_fremove("/mp/hello.txt");
323 printf("remove /mp/test1\n");
324 ext4_fremove("/mp/test1");
326 printf("remove /mp/dir1\n");
329 ext4_dir_rm("/mp/dir1");
332 printf("cleanup: time: %d ms\n", (int)diff);
333 printf_io_timings(diff);
336 bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
344 printf("test_lwext4_mount: no block device\n");
348 ext4_dmask_set(EXT4_DEBUG_ALL);
350 r = ext4_device_register(bd, bc ? bc : 0, "ext4_fs");
352 printf("ext4_device_register: rc = %d\n", r);
356 r = ext4_mount("ext4_fs", "/mp/");
358 printf("ext4_mount: rc = %d\n", r);
362 ext4_cache_write_back("/mp/", 1);
366 bool test_lwext4_umount(void)
368 ext4_cache_write_back("/mp/", 0);
369 int r = ext4_umount("/mp/");
371 printf("ext4_umount: fail %d", r);