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