2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\r
9 * - Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
11 * - Redistributions in binary form must reproduce the above copyright
\r
12 * notice, this list of conditions and the following disclaimer in the
\r
13 * documentation and/or other materials provided with the distribution.
\r
14 * - The name of the author may not be used to endorse or promote products
\r
15 * derived from this software without specific prior written permission.
\r
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
30 #include <hw_init.h>
\r
37 #include <stdbool.h>
\r
42 #include <usb_msc_lwext4.h>
\r
45 /**@brief Read-write size*/
\r
46 #define READ_WRITE_SZIZE 1024 * 16
\r
48 /**@brief Delay test (slower LCD scroll)*/
\r
49 #define TEST_DELAY_MS 1000
\r
51 /**@brief Input stream name.*/
\r
52 char input_name[128] = "ext2";
\r
54 /**@brief Read-write size*/
\r
55 static int rw_szie = READ_WRITE_SZIZE;
\r
57 /**@brief Read-write size*/
\r
58 static int rw_count = 100;
\r
60 /**@brief Directory test count*/
\r
61 static int dir_cnt = 100;
\r
63 /**@brief Static or dynamic cache mode*/
\r
64 static bool cache_mode = false;
\r
66 /**@brief Cleanup after test.*/
\r
67 static bool cleanup_flag = false;
\r
69 /**@brief Block device stats.*/
\r
70 static bool bstat = false;
\r
72 /**@brief Superblock stats.*/
\r
73 static bool sbstat = false;
\r
75 /**@brief File write buffer*/
\r
76 static uint8_t wr_buff[READ_WRITE_SZIZE];
\r
78 /**@brief File read buffer.*/
\r
79 static uint8_t rd_buff[READ_WRITE_SZIZE];
\r
81 /**@brief Block device handle.*/
\r
82 static struct ext4_blockdev *bd;
\r
84 /**@brief Block cache handle.*/
\r
85 static struct ext4_bcache *bc;
\r
87 static char* entry_to_str(uint8_t type)
\r
90 case EXT4_DIRENTRY_UNKNOWN:
\r
92 case EXT4_DIRENTRY_REG_FILE:
\r
94 case EXT4_DIRENTRY_DIR:
\r
96 case EXT4_DIRENTRY_CHRDEV:
\r
98 case EXT4_DIRENTRY_BLKDEV:
\r
100 case EXT4_DIRENTRY_FIFO:
\r
102 case EXT4_DIRENTRY_SOCK:
\r
104 case EXT4_DIRENTRY_SYMLINK:
\r
112 static void dir_ls(const char *path)
\r
116 const ext4_direntry *de;
\r
118 printf("ls %s\n", path);
\r
120 ext4_dir_open(&d, path);
\r
121 de = ext4_dir_entry_next(&d);
\r
124 memcpy(sss, de->name, de->name_length);
\r
125 sss[de->name_length] = 0;
\r
126 printf(" %s", entry_to_str(de->inode_type));
\r
129 de = ext4_dir_entry_next(&d);
\r
131 ext4_dir_close(&d);
\r
134 static void mp_stats(void)
\r
136 struct ext4_mount_stats stats;
\r
137 ext4_mount_point_stats("/mp/", &stats);
\r
139 printf("********************\n");
\r
140 printf("ext4_mount_point_stats\n");
\r
141 printf("inodes_count = %u\n", stats.inodes_count);
\r
142 printf("free_inodes_count = %u\n", stats.free_inodes_count);
\r
143 printf("blocks_count = %u\n", (uint32_t)stats.blocks_count);
\r
144 printf("free_blocks_count = %u\n", (uint32_t)stats.free_blocks_count);
\r
145 printf("block_size = %u\n", stats.block_size);
\r
146 printf("block_group_count = %u\n", stats.block_group_count);
\r
147 printf("blocks_per_group= %u\n", stats.blocks_per_group);
\r
148 printf("inodes_per_group = %u\n", stats.inodes_per_group);
\r
149 printf("volume_name = %s\n", stats.volume_name);
\r
151 printf("********************\n");
\r
155 static void block_stats(void)
\r
159 printf("********************\n");
\r
160 printf("ext4 blockdev stats\n");
\r
161 printf("bdev->bread_ctr = %u\n", bd->bread_ctr);
\r
162 printf("bdev->bwrite_ctr = %u\n", bd->bwrite_ctr);
\r
165 printf("bcache->ref_blocks = %u\n", bc->ref_blocks);
\r
166 printf("bcache->max_ref_blocks = %u\n", bc->max_ref_blocks);
\r
167 printf("bcache->lru_ctr = %u\n", bc->lru_ctr);
\r
170 for (i = 0; i < bc->cnt; ++i) {
\r
171 printf("bcache->refctr[%d]= %u\n", i, bc->refctr[i]);
\r
175 for (i = 0; i < bc->cnt; ++i) {
\r
176 printf("bcache->lru_id[%d] = %u\n", i, bc->lru_id[i]);
\r
180 for (i = 0; i < bc->cnt; ++i) {
\r
181 printf("bcache->free_delay[%d] = %d\n", i, bc->free_delay[i]);
\r
185 for (i = 0; i < bc->cnt; ++i) {
\r
186 printf("bcache->lba[%d] = %u\n", i, (uint32_t)bc->lba[i]);
\r
189 printf("********************\n");
\r
192 static clock_t get_ms(void)
\r
194 return tim_get_ms();
\r
197 static void printf_io_timings(clock_t diff)
\r
199 const struct ext4_io_stats *stats = io_timings_get(diff);
\r
200 printf("io_timings:\n");
\r
201 printf(" io_read: %.3f%%\n", stats->io_read);
\r
202 printf(" io_write: %.3f%%\n", stats->io_write);
\r
203 printf(" io_cpu: %.3f%%\n", stats->cpu);
\r
206 static bool dir_test(int len)
\r
216 printf("dir_test: %d\n", len);
\r
217 io_timings_clear();
\r
220 printf("directory create: /mp/dir1\n");
\r
221 r = ext4_dir_mk("/mp/dir1");
\r
223 printf("ext4_dir_mk: rc = %d\n", r);
\r
227 printf("add files to: /mp/dir1\n");
\r
228 for (i = 0; i < len; ++i) {
\r
229 sprintf(path, "/mp/dir1/f%d", i);
\r
230 r = ext4_fopen(&f, path, "wb");
\r
232 printf("ext4_fopen: rc = %d\n", r);
\r
238 diff = stop - start;
\r
239 dir_ls("/mp/dir1");
\r
240 printf("dir_test: time: %d ms\n", (int)diff);
\r
241 printf("dir_test: av: %d ms/entry\n", (int)diff / len);
\r
242 printf_io_timings(diff);
\r
247 static bool file_test(void)
\r
257 uint64_t size_bytes;
\r
259 printf("file_test:\n");
\r
261 /*Add hello world file.*/
\r
262 r = ext4_fopen(&f, "/mp/hello.txt", "wb");
\r
263 r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
\r
264 r = ext4_fclose(&f);
\r
267 io_timings_clear();
\r
269 r = ext4_fopen(&f, "/mp/test1", "wb");
\r
271 printf("ext4_fopen ERROR = %d\n", r);
\r
275 printf("ext4_write: %d * %d ...\n" , rw_szie, rw_count);
\r
276 for (i = 0; i < rw_count; ++i) {
\r
278 memset(wr_buff, i % 10 + '0', rw_szie);
\r
280 r = ext4_fwrite(&f, wr_buff, rw_szie, &size);
\r
282 if((r != EOK) || (size != rw_szie))
\r
287 printf(" file_test: rw_count = %d\n", i);
\r
292 diff = stop - start;
\r
293 size_bytes = rw_szie * rw_count;
\r
294 size_bytes = (size_bytes * 1000) / 1024;
\r
295 kbps = (size_bytes) / (diff + 1);
\r
296 printf(" write time: %d ms\n", (int)diff);
\r
297 printf(" write speed: %d KB/s\n", kbps);
\r
298 printf_io_timings(diff);
\r
299 r = ext4_fclose(&f);
\r
302 io_timings_clear();
\r
304 r = ext4_fopen(&f, "/mp/test1", "r+");
\r
306 printf("ext4_fopen ERROR = %d\n", r);
\r
310 printf("ext4_read: %d * %d ...\n" , rw_szie, rw_count);
\r
312 for (i = 0; i < rw_count; ++i) {
\r
313 memset(wr_buff, i % 10 + '0', rw_szie);
\r
314 r = ext4_fread(&f, rd_buff, rw_szie, &size);
\r
316 if((r != EOK) || (size != rw_szie))
\r
319 if(memcmp(rd_buff, wr_buff, rw_szie))
\r
324 printf(" file_test: rw_count = %d\n", i);
\r
329 diff = stop - start;
\r
330 size_bytes = rw_szie * rw_count;
\r
331 size_bytes = (size_bytes * 1000) / 1024;
\r
332 kbps = (size_bytes) / (diff + 1);
\r
333 printf(" read time: %d ms\n", (int)diff);
\r
334 printf(" read speed: %d KB/s\n", kbps);
\r
335 printf_io_timings(diff);
\r
337 r = ext4_fclose(&f);
\r
341 static void cleanup(void)
\r
347 printf("\ncleanup:\n");
\r
348 ext4_fremove("/mp/hello.txt");
\r
350 printf("remove /mp/test1\n");
\r
351 ext4_fremove("/mp/test1");
\r
354 printf("remove /mp/dir1\n");
\r
355 io_timings_clear();
\r
357 ext4_dir_rm("/mp/dir1");
\r
359 diff = stop - start;
\r
360 printf("cleanup: time: %d ms\n", (int)diff);
\r
361 printf_io_timings(diff);
\r
364 static bool open_filedev(void)
\r
367 bd = ext4_usb_msc_get();
\r
368 bc = ext4_usb_msc_cache_get();
\r
370 printf("open_filedev: fail\n");
\r
376 static bool mount(void)
\r
380 if(!open_filedev())
\r
383 ext4_dmask_set(EXT4_DEBUG_ALL);
\r
385 r = ext4_device_register(bd, cache_mode ? 0 : bc, "ext4_fs");
\r
387 printf("ext4_device_register: rc = %d\n", r);
\r
391 r = ext4_mount("ext4_fs", "/mp/");
\r
393 printf("ext4_mount: rc = %d\n", r);
\r
400 static bool umount(void)
\r
402 int r = ext4_umount("/mp/");
\r
404 printf("ext4_umount: fail %d", r);
\r
416 printf("connect usb drive...\n");
\r
418 while(!hw_usb_connected())
\r
420 printf("usb drive connected\n");
\r
422 while(!hw_usb_enum_done())
\r
424 printf("usb drive enum done\n");
\r
428 printf("test conditions:\n");
\r
429 printf(" rw size: %d\n", rw_szie);
\r
430 printf(" rw count: %d\n", rw_count);
\r
431 printf(" cache mode: %s\n", cache_mode ? "dynamic" : "static");
\r
434 tim_wait_ms(TEST_DELAY_MS);
\r
436 return EXIT_FAILURE;
\r
438 tim_wait_ms(TEST_DELAY_MS);
\r
440 ext4_cache_write_back("/mp/", 1);
\r
444 tim_wait_ms(TEST_DELAY_MS);
\r
448 tim_wait_ms(TEST_DELAY_MS);
\r
450 if(!dir_test(dir_cnt))
\r
451 return EXIT_FAILURE;
\r
453 tim_wait_ms(TEST_DELAY_MS);
\r
455 return EXIT_FAILURE;
\r
458 tim_wait_ms(TEST_DELAY_MS);
\r
463 tim_wait_ms(TEST_DELAY_MS);
\r
468 tim_wait_ms(TEST_DELAY_MS);
\r
472 ext4_cache_write_back("/mp/", 0);
\r
474 return EXIT_FAILURE;
\r
476 printf("press RESET button to restart\n");
\r