1 #include <darwintest.h>
2 #include <errno.h>
3 #include <TargetConditionals.h>
4 #include <mach/mach.h>
5 #include <sys/types.h>
6 #include <sys/sysctl.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <sys/proc.h>
11
12 T_GLOBAL_META(
13 T_META_NAMESPACE("xnu.vm"),
14 T_META_RADAR_COMPONENT_NAME("xnu"),
15 T_META_RADAR_COMPONENT_VERSION("VM"));
16
17 static int orig_age = 0;
18 static const char *ripe_target_age_sysctl = "vm.vm_ripe_target_age_in_secs";
19
20 static void
cleanup_ripe_age(void)21 cleanup_ripe_age(void)
22 {
23 int ret = sysctlbyname(ripe_target_age_sysctl, NULL, NULL, &orig_age,
24 sizeof(orig_age));
25 if (ret == -1) {
26 T_LOG("non-fatal: failed to reset %s: %s", ripe_target_age_sysctl,
27 strerror(errno));
28 }
29 }
30
31 T_DECL(compression_sweep,
32 "ensure some pages are compressed due to pid_hibernate",
33 T_META_ASROOT(true),
34 T_META_ENABLED(!TARGET_OS_OSX && !TARGET_OS_SIMULATOR),
35 T_META_TAG_VM_PREFERRED)
36 {
37 /*
38 * Change the system to sweep out compressed pages that are older than
39 * `compressed_page_target_age_secs` seconds and induce `sweep_count` sweeps
40 * every `sleep_dur_secs` seconds.
41 */
42
43 int compressed_page_target_age_secs = 1;
44 const int sweep_period_secs = 10;
45 T_QUIET; T_ASSERT_GT(sweep_period_secs, compressed_page_target_age_secs,
46 "should sleep longer than target age");
47 const int sweep_count = 3;
48
49 vm_statistics64_data_t vm_stat_before;
50 unsigned int count = HOST_VM_INFO64_COUNT;
51 kern_return_t kret = host_statistics64(mach_host_self(), HOST_VM_INFO64,
52 (host_info64_t)&vm_stat_before, &count);
53 T_QUIET; T_ASSERT_MACH_SUCCESS(kret, "host_statistics64");
54
55 size_t size = sizeof(orig_age);
56 int ret = sysctlbyname(ripe_target_age_sysctl, &orig_age, &size,
57 &compressed_page_target_age_secs,
58 sizeof(compressed_page_target_age_secs));
59 T_ASSERT_POSIX_SUCCESS(ret, "temporarily set sysctl(%s) to %d",
60 ripe_target_age_sysctl, compressed_page_target_age_secs);
61 T_ATEND(cleanup_ripe_age);
62
63 for (int i = 0; i < sweep_count; i++) {
64 const int sweep_out_unused_compressed_command = -2;
65 ret = pid_hibernate(sweep_out_unused_compressed_command);
66 T_ASSERT_POSIX_SUCCESS(ret, "pid_hibernate(sweep-unused-compressed)");
67 sleep(sweep_period_secs);
68 }
69
70 vm_statistics64_data_t vm_stat_after;
71 count = HOST_VM_INFO64_COUNT;
72 kret = host_statistics64(mach_host_self(), HOST_VM_INFO64,
73 (host_info64_t)&vm_stat_after, &count);
74 T_QUIET; T_ASSERT_MACH_SUCCESS(kret, "host_statistics64");
75
76 T_LOG("compressed %llu pages",
77 vm_stat_after.compressions - vm_stat_before.swapouts);
78 T_EXPECT_GT(vm_stat_after.compressions, vm_stat_before.compressions,
79 "should have compressed some pages during sweeps");
80 // rdar://71454311 (Compression sweep swap outs are flaky, should induce compressions)
81 T_MAYFAIL;
82 T_EXPECT_GT(vm_stat_after.swapouts, vm_stat_before.swapouts,
83 "should have swapped out some pages during sweeps");
84 }
85