529f67b5137435e77c2311b730b72d7a95efd732
[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         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_szie, 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         printf("file_test:\n");
237         printf("  rw size: %" PRIu32 "\n", rw_szie);
238         printf("  rw count: %" PRIu32 "\n", rw_count);
239
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);
243         r = ext4_fclose(&f);
244
245         io_timings_clear();
246         start = get_ms();
247         r = ext4_fopen(&f, "/mp/test1", "wb");
248         if (r != EOK) {
249                 printf("ext4_fopen ERROR = %d\n", r);
250                 return false;
251         }
252
253         printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_szie,
254                rw_count);
255         for (i = 0; i < rw_count; ++i) {
256
257                 memset(rw_buff, i % 10 + '0', rw_szie);
258
259                 r = ext4_fwrite(&f, rw_buff, rw_szie, &size);
260
261                 if ((r != EOK) || (size != rw_szie))
262                         break;
263         }
264
265         if (i != rw_count) {
266                 printf("  file_test: rw_count = %" PRIu32 "\n", i);
267                 return false;
268         }
269
270         stop = get_ms();
271         diff = stop - start;
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);
278         r = ext4_fclose(&f);
279
280         io_timings_clear();
281         start = get_ms();
282         r = ext4_fopen(&f, "/mp/test1", "r+");
283         if (r != EOK) {
284                 printf("ext4_fopen ERROR = %d\n", r);
285                 return false;
286         }
287
288         printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_szie, rw_count);
289
290         for (i = 0; i < rw_count; ++i) {
291                 r = ext4_fread(&f, rw_buff, rw_szie, &size);
292
293                 if ((r != EOK) || (size != rw_szie))
294                         break;
295
296                 if (verify_buf(rw_buff, rw_szie, i % 10 + '0'))
297                         break;
298         }
299
300         if (i != rw_count) {
301                 printf("  file_test: rw_count = %" PRIu32 "\n", i);
302                 return false;
303         }
304
305         stop = get_ms();
306         diff = stop - start;
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);
313
314         r = ext4_fclose(&f);
315         return true;
316 }
317 void test_lwext4_cleanup(void)
318 {
319         clock_t start;
320         clock_t stop;
321         clock_t diff;
322
323         printf("\ncleanup:\n");
324         ext4_fremove("/mp/hello.txt");
325
326         printf("remove /mp/test1\n");
327         ext4_fremove("/mp/test1");
328
329         printf("remove /mp/dir1\n");
330         io_timings_clear();
331         start = get_ms();
332         ext4_dir_rm("/mp/dir1");
333         stop = get_ms();
334         diff = stop - start;
335         printf("cleanup: time: %d ms\n", (int)diff);
336         printf_io_timings(diff);
337 }
338
339 bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
340 {
341         int r;
342
343         bc = bcache;
344         bd = bdev;
345
346         if (!bd) {
347                 printf("test_lwext4_mount: no block device\n");
348                 return false;
349         }
350
351         ext4_dmask_set(EXT4_DEBUG_ALL);
352
353         r = ext4_device_register(bd, bc ? bc : 0, "ext4_fs");
354         if (r != EOK) {
355                 printf("ext4_device_register: rc = %d\n", r);
356                 return false;
357         }
358
359         r = ext4_mount("ext4_fs", "/mp/");
360         if (r != EOK) {
361                 printf("ext4_mount: rc = %d\n", r);
362                 return false;
363         }
364
365         ext4_cache_write_back("/mp/", 1);
366         return true;
367 }
368
369 bool test_lwext4_umount(void)
370 {
371         ext4_cache_write_back("/mp/", 0);
372         int r = ext4_umount("/mp/");
373         if (r != EOK) {
374                 printf("ext4_umount: fail %d", r);
375                 return false;
376         }
377         return true;
378 }