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