1*43a90889SApple OSS Distributions #include <darwintest.h>
2*43a90889SApple OSS Distributions #include <dispatch/dispatch.h>
3*43a90889SApple OSS Distributions #include <execinfo.h>
4*43a90889SApple OSS Distributions #include <pthread.h>
5*43a90889SApple OSS Distributions #include <ptrauth.h>
6*43a90889SApple OSS Distributions #include <mach/mach.h>
7*43a90889SApple OSS Distributions #include <stdalign.h>
8*43a90889SApple OSS Distributions #include <sys/mman.h>
9*43a90889SApple OSS Distributions #include <sys/sysctl.h>
10*43a90889SApple OSS Distributions
11*43a90889SApple OSS Distributions
12*43a90889SApple OSS Distributions static const off_t gb = 1024 * 1024 * 1024;
13*43a90889SApple OSS Distributions
14*43a90889SApple OSS Distributions static void
get_release_type(char * release_type,size_t release_type_len)15*43a90889SApple OSS Distributions get_release_type(char *release_type, size_t release_type_len)
16*43a90889SApple OSS Distributions {
17*43a90889SApple OSS Distributions int ret;
18*43a90889SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret = sysctlbyname("kern.osreleasetype", release_type, &release_type_len, NULL, 0), "sysctlbyname kern.osreleasetype");
19*43a90889SApple OSS Distributions }
20*43a90889SApple OSS Distributions
21*43a90889SApple OSS Distributions static int64_t
get_hw_memsize(int64_t memsize)22*43a90889SApple OSS Distributions get_hw_memsize(int64_t memsize)
23*43a90889SApple OSS Distributions {
24*43a90889SApple OSS Distributions int ret;
25*43a90889SApple OSS Distributions size_t size = sizeof(memsize);
26*43a90889SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret = sysctlbyname("hw.memsize", &memsize, &size, NULL, 0), "sysctlbyname hw.memsize");
27*43a90889SApple OSS Distributions
28*43a90889SApple OSS Distributions return memsize;
29*43a90889SApple OSS Distributions }
30*43a90889SApple OSS Distributions
31*43a90889SApple OSS Distributions T_DECL(big_map_test,
32*43a90889SApple OSS Distributions "Test that loads large blobs into memory up to 60 percent of gigs available.",
33*43a90889SApple OSS Distributions T_META_ASROOT(true),
34*43a90889SApple OSS Distributions T_META_CHECK_LEAKS(false))
35*43a90889SApple OSS Distributions {
36*43a90889SApple OSS Distributions int fd;
37*43a90889SApple OSS Distributions int64_t memsize = 0;
38*43a90889SApple OSS Distributions size_t release_type_len = 256;
39*43a90889SApple OSS Distributions char release_type[release_type_len];
40*43a90889SApple OSS Distributions const char required_release_type[] = "Darwin Cloud";
41*43a90889SApple OSS Distributions
42*43a90889SApple OSS Distributions get_release_type(release_type, release_type_len);
43*43a90889SApple OSS Distributions if (strstr(release_type, required_release_type) == NULL) {
44*43a90889SApple OSS Distributions T_SKIP("Attempted to run on non psOS release type, skipping...");
45*43a90889SApple OSS Distributions }
46*43a90889SApple OSS Distributions
47*43a90889SApple OSS Distributions memsize = get_hw_memsize(memsize);
48*43a90889SApple OSS Distributions float max_memory_gib = ((float)memsize / (float)gb) * .6;
49*43a90889SApple OSS Distributions
50*43a90889SApple OSS Distributions if (max_memory_gib <= 11) {
51*43a90889SApple OSS Distributions T_SKIP("Not enough memory on device atleast (11GBs required), skipping...");
52*43a90889SApple OSS Distributions }
53*43a90889SApple OSS Distributions
54*43a90889SApple OSS Distributions char file_path[] = "/tmp/bigfile.XXXXXX";
55*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(fd = mkstemp(file_path), NULL);
56*43a90889SApple OSS Distributions for (int gigs = 1; gigs <= max_memory_gib; gigs++) {
57*43a90889SApple OSS Distributions size_t bytes = gigs * gb;
58*43a90889SApple OSS Distributions
59*43a90889SApple OSS Distributions T_LOG("trying %zu bytes (%d GB)\n", bytes, gigs);
60*43a90889SApple OSS Distributions
61*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ftruncate(fd, bytes), "ftruncate");
62*43a90889SApple OSS Distributions
63*43a90889SApple OSS Distributions void *p = mmap(NULL, bytes, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
64*43a90889SApple OSS Distributions
65*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_NE(p, MAP_FAILED, "map");
66*43a90889SApple OSS Distributions
67*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(munmap(p, bytes), "munmap");
68*43a90889SApple OSS Distributions }
69*43a90889SApple OSS Distributions }
70