]>
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
;
16 char* setup_tempdir(char* buf
) {
17 strcpy(buf
, "/tmp/perfindex.XXXXXX");
21 int cleanup_tempdir(char* path
) {
25 int test_file_create(char* path
, int thread_id
, int num_threads
, long long length
) {
29 char filepath
[MAXPATHLEN
];
31 for(i
=0; i
<length
; i
++) {
32 snprintf(filepath
, MAXPATHLEN
, "%s/file_create-%d-%lld", path
, thread_id
, i
);
33 fd
= open(filepath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
34 VERIFY(fd
>= 0, "open failed");
39 for(i
=0; i
<length
; i
++) {
40 snprintf(filepath
, MAXPATHLEN
, "%s/file_create-%d-%lld", path
, thread_id
, i
);
41 retval
= unlink(filepath
);
42 VERIFY(retval
== 0, "unlink failed");
45 return PERFINDEX_SUCCESS
;
48 int test_file_read_setup(char* path
, int num_threads
, long long length
, long long max_file_size
) {
50 char filepath
[MAXPATHLEN
];
55 if(max_file_size
== 0)
56 max_file_size
= MAXFILESIZE
;
58 left
= MIN(length
, max_file_size
/num_threads
);
60 snprintf(filepath
, sizeof(filepath
), "%s/file_read", path
);
61 fd
= open(filepath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
63 VERIFY(fd
>= 0, "open failed");
65 bzero(readbuff
, sizeof(readbuff
));
68 writelen
= sizeof(readbuff
) < left
? sizeof(readbuff
) : left
;
69 retval
= write(fd
, readbuff
, writelen
);
70 VERIFY(retval
== writelen
, "write failed");
74 return PERFINDEX_SUCCESS
;
77 int test_file_read(char* path
, int thread_id
, int num_threads
, long long length
, long long max_file_size
) {
79 size_t file_offset
= 0;
83 char filepath
[MAXPATHLEN
];
87 if(max_file_size
== 0)
88 max_file_size
= MAXFILESIZE
;
89 filesize
= MIN(length
, max_file_size
/num_threads
);
91 snprintf(filepath
, sizeof(filepath
), "%s/file_read", path
);
92 fd
= open(filepath
, O_RDONLY
);
93 VERIFY(fd
>= 0, "open failed");
95 for(left
=length
; left
>0;) {
96 readlen
= sizeof(readbuff
) < left
? sizeof(readbuff
) : left
;
97 if(file_offset
+readlen
> filesize
) {
98 retval
= lseek(fd
, 0, SEEK_SET
);
101 VERIFY(retval
>= 0, "lseek failed");
106 retval
= read(fd
, readbuff
, readlen
);
107 VERIFY(retval
== readlen
, "read failed");
109 file_offset
+= readlen
;
111 return PERFINDEX_SUCCESS
;
114 int test_file_read_cleanup(char* path
, int num_threads
, long long length
) {
115 char filepath
[MAXPATHLEN
];
118 snprintf(filepath
, sizeof(filepath
), "%s/file_read", path
);
119 retval
= unlink(filepath
);
120 VERIFY(retval
== 0, "unlink failed");
122 return PERFINDEX_SUCCESS
;
125 int test_file_write_setup(char* path
, int num_threads
, long long length
) {
127 char filepath
[MAXPATHLEN
];
130 fds
= (int*)malloc(sizeof(int)*num_threads
);
131 VERIFY(fds
, "malloc failed");
134 for(i
=0; i
<num_threads
; i
++) {
135 snprintf(filepath
, sizeof(filepath
), "%s/file_write-%d", path
, i
);
136 fds
[i
] = open(filepath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
144 bzero(writebuff
, sizeof(writebuff
));
146 return PERFINDEX_SUCCESS
;
149 int test_file_write(char* path
, int thread_id
, int num_threads
, long long length
, long long max_file_size
) {
151 size_t file_offset
= 0;
154 int fd
= fds
[thread_id
];
156 if(max_file_size
== 0)
157 max_file_size
= MAXFILESIZE
;
159 for(left
=length
; left
>0;) {
160 writelen
= sizeof(writebuff
) < left
? sizeof(writebuff
) : left
;
161 retval
= write(fd
, writebuff
, writelen
);
162 VERIFY(retval
== writelen
, "write failed");
165 file_offset
+= writelen
;
166 if(file_offset
>max_file_size
/num_threads
) {
167 retval
= lseek(fd
, 0, SEEK_SET
);
168 VERIFY(retval
>= 0, "leeks failed");
173 return PERFINDEX_SUCCESS
;
177 int test_file_write_cleanup(char* path
, int num_threads
, long long length
) {
179 char filepath
[MAXPATHLEN
];
182 for(i
=0; i
<num_threads
; i
++) {
183 snprintf(filepath
, sizeof(filepath
), "%s/file_write-%d", path
, i
);
184 retval
= unlink(filepath
);
185 VERIFY(retval
== 0, "unlink failed");
188 return PERFINDEX_SUCCESS
;