Merge remote-tracking branch 'upstream/master'
[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 * 16
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         if (!stats)
81                 return;
82
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);
87 }
88
89 void test_lwext4_dir_ls(const char *path)
90 {
91         char sss[255];
92         ext4_dir d;
93         const ext4_direntry *de;
94
95         printf("ls %s\n", path);
96
97         ext4_dir_open(&d, path);
98         de = ext4_dir_entry_next(&d);
99
100         while (de) {
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);
105         }
106         ext4_dir_close(&d);
107 }
108
109 void test_lwext4_mp_stats(void)
110 {
111         struct ext4_mount_stats stats;
112         ext4_mount_point_stats("/mp/", &stats);
113
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");
127 }
128
129 void test_lwext4_block_stats(void)
130 {
131         if (!bd)
132                 return;
133
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);
138
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);
142
143         printf("\n");
144
145         uint32_t i;
146         for (i = 0; i < bc->cnt; ++i) {
147                 printf("bcache->refctr[%" PRIu32 "]= %" PRIu32 "\n", i,
148                        bc->refctr[i]);
149         }
150
151         printf("\n");
152         for (i = 0; i < bc->cnt; ++i) {
153                 printf("bcache->lru_id[%" PRIu32 "] = %" PRIu32 "\n", i,
154                        bc->lru_id[i]);
155         }
156
157         printf("\n");
158         for (i = 0; i < bc->cnt; ++i) {
159                 printf("bcache->free_delay[%" PRIu32 "] = %d\n", i,
160                        bc->free_delay[i]);
161         }
162
163         printf("\n");
164         for (i = 0; i < bc->cnt; ++i) {
165                 printf("bcache->lba[%" PRIu32 "] = %" PRIu32 "\n", i,
166                        (uint32_t)bc->lba[i]);
167         }
168
169         printf("********************\n");
170 }
171
172 bool test_lwext4_dir_test(int len)
173 {
174         ext4_file f;
175         int r;
176         int i;
177         char path[64];
178         clock_t diff;
179         clock_t stop;
180         clock_t start;
181
182         printf("test_lwext4_dir_test: %d\n", len);
183         io_timings_clear();
184         start = get_ms();
185
186         printf("directory create: /mp/dir1\n");
187         r = ext4_dir_mk("/mp/dir1");
188         if (r != EOK) {
189                 printf("ext4_dir_mk: rc = %d\n", r);
190                 return false;
191         }
192
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");
197                 if (r != EOK) {
198                         printf("ext4_fopen: rc = %d\n", r);
199                         return false;
200                 }
201         }
202
203         stop = get_ms();
204         diff = stop - start;
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);
209         return true;
210 }
211
212 static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
213 {
214         size_t i;
215         for (i = 0; i < len; ++i) {
216                 if (b[i] != c)
217                         return c - b[i];
218         }
219
220         return 0;
221 }
222
223 bool test_lwext4_file_test(uint32_t rw_size, uint32_t rw_count)
224 {
225         int r;
226         size_t size;
227         uint32_t i;
228         clock_t start;
229         clock_t stop;
230         clock_t diff;
231         uint32_t kbps;
232         uint64_t size_bytes;
233
234         ext4_file f;
235
236         if (rw_size > READ_MAX_WRITE_SZIZE)
237                 return false;
238
239         printf("file_test:\n");
240         printf("  rw size: %" PRIu32 "\n", rw_size);
241         printf("  rw count: %" PRIu32 "\n", rw_count);
242
243         /*Add hello world file.*/
244         r = ext4_fopen(&f, "/mp/hello.txt", "wb");
245         r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
246         r = ext4_fclose(&f);
247
248         io_timings_clear();
249         start = get_ms();
250         r = ext4_fopen(&f, "/mp/test1", "wb");
251         if (r != EOK) {
252                 printf("ext4_fopen ERROR = %d\n", r);
253                 return false;
254         }
255
256         printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_size,
257                rw_count);
258         for (i = 0; i < rw_count; ++i) {
259
260                 memset(rw_buff, i % 10 + '0', rw_size);
261
262                 r = ext4_fwrite(&f, rw_buff, rw_size, &size);
263
264                 if ((r != EOK) || (size != rw_size))
265                         break;
266         }
267
268         if (i != rw_count) {
269                 printf("  file_test: rw_count = %" PRIu32 "\n", i);
270                 return false;
271         }
272
273         stop = get_ms();
274         diff = stop - start;
275         size_bytes = rw_size * rw_count;
276         size_bytes = (size_bytes * 1000) / 1024;
277         kbps = (size_bytes) / (diff + 1);
278         printf("  write time: %d ms\n", (int)diff);
279         printf("  write speed: %" PRIu32 " KB/s\n", kbps);
280         printf_io_timings(diff);
281         r = ext4_fclose(&f);
282
283         io_timings_clear();
284         start = get_ms();
285         r = ext4_fopen(&f, "/mp/test1", "r+");
286         if (r != EOK) {
287                 printf("ext4_fopen ERROR = %d\n", r);
288                 return false;
289         }
290
291         printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_size, rw_count);
292
293         for (i = 0; i < rw_count; ++i) {
294                 r = ext4_fread(&f, rw_buff, rw_size, &size);
295
296                 if ((r != EOK) || (size != rw_size))
297                         break;
298
299                 if (verify_buf(rw_buff, rw_size, i % 10 + '0'))
300                         break;
301         }
302
303         if (i != rw_count) {
304                 printf("  file_test: rw_count = %" PRIu32 "\n", i);
305                 return false;
306         }
307
308         stop = get_ms();
309         diff = stop - start;
310         size_bytes = rw_size * rw_count;
311         size_bytes = (size_bytes * 1000) / 1024;
312         kbps = (size_bytes) / (diff + 1);
313         printf("  read time: %d ms\n", (int)diff);
314         printf("  read speed: %d KB/s\n", (int)kbps);
315         printf_io_timings(diff);
316
317         r = ext4_fclose(&f);
318         return true;
319 }
320 void test_lwext4_cleanup(void)
321 {
322         clock_t start;
323         clock_t stop;
324         clock_t diff;
325
326         printf("\ncleanup:\n");
327         ext4_fremove("/mp/hello.txt");
328
329         printf("remove /mp/test1\n");
330         ext4_fremove("/mp/test1");
331
332         printf("remove /mp/dir1\n");
333         io_timings_clear();
334         start = get_ms();
335         ext4_dir_rm("/mp/dir1");
336         stop = get_ms();
337         diff = stop - start;
338         printf("cleanup: time: %d ms\n", (int)diff);
339         printf_io_timings(diff);
340 }
341
342 bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
343 {
344         int r;
345
346         bc = bcache;
347         bd = bdev;
348
349         if (!bd) {
350                 printf("test_lwext4_mount: no block device\n");
351                 return false;
352         }
353
354         ext4_dmask_set(EXT4_DEBUG_ALL);
355
356         r = ext4_device_register(bd, bc ? bc : 0, "ext4_fs");
357         if (r != EOK) {
358                 printf("ext4_device_register: rc = %d\n", r);
359                 return false;
360         }
361
362         r = ext4_mount("ext4_fs", "/mp/");
363         if (r != EOK) {
364                 printf("ext4_mount: rc = %d\n", r);
365                 return false;
366         }
367
368         ext4_cache_write_back("/mp/", 1);
369         return true;
370 }
371
372 bool test_lwext4_umount(void)
373 {
374         ext4_cache_write_back("/mp/", 0);
375         int r = ext4_umount("/mp/");
376         if (r != EOK) {
377                 printf("ext4_umount: fail %d", r);
378                 return false;
379         }
380         return true;
381 }