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