1*1031c584SApple OSS Distributions #include "test_file_helper.h"
2*1031c584SApple OSS Distributions #include "fail.h"
3*1031c584SApple OSS Distributions #include <stdlib.h>
4*1031c584SApple OSS Distributions #include <fcntl.h>
5*1031c584SApple OSS Distributions #include <sys/param.h>
6*1031c584SApple OSS Distributions #include <assert.h>
7*1031c584SApple OSS Distributions #include <stdio.h>
8*1031c584SApple OSS Distributions #include <strings.h>
9*1031c584SApple OSS Distributions #include <unistd.h>
10*1031c584SApple OSS Distributions #include <pthread.h>
11*1031c584SApple OSS Distributions
12*1031c584SApple OSS Distributions static char readbuff[4096];
13*1031c584SApple OSS Distributions static char writebuff[4096];
14*1031c584SApple OSS Distributions static int* fds = NULL;
15*1031c584SApple OSS Distributions
16*1031c584SApple OSS Distributions char*
setup_tempdir(char * buf)17*1031c584SApple OSS Distributions setup_tempdir(char* buf)
18*1031c584SApple OSS Distributions {
19*1031c584SApple OSS Distributions strcpy(buf, "/tmp/perfindex.XXXXXX");
20*1031c584SApple OSS Distributions return mkdtemp(buf);
21*1031c584SApple OSS Distributions }
22*1031c584SApple OSS Distributions
23*1031c584SApple OSS Distributions int
cleanup_tempdir(char * path)24*1031c584SApple OSS Distributions cleanup_tempdir(char* path)
25*1031c584SApple OSS Distributions {
26*1031c584SApple OSS Distributions return rmdir(path);
27*1031c584SApple OSS Distributions }
28*1031c584SApple OSS Distributions
29*1031c584SApple OSS Distributions int
test_file_create(char * path,int thread_id,int num_threads,long long length)30*1031c584SApple OSS Distributions test_file_create(char* path, int thread_id, int num_threads, long long length)
31*1031c584SApple OSS Distributions {
32*1031c584SApple OSS Distributions long long i;
33*1031c584SApple OSS Distributions int fd;
34*1031c584SApple OSS Distributions int retval;
35*1031c584SApple OSS Distributions char filepath[MAXPATHLEN];
36*1031c584SApple OSS Distributions
37*1031c584SApple OSS Distributions for (i = 0; i < length; i++) {
38*1031c584SApple OSS Distributions snprintf(filepath, MAXPATHLEN, "%s/file_create-%d-%lld", path, thread_id, i);
39*1031c584SApple OSS Distributions fd = open(filepath, O_CREAT | O_EXCL | O_WRONLY, 0644);
40*1031c584SApple OSS Distributions VERIFY(fd >= 0, "open failed");
41*1031c584SApple OSS Distributions
42*1031c584SApple OSS Distributions close(fd);
43*1031c584SApple OSS Distributions }
44*1031c584SApple OSS Distributions
45*1031c584SApple OSS Distributions for (i = 0; i < length; i++) {
46*1031c584SApple OSS Distributions snprintf(filepath, MAXPATHLEN, "%s/file_create-%d-%lld", path, thread_id, i);
47*1031c584SApple OSS Distributions retval = unlink(filepath);
48*1031c584SApple OSS Distributions VERIFY(retval == 0, "unlink failed");
49*1031c584SApple OSS Distributions }
50*1031c584SApple OSS Distributions
51*1031c584SApple OSS Distributions return PERFINDEX_SUCCESS;
52*1031c584SApple OSS Distributions }
53*1031c584SApple OSS Distributions
54*1031c584SApple OSS Distributions int
test_file_read_setup(char * path,int num_threads,long long length,long long max_file_size)55*1031c584SApple OSS Distributions test_file_read_setup(char* path, int num_threads, long long length, long long max_file_size)
56*1031c584SApple OSS Distributions {
57*1031c584SApple OSS Distributions int fd;
58*1031c584SApple OSS Distributions char filepath[MAXPATHLEN];
59*1031c584SApple OSS Distributions long long left;
60*1031c584SApple OSS Distributions int retval;
61*1031c584SApple OSS Distributions size_t writelen;
62*1031c584SApple OSS Distributions
63*1031c584SApple OSS Distributions if (max_file_size == 0) {
64*1031c584SApple OSS Distributions max_file_size = MAXFILESIZE;
65*1031c584SApple OSS Distributions }
66*1031c584SApple OSS Distributions
67*1031c584SApple OSS Distributions left = MIN(length, max_file_size / num_threads);
68*1031c584SApple OSS Distributions
69*1031c584SApple OSS Distributions snprintf(filepath, sizeof(filepath), "%s/file_read", path);
70*1031c584SApple OSS Distributions fd = open(filepath, O_CREAT | O_EXCL | O_WRONLY, 0644);
71*1031c584SApple OSS Distributions printf("%d\n", fd);
72*1031c584SApple OSS Distributions VERIFY(fd >= 0, "open failed");
73*1031c584SApple OSS Distributions
74*1031c584SApple OSS Distributions bzero(readbuff, sizeof(readbuff));
75*1031c584SApple OSS Distributions
76*1031c584SApple OSS Distributions while (left > 0) {
77*1031c584SApple OSS Distributions writelen = sizeof(readbuff) < left ? sizeof(readbuff) : left;
78*1031c584SApple OSS Distributions retval = write(fd, readbuff, writelen);
79*1031c584SApple OSS Distributions VERIFY(retval == writelen, "write failed");
80*1031c584SApple OSS Distributions left -= writelen;
81*1031c584SApple OSS Distributions }
82*1031c584SApple OSS Distributions
83*1031c584SApple OSS Distributions return PERFINDEX_SUCCESS;
84*1031c584SApple OSS Distributions }
85*1031c584SApple OSS Distributions
86*1031c584SApple OSS Distributions int
test_file_read(char * path,int thread_id,int num_threads,long long length,long long max_file_size)87*1031c584SApple OSS Distributions test_file_read(char* path, int thread_id, int num_threads, long long length, long long max_file_size)
88*1031c584SApple OSS Distributions {
89*1031c584SApple OSS Distributions long long left;
90*1031c584SApple OSS Distributions size_t file_offset = 0;
91*1031c584SApple OSS Distributions int readlen;
92*1031c584SApple OSS Distributions int fd;
93*1031c584SApple OSS Distributions int retval;
94*1031c584SApple OSS Distributions char filepath[MAXPATHLEN];
95*1031c584SApple OSS Distributions long long filesize;
96*1031c584SApple OSS Distributions
97*1031c584SApple OSS Distributions
98*1031c584SApple OSS Distributions if (max_file_size == 0) {
99*1031c584SApple OSS Distributions max_file_size = MAXFILESIZE;
100*1031c584SApple OSS Distributions }
101*1031c584SApple OSS Distributions filesize = MIN(length, max_file_size / num_threads);
102*1031c584SApple OSS Distributions
103*1031c584SApple OSS Distributions snprintf(filepath, sizeof(filepath), "%s/file_read", path);
104*1031c584SApple OSS Distributions fd = open(filepath, O_RDONLY);
105*1031c584SApple OSS Distributions VERIFY(fd >= 0, "open failed");
106*1031c584SApple OSS Distributions
107*1031c584SApple OSS Distributions for (left = length; left > 0;) {
108*1031c584SApple OSS Distributions readlen = sizeof(readbuff) < left ? sizeof(readbuff) : left;
109*1031c584SApple OSS Distributions if (file_offset + readlen > filesize) {
110*1031c584SApple OSS Distributions retval = lseek(fd, 0, SEEK_SET);
111*1031c584SApple OSS Distributions
112*1031c584SApple OSS Distributions
113*1031c584SApple OSS Distributions VERIFY(retval >= 0, "lseek failed");
114*1031c584SApple OSS Distributions
115*1031c584SApple OSS Distributions file_offset = 0;
116*1031c584SApple OSS Distributions continue;
117*1031c584SApple OSS Distributions }
118*1031c584SApple OSS Distributions retval = read(fd, readbuff, readlen);
119*1031c584SApple OSS Distributions VERIFY(retval == readlen, "read failed");
120*1031c584SApple OSS Distributions left -= readlen;
121*1031c584SApple OSS Distributions file_offset += readlen;
122*1031c584SApple OSS Distributions }
123*1031c584SApple OSS Distributions return PERFINDEX_SUCCESS;
124*1031c584SApple OSS Distributions }
125*1031c584SApple OSS Distributions
126*1031c584SApple OSS Distributions int
test_file_read_cleanup(char * path,int num_threads,long long length)127*1031c584SApple OSS Distributions test_file_read_cleanup(char* path, int num_threads, long long length)
128*1031c584SApple OSS Distributions {
129*1031c584SApple OSS Distributions char filepath[MAXPATHLEN];
130*1031c584SApple OSS Distributions int retval;
131*1031c584SApple OSS Distributions
132*1031c584SApple OSS Distributions snprintf(filepath, sizeof(filepath), "%s/file_read", path);
133*1031c584SApple OSS Distributions retval = unlink(filepath);
134*1031c584SApple OSS Distributions VERIFY(retval == 0, "unlink failed");
135*1031c584SApple OSS Distributions
136*1031c584SApple OSS Distributions return PERFINDEX_SUCCESS;
137*1031c584SApple OSS Distributions }
138*1031c584SApple OSS Distributions
139*1031c584SApple OSS Distributions int
test_file_write_setup(char * path,int num_threads,long long length)140*1031c584SApple OSS Distributions test_file_write_setup(char* path, int num_threads, long long length)
141*1031c584SApple OSS Distributions {
142*1031c584SApple OSS Distributions int i;
143*1031c584SApple OSS Distributions char filepath[MAXPATHLEN];
144*1031c584SApple OSS Distributions
145*1031c584SApple OSS Distributions if (fds == NULL) {
146*1031c584SApple OSS Distributions fds = (int*)malloc(sizeof(int) * num_threads);
147*1031c584SApple OSS Distributions VERIFY(fds, "malloc failed");
148*1031c584SApple OSS Distributions }
149*1031c584SApple OSS Distributions
150*1031c584SApple OSS Distributions for (i = 0; i < num_threads; i++) {
151*1031c584SApple OSS Distributions snprintf(filepath, sizeof(filepath), "%s/file_write-%d", path, i);
152*1031c584SApple OSS Distributions fds[i] = open(filepath, O_CREAT | O_EXCL | O_WRONLY, 0644);
153*1031c584SApple OSS Distributions if (fds[i] < 0) {
154*1031c584SApple OSS Distributions free(fds);
155*1031c584SApple OSS Distributions fds = NULL;
156*1031c584SApple OSS Distributions FAIL("open failed");
157*1031c584SApple OSS Distributions }
158*1031c584SApple OSS Distributions }
159*1031c584SApple OSS Distributions
160*1031c584SApple OSS Distributions bzero(writebuff, sizeof(writebuff));
161*1031c584SApple OSS Distributions
162*1031c584SApple OSS Distributions return PERFINDEX_SUCCESS;
163*1031c584SApple OSS Distributions }
164*1031c584SApple OSS Distributions
165*1031c584SApple OSS Distributions int
test_file_write(char * path,int thread_id,int num_threads,long long length,long long max_file_size)166*1031c584SApple OSS Distributions test_file_write(char* path, int thread_id, int num_threads, long long length, long long max_file_size)
167*1031c584SApple OSS Distributions {
168*1031c584SApple OSS Distributions long long left;
169*1031c584SApple OSS Distributions size_t file_offset = 0;
170*1031c584SApple OSS Distributions int writelen;
171*1031c584SApple OSS Distributions int retval;
172*1031c584SApple OSS Distributions int fd = fds[thread_id];
173*1031c584SApple OSS Distributions
174*1031c584SApple OSS Distributions if (max_file_size == 0) {
175*1031c584SApple OSS Distributions max_file_size = MAXFILESIZE;
176*1031c584SApple OSS Distributions }
177*1031c584SApple OSS Distributions
178*1031c584SApple OSS Distributions for (left = length; left > 0;) {
179*1031c584SApple OSS Distributions writelen = sizeof(writebuff) < left ? sizeof(writebuff) : left;
180*1031c584SApple OSS Distributions retval = write(fd, writebuff, writelen);
181*1031c584SApple OSS Distributions VERIFY(retval == writelen, "write failed");
182*1031c584SApple OSS Distributions
183*1031c584SApple OSS Distributions left -= writelen;
184*1031c584SApple OSS Distributions file_offset += writelen;
185*1031c584SApple OSS Distributions if (file_offset > max_file_size / num_threads) {
186*1031c584SApple OSS Distributions retval = lseek(fd, 0, SEEK_SET);
187*1031c584SApple OSS Distributions VERIFY(retval >= 0, "leeks failed");
188*1031c584SApple OSS Distributions file_offset = 0;
189*1031c584SApple OSS Distributions }
190*1031c584SApple OSS Distributions }
191*1031c584SApple OSS Distributions
192*1031c584SApple OSS Distributions return PERFINDEX_SUCCESS;
193*1031c584SApple OSS Distributions }
194*1031c584SApple OSS Distributions
195*1031c584SApple OSS Distributions
196*1031c584SApple OSS Distributions int
test_file_write_cleanup(char * path,int num_threads,long long length)197*1031c584SApple OSS Distributions test_file_write_cleanup(char* path, int num_threads, long long length)
198*1031c584SApple OSS Distributions {
199*1031c584SApple OSS Distributions int i;
200*1031c584SApple OSS Distributions char filepath[MAXPATHLEN];
201*1031c584SApple OSS Distributions int retval;
202*1031c584SApple OSS Distributions
203*1031c584SApple OSS Distributions for (i = 0; i < num_threads; i++) {
204*1031c584SApple OSS Distributions snprintf(filepath, sizeof(filepath), "%s/file_write-%d", path, i);
205*1031c584SApple OSS Distributions retval = unlink(filepath);
206*1031c584SApple OSS Distributions VERIFY(retval == 0, "unlink failed");
207*1031c584SApple OSS Distributions }
208*1031c584SApple OSS Distributions
209*1031c584SApple OSS Distributions return PERFINDEX_SUCCESS;
210*1031c584SApple OSS Distributions }
211