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