Demo apps refactoring
[lwext4.git] / blockdev / test_lwext4.c
1 /*
2  * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
16  *
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.
27  */
28
29 #include <ext4.h>
30
31 #include <stdio.h>
32 #include <time.h>
33 #include <inttypes.h>
34 #include <string.h>
35
36 #include "test_lwext4.h"
37
38 /**@brief   Read-write size*/
39 #define READ_MAX_WRITE_SZIZE 1024 * 8
40
41 /**@brief   File read/write buffer*/
42 static uint8_t rw_buff[READ_MAX_WRITE_SZIZE];
43
44 /**@brief   Block device handle.*/
45 static struct ext4_blockdev *bd;
46
47 /**@brief   Block cache handle.*/
48 static struct ext4_bcache *bc;
49
50 static char *entry_to_str(uint8_t type)
51 {
52         switch (type) {
53         case EXT4_DIRENTRY_UNKNOWN:
54                 return "[unk] ";
55         case EXT4_DIRENTRY_REG_FILE:
56                 return "[fil] ";
57         case EXT4_DIRENTRY_DIR:
58                 return "[dir] ";
59         case EXT4_DIRENTRY_CHRDEV:
60                 return "[cha] ";
61         case EXT4_DIRENTRY_BLKDEV:
62                 return "[blk] ";
63         case EXT4_DIRENTRY_FIFO:
64                 return "[fif] ";
65         case EXT4_DIRENTRY_SOCK:
66                 return "[soc] ";
67         case EXT4_DIRENTRY_SYMLINK:
68                 return "[sym] ";
69         default:
70                 break;
71         }
72         return "[???]";
73 }
74
75 static clock_t get_ms(void) { return tim_get_ms(); }
76
77 static void printf_io_timings(clock_t diff)
78 {
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);
84 }
85
86 void test_lwext4_dir_ls(const char *path)
87 {
88         char sss[255];
89         ext4_dir d;
90         const ext4_direntry *de;
91
92         printf("ls %s\n", path);
93
94         ext4_dir_open(&d, path);
95         de = ext4_dir_entry_next(&d);
96
97         while (de) {
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);
102         }
103         ext4_dir_close(&d);
104 }
105
106 void test_lwext4_mp_stats(void)
107 {
108         struct ext4_mount_stats stats;
109         ext4_mount_point_stats("/mp/", &stats);
110
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");
124 }
125
126 void test_lwext4_block_stats(void)
127 {
128         if (!bd)
129                 return;
130
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);
135
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);
139
140         printf("\n");
141
142         uint32_t i;
143         for (i = 0; i < bc->cnt; ++i) {
144                 printf("bcache->refctr[%" PRIu32 "]= %" PRIu32 "\n", i,
145                        bc->refctr[i]);
146         }
147
148         printf("\n");
149         for (i = 0; i < bc->cnt; ++i) {
150                 printf("bcache->lru_id[%" PRIu32 "] = %" PRIu32 "\n", i,
151                        bc->lru_id[i]);
152         }
153
154         printf("\n");
155         for (i = 0; i < bc->cnt; ++i) {
156                 printf("bcache->free_delay[%" PRIu32 "] = %d\n", i,
157                        bc->free_delay[i]);
158         }
159
160         printf("\n");
161         for (i = 0; i < bc->cnt; ++i) {
162                 printf("bcache->lba[%" PRIu32 "] = %" PRIu32 "\n", i,
163                        (uint32_t)bc->lba[i]);
164         }
165
166         printf("********************\n");
167 }
168
169 bool test_lwext4_dir_test(int len)
170 {
171         ext4_file f;
172         int r;
173         int i;
174         char path[64];
175         clock_t diff;
176         clock_t stop;
177         clock_t start;
178
179         printf("test_lwext4_dir_test: %d\n", len);
180         io_timings_clear();
181         start = get_ms();
182
183         printf("directory create: /mp/dir1\n");
184         r = ext4_dir_mk("/mp/dir1");
185         if (r != EOK) {
186                 printf("ext4_dir_mk: rc = %d\n", r);
187                 return false;
188         }
189
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");
194                 if (r != EOK) {
195                         printf("ext4_fopen: rc = %d\n", r);
196                         return false;
197                 }
198         }
199
200         stop = get_ms();
201         diff = stop - start;
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);
206         return true;
207 }
208
209 static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
210 {
211         size_t i;
212         for (i = 0; i < len; ++i) {
213                 if (b[i] != c)
214                         return c - b[i];
215         }
216
217         return 0;
218 }
219
220 bool test_lwext4_file_test(uint32_t rw_szie, uint32_t rw_count)
221 {
222         int r;
223         size_t size;
224         uint32_t i;
225         clock_t start;
226         clock_t stop;
227         clock_t diff;
228         uint32_t kbps;
229         uint64_t size_bytes;
230
231         ext4_file f;
232
233         printf("file_test:\n");
234         printf("  rw size: %" PRIu32 "\n", rw_szie);
235         printf("  rw count: %" PRIu32 "\n", rw_count);
236
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);
240         r = ext4_fclose(&f);
241
242         io_timings_clear();
243         start = get_ms();
244         r = ext4_fopen(&f, "/mp/test1", "wb");
245         if (r != EOK) {
246                 printf("ext4_fopen ERROR = %d\n", r);
247                 return false;
248         }
249
250         printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_szie,
251                rw_count);
252         for (i = 0; i < rw_count; ++i) {
253
254                 memset(rw_buff, i % 10 + '0', rw_szie);
255
256                 r = ext4_fwrite(&f, rw_buff, rw_szie, &size);
257
258                 if ((r != EOK) || (size != rw_szie))
259                         break;
260         }
261
262         if (i != rw_count) {
263                 printf("  file_test: rw_count = %" PRIu32 "\n", i);
264                 return false;
265         }
266
267         stop = get_ms();
268         diff = stop - start;
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);
275         r = ext4_fclose(&f);
276
277         io_timings_clear();
278         start = get_ms();
279         r = ext4_fopen(&f, "/mp/test1", "r+");
280         if (r != EOK) {
281                 printf("ext4_fopen ERROR = %d\n", r);
282                 return false;
283         }
284
285         printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_szie, rw_count);
286
287         for (i = 0; i < rw_count; ++i) {
288                 r = ext4_fread(&f, rw_buff, rw_szie, &size);
289
290                 if ((r != EOK) || (size != rw_szie))
291                         break;
292
293                 if (verify_buf(rw_buff, rw_szie, i % 10 + '0'))
294                         break;
295         }
296
297         if (i != rw_count) {
298                 printf("  file_test: rw_count = %" PRIu32 "\n", i);
299                 return false;
300         }
301
302         stop = get_ms();
303         diff = stop - start;
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);
310
311         r = ext4_fclose(&f);
312         return true;
313 }
314 void test_lwext4_cleanup(void)
315 {
316         clock_t start;
317         clock_t stop;
318         clock_t diff;
319
320         printf("\ncleanup:\n");
321         ext4_fremove("/mp/hello.txt");
322
323         printf("remove /mp/test1\n");
324         ext4_fremove("/mp/test1");
325
326         printf("remove /mp/dir1\n");
327         io_timings_clear();
328         start = get_ms();
329         ext4_dir_rm("/mp/dir1");
330         stop = get_ms();
331         diff = stop - start;
332         printf("cleanup: time: %d ms\n", (int)diff);
333         printf_io_timings(diff);
334 }
335
336 bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
337 {
338         int r;
339
340         bc = bcache;
341         bd = bdev;
342
343         if (!bd) {
344                 printf("test_lwext4_mount: no block device\n");
345                 return false;
346         }
347
348         ext4_dmask_set(EXT4_DEBUG_ALL);
349
350         r = ext4_device_register(bd, bc ? bc : 0, "ext4_fs");
351         if (r != EOK) {
352                 printf("ext4_device_register: rc = %d\n", r);
353                 return false;
354         }
355
356         r = ext4_mount("ext4_fs", "/mp/");
357         if (r != EOK) {
358                 printf("ext4_mount: rc = %d\n", r);
359                 return false;
360         }
361
362         ext4_cache_write_back("/mp/", 1);
363         return true;
364 }
365
366 bool test_lwext4_umount(void)
367 {
368         ext4_cache_write_back("/mp/", 0);
369         int r = ext4_umount("/mp/");
370         if (r != EOK) {
371                 printf("ext4_umount: fail %d", r);
372                 return false;
373         }
374         return true;
375 }