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