1 #include <sys/sysctl.h>
2 #include <darwintest.h>
3 #include <darwintest_utils.h>
4 #include <spawn.h>
5 #include <pthread.h>
6
7 T_GLOBAL_META(
8 T_META_NAMESPACE("xnu.sync"),
9 T_META_RUN_CONCURRENTLY(true),
10 T_META_CHECK_LEAKS(false),
11 T_META_RADAR_COMPONENT_NAME("xnu"),
12 T_META_RADAR_COMPONENT_VERSION("locks"));
13
14 static int64_t
run_sysctl_test(const char * t,int64_t value)15 run_sysctl_test(const char *t, int64_t value)
16 {
17 char name[1024];
18 int64_t result = 0;
19 size_t s = sizeof(value);
20 int rc;
21
22 snprintf(name, sizeof(name), "debug.test.%s", t);
23 rc = sysctlbyname(name, &result, &s, &value, s);
24 T_ASSERT_POSIX_SUCCESS(rc, "sysctlbyname(%s)", t);
25 return result;
26 }
27
28 T_DECL(hw_lck_ticket_allow_invalid, "hw_lck_ticket_allow_invalid",
29 T_META_RUN_CONCURRENTLY(false), T_META_TAG_VM_NOT_ELIGIBLE)
30 {
31 T_EXPECT_EQ(1ll, run_sysctl_test("hw_lck_ticket_allow_invalid", 0), "test succeeded");
32 }
33
34 T_DECL(smr_hash_basic, "smr_hash basic test", T_META_TAG_VM_PREFERRED)
35 {
36 T_EXPECT_EQ(1ll, run_sysctl_test("smr_hash_basic", 0), "test succeeded");
37 }
38
39 T_DECL(smr_shash_basic, "smr_shash basic test", T_META_TAG_VM_PREFERRED)
40 {
41 T_EXPECT_EQ(1ll, run_sysctl_test("smr_shash_basic", 0), "test succeeded");
42 }
43
44 static void
clpc_set_core_count(int ncpus)45 clpc_set_core_count(int ncpus)
46 {
47 #if __arm64__
48 char arg[20];
49 char *const clpcctrl_args[] = {
50 "/usr/local/bin/clpcctrl",
51 "-c",
52 arg,
53 NULL,
54 };
55 pid_t pid;
56 int rc;
57
58 snprintf(arg, sizeof(arg), "%d", ncpus);
59 rc = posix_spawn(&pid, clpcctrl_args[0], NULL, NULL, clpcctrl_args, NULL);
60 T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "posix_spawn");
61 waitpid(pid, &rc, 0);
62 #else
63 (void)ncpus;
64 #endif
65 }
66
67 static void *
toggle_cpus_thread(void * donep)68 toggle_cpus_thread(void *donep)
69 {
70 int ncpus = dt_ncpu();
71
72 do {
73 usleep(200 * 1000);
74 clpc_set_core_count(ncpus - 1);
75 usleep(200 * 1000);
76 clpc_set_core_count(ncpus);
77 } while (!*(bool *)donep);
78
79 return NULL;
80 }
81
82 T_DECL(smr_sleepable_stress, "smr_sleepable_stress_test",
83 T_META_RUN_CONCURRENTLY(false), T_META_TAG_VM_NOT_ELIGIBLE)
84 {
85 uint32_t secs = 4;
86 pthread_t pth;
87 bool done = false;
88 int rc;
89
90 rc = pthread_create(&pth, NULL, toggle_cpus_thread, &done);
91 T_ASSERT_POSIX_SUCCESS(rc, "pthread_create");
92
93 T_EXPECT_EQ(1ll, run_sysctl_test("smr_sleepable_stress", secs), "test succeeded");
94
95 done = true;
96 pthread_join(pth, NULL);
97 }
98