Merge pull request #44 from MaskRay/const-const
[lwext4.git] / fs_test / common / 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 "../common/test_lwext4.h"
30
31 #include <ext4.h>
32
33 #include <stdio.h>
34 #include <inttypes.h>
35 #include <string.h>
36
37
38 /**@brief   Block device handle.*/
39 static struct ext4_blockdev *bd;
40
41 /**@brief   Block cache handle.*/
42 static struct ext4_bcache *bc;
43
44 static char *entry_to_str(uint8_t type)
45 {
46         switch (type) {
47         case EXT4_DE_UNKNOWN:
48                 return "[unk] ";
49         case EXT4_DE_REG_FILE:
50                 return "[fil] ";
51         case EXT4_DE_DIR:
52                 return "[dir] ";
53         case EXT4_DE_CHRDEV:
54                 return "[cha] ";
55         case EXT4_DE_BLKDEV:
56                 return "[blk] ";
57         case EXT4_DE_FIFO:
58                 return "[fif] ";
59         case EXT4_DE_SOCK:
60                 return "[soc] ";
61         case EXT4_DE_SYMLINK:
62                 return "[sym] ";
63         default:
64                 break;
65         }
66         return "[???]";
67 }
68
69 static long int get_ms(void) { return tim_get_ms(); }
70
71 static void printf_io_timings(long int diff)
72 {
73         const struct ext4_io_stats *stats = io_timings_get(diff);
74         if (!stats)
75                 return;
76
77         printf("io_timings:\n");
78         printf("  io_read: %.3f%%\n", (double)stats->io_read);
79         printf("  io_write: %.3f%%\n", (double)stats->io_write);
80         printf("  io_cpu: %.3f%%\n", (double)stats->cpu);
81 }
82
83 void test_lwext4_dir_ls(const char *path)
84 {
85         char sss[255];
86         ext4_dir d;
87         const ext4_direntry *de;
88
89         printf("ls %s\n", path);
90
91         ext4_dir_open(&d, path);
92         de = ext4_dir_entry_next(&d);
93
94         while (de) {
95                 memcpy(sss, de->name, de->name_length);
96                 sss[de->name_length] = 0;
97                 printf("  %s%s\n", entry_to_str(de->inode_type), sss);
98                 de = ext4_dir_entry_next(&d);
99         }
100         ext4_dir_close(&d);
101 }
102
103 void test_lwext4_mp_stats(void)
104 {
105         struct ext4_mount_stats stats;
106         ext4_mount_point_stats("/mp/", &stats);
107
108         printf("********************\n");
109         printf("ext4_mount_point_stats\n");
110         printf("inodes_count = %" PRIu32 "\n", stats.inodes_count);
111         printf("free_inodes_count = %" PRIu32 "\n", stats.free_inodes_count);
112         printf("blocks_count = %" PRIu32 "\n", (uint32_t)stats.blocks_count);
113         printf("free_blocks_count = %" PRIu32 "\n",
114                (uint32_t)stats.free_blocks_count);
115         printf("block_size = %" PRIu32 "\n", stats.block_size);
116         printf("block_group_count = %" PRIu32 "\n", stats.block_group_count);
117         printf("blocks_per_group= %" PRIu32 "\n", stats.blocks_per_group);
118         printf("inodes_per_group = %" PRIu32 "\n", stats.inodes_per_group);
119         printf("volume_name = %s\n", stats.volume_name);
120         printf("********************\n");
121 }
122
123 void test_lwext4_block_stats(void)
124 {
125         if (!bd)
126                 return;
127
128         printf("********************\n");
129         printf("ext4 blockdev stats\n");
130         printf("bdev->bread_ctr = %" PRIu32 "\n", bd->bdif->bread_ctr);
131         printf("bdev->bwrite_ctr = %" PRIu32 "\n", bd->bdif->bwrite_ctr);
132
133         printf("bcache->ref_blocks = %" PRIu32 "\n", bd->bc->ref_blocks);
134         printf("bcache->max_ref_blocks = %" PRIu32 "\n", bd->bc->max_ref_blocks);
135         printf("bcache->lru_ctr = %" PRIu32 "\n", bd->bc->lru_ctr);
136
137         printf("\n");
138
139         printf("********************\n");
140 }
141
142 bool test_lwext4_dir_test(int len)
143 {
144         ext4_file f;
145         int r;
146         int i;
147         char path[64];
148         long int diff;
149         long int stop;
150         long int start;
151
152         printf("test_lwext4_dir_test: %d\n", len);
153         io_timings_clear();
154         start = get_ms();
155
156         printf("directory create: /mp/dir1\n");
157         r = ext4_dir_mk("/mp/dir1");
158         if (r != EOK) {
159                 printf("ext4_dir_mk: rc = %d\n", r);
160                 return false;
161         }
162
163         printf("add files to: /mp/dir1\n");
164         for (i = 0; i < len; ++i) {
165                 sprintf(path, "/mp/dir1/f%d", i);
166                 r = ext4_fopen(&f, path, "wb");
167                 if (r != EOK) {
168                         printf("ext4_fopen: rc = %d\n", r);
169                         return false;
170                 }
171         }
172
173         stop = get_ms();
174         diff = stop - start;
175         test_lwext4_dir_ls("/mp/dir1");
176         printf("test_lwext4_dir_test: time: %d ms\n", (int)diff);
177         printf("test_lwext4_dir_test: av: %d ms/entry\n", (int)diff / (len + 1));
178         printf_io_timings(diff);
179         return true;
180 }
181
182 static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
183 {
184         size_t i;
185         for (i = 0; i < len; ++i) {
186                 if (b[i] != c)
187                         return c - b[i];
188         }
189
190         return 0;
191 }
192
193 bool test_lwext4_file_test(uint8_t *rw_buff, uint32_t rw_size, uint32_t rw_count)
194 {
195         int r;
196         size_t size;
197         uint32_t i;
198         long int start;
199         long int stop;
200         long int diff;
201         uint32_t kbps;
202         uint64_t size_bytes;
203
204         ext4_file f;
205
206         printf("file_test:\n");
207         printf("  rw size: %" PRIu32 "\n", rw_size);
208         printf("  rw count: %" PRIu32 "\n", rw_count);
209
210         /*Add hello world file.*/
211         r = ext4_fopen(&f, "/mp/hello.txt", "wb");
212         r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
213         r = ext4_fclose(&f);
214
215         io_timings_clear();
216         start = get_ms();
217         r = ext4_fopen(&f, "/mp/test1", "wb");
218         if (r != EOK) {
219                 printf("ext4_fopen ERROR = %d\n", r);
220                 return false;
221         }
222
223         printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_size,
224                rw_count);
225         for (i = 0; i < rw_count; ++i) {
226
227                 memset(rw_buff, i % 10 + '0', rw_size);
228
229                 r = ext4_fwrite(&f, rw_buff, rw_size, &size);
230
231                 if ((r != EOK) || (size != rw_size))
232                         break;
233         }
234
235         if (i != rw_count) {
236                 printf("  file_test: rw_count = %" PRIu32 "\n", i);
237                 return false;
238         }
239
240         stop = get_ms();
241         diff = stop - start;
242         size_bytes = rw_size * rw_count;
243         size_bytes = (size_bytes * 1000) / 1024;
244         kbps = (size_bytes) / (diff + 1);
245         printf("  write time: %d ms\n", (int)diff);
246         printf("  write speed: %" PRIu32 " KB/s\n", kbps);
247         printf_io_timings(diff);
248         r = ext4_fclose(&f);
249
250         io_timings_clear();
251         start = get_ms();
252         r = ext4_fopen(&f, "/mp/test1", "r+");
253         if (r != EOK) {
254                 printf("ext4_fopen ERROR = %d\n", r);
255                 return false;
256         }
257
258         printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_size, rw_count);
259
260         for (i = 0; i < rw_count; ++i) {
261                 r = ext4_fread(&f, rw_buff, rw_size, &size);
262
263                 if ((r != EOK) || (size != rw_size))
264                         break;
265
266                 if (verify_buf(rw_buff, rw_size, i % 10 + '0'))
267                         break;
268         }
269
270         if (i != rw_count) {
271                 printf("  file_test: rw_count = %" PRIu32 "\n", i);
272                 return false;
273         }
274
275         stop = get_ms();
276         diff = stop - start;
277         size_bytes = rw_size * rw_count;
278         size_bytes = (size_bytes * 1000) / 1024;
279         kbps = (size_bytes) / (diff + 1);
280         printf("  read time: %d ms\n", (int)diff);
281         printf("  read speed: %d KB/s\n", (int)kbps);
282         printf_io_timings(diff);
283
284         r = ext4_fclose(&f);
285         return true;
286 }
287 void test_lwext4_cleanup(void)
288 {
289         long int start;
290         long int stop;
291         long int diff;
292         int r;
293
294         printf("\ncleanup:\n");
295         r = ext4_fremove("/mp/hello.txt");
296         if (r != EOK && r != ENOENT) {
297                 printf("ext4_fremove error: rc = %d\n", r);
298         }
299
300         printf("remove /mp/test1\n");
301         r = ext4_fremove("/mp/test1");
302         if (r != EOK && r != ENOENT) {
303                 printf("ext4_fremove error: rc = %d\n", r);
304         }
305
306         printf("remove /mp/dir1\n");
307         io_timings_clear();
308         start = get_ms();
309         r = ext4_dir_rm("/mp/dir1");
310         if (r != EOK && r != ENOENT) {
311                 printf("ext4_fremove ext4_dir_rm: rc = %d\n", r);
312         }
313         stop = get_ms();
314         diff = stop - start;
315         printf("cleanup: time: %d ms\n", (int)diff);
316         printf_io_timings(diff);
317 }
318
319 bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
320 {
321         int r;
322
323         bc = bcache;
324         bd = bdev;
325
326         if (!bd) {
327                 printf("test_lwext4_mount: no block device\n");
328                 return false;
329         }
330
331         ext4_dmask_set(DEBUG_ALL);
332
333         r = ext4_device_register(bd, "ext4_fs");
334         if (r != EOK) {
335                 printf("ext4_device_register: rc = %d\n", r);
336                 return false;
337         }
338
339         r = ext4_mount("ext4_fs", "/mp/", false);
340         if (r != EOK) {
341                 printf("ext4_mount: rc = %d\n", r);
342                 return false;
343         }
344
345         r = ext4_recover("/mp/");
346         if (r != EOK && r != ENOTSUP) {
347                 printf("ext4_recover: rc = %d\n", r);
348                 return false;
349         }
350
351         r = ext4_journal_start("/mp/");
352         if (r != EOK) {
353                 printf("ext4_journal_start: rc = %d\n", r);
354                 return false;
355         }
356
357         ext4_cache_write_back("/mp/", 1);
358         return true;
359 }
360
361 bool test_lwext4_umount(void)
362 {
363         int r;
364
365         ext4_cache_write_back("/mp/", 0);
366
367         r = ext4_journal_stop("/mp/");
368         if (r != EOK) {
369                 printf("ext4_journal_stop: fail %d", r);
370                 return false;
371         }
372
373         r = ext4_umount("/mp/");
374         if (r != EOK) {
375                 printf("ext4_umount: fail %d", r);
376                 return false;
377         }
378         return true;
379 }