]> git.saurik.com Git - apple/xnu.git/blob - tests/os_proc.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / tests / os_proc.c
1 #include <darwintest.h>
2 #include <darwintest_utils.h>
3 #include <mach/mach.h>
4 #include <mach/task_info.h>
5 #include <os/proc.h>
6
7 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
8
9 #if !TARGET_OS_OSX
10 void test_os_proc_available_memory(void);
11 extern int getpid(void);
12
13 T_DECL(test_os_proc_available_memory, "Basic available memory")
14 {
15 kern_return_t err;
16 task_vm_info_data_t vm_info = {};
17 mach_msg_type_number_t count = TASK_VM_INFO_REV4_COUNT;
18 uint64_t remainingBytes;
19
20 err = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vm_info, &count);
21 remainingBytes = os_proc_available_memory();
22
23 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
24 T_EXPECT_EQ(count, TASK_VM_INFO_REV4_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV4_COUNT (%d)\n", count, TASK_VM_INFO_REV4_COUNT);
25 T_EXPECT_NE(remainingBytes, 0ULL, "os_proc_available_memory() should not return 0");
26 T_EXPECT_NE(vm_info.limit_bytes_remaining, 0ULL, "vm_info.limit_bytes_remaining should not return 0");
27 T_EXPECT_EQ(vm_info.limit_bytes_remaining, remainingBytes,
28 "task_info --rev4 call returned value 0x%llx for vm_info.limit_bytes_remaining. Expected 0x%llx",
29 vm_info.limit_bytes_remaining, remainingBytes);
30
31 /* this should now make the available memory return 0 */
32 proc_track_dirty(getpid(), PROC_DIRTY_TRACK);
33
34 count = TASK_VM_INFO_REV4_COUNT;
35 err = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vm_info, &count);
36 remainingBytes = os_proc_available_memory();
37
38 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
39 T_EXPECT_EQ(count, TASK_VM_INFO_REV4_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV4_COUNT\n", count);
40 T_EXPECT_EQ(remainingBytes, 0ULL, "os_proc_available_memory() should return 0");
41 T_EXPECT_EQ(vm_info.limit_bytes_remaining, 0ULL, "vm_info.limit_bytes_remaining should return 0");
42 T_EXPECT_EQ(vm_info.limit_bytes_remaining, remainingBytes,
43 "task_info --rev4 call returned value 0x%llx for vm_info.limit_bytes_remaining. Expected 0x%llx",
44 vm_info.limit_bytes_remaining, remainingBytes);
45 }
46 #else
47 T_DECL(test_os_proc_available_memory, "Basic available memory")
48 {
49 T_SKIP("Not available on macOS");
50 }
51 #endif