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