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