xref: /xnu-11215.41.3/tests/vm/diag_threshold_test_with_high_watermark.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /**
2  *  double_limit_test.c
3  *  DiagThresholdTest
4  *
5  * Test the check if reloading a memory diagnostics limit retriggers exceptions
6  * Copyright (c) 2022 Apple Inc. All rights reserved.
7  */
8 #include <stdio.h>
9 #include "vm/diag_threshold_test.h"
10 #include <sys/kern_memorystatus.h>
11 static void diag_threshold_test_with_high_watermark(struct test_case *test_case, void *param);
12 static test_case_t diag_threshold_test_with_high_watermark_test = {
13 	.short_name = "diag_threshold_test_with_high_watermark",
14 	.test_name = "Test on which a diag threshold is set and a watermark, the watermark is bigger than the diag threshold",
15 	.test_code = diag_threshold_test_with_high_watermark,
16 	.result_already_present = FALSE,
17 	.exception_not_expected = FALSE,
18 	.exceptions_handled_in_test = TRUE,
19 };
20 
21 T_GLOBAL_META(
22 	T_META_ENABLED(TARGET_OS_IPHONE),
23 	T_META_NAMESPACE("xnu.vm.100432442"),
24 	T_META_RADAR_COMPONENT_NAME("xnu"),
25 	T_META_OWNER("jsolsona"),
26 	T_META_RADAR_COMPONENT_VERSION("VM")
27 	);
28 
29 static void
diag_threshold_test_with_high_watermark(struct test_case * test_case,void * param)30 diag_threshold_test_with_high_watermark(struct test_case *test_case, void *param)
31 {
32 	test_context_t *info = (test_context_t *)param;
33 	(void)set_memory_diagnostics_threshold_limits(LOW_JETSAM_LIMIT, true);
34 	diag_mem_set_jetsam_watermark(WORKING_LIMIT);
35 	dispatch_semaphore_signal(info->executor_ready_for_exceptions);
36 	diag_mem_threshold_log_test("Going to waste memory 1\n");
37 	diag_mem_threshold_waste_memory(ABOVE_JETSAM_LIMIT);
38 	diag_mem_threshold_log_test("memory wasted #1\n");
39 	sleep(1);
40 	diag_mem_threshold_log_test("step #2\n");
41 	if (FALSE == diag_mem_threshold_wait_for_exception(info)) {
42 		test_case->did_pass = FALSE;
43 		test_case->result_already_present = TRUE;
44 		diag_mem_threshold_log_test("Giving up in wait, terminating\n");
45 		pthread_exit(NULL);
46 	}
47 	diag_mem_threshold_log_test("Got first exception ensuring no false positives (timeout expected)\n");
48 	diag_mem_threshold_wait_for_exception(info);
49 	diag_mem_threshold_log_test("Got first exception wasting memory\n");
50 	diag_mem_threshold_waste_memory(2 * WORKING_LIMIT);
51 	if (FALSE == diag_mem_threshold_wait_for_exception(info)) {
52 		test_case->did_pass = FALSE;
53 		test_case->result_already_present = TRUE;
54 		diag_mem_threshold_log_test("Giving up in wait, terminating\n");
55 		pthread_exit(NULL);
56 	}
57 	test_case->did_pass = TRUE;
58 	diag_mem_threshold_log_test("Got second exception\n");
59 }
60 
61 T_DECL(diag_threshold_test_with_high_watermark,
62     "Test on which a diag threshold is set and a watermark, the watermark is bigger than the diag threshold",
63     T_META_TAG_VM_PREFERRED)
64 {
65 	diag_mem_threshold_set_setup(&diag_threshold_test_with_high_watermark_test);
66 }
67