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