summaryrefslogtreecommitdiffstats
path: root/fsstress.c
blob: b0567b5febc8c38c40a232c3da73d73495927fba (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
/*
  The purpose of this program is simulating a typical workload in short time.
  Especially filesystem read's are interesting for us as they can lead
  to read disturb on the NAND flash. UBIFS utilizes the page cache,
  therefore reading the same file multiple times will not lead to multiple
  reads at MTD level.
  But the page cache is not an infinite resource and the kernel is allowed to
  shrink/flush it at any time, this can lead to reads on MTD level again.
  To simulate that the script regularly flushes the page cache and the inode
  cache.
 
  TODO:
  - report results from UBI stats interface
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <getopt.h>
#include <fcntl.h>

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>

#define WRITE_SYNC 1
#define WRITE_APPEND 2

#define F_BULK_WRITE 1
#define F_WRITE_MUCH 2

static const char* outdir = "testdir";
extern char* __progname;

static const struct option options[] = {
	{"bulk", 0, 0, 'b'},
	{"writemuch", 0, 0, 'w'},
	{"runs", 1, 0, 'r'},
	{"files", 1, 0, 'n'},
	{"big-files", 1, 0, 'N'},
	{"help", 0, 0, 'h'},
	{"min-mb", 1, 0, 'm'},
	{"max-mb", 1, 0, 'M'},
	{"min-bulk-mb", 1, 0, 'f'},
	{"max-bulk-mb", 1, 0, 'F'},
	{NULL, 0, NULL, 0}
};

static int tryopen(const char* path, int flags, int mode)
{
	int fd = open(path, flags, mode);

	if (fd<0)
		fprintf(stderr, "Opening %s: %s\n", path, strerror(errno));

	return fd;
}

static int copy_mb(const char* name, int flags, int infd, size_t megs)
{
	int outfd, status=0;
	char buffer[4096];
	ssize_t count;
	size_t i, j;

	outfd = tryopen(name, (flags & WRITE_APPEND) ? O_WRONLY|O_APPEND :
					O_WRONLY|O_CREAT|O_TRUNC, 0644);

	if (outfd<0)
		return 0;

	count = read(infd, buffer, sizeof(buffer));

	if (count!=sizeof(buffer))
		goto fail;

	for (i=0; i<megs; ++i) {
		for (j=0; j<((1024*1024)/sizeof(buffer)); ++j) {
			count = write(outfd, buffer, sizeof(buffer));

			if (count!=sizeof(buffer))
				goto fail;
		}
	}

	if (flags & WRITE_SYNC)
		sync();

	status = 1;
out:
	close(outfd);
	return status;
fail:
	fprintf(stderr, "copy_mb %s: %s\n", name, count<0 ? strerror(errno) :
			(count==0 ? "EOF" : "could not transfer entire 1M block"));
	goto out;
}

static void drop_caches(void)
{
	int fd = open("/proc/sys/vm/drop_caches", O_RDONLY, 0);
	const char* str = "3\n";

	write(fd, str, strlen(str));
	close(fd);
}

static void write_files(const char* prefix, int flags, int count,
						int minsize, int maxsize)
{
	int i, size, infd;
	char buffer[64];

	infd = tryopen("/dev/urandom", O_RDONLY, 0);

	if (infd<0)
		exit(EXIT_FAILURE);

	for (i=0; i<count; ++i) {
		size = rand();
		size = (size<0 ? -size : size) % (maxsize - minsize) + minsize;

		if (!size)
			continue;

		sprintf(buffer, "%s/%s%d", outdir, prefix, i);

		if (!copy_mb(buffer, flags, infd, size))
			exit(EXIT_FAILURE);
	}

	close(infd);
}

static void write_rand_file(int flags, int count)
{
	char buffer[64];
	int i, infd;

	infd = tryopen("/dev/urandom", O_RDONLY, 0);

	if (infd<0)
		exit(EXIT_FAILURE);

	i = rand();
	sprintf(buffer, "%s/smallfile%d", outdir, (i<0?-i:i) % count);

	if (!copy_mb(buffer, flags, infd, 1))
		exit(EXIT_FAILURE);

	close(infd);
}

static void read_files(const char* prefix, int count)
{
	char buffer[1024];
	int i, fd;

	for (i=0; i<count; ++i) {
		sprintf(buffer, "%s/%s%d", outdir, prefix, i);
		fd = tryopen(buffer, O_RDONLY, 0);
		if (fd<0)
			exit(EXIT_FAILURE);
		while (read(fd, buffer, sizeof(buffer))>0) { }
		close(fd);
	}
}

static void usage(void)
{
	printf( "Usage: %s [arguments]\n", __progname );

	puts(
	"  -b, --bulk              If set, perform bulk write test.\n"
	"  -w, --writemuch         If set, perform write stress test.\n"
	"  -r, --runs <count>      Specify the number of test iterations.\n"
	"  -n, --files <count>     Specify the number of small files to create.\n"
	"  -N, --big-files <count> Specify the number of large files to create.\n"
	"  -h, --help              Display this text and exit.\n");
	puts(
	"  --min-mb <count>        The minimum size (MiB) of small files.\n"
	"  --max-mb <count>        The maximum size (MiB) of small files.\n"
	"  --min-bulk-mb <count>   The minimum size (MiB) of large files.\n"
	"  --max-bulk-mb <count>   The maximum size (MiB) of large files.");

	exit(EXIT_SUCCESS);
}

int main(int argc, char** argv)
{
	int i, j, idx=0, max_mb=5, min_mb=1, min_bulk_mb=10, max_bulk_mb=20;
	int flags=0, runs=10, bigfiles=20, files=100;
	struct stat sb;

	while ((i=getopt_long(argc, argv, "bwr:n:N:h", options, &idx))!=-1) {
		switch (i) {
		case 'b': flags |= F_BULK_WRITE; break;
		case 'w': flags |= F_WRITE_MUCH; break;
		case 'h': usage(); break;
		case 'r': runs = strtol(optarg, NULL, 10); break;
		case 'n': files = strtol(optarg, NULL, 10); break;
		case 'N': bigfiles = strtol(optarg, NULL, 10); break;
		case 'm': min_mb = strtol(optarg, NULL, 10); break;
		case 'M': max_mb = strtol(optarg, NULL, 10); break;
		case 'f': min_bulk_mb = strtol(optarg, NULL, 10); break;
		case 'F': max_bulk_mb = strtol(optarg, NULL, 10); break;
		default:
			fputs( "Unknown option\n", stderr );
			return EXIT_FAILURE;
		}
	}

	if (stat(outdir, &sb)!=0) {
		if (mkdir(outdir, 0755)!=0) {
			fprintf( stderr, "mkdir %s: %s", outdir, strerror(errno) );
			return EXIT_FAILURE;
		}
	} else if (!S_ISDIR(sb.st_mode)) {
		fprintf( stderr, "'%s' exists and is not a directory!\n", outdir );
		return EXIT_FAILURE;
	}

	srand(time(NULL));

	for (i=0; i<runs; ++i) {
		drop_caches();
		write_files("smallfile", 0, files, min_mb, max_mb);

		drop_caches();
		write_files("smallfile", WRITE_APPEND, files, min_mb, max_mb);

		for (j=0; j<20; ++j)
			read_files("smallfile", files);

		for (j=0; j<20; ++j) {
			drop_caches();
			read_files("smallfile", files);
		}

		if (files > 0) {
			for (j=0; j<20; ++j) {
				read_files("smallfile", files);
				write_rand_file(0, files);
			}

			for (j=0; j<20; ++j) {
				read_files("smallfile", files);
				write_rand_file(WRITE_SYNC, files);
			}
		}

		if (flags & F_WRITE_MUCH) {
			for (j=0; j<20; ++j) {
				write_files("smallfile", WRITE_SYNC, files, min_mb, max_mb);
				read_files("smallfile", files);
			}

			for (j=0; j<20; ++j) {
				write_files("smallfile", WRITE_APPEND|WRITE_SYNC, files,
							min_mb, max_mb);
				read_files("smallfile", files);
			}
		}

		if (flags & F_BULK_WRITE) {
			for (j=0; j<20; ++j) {
				write_files("bigfile", WRITE_SYNC, bigfiles,
							min_bulk_mb, max_bulk_mb);
				read_files("bigfile", bigfiles);
			}
		}
	}
	return EXIT_SUCCESS;
}