summaryrefslogtreecommitdiff
path: root/fs_test/common/test_lwext4.c
blob: 78ec6262a8530e240222377d3a86e8f868f94834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "../common/test_lwext4.h"

#include <ext4.h>

#include <stdio.h>
#include <inttypes.h>
#include <string.h>


/**@brief   Block device handle.*/
static struct ext4_blockdev *bd;

/**@brief   Block cache handle.*/
static struct ext4_bcache *bc;

static char *entry_to_str(uint8_t type)
{
	switch (type) {
	case EXT4_DE_UNKNOWN:
		return "[unk] ";
	case EXT4_DE_REG_FILE:
		return "[fil] ";
	case EXT4_DE_DIR:
		return "[dir] ";
	case EXT4_DE_CHRDEV:
		return "[cha] ";
	case EXT4_DE_BLKDEV:
		return "[blk] ";
	case EXT4_DE_FIFO:
		return "[fif] ";
	case EXT4_DE_SOCK:
		return "[soc] ";
	case EXT4_DE_SYMLINK:
		return "[sym] ";
	default:
		break;
	}
	return "[???]";
}

static long int get_ms(void) { return tim_get_ms(); }

static void printf_io_timings(long int diff)
{
	const struct ext4_io_stats *stats = io_timings_get(diff);
	if (!stats)
		return;

	printf("io_timings:\n");
	printf("  io_read: %.3f%%\n", (double)stats->io_read);
	printf("  io_write: %.3f%%\n", (double)stats->io_write);
	printf("  io_cpu: %.3f%%\n", (double)stats->cpu);
}

void test_lwext4_dir_ls(const char *path)
{
	char sss[255];
	ext4_dir d;
	const ext4_direntry *de;

	printf("ls %s\n", path);

	ext4_dir_open(&d, path);
	de = ext4_dir_entry_next(&d);

	while (de) {
		memcpy(sss, de->name, de->name_length);
		sss[de->name_length] = 0;
		printf("  %s%s\n", entry_to_str(de->inode_type), sss);
		de = ext4_dir_entry_next(&d);
	}
	ext4_dir_close(&d);
}

void test_lwext4_mp_stats(void)
{
	struct ext4_mount_stats stats;
	ext4_mount_point_stats("/mp/", &stats);

	printf("********************\n");
	printf("ext4_mount_point_stats\n");
	printf("inodes_count = %" PRIu32 "\n", stats.inodes_count);
	printf("free_inodes_count = %" PRIu32 "\n", stats.free_inodes_count);
	printf("blocks_count = %" PRIu32 "\n", (uint32_t)stats.blocks_count);
	printf("free_blocks_count = %" PRIu32 "\n",
	       (uint32_t)stats.free_blocks_count);
	printf("block_size = %" PRIu32 "\n", stats.block_size);
	printf("block_group_count = %" PRIu32 "\n", stats.block_group_count);
	printf("blocks_per_group= %" PRIu32 "\n", stats.blocks_per_group);
	printf("inodes_per_group = %" PRIu32 "\n", stats.inodes_per_group);
	printf("volume_name = %s\n", stats.volume_name);
	printf("********************\n");
}

void test_lwext4_block_stats(void)
{
	if (!bd)
		return;

	printf("********************\n");
	printf("ext4 blockdev stats\n");
	printf("bdev->bread_ctr = %" PRIu32 "\n", bd->bdif->bread_ctr);
	printf("bdev->bwrite_ctr = %" PRIu32 "\n", bd->bdif->bwrite_ctr);

	printf("bcache->ref_blocks = %" PRIu32 "\n", bd->bc->ref_blocks);
	printf("bcache->max_ref_blocks = %" PRIu32 "\n", bd->bc->max_ref_blocks);
	printf("bcache->lru_ctr = %" PRIu32 "\n", bd->bc->lru_ctr);

	printf("\n");

	printf("********************\n");
}

bool test_lwext4_dir_test(int len)
{
	ext4_file f;
	int r;
	int i;
	char path[64];
	long int diff;
	long int stop;
	long int start;

	printf("test_lwext4_dir_test: %d\n", len);
	io_timings_clear();
	start = get_ms();

	printf("directory create: /mp/dir1\n");
	r = ext4_dir_mk("/mp/dir1");
	if (r != EOK) {
		printf("ext4_dir_mk: rc = %d\n", r);
		return false;
	}

	printf("add files to: /mp/dir1\n");
	for (i = 0; i < len; ++i) {
		sprintf(path, "/mp/dir1/f%d", i);
		r = ext4_fopen(&f, path, "wb");
		if (r != EOK) {
			printf("ext4_fopen: rc = %d\n", r);
			return false;
		}
	}

	stop = get_ms();
	diff = stop - start;
	test_lwext4_dir_ls("/mp/dir1");
	printf("test_lwext4_dir_test: time: %d ms\n", (int)diff);
	printf("test_lwext4_dir_test: av: %d ms/entry\n", (int)diff / (len + 1));
	printf_io_timings(diff);
	return true;
}

static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
{
	size_t i;
	for (i = 0; i < len; ++i) {
		if (b[i] != c)
			return c - b[i];
	}

	return 0;
}

