xref: /xnu-10063.101.15/tests/vm/diag_threshold_test_limit_and_threshold_same.c (revision 94d3b452840153a99b38a3a9659680b2a006908e)
1*94d3b452SApple OSS Distributions /**
2*94d3b452SApple OSS Distributions  *  double_limit_test.c
3*94d3b452SApple OSS Distributions  *  DiagThresholdTest
4*94d3b452SApple OSS Distributions  *
5*94d3b452SApple OSS Distributions  * Test the check if reloading a memory diagnostics limit retriggers exceptions
6*94d3b452SApple OSS Distributions  * Copyright (c) 2022 Apple Inc. All rights reserved.
7*94d3b452SApple OSS Distributions  */
8*94d3b452SApple OSS Distributions #include <stdio.h>
9*94d3b452SApple OSS Distributions #include "vm/diag_threshold_test.h"
10*94d3b452SApple OSS Distributions #include <sys/kern_memorystatus.h>
11*94d3b452SApple OSS Distributions #include <darwintest.h>
12*94d3b452SApple OSS Distributions 
13*94d3b452SApple OSS Distributions static void diag_threshold_test_limit_and_threshold_same(struct test_case *test_case, void *param);
14*94d3b452SApple OSS Distributions static test_case_t diag_threshold_test_limit_and_threshold_same_test = {
15*94d3b452SApple OSS Distributions 	.short_name = "diag_threshold_test_limit_and_threshold_same",
16*94d3b452SApple OSS Distributions 	.test_name = "Test on which a diag threshold and a limit is set with same value",
17*94d3b452SApple OSS Distributions 	.test_code = diag_threshold_test_limit_and_threshold_same,
18*94d3b452SApple OSS Distributions 	.result_already_present = FALSE,
19*94d3b452SApple OSS Distributions 	.exception_not_expected = FALSE,
20*94d3b452SApple OSS Distributions 	.exceptions_handled_in_test = TRUE,
21*94d3b452SApple OSS Distributions };
22*94d3b452SApple OSS Distributions 
23*94d3b452SApple OSS Distributions T_GLOBAL_META(
24*94d3b452SApple OSS Distributions 	T_META_ENABLED(TARGET_OS_IPHONE),
25*94d3b452SApple OSS Distributions 	T_META_NAMESPACE("xnu.vm.100432442"),
26*94d3b452SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
27*94d3b452SApple OSS Distributions 	T_META_OWNER("jsolsona"),
28*94d3b452SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM")
29*94d3b452SApple OSS Distributions 	);
30*94d3b452SApple OSS Distributions 
31*94d3b452SApple OSS Distributions /**
32*94d3b452SApple OSS Distributions  * This function sets a threshold, but is expected to fail, so we cannot use
33*94d3b452SApple OSS Distributions  * the standard test threshold function
34*94d3b452SApple OSS Distributions  */
35*94d3b452SApple OSS Distributions static bool
get_diagthreshold_limits(int * limit_param,boolean_t * status)36*94d3b452SApple OSS Distributions get_diagthreshold_limits(int *limit_param, boolean_t *status)
37*94d3b452SApple OSS Distributions {
38*94d3b452SApple OSS Distributions 	memorystatus_diag_memlimit_properties_t limit;
39*94d3b452SApple OSS Distributions 	diag_mem_threshold_log_test("Get threshold limit");
40*94d3b452SApple OSS Distributions 	int pid = getpid();
41*94d3b452SApple OSS Distributions 	int retValue = memorystatus_control(
42*94d3b452SApple OSS Distributions 		MEMORYSTATUS_CMD_GET_DIAG_LIMIT,
43*94d3b452SApple OSS Distributions 		pid,
44*94d3b452SApple OSS Distributions 		0,
45*94d3b452SApple OSS Distributions 		&limit, sizeof(limit)
46*94d3b452SApple OSS Distributions 		);
47*94d3b452SApple OSS Distributions 	T_ASSERT_MACH_ERROR( retValue, KERN_SUCCESS, "Verification diagnostics threshold limit adjustment");
48*94d3b452SApple OSS Distributions 	*limit_param = (int)(limit.memlimit);
49*94d3b452SApple OSS Distributions 	*status = limit.threshold_enabled;
50*94d3b452SApple OSS Distributions 
51*94d3b452SApple OSS Distributions 	return (retValue == KERN_SUCCESS) ? false : true;
52*94d3b452SApple OSS Distributions }
53*94d3b452SApple OSS Distributions static void
diag_threshold_test_limit_and_threshold_same(struct test_case * test_case,__unused void * param)54*94d3b452SApple OSS Distributions diag_threshold_test_limit_and_threshold_same(struct test_case *test_case, __unused void *param)
55*94d3b452SApple OSS Distributions {
56*94d3b452SApple OSS Distributions 	test_context_t *info = (test_context_t *)param;
57*94d3b452SApple OSS Distributions 	int limit_param;
58*94d3b452SApple OSS Distributions 	boolean_t status;
59*94d3b452SApple OSS Distributions 
60*94d3b452SApple OSS Distributions 	dispatch_semaphore_signal(info->executor_ready_for_exceptions);
61*94d3b452SApple OSS Distributions 	diag_mem_set_jetsam_watermark(LOW_JETSAM_LIMIT);
62*94d3b452SApple OSS Distributions 	diag_mem_set_jetsam_limit(WORKING_LIMIT);
63*94d3b452SApple OSS Distributions 	bool retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT, true);
64*94d3b452SApple OSS Distributions 	retValue = get_diagthreshold_limits(&limit_param, &status);
65*94d3b452SApple OSS Distributions 	T_ASSERT_EQ(false, status, "Threshold is disabled automatically");
66*94d3b452SApple OSS Distributions 	T_ASSERT_EQ(WORKING_LIMIT, limit_param, "Adjusted threshold is correct");
67*94d3b452SApple OSS Distributions 
68*94d3b452SApple OSS Distributions 	retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT << 1, true);
69*94d3b452SApple OSS Distributions 	diag_mem_threshold_log_test("Modifying threshold limit,expecting threshold is automatically enabled");
70*94d3b452SApple OSS Distributions 	retValue = get_diagthreshold_limits(&limit_param, &status);
71*94d3b452SApple OSS Distributions 	T_ASSERT_EQ(true, status, "Threshold is enabled automatically");
72*94d3b452SApple OSS Distributions 	T_ASSERT_EQ(WORKING_LIMIT << 1, limit_param, "Adjusted threshold is correct");
73*94d3b452SApple OSS Distributions 
74*94d3b452SApple OSS Distributions 	retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT, true);
75*94d3b452SApple OSS Distributions 	diag_mem_set_jetsam_watermark(LOW_JETSAM_LIMIT );
76*94d3b452SApple OSS Distributions 	diag_mem_set_jetsam_limit(WORKING_LIMIT);
77*94d3b452SApple OSS Distributions 	retValue = get_diagthreshold_limits(&limit_param, &status);
78*94d3b452SApple OSS Distributions 	T_ASSERT_EQ(false, status, "Threshold is disabled automatically");
79*94d3b452SApple OSS Distributions 	T_ASSERT_EQ(WORKING_LIMIT, limit_param, "Adjusted threshold is correct");
80*94d3b452SApple OSS Distributions 
81*94d3b452SApple OSS Distributions 	retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT << 1, true);
82*94d3b452SApple OSS Distributions 	diag_mem_threshold_log_test("Modifying threshold limit,expecting threshold is automatically enabled");
83*94d3b452SApple OSS Distributions 	retValue = get_diagthreshold_limits(&limit_param, &status);
84*94d3b452SApple OSS Distributions 	T_ASSERT_EQ(true, status, "Threshold is enabled automatically");
85*94d3b452SApple OSS Distributions 	T_ASSERT_EQ(WORKING_LIMIT << 1, limit_param, "Adjusted threshold is correct");
86*94d3b452SApple OSS Distributions 
87*94d3b452SApple OSS Distributions 
88*94d3b452SApple OSS Distributions 	diag_mem_set_jetsam_watermark(LOW_JETSAM_LIMIT << 1);
89*94d3b452SApple OSS Distributions 	diag_mem_set_jetsam_limit(WORKING_LIMIT << 1);
90*94d3b452SApple OSS Distributions 	diag_mem_threshold_log_test("Modifying jetsam limit,expecting threshold is automatically enabled");
91*94d3b452SApple OSS Distributions 	retValue = get_diagthreshold_limits(&limit_param, &status);
92*94d3b452SApple OSS Distributions 	T_ASSERT_EQ(false, status, "Threshold is disabled automatically");
93*94d3b452SApple OSS Distributions 	T_ASSERT_EQ(WORKING_LIMIT << 1, limit_param, "Adjusted threshold is correct");
94*94d3b452SApple OSS Distributions 	test_case->did_pass = TRUE;
95*94d3b452SApple OSS Distributions 	test_case->result_already_present = TRUE;
96*94d3b452SApple OSS Distributions }
97*94d3b452SApple OSS Distributions 
98*94d3b452SApple OSS Distributions T_DECL(diag_threshold_test_limit_and_threshold_same,
99*94d3b452SApple OSS Distributions     "Test on which a diag watermark and a threshold is set with same value"
100*94d3b452SApple OSS Distributions     )
101*94d3b452SApple OSS Distributions {
102*94d3b452SApple OSS Distributions 	diag_mem_threshold_set_setup(&diag_threshold_test_limit_and_threshold_same_test);
103*94d3b452SApple OSS Distributions }
104