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_QUIET; 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 size_t n = 0;
32
33 /*
34 * Keep kicking the test for 15 seconds to see if we can panic() the kernel
35 */
36 while (time(NULL) < start + 15) {
37 run_sysctl_test("zone_gc_replenish_test", 0);
38 if (++n % 100000 == 0) {
39 T_LOG("%zd zone_gc_replenish_test done", n);
40 }
41 }
42 return NULL;
43 }
44
45 static void *
alloc_thread_func(__unused void * arg)46 alloc_thread_func(__unused void *arg)
47 {
48 time_t start = time(NULL);
49 size_t n = 0;
50
51 /*
52 * Keep kicking the test for 15 seconds to see if we can panic() the kernel
53 */
54 while (time(NULL) < start + 15) {
55 run_sysctl_test("zone_alloc_replenish_test", 0);
56 if (++n % 10000 == 0) {
57 T_LOG("%zd zone_alloc_replenish_test done", n);
58 }
59 }
60 return NULL;
61 }
62
63 T_DECL(zone_gc_replenish_test,
64 "Test zone garbage collection, exhaustion and replenishment",
65 T_META_CHECK_LEAKS(false), T_META_TAG_VM_PREFERRED)
66 {
67 pthread_attr_t attr;
68 pthread_t gc_thread;
69 pthread_t alloc_thread;
70 int ret;
71
72 ret = pthread_attr_init(&attr);
73 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_attr_init");
74
75 ret = pthread_create(&gc_thread, &attr, gc_thread_func, NULL);
76 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "gc pthread_create");
77
78 ret = pthread_create(&alloc_thread, &attr, alloc_thread_func, NULL);
79 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "alloc pthread_create");
80
81 T_ASSERT_POSIX_ZERO(pthread_join(gc_thread, NULL), NULL);
82 T_ASSERT_POSIX_ZERO(pthread_join(alloc_thread, NULL), NULL);
83 T_PASS("Ran 15 seconds with no panic");
84 }
85