]>
git.saurik.com Git - apple/hfs.git/blob - tests/cases/test-throttled-io.c
4 #include <sys/resource.h>
5 #include <CommonCrypto/CommonDigest.h>
12 #include <sys/errno.h>
13 #include <libkern/OSAtomic.h>
17 #include "hfs-tests.h"
18 #include "test-utils.h"
19 #include "disk-image.h"
23 static disk_image_t
*di
;
24 static char *file1
, *file2
, *file3
;
28 static const size_t buf_size
= 64 * 1024 * 1024;
30 static int run_test1(void)
33 asprintf(&of
, "of=%s", file1
);
35 // Kick off another process to ensure we get throttled
36 assert_no_err(posix_spawn(&pid
, "/bin/dd", NULL
, NULL
,
37 (char *[]){ "/bin/dd", "if=/dev/random",
41 assert_no_err(setiopolicy_np(IOPOL_TYPE_DISK
, IOPOL_SCOPE_PROCESS
,
45 assert_with_errno((fd
= open("/dev/random", O_RDONLY
)) >= 0);
46 assert_with_errno((fd2
= open(file2
,
47 O_RDWR
| O_CREAT
| O_TRUNC
, 0666)) >= 0);
49 assert_no_err(fcntl(fd2
, F_SINGLE_WRITER
, 1));
50 assert_no_err(fcntl(fd2
, F_NOCACHE
, 1));
52 buf
= valloc(buf_size
);
56 ssize_t res
= check_io(read(fd
, buf
, buf_size
), buf_size
);
58 CC_SHA1_Update(&ctx
, buf
, (CC_LONG
)res
);
60 res
= check_io(write(fd2
, buf
, res
), res
);
67 lseek(fd2
, 0, SEEK_SET
);
69 res
= check_io(read(fd2
, buf
, buf_size
), buf_size
);
71 CC_SHA1_Update(&ctx2
, buf
, (CC_LONG
)res
);
73 uint8_t digest1
[CC_SHA1_DIGEST_LENGTH
], digest2
[CC_SHA1_DIGEST_LENGTH
];
74 CC_SHA1_Final(digest1
, &ctx
);
75 CC_SHA1_Final(digest2
, &ctx2
);
77 assert(!memcmp(digest1
, digest2
, CC_SHA1_DIGEST_LENGTH
));
82 static volatile uint64_t written
;
83 static volatile bool done
;
85 static void test2_thread(void)
87 int fd
= open(file3
, O_RDONLY
);
90 void *b
= buf
+ buf_size
/ 2;
91 uLong seq
= crc32(0, Z_NULL
, 0);
98 res
= check_io(pread(fd
, b
, buf_size
/ 2, offset
), -1);
99 } while (res
== 0 && !done
);
101 assert (res
% 4 == 0);
105 for (uLong
*p
= b
; res
; ++p
, res
-= sizeof(uLong
)) {
106 seq
= crc32(Z_NULL
, (void *)&seq
, 4);
110 if (offset
< written
)
116 static int run_test2(void)
118 int fd
= open(file3
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
121 assert_no_err(fcntl(fd
, F_SINGLE_WRITER
, 1));
122 assert_no_err(fcntl(fd
, F_NOCACHE
, 1));
125 pthread_create(&thread
, NULL
, (void *(*)(void *))test2_thread
, NULL
);
126 uLong seq
= crc32(0, Z_NULL
, 0);
128 for (int i
= 0; i
< 4; ++i
) {
130 for (unsigned i
= 0; i
< buf_size
/ 2 / sizeof(uLong
); ++i
) {
131 seq
= crc32(Z_NULL
, (void *)&seq
, 4);
135 ssize_t res
= check_io(write(fd
, buf
, buf_size
/ 2), buf_size
/ 2);
144 pthread_join(thread
, NULL
);
149 static bool clean_up(void)
162 int run_throttled_io(__unused test_ctx_t
*ctx
)
164 test_cleanup(^ bool {
168 di
= disk_image_get();
169 asprintf(&file1
, "%s/throttled_io.1", di
->mount_point
);
170 asprintf(&file2
, "%s/throttled_io.2", di
->mount_point
);
171 asprintf(&file3
, "%s/throttled_io.3", di
->mount_point
);
173 int res
= run_test1();