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