xref: /xnu-10063.121.3/tests/phys_footprint_interval_max.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2018 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*2c2f96dcSApple OSS Distributions  * file.
12*2c2f96dcSApple OSS Distributions  *
13*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
14*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
19*2c2f96dcSApple OSS Distributions  * limitations under the License.
20*2c2f96dcSApple OSS Distributions  *
21*2c2f96dcSApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*2c2f96dcSApple OSS Distributions  */
23*2c2f96dcSApple OSS Distributions 
24*2c2f96dcSApple OSS Distributions #include <darwintest.h>
25*2c2f96dcSApple OSS Distributions 
26*2c2f96dcSApple OSS Distributions #include <stdlib.h>
27*2c2f96dcSApple OSS Distributions #include <unistd.h>
28*2c2f96dcSApple OSS Distributions #include <string.h>
29*2c2f96dcSApple OSS Distributions #include <mach/mach_vm.h>
30*2c2f96dcSApple OSS Distributions #include <mach/mach_init.h>
31*2c2f96dcSApple OSS Distributions #include <sys/resource.h>
32*2c2f96dcSApple OSS Distributions #include <libproc.h>
33*2c2f96dcSApple OSS Distributions #include <libproc_internal.h>
34*2c2f96dcSApple OSS Distributions #include <TargetConditionals.h>
35*2c2f96dcSApple OSS Distributions 
36*2c2f96dcSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
37*2c2f96dcSApple OSS Distributions 
38*2c2f96dcSApple OSS Distributions #define ALLOC_SIZE_LARGE 5*1024*1024
39*2c2f96dcSApple OSS Distributions #define ALLOC_SIZE_SMALL 2*1024*1024
40*2c2f96dcSApple OSS Distributions 
41*2c2f96dcSApple OSS Distributions int proc_rlimit_control(pid_t pid, int flavor, void *arg);
42*2c2f96dcSApple OSS Distributions 
43*2c2f96dcSApple OSS Distributions T_DECL(phys_footprint_interval_max,
44*2c2f96dcSApple OSS Distributions     "Validate physical footprint interval tracking")
45*2c2f96dcSApple OSS Distributions {
46*2c2f96dcSApple OSS Distributions 	int ret;
47*2c2f96dcSApple OSS Distributions 	struct rusage_info_v4 ru;
48*2c2f96dcSApple OSS Distributions 	mach_vm_address_t addr = (mach_vm_address_t)NULL;
49*2c2f96dcSApple OSS Distributions 
50*2c2f96dcSApple OSS Distributions 	ret = proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru);
51*2c2f96dcSApple OSS Distributions 	T_QUIET;
52*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "proc_pid_rusage");
53*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(ru.ri_lifetime_max_phys_footprint, ru.ri_interval_max_phys_footprint,
54*2c2f96dcSApple OSS Distributions 	    "Max footprint and interval footprint are equal prior to dirtying memory");
55*2c2f96dcSApple OSS Distributions 
56*2c2f96dcSApple OSS Distributions 	ret = mach_vm_allocate(mach_task_self(), &addr, (mach_vm_size_t)ALLOC_SIZE_LARGE, VM_FLAGS_ANYWHERE);
57*2c2f96dcSApple OSS Distributions 	T_QUIET;
58*2c2f96dcSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "mach_vm_allocate(ALLOC_SIZE_LARGE)");
59*2c2f96dcSApple OSS Distributions 
60*2c2f96dcSApple OSS Distributions 	memset((void *)addr, 0xab, ALLOC_SIZE_LARGE);
61*2c2f96dcSApple OSS Distributions 
62*2c2f96dcSApple OSS Distributions 	ret = proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru);
63*2c2f96dcSApple OSS Distributions 	T_QUIET;
64*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "proc_pid_rusage");
65*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(ru.ri_lifetime_max_phys_footprint, ru.ri_interval_max_phys_footprint,
66*2c2f96dcSApple OSS Distributions 	    "Max footprint and interval footprint are equal after dirtying large memory region");
67*2c2f96dcSApple OSS Distributions 
68*2c2f96dcSApple OSS Distributions 	mach_vm_deallocate(mach_task_self(), addr, (mach_vm_size_t)ALLOC_SIZE_LARGE);
69*2c2f96dcSApple OSS Distributions 
70*2c2f96dcSApple OSS Distributions 	ret = proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru);
71*2c2f96dcSApple OSS Distributions 	T_QUIET;
72*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "proc_pid_rusage");
73*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(ru.ri_lifetime_max_phys_footprint, ru.ri_interval_max_phys_footprint,
74*2c2f96dcSApple OSS Distributions 	    "Max footprint and interval footprint are still equal after freeing large memory region");
75*2c2f96dcSApple OSS Distributions 
76*2c2f96dcSApple OSS Distributions 	ret = proc_reset_footprint_interval(getpid());
77*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "proc_reset_footprint_interval()");
78*2c2f96dcSApple OSS Distributions 
79*2c2f96dcSApple OSS Distributions 	ret = proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru);
80*2c2f96dcSApple OSS Distributions 	T_QUIET;
81*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "proc_pid_rusage");
82*2c2f96dcSApple OSS Distributions 	T_ASSERT_GT(ru.ri_lifetime_max_phys_footprint, ru.ri_interval_max_phys_footprint,
83*2c2f96dcSApple OSS Distributions 	    "Max footprint is greater than interval footprint after resetting interval");
84*2c2f96dcSApple OSS Distributions 
85*2c2f96dcSApple OSS Distributions 	ret = mach_vm_allocate(mach_task_self(), &addr, (mach_vm_size_t)ALLOC_SIZE_SMALL, VM_FLAGS_ANYWHERE);
86*2c2f96dcSApple OSS Distributions 	T_QUIET;
87*2c2f96dcSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "mach_vm_allocate(ALLOC_SIZE_SMALL)");
88*2c2f96dcSApple OSS Distributions 	memset((void *)addr, 0xab, ALLOC_SIZE_SMALL);
89*2c2f96dcSApple OSS Distributions 
90*2c2f96dcSApple OSS Distributions 	ret = proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru);
91*2c2f96dcSApple OSS Distributions 	T_QUIET;
92*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "proc_pid_rusage");
93*2c2f96dcSApple OSS Distributions 	T_ASSERT_GT(ru.ri_lifetime_max_phys_footprint, ru.ri_interval_max_phys_footprint,
94*2c2f96dcSApple OSS Distributions 	    "Max footprint is still greater than interval footprint after dirtying small memory region");
95*2c2f96dcSApple OSS Distributions }
96