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