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