]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/perf_index/test_file_helper.c
1 #include "test_file_helper.h"
12 static char readbuff
[4096];
13 static char writebuff
[4096];
14 static int* fds
= NULL
;
17 setup_tempdir(char* buf
)
19 strcpy(buf
, "/tmp/perfindex.XXXXXX");
24 cleanup_tempdir(char* path
)
30 test_file_create(char* path
, int thread_id
, int num_threads
, long long length
)
35 char filepath
[MAXPATHLEN
];
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");
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");
51 return PERFINDEX_SUCCESS
;
55 test_file_read_setup(char* path
, int num_threads
, long long length
, long long max_file_size
)
58 char filepath
[MAXPATHLEN
];
63 if (max_file_size
== 0) {
64 max_file_size
= MAXFILESIZE
;
67 left
= MIN(length
, max_file_size
/ num_threads
);
69 snprintf(filepath
, sizeof(filepath
), "%s/file_read", path
);
70 fd
= open(filepath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
72 VERIFY(fd
>= 0, "open failed");
74 bzero(readbuff
, sizeof(readbuff
));
77 writelen
= sizeof(readbuff
) < left
? sizeof(readbuff
) : left
;
78 retval
= write(fd
, readbuff
, writelen
);
79 VERIFY(retval
== writelen
, "write failed");
83 return PERFINDEX_SUCCESS
;
87 test_file_read(char* path
, int thread_id
, int num_threads
, long long length
, long long max_file_size
)
90 size_t file_offset
= 0;
94 char filepath
[MAXPATHLEN
];
98 if (max_file_size
== 0) {
99 max_file_size
= MAXFILESIZE
;
101 filesize
= MIN(length
, max_file_size
/ num_threads
);
103 snprintf(filepath
, sizeof(filepath
), "%s/file_read", path
);
104 fd
= open(filepath
, O_RDONLY
);
105 VERIFY(fd
>= 0, "open failed");
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
);
113 VERIFY(retval
>= 0, "lseek failed");
118 retval
= read(fd
, readbuff
, readlen
);
119 VERIFY(retval
== readlen
, "read failed");
121 file_offset
+= readlen
;
123 return PERFINDEX_SUCCESS
;
127 test_file_read_cleanup(char* path
, int num_threads
, long long length
)
129 char filepath
[MAXPATHLEN
];
132 snprintf(filepath
, sizeof(filepath
), "%s/file_read", path
);
133 retval
= unlink(filepath
);
134 VERIFY(retval
== 0, "unlink failed");
136 return PERFINDEX_SUCCESS
;
140 test_file_write_setup(char* path
, int num_threads
, long long length
)
143 char filepath
[MAXPATHLEN
];
146 fds
= (int*)malloc(sizeof(int) * num_threads
);
147 VERIFY(fds
, "malloc failed");
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);
160 bzero(writebuff
, sizeof(writebuff
));
162 return PERFINDEX_SUCCESS
;
166 test_file_write(char* path
, int thread_id
, int num_threads
, long long length
, long long max_file_size
)
169 size_t file_offset
= 0;
172 int fd
= fds
[thread_id
];
174 if (max_file_size
== 0) {
175 max_file_size
= MAXFILESIZE
;
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");
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");
192 return PERFINDEX_SUCCESS
;
197 test_file_write_cleanup(char* path
, int num_threads
, long long length
)
200 char filepath
[MAXPATHLEN
];
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");
209 return PERFINDEX_SUCCESS
;