aboutsummaryrefslogtreecommitdiff
path: root/tests/fs-tests/stress/atoms/rndwrite00.c
blob: 7dba6bed01de8257d35e9e6c20848ff70c8c669d (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
/*
 * Copyright (C) 2007 Nokia Corporation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Author: Adrian Hunter
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>

#include "tests.h"

#define BLOCK_SIZE 32768
#define BUFFER_SIZE 32768

static void check_file(int fd, char *data, size_t length)
{
	ssize_t n, i;
	char buf[BUFFER_SIZE];

	CHECK(lseek(fd, 0, SEEK_SET) != -1);
	n = 0;
	for (;;) {
		i = read(fd, buf, BUFFER_SIZE);
		CHECK(i >= 0);
		if (i == 0)
			break;
		CHECK(memcmp(buf, data + n, i) == 0);
		n += i;
	}
	CHECK(n == length);
}

static void rndwrite00(void)
{
	int fd;
	pid_t pid;
	ssize_t written;
	size_t remains;
	size_t block;
	size_t actual_size;
	size_t check_every;
	char *data, *p, *q;
	off_t offset;
	size_t size;
	int64_t repeat;
	char file_name[256];
	char buf[4096];

	/* Create file */
	pid = getpid();
	tests_cat_pid(file_name, "rndwrite00_test_file_", pid);
	fd = open(file_name, O_CREAT | O_RDWR | O_TRUNC,
		S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
	CHECK(fd != -1);
	/* Allocate memory to hold file data */
	CHECK(tests_size_parameter > 0);
	CHECK(tests_size_parameter <= SIZE_MAX);
	data = (char *) malloc(tests_size_parameter);
	CHECK(data != NULL);
	/* Fill with random data */
	srand(pid);
	for (p = data, q = data + tests_size_parameter; p != q; ++p)
		*p = rand();
	/* Write to file */
	p = data;
	remains = tests_size_parameter;
	while (remains > 0) {
		if (remains > BLOCK_SIZE)
			block = BLOCK_SIZE;
		else
			block = remains;
		written = write(fd, p, block);
		if (written <= 0) {
			CHECK(errno == ENOSPC); /* File system full */
			errno = 0;
			break;
		}
		remains -= written;
		p += written;
	}
	actual_size = p - data;
	/* Repeating bit */
	repeat = tests_repeat_parameter;
	check_every = actual_size / 8192;
	for (;;) {
		offset = tests_random_no(actual_size);
		size = tests_random_no(4096);
		/* Don't change the file size */
		if (offset + size > actual_size)
			size = actual_size - offset;
		if (!size)
			continue;
		for (p = buf, q = p + size; p != q; ++p)
			*p = rand();
		CHECK(lseek(fd, offset, SEEK_SET) != -1);
		written = write(fd, buf, size);
		if (written <= 0) {
			CHECK(errno == ENOSPC); /* File system full */
			errno = 0;
		} else
			memcpy(data + offset, buf, written);
		/* Break if repeat count exceeded */
		if (tests_repeat_parameter > 0 && --repeat <= 0)
			break;
		if (repeat % check_every == 0)
			check_file(fd, data, actual_size);
		/* Sleep */
		if (tests_sleep_parameter > 0) {
			unsigned us = tests_sleep_parameter * 1000;
			unsigned rand_divisor = RAND_MAX / us;
			unsigned s = (us / 2) + (rand() / rand_divisor);
			usleep(s);
		}
	}
	/* Check and close file */
	check_file(fd, data, actual_size);
	CHECK(close(fd) != -1);
	if (tests_delete_flag)
		CHECK(unlink(file_name) != -1);
}

/* Title of this test */

static const char *rndwrite00_get_title(void)
{
	return "Randomly write a large test file";
}

/* Description of this test */

static const char *rndwrite00_get_description(void)
{
	return
		"Create a file named rndwrite00_test_file_pid, where " \
		"pid is the process id. " \
		"The file is filled with random data. " \
		"The size of the file is given by the -z or --size option, " \
		"otherwise it defaults to 1000000. " \
		"Then a randomly sized block of random data is written at a " \
		"random location in the file. "\
		"The block size is always in the range 1 to 4095. " \
		"If a sleep value is specified, the process sleeps. " \
		"The number of writes is given by the repeat count. " \
		"The repeat count is set by the -n or --repeat option, " \
		"otherwise it defaults to 10000. " \
		"A repeat count of zero repeats forever. " \
		"The sleep value is given by the -p or --sleep option, " \
		"otherwise it defaults to 0. "
		"Sleep is specified in milliseconds. " \
		"Periodically the data in the file is checked with a copy " \
		"held in memory. " \
		"If the delete option is specified the file is finally " \
		"deleted.";
}

int main(int argc, char *argv[])
{
	int run_test;

	/* Set default test file size */
	tests_size_parameter = 1000000;

	/* Set default test repetition */
	tests_repeat_parameter = 10000;

	/* Set default test sleep */
	tests_sleep_parameter = 0;

	/* Handle common arguments */
	run_test = tests_get_args(argc, argv, rndwrite00_get_title(),
			rndwrite00_get_description(), "zne");
	if (!run_test)
		return 1;
	/* Change directory to the file system and check it is ok for testing */
	tests_check_test_file_system();
	/* Do the actual test */
	rndwrite00();
	return 0;
}