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