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