xref: /xnu-8792.41.9/tests/os_proc.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions #include <darwintest.h>
2*5c2921b0SApple OSS Distributions #include <darwintest_utils.h>
3*5c2921b0SApple OSS Distributions #include <mach/mach.h>
4*5c2921b0SApple OSS Distributions #include <mach/task_info.h>
5*5c2921b0SApple OSS Distributions #include <os/proc.h>
6*5c2921b0SApple OSS Distributions #include <sys/kern_memorystatus.h>
7*5c2921b0SApple OSS Distributions #include <unistd.h>
8*5c2921b0SApple OSS Distributions 
9*5c2921b0SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
10*5c2921b0SApple OSS Distributions 
11*5c2921b0SApple OSS Distributions #if !TARGET_OS_OSX
12*5c2921b0SApple OSS Distributions void test_os_proc_available_memory(void);
13*5c2921b0SApple OSS Distributions extern int getpid(void);
14*5c2921b0SApple OSS Distributions 
15*5c2921b0SApple OSS Distributions T_DECL(test_os_proc_available_memory, "Basic available memory")
16*5c2921b0SApple OSS Distributions {
17*5c2921b0SApple OSS Distributions 	kern_return_t err;
18*5c2921b0SApple OSS Distributions 	task_vm_info_data_t vm_info = {};
19*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t count = TASK_VM_INFO_REV4_COUNT;
20*5c2921b0SApple OSS Distributions 	uint64_t remainingBytes;
21*5c2921b0SApple OSS Distributions 
22*5c2921b0SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vm_info, &count);
23*5c2921b0SApple OSS Distributions 	remainingBytes = os_proc_available_memory();
24*5c2921b0SApple OSS Distributions 
25*5c2921b0SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
26*5c2921b0SApple OSS Distributions 	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);
27*5c2921b0SApple OSS Distributions 	T_EXPECT_NE(remainingBytes, 0ULL, "os_proc_available_memory() should not return 0");
28*5c2921b0SApple OSS Distributions 	T_EXPECT_NE(vm_info.limit_bytes_remaining, 0ULL, "vm_info.limit_bytes_remaining should not return 0");
29*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(vm_info.limit_bytes_remaining, remainingBytes,
30*5c2921b0SApple OSS Distributions 	    "task_info --rev4 call returned value 0x%llx for vm_info.limit_bytes_remaining. Expected 0x%llx",
31*5c2921b0SApple OSS Distributions 	    vm_info.limit_bytes_remaining, remainingBytes);
32*5c2921b0SApple OSS Distributions 
33*5c2921b0SApple OSS Distributions 	/* this should now make the available memory return 0 */
34*5c2921b0SApple OSS Distributions 	proc_track_dirty(getpid(), PROC_DIRTY_TRACK);
35*5c2921b0SApple OSS Distributions 
36*5c2921b0SApple OSS Distributions 	count = TASK_VM_INFO_REV4_COUNT;
37*5c2921b0SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vm_info, &count);
38*5c2921b0SApple OSS Distributions 	remainingBytes = os_proc_available_memory();
39*5c2921b0SApple OSS Distributions 
40*5c2921b0SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
41*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(count, TASK_VM_INFO_REV4_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV4_COUNT\n", count);
42*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(remainingBytes, 0ULL, "os_proc_available_memory() should return 0");
43*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(vm_info.limit_bytes_remaining, 0ULL, "vm_info.limit_bytes_remaining should return 0");
44*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(vm_info.limit_bytes_remaining, remainingBytes,
45*5c2921b0SApple OSS Distributions 	    "task_info --rev4 call returned value 0x%llx for vm_info.limit_bytes_remaining. Expected 0x%llx",
46*5c2921b0SApple OSS Distributions 	    vm_info.limit_bytes_remaining, remainingBytes);
47*5c2921b0SApple OSS Distributions }
48*5c2921b0SApple OSS Distributions #else
49*5c2921b0SApple OSS Distributions 
50*5c2921b0SApple OSS Distributions /*
51*5c2921b0SApple OSS Distributions  * os_proc_available_memory is only available on embedded.
52*5c2921b0SApple OSS Distributions  * But the underlying syscall works on macOS to support catalyst
53*5c2921b0SApple OSS Distributions  * extensions. So we test the syscall directly here.
54*5c2921b0SApple OSS Distributions  */
55*5c2921b0SApple OSS Distributions extern uint64_t __memorystatus_available_memory(void);
56*5c2921b0SApple OSS Distributions 
57*5c2921b0SApple OSS Distributions static int
set_memlimit(pid_t pid,int32_t limit_mb)58*5c2921b0SApple OSS Distributions set_memlimit(pid_t pid, int32_t limit_mb)
59*5c2921b0SApple OSS Distributions {
60*5c2921b0SApple OSS Distributions 	memorystatus_memlimit_properties_t mmprops;
61*5c2921b0SApple OSS Distributions 
62*5c2921b0SApple OSS Distributions 	memset(&mmprops, 0, sizeof(memorystatus_memlimit_properties_t));
63*5c2921b0SApple OSS Distributions 
64*5c2921b0SApple OSS Distributions 	mmprops.memlimit_active = limit_mb;
65*5c2921b0SApple OSS Distributions 	mmprops.memlimit_inactive = limit_mb;
66*5c2921b0SApple OSS Distributions 
67*5c2921b0SApple OSS Distributions 	/* implies we want to set fatal limits */
68*5c2921b0SApple OSS Distributions 	mmprops.memlimit_active_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
69*5c2921b0SApple OSS Distributions 	mmprops.memlimit_inactive_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
70*5c2921b0SApple OSS Distributions 	return memorystatus_control(MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES, pid, 0, &mmprops, sizeof(mmprops));
71*5c2921b0SApple OSS Distributions }
72*5c2921b0SApple OSS Distributions T_DECL(test_os_proc_available_memory, "Basic available memory")
73*5c2921b0SApple OSS Distributions {
74*5c2921b0SApple OSS Distributions 	uint64_t available_memory;
75*5c2921b0SApple OSS Distributions 	int ret;
76*5c2921b0SApple OSS Distributions 	pid_t pid = getpid();
77*5c2921b0SApple OSS Distributions 	static const size_t kLimitMb = 1024;
78*5c2921b0SApple OSS Distributions 
79*5c2921b0SApple OSS Distributions 	/*
80*5c2921b0SApple OSS Distributions 	 * Should return 0 unless an proccess is both memory managed and has a
81*5c2921b0SApple OSS Distributions 	 * hard memory limit.
82*5c2921b0SApple OSS Distributions 	 */
83*5c2921b0SApple OSS Distributions 	ret = memorystatus_control(MEMORYSTATUS_CMD_SET_PROCESS_IS_MANAGED, pid, 0, NULL, 0);
84*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "memorystatus_control");
85*5c2921b0SApple OSS Distributions 
86*5c2921b0SApple OSS Distributions 	available_memory = __memorystatus_available_memory();
87*5c2921b0SApple OSS Distributions 	T_ASSERT_EQ(available_memory, 0ULL, "__memorystatus_available_memory == 0");
88*5c2921b0SApple OSS Distributions 
89*5c2921b0SApple OSS Distributions 	ret = memorystatus_control(MEMORYSTATUS_CMD_SET_PROCESS_IS_MANAGED, pid, 1, NULL, 0);
90*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "memorystatus_control");
91*5c2921b0SApple OSS Distributions 	available_memory = __memorystatus_available_memory();
92*5c2921b0SApple OSS Distributions 	T_ASSERT_EQ(available_memory, 0ULL, "__memorystatus_available_memory == 0");
93*5c2921b0SApple OSS Distributions 
94*5c2921b0SApple OSS Distributions 	/*
95*5c2921b0SApple OSS Distributions 	 * Should not return 0 for managed procs with a hard memory limit.
96*5c2921b0SApple OSS Distributions 	 */
97*5c2921b0SApple OSS Distributions 	ret = set_memlimit(pid, kLimitMb);
98*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "memorystatus_control");
99*5c2921b0SApple OSS Distributions 	available_memory = __memorystatus_available_memory();
100*5c2921b0SApple OSS Distributions 	T_ASSERT_NE(available_memory, 0ULL, "__memorystatus_available_memory != 0");
101*5c2921b0SApple OSS Distributions }
102*5c2921b0SApple OSS Distributions #endif
103