xref: /xnu-10002.81.5/tests/vm_kern_count_wired_kernelcache.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587) !
1*5e3eaea3SApple OSS Distributions #include <unistd.h>
2*5e3eaea3SApple OSS Distributions #include <errno.h>
3*5e3eaea3SApple OSS Distributions 
4*5e3eaea3SApple OSS Distributions #include <vm_statistics.h>
5*5e3eaea3SApple OSS Distributions #include <mach/mach.h>
6*5e3eaea3SApple OSS Distributions #include <mach_debug/mach_debug.h>
7*5e3eaea3SApple OSS Distributions 
8*5e3eaea3SApple OSS Distributions #include <darwintest.h>
9*5e3eaea3SApple OSS Distributions 
10*5e3eaea3SApple OSS Distributions /*
11*5e3eaea3SApple OSS Distributions  * Ensure that mach_memory_info includes a counter for the kernelcache size.
12*5e3eaea3SApple OSS Distributions  */
13*5e3eaea3SApple OSS Distributions 
14*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
15*5e3eaea3SApple OSS Distributions 	T_META_NAMESPACE("xnu.vm"),
16*5e3eaea3SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
17*5e3eaea3SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"));
18*5e3eaea3SApple OSS Distributions 
19*5e3eaea3SApple OSS Distributions T_DECL(vm_kern_count_wired_kernelcache,
20*5e3eaea3SApple OSS Distributions     "mach_memory_info returns a counter for for kernelcache",
21*5e3eaea3SApple OSS Distributions     T_META_ASROOT(true))
22*5e3eaea3SApple OSS Distributions {
23*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
24*5e3eaea3SApple OSS Distributions 	uint64_t i;
25*5e3eaea3SApple OSS Distributions 	mach_zone_name_t *name = NULL;
26*5e3eaea3SApple OSS Distributions 	unsigned int nameCnt = 0;
27*5e3eaea3SApple OSS Distributions 	mach_zone_info_t *info = NULL;
28*5e3eaea3SApple OSS Distributions 	unsigned int infoCnt = 0;
29*5e3eaea3SApple OSS Distributions 	mach_memory_info_t *wiredInfo = NULL;
30*5e3eaea3SApple OSS Distributions 	unsigned int wiredInfoCnt = 0;
31*5e3eaea3SApple OSS Distributions 
32*5e3eaea3SApple OSS Distributions 	kr = mach_memory_info(mach_host_self(), &name, &nameCnt, &info, &infoCnt,
33*5e3eaea3SApple OSS Distributions 	    &wiredInfo, &wiredInfoCnt);
34*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_memory_info");
35*5e3eaea3SApple OSS Distributions 
36*5e3eaea3SApple OSS Distributions 	bool found_kernelcache_counter = false;
37*5e3eaea3SApple OSS Distributions 	uint64_t static_kernelcache_size = 0;
38*5e3eaea3SApple OSS Distributions 	uint64_t wired_memory_boot = 0;
39*5e3eaea3SApple OSS Distributions 	for (i = 0; i < wiredInfoCnt; i++) {
40*5e3eaea3SApple OSS Distributions 		const mach_memory_info_t *curr = &wiredInfo[i];
41*5e3eaea3SApple OSS Distributions 		uint32_t type = curr->flags & VM_KERN_SITE_TYPE;
42*5e3eaea3SApple OSS Distributions 		if (type == VM_KERN_SITE_COUNTER) {
43*5e3eaea3SApple OSS Distributions 			if (curr->site == VM_KERN_COUNT_WIRED_STATIC_KERNELCACHE) {
44*5e3eaea3SApple OSS Distributions 				found_kernelcache_counter = true;
45*5e3eaea3SApple OSS Distributions 				static_kernelcache_size = curr->size;
46*5e3eaea3SApple OSS Distributions 			} else if (curr->site == VM_KERN_COUNT_WIRED_BOOT) {
47*5e3eaea3SApple OSS Distributions 				wired_memory_boot = curr->size;
48*5e3eaea3SApple OSS Distributions 			}
49*5e3eaea3SApple OSS Distributions 		}
50*5e3eaea3SApple OSS Distributions 	}
51*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(found_kernelcache_counter, "mach_memory_info returned kernelcache counter.");
52*5e3eaea3SApple OSS Distributions 	// Sanity check that the counter isn't 0.
53*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_GT(static_kernelcache_size, 0ULL, "kernelcache counter > 0");
54*5e3eaea3SApple OSS Distributions 	// Sanity check that the counter is less than the amount of wired memory
55*5e3eaea3SApple OSS Distributions 	// at boot.
56*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_LE(static_kernelcache_size, wired_memory_boot, "kernelcache counter <= VM_KERN_COUNT_WIRED_BOOT");
57*5e3eaea3SApple OSS Distributions 
58*5e3eaea3SApple OSS Distributions 	// Cleanup
59*5e3eaea3SApple OSS Distributions 	if ((name != NULL) && (nameCnt != 0)) {
60*5e3eaea3SApple OSS Distributions 		kr = vm_deallocate(mach_task_self(), (vm_address_t) name,
61*5e3eaea3SApple OSS Distributions 		    (vm_size_t) (nameCnt * sizeof *name));
62*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate name");
63*5e3eaea3SApple OSS Distributions 	}
64*5e3eaea3SApple OSS Distributions 
65*5e3eaea3SApple OSS Distributions 	if ((info != NULL) && (infoCnt != 0)) {
66*5e3eaea3SApple OSS Distributions 		kr = vm_deallocate(mach_task_self(), (vm_address_t) info,
67*5e3eaea3SApple OSS Distributions 		    (vm_size_t) (infoCnt * sizeof *info));
68*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate info");
69*5e3eaea3SApple OSS Distributions 	}
70*5e3eaea3SApple OSS Distributions 
71*5e3eaea3SApple OSS Distributions 	if ((wiredInfo != NULL) && (wiredInfoCnt != 0)) {
72*5e3eaea3SApple OSS Distributions 		kr = vm_deallocate(mach_task_self(), (vm_address_t) wiredInfo,
73*5e3eaea3SApple OSS Distributions 		    (vm_size_t) (wiredInfoCnt * sizeof *wiredInfo));
74*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate wiredInfo");
75*5e3eaea3SApple OSS Distributions 	}
76*5e3eaea3SApple OSS Distributions }
77