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