xref: /xnu-8792.81.2/tests/vm/zone_gc_replenish_test.c (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1 #include <sys/sysctl.h>
2 #include <time.h>
3 
4 #include <darwintest.h>
5 #include <darwintest_utils.h>
6 
7 T_GLOBAL_META(
8 	T_META_NAMESPACE("xnu.vm"),
9 	T_META_RADAR_COMPONENT_NAME("xnu"),
10 	T_META_RADAR_COMPONENT_VERSION("VM"));
11 
12 static int64_t
run_sysctl_test(const char * t,int64_t value)13 run_sysctl_test(const char *t, int64_t value)
14 {
15 	char name[1024];
16 	int64_t result = 0;
17 	size_t s = sizeof(value);
18 	int rc;
19 
20 	snprintf(name, sizeof(name), "debug.test.%s", t);
21 	rc = sysctlbyname(name, &result, &s, &value, s);
22 	T_ASSERT_POSIX_SUCCESS(rc, "sysctlbyname(%s)", t);
23 	return result;
24 }
25 
26 
27 static void *
gc_thread_func(__unused void * arg)28 gc_thread_func(__unused void *arg)
29 {
30 	time_t start = time(NULL);
31 
32 	/*
33 	 * Keep kicking the test for 15 seconds to see if we can panic() the kernel
34 	 */
35 	while (time(NULL) < start + 15) {
36 		run_sysctl_test("zone_gc_replenish_test", 0);
37 	}
38 	return NULL;
39 }
40 
41 static void *
alloc_thread_func(__unused void * arg)42 alloc_thread_func(__unused void *arg)
43 {
44 	time_t start = time(NULL);
45 
46 	/*
47 	 * Keep kicking the test for 15 seconds to see if we can panic() the kernel
48 	 */
49 	while (time(NULL) < start + 15) {
50 		run_sysctl_test("zone_alloc_replenish_test", 0);
51 	}
52 	return NULL;
53 }
54 
55 T_DECL(zone_gc_replenish_test,
56     "Test zone garbage collection, exhaustion and replenishment",
57     T_META_CHECK_LEAKS(false))
58 {
59 	pthread_attr_t attr;
60 	pthread_t gc_thread;
61 	pthread_t alloc_thread;
62 	int ret;
63 
64 	ret = pthread_attr_init(&attr);
65 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_attr_init");
66 
67 	ret = pthread_create(&gc_thread, &attr, gc_thread_func, NULL);
68 	T_QUIET; T_ASSERT_POSIX_ZERO(ret, "gc pthread_create");
69 
70 	ret = pthread_create(&alloc_thread, &attr, alloc_thread_func, NULL);
71 	T_QUIET; T_ASSERT_POSIX_ZERO(ret, "alloc pthread_create");
72 
73 	T_ASSERT_POSIX_ZERO(pthread_join(gc_thread, NULL), NULL);
74 	T_ASSERT_POSIX_ZERO(pthread_join(alloc_thread, NULL), NULL);
75 	T_PASS("Ran 15 seconds with no panic");
76 }
77