4 #include <vm_statistics.h>
6 #include <mach_debug/mach_debug.h>
8 #include <darwintest.h>
11 * Ensure that mach_memory_info includes a counter for the kernelcache size.
14 T_GLOBAL_META(T_META_NAMESPACE("xnu.vm"));
16 T_DECL(vm_kern_count_wired_kernelcache
,
17 "mach_memory_info returns a counter for for kernelcache",
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;
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");
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
;
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
53 T_QUIET
; T_ASSERT_LE(static_kernelcache_size
, wired_memory_boot
, "kernelcache counter <= VM_KERN_COUNT_WIRED_BOOT");
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");
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");
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");