]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/perf_index/stress_file_read.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / tools / tests / perf_index / stress_file_read.c
CommitLineData
39236c6e
A
1#include <fcntl.h>
2#include "perf_index.h"
3#include <errno.h>
4
5#define MAXFILESIZE 8589934592L
6#define MIN(a,b) ((a)<(b) ? (a) : (b))
7
8static char readbuff[4096];
9
10void stress_file_read_init(const char *fs_path, int num_threads, long long length, long long max_file_size) {
11 int fd;
12 char filepath[MAXPATHLEN];
13 long long left;
14 size_t writelen;
15
16 if(max_file_size == 0)
17 max_file_size = MAXFILESIZE;
18
19 left = MIN(length, max_file_size/num_threads);
20
21 snprintf(filepath, sizeof(filepath), "%s/file_read", fs_path);
22 fd = open(filepath, O_CREAT | O_EXCL | O_WRONLY, 0644);
23 assert(fd > 0);
24 bzero(readbuff, sizeof(readbuff));
25
26 while(left > 0) {
27 writelen = sizeof(readbuff) < left ? sizeof(readbuff) : left;
28 assert(write(fd, readbuff, writelen) == writelen);
29 left -= writelen;
30 }
31}
32
33void stress_file_read(const char *fs_path, int thread_id, int num_threads, long long length, long long max_file_size) {
34 long long left;
35 size_t file_offset = 0;
36 int readlen;
37 int fd;
38 char filepath[MAXPATHLEN];
39 long long filesize;
40
41
42 if(max_file_size == 0)
43 max_file_size = MAXFILESIZE;
44 filesize = MIN(length, max_file_size/num_threads);
45
46 snprintf(filepath, sizeof(filepath), "%s/file_read", fs_path);
47 fd = open(filepath, O_RDONLY);
48 assert(fd > 0);
49 for(left=length; left>0;) {
50 readlen = sizeof(readbuff) < left ? sizeof(readbuff) : left;
51 if(file_offset+readlen > filesize) {
52 lseek(fd, 0, SEEK_SET);
53 file_offset = 0;
54 continue;
55 }
56 assert(read(fd, readbuff, readlen) == readlen);
57 left -= readlen;
58 file_offset += readlen;
59 }
60}
61
62void stress_file_read_cleanup(const char *fs_path, int num_threads, long long length) {
63 char filepath[MAXPATHLEN];
64 snprintf(filepath, sizeof(filepath), "%s/file_read", fs_path);
65 assert(unlink(filepath)>=0);
66}