bool test_lwext4_file_test(uint8_t *rw_buff, uint32_t rw_size, uint32_t rw_count)
{
	int r;
	size_t size;
	uint32_t i;
	long int start;
	long int stop;
	long int diff;
	uint32_t kbps;
	uint64_t size_bytes;

	ext4_file f;

	printf("file_test:\n");
	printf("  rw size: %" PRIu32 "\n", rw_size);
	printf("  rw count: %" PRIu32 "\n", rw_count);

	/*Add hello world file.*/
	r = ext4_fopen(&f, "/mp/hello.txt", "wb");
	r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
	r = ext4_fclose(&f);

	io_timings_clear();
	start = get_ms();
	r = ext4_fopen(&f, "/mp/test1", "wb");
	if (r != EOK) {
		printf("ext4_fopen ERROR = %d\n", r);
		return false;
	}

	printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_size,
	       rw_count);
	for (i = 0; i < rw_count; ++i) {

		memset(rw_buff, i % 10 + '0', rw_size);

		r = ext4_fwrite(&f, rw_buff, rw_size, &size);

		if ((r != EOK) || (size != rw_size))
			break;
	}

	if (i != rw_count) {
		printf("  file_test: rw_count = %" PRIu32 "\n", i);
		return false;
	}

	stop = get_ms();
	diff = stop - start;
	size_bytes = rw_size * rw_count;
	size_bytes = (size_bytes * 1000) / 1024;
	kbps = (size_bytes) / (diff + 1);
	printf("  write time: %d ms\n", (int)diff);
	printf("  write speed: %" PRIu32 " KB/s\n", kbps);
	printf_io_timings(diff);
	r = ext4_fclose(&f);

	io_timings_clear();
	start = get_ms();
	r = ext4_fopen(&f, "/mp/test1", "r+");
	if (r != EOK) {
		printf("ext4_fopen ERROR = %d\n", r);
		return false;
	}

	printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_size, rw_count);

	for (i = 0; i < rw_count; ++i) {
		r = ext4_fread(&f, rw_buff, rw_size, &size);

		if ((r != EOK) || (size != rw_size))
			break;

		if (verify_buf(rw_buff, rw_size, i % 10 + '0'))
			break;
	}

	if (i != rw_count) {
		printf("  file_test: rw_count = %" PRIu32 "\n", i);
		return false;
	}

	stop = get_ms();
	diff = stop - start;
	size_bytes = rw_size * rw_count;
	size_bytes = (size_bytes * 1000) / 1024;
	kbps = (size_bytes) / (diff + 1);
	printf("  read time: %d ms\n", (int)diff);
	printf("  read speed: %d KB/s\n", (int)kbps);
	printf_io_timings(diff);

	r = ext4_fclose(&f);
	return true;
}
void test_lwext4_cleanup(void)
{
	long int start;
	long int stop;
	long int diff;
	int r;

	printf("\ncleanup:\n");
	r = ext4_fremove("/mp/hello.txt");
	if (r != EOK && r != ENOENT) {
		printf("ext4_fremove error: rc = %d\n", r);
	}

	printf("remove /mp/test1\n");
	r = ext4_fremove("/mp/test1");
	if (r != EOK && r != ENOENT) {
		printf("ext4_fremove error: rc = %d\n", r);
	}

	printf("remove /mp/dir1\n");
	io_timings_clear();
	start = get_ms();
	r = ext4_dir_rm("/mp/dir1");
	if (r != EOK && r != ENOENT) {
		printf("ext4_fremove ext4_dir_rm: rc = %d\n", r);
	}
	stop = get_ms();
	diff = stop - start;
	printf("cleanup: time: %d ms\n", (int)diff);
	printf_io_timings(diff);
}

bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
{
	int r;

	bc = bcache;
	bd = bdev;

	if (!bd) {
		printf("test_lwext4_mount: no block device\n");
		return false;
	}

	ext4_dmask_set(DEBUG_ALL);

	r = ext4_device_register(bd, "ext4_fs");
	if (r != EOK) {
		printf("ext4_device_register: rc = %d\n", r);
		return false;
	}

	r = ext4_mount("ext4_fs", "/mp/", false);
	if (r != EOK) {
		printf("ext4_mount: rc = %d\n", r);
		return false;
	}

	r = ext4_recover("/mp/");
	if (r != EOK && r != ENOTSUP) {
		printf("ext4_recover: rc = %d\n", r);
		return false;
	}

	r = ext4_journal_start("/mp/");
	if (r != EOK) {
		printf("ext4_journal_start: rc = %d\n", r);
		return false;
	}

	ext4_cache_write_back("/mp/", 1);
	return true;
}

bool test_lwext4_umount(void)
{
	int r;

	ext4_cache_write_back("/mp/", 0);

	r = ext4_journal_stop("/mp/");
	if (r != EOK) {
		printf("ext4_journal_stop: fail %d", r);
		return false;
	}

	r = ext4_umount("/mp/");
	if (r != EOK) {
		printf("ext4_umount: fail %d", r);
		return false;
	}
	return true;
}