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