summaryrefslogtreecommitdiffstats
path: root/test-file-zero-alloc-speed.c
blob: 8e8fe5c8037c4fcf91ab64823bc7803380687a8c (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
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <asm/unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>

#define GB (1024 * 1024 * 1024)

int do_posix_fallocate(char *name, off_t len)
{
	int r, fd;
	struct timeval tv1, tv2;

	unlink(name);
	fd = open(name, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
	if (fd < 0) {
		perror("open");
		return -1;
	}

	printf("posix-fallocate run time:\n");
	gettimeofday(&tv1, NULL);
	r = posix_fallocate(fd, 0, len);
	close(fd);
	gettimeofday(&tv2, NULL);
	if (r < 0)
		return r;
	printf("\tseconds:microseconds: %llu:%llu\n", tv1.tv_sec, tv1.tv_usec);
	printf("\tseconds:microseconds: %llu:%llu\n", tv2.tv_sec, tv2.tv_usec);
	printf("\t(approx %us)\n", tv2.tv_sec - tv1.tv_sec);

	return 0;
}

#if 1
int do_fallocate(char *name, off_t len)
{
	int r, fd;
	struct timeval tv1, tv2;

	unlink(name);
	fd = open(name, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
	if (fd < 0) {
		perror("open");
		return -1;
	}

	printf("fallocate run time:\n");
	gettimeofday(&tv1, NULL);
	r = syscall(__NR_fallocate, fd, 0, len);
	close(fd);
	gettimeofday(&tv2, NULL);
	if (r < 0) {
		perror("fallocate");
		return r;
	}
	printf("\tseconds:microseconds: %llu:%llu\n", tv1.tv_sec, tv1.tv_usec);
	printf("\tseconds:microseconds: %llu:%llu\n", tv2.tv_sec, tv2.tv_usec);
	printf("\t(approx %us)\n", tv2.tv_sec - tv1.tv_sec);

	return 0;
}
#endif

int do_mmap(char *name, off_t len)
{
	int fd;
	struct timeval tv1, tv2;
	char *addr;

	unlink(name);
	fd = open(name, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
	if (fd < 0) {
		perror("open");
		return -1;
	}
	/* memset has to have the mmap'ed file backed by something on
	 * disk -- bus error otherwise */
	lseek(fd, len - 1, SEEK_SET);
	write(fd, "0", 1);

	addr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	if (addr == MAP_FAILED) {
		perror("mmap");
		return -2;
	}

	printf("mmap run time:\n");
	gettimeofday(&tv1, NULL);
	memset(addr, 0, len);
	munmap(addr, len);
	close(fd);
	gettimeofday(&tv2, NULL);

	printf("\tseconds:microseconds: %llu:%llu\n", tv1.tv_sec, tv1.tv_usec);
	printf("\tseconds:microseconds: %llu:%llu\n", tv2.tv_sec, tv2.tv_usec);
	printf("\t(approx %us)\n", tv2.tv_sec - tv1.tv_sec);

	return 0;
}

int do_write_chunks(char *name, off_t len, size_t chunk_size)
{
	int fd;
	struct timeval tv1, tv2;

	unsigned long long remain = len;
	char *zeros;

	zeros = calloc(1, len);

	unlink(name);
	fd = open(name, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
	if (fd < 0) {
		perror("open");
		return -1;
	}

	printf("%llu-sized chunk run time:\n", chunk_size);
	gettimeofday(&tv1, NULL);
	while (remain) {
		int bytes = chunk_size;
		if (bytes > remain)
			bytes = remain;
		if ((bytes = write(fd, zeros, bytes)) < 0) {
			perror("write");
			return -2;
		}
		remain -= bytes;
	}
	close(fd);
	gettimeofday(&tv2, NULL);

	printf("\tseconds:microseconds: %llu:%llu\n", tv1.tv_sec, tv1.tv_usec);
	printf("\tseconds:microseconds: %llu:%llu\n", tv2.tv_sec, tv2.tv_usec);
	printf("\t(approx %us)\n", tv2.tv_sec - tv1.tv_sec);
	free(zeros);
	
	return 0;
}

int main(int argc, char **argv)
{
	char *basename;
	char *filename;

	if (argc < 2) {
		printf("usage: %s base-dir\n", argv[0]);
		return -1;
	}

	basename = argv[1];

	sprintf(filename, "%s/file-pf", basename);
	do_posix_fallocate(filename, 1 * GB);

#if 0
	sprintf(filename, "%s/file-pf", basename);
	do_fallocate(filename, 1 * GB);
#endif

	sprintf(filename, "%s/file-mmap", basename);
	do_mmap(filename, 1 * GB);

	sprintf(filename, "%s/file-chunk4", basename);
	do_write_chunks(filename, 1 * GB, 4 * 1024);

	sprintf(filename, "%s/file-chunk8", basename);
	do_write_chunks(filename, 1 * GB, 8 * 1024);

	return 0;
}