xref: /xnu-8796.141.3/tests/counter/counter.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <stdatomic.h>
2*1b191cb5SApple OSS Distributions #include <sys/kern_sysctl.h>
3*1b191cb5SApple OSS Distributions 
4*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
5*1b191cb5SApple OSS Distributions #include <darwintest.h>
6*1b191cb5SApple OSS Distributions 
7*1b191cb5SApple OSS Distributions #include "counter/common.h"
8*1b191cb5SApple OSS Distributions #include "test_utils.h"
9*1b191cb5SApple OSS Distributions 
10*1b191cb5SApple OSS Distributions static unsigned int ncpu(void);
11*1b191cb5SApple OSS Distributions 
12*1b191cb5SApple OSS Distributions static uint64_t
sysctl_read(const char * name)13*1b191cb5SApple OSS Distributions sysctl_read(const char *name)
14*1b191cb5SApple OSS Distributions {
15*1b191cb5SApple OSS Distributions 	int result;
16*1b191cb5SApple OSS Distributions 	uint64_t value;
17*1b191cb5SApple OSS Distributions 	size_t size = sizeof(value);
18*1b191cb5SApple OSS Distributions 	result = sysctlbyname(name, &value, &size, NULL, 0);
19*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(result, "Read from %s", name);
20*1b191cb5SApple OSS Distributions 	return value;
21*1b191cb5SApple OSS Distributions }
22*1b191cb5SApple OSS Distributions 
23*1b191cb5SApple OSS Distributions static void
sysctl_write(const char * name,int64_t amount)24*1b191cb5SApple OSS Distributions sysctl_write(const char* name, int64_t amount)
25*1b191cb5SApple OSS Distributions {
26*1b191cb5SApple OSS Distributions 	kern_return_t result;
27*1b191cb5SApple OSS Distributions 	result = sysctlbyname(name, NULL, NULL, &amount, sizeof(int64_t));
28*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(result, "Write to %s", name);
29*1b191cb5SApple OSS Distributions }
30*1b191cb5SApple OSS Distributions 
31*1b191cb5SApple OSS Distributions static void
scalable_counter_add(int64_t amount)32*1b191cb5SApple OSS Distributions scalable_counter_add(int64_t amount)
33*1b191cb5SApple OSS Distributions {
34*1b191cb5SApple OSS Distributions 	sysctl_write("kern.scalable_counter_test_add", amount);
35*1b191cb5SApple OSS Distributions }
36*1b191cb5SApple OSS Distributions 
37*1b191cb5SApple OSS Distributions static void
static_scalable_counter_add(int64_t amount)38*1b191cb5SApple OSS Distributions static_scalable_counter_add(int64_t amount)
39*1b191cb5SApple OSS Distributions {
40*1b191cb5SApple OSS Distributions 	sysctl_write("kern.static_scalable_counter_test_add", amount);
41*1b191cb5SApple OSS Distributions }
42*1b191cb5SApple OSS Distributions 
43*1b191cb5SApple OSS Distributions static int64_t
scalable_counter_load(void)44*1b191cb5SApple OSS Distributions scalable_counter_load(void)
45*1b191cb5SApple OSS Distributions {
46*1b191cb5SApple OSS Distributions 	return (int64_t) sysctl_read("kern.scalable_counter_test_load");
47*1b191cb5SApple OSS Distributions }
48*1b191cb5SApple OSS Distributions 
49*1b191cb5SApple OSS Distributions static int64_t
static_scalable_counter_load(void)50*1b191cb5SApple OSS Distributions static_scalable_counter_load(void)
51*1b191cb5SApple OSS Distributions {
52*1b191cb5SApple OSS Distributions 	return (int64_t) sysctl_read("kern.static_scalable_counter_test_load");
53*1b191cb5SApple OSS Distributions }
54*1b191cb5SApple OSS Distributions 
55*1b191cb5SApple OSS Distributions /*
56*1b191cb5SApple OSS Distributions  * A background thread that bangs on the percpu counter and then exits.
57*1b191cb5SApple OSS Distributions  * @param num_iterations How many times to bang on the counter. Each iteration makes the counter
58*1b191cb5SApple OSS Distributions  * bigger by 100.
59*1b191cb5SApple OSS Distributions  */
60*1b191cb5SApple OSS Distributions static void*
background_scalable_counter_thread(void * num_iterations_ptr)61*1b191cb5SApple OSS Distributions background_scalable_counter_thread(void* num_iterations_ptr)
62*1b191cb5SApple OSS Distributions {
63*1b191cb5SApple OSS Distributions 	int64_t i, num_iterations;
64*1b191cb5SApple OSS Distributions 	num_iterations = (int64_t)(num_iterations_ptr);
65*1b191cb5SApple OSS Distributions 	for (i = 0; i < num_iterations; i++) {
66*1b191cb5SApple OSS Distributions 		scalable_counter_add(-25);
67*1b191cb5SApple OSS Distributions 		scalable_counter_add(75);
68*1b191cb5SApple OSS Distributions 		scalable_counter_add(-100);
69*1b191cb5SApple OSS Distributions 		scalable_counter_add(150);
70*1b191cb5SApple OSS Distributions 	}
71*1b191cb5SApple OSS Distributions 	atomic_thread_fence(memory_order_release);
72*1b191cb5SApple OSS Distributions 	return 0;
73*1b191cb5SApple OSS Distributions }
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions static
76*1b191cb5SApple OSS Distributions void
darwin_test_fini_scalable_counter_test()77*1b191cb5SApple OSS Distributions darwin_test_fini_scalable_counter_test()
78*1b191cb5SApple OSS Distributions {
79*1b191cb5SApple OSS Distributions 	int ret = fini_scalable_counter_test();
80*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "fini_scalable_counter_test");
81*1b191cb5SApple OSS Distributions }
82*1b191cb5SApple OSS Distributions 
83*1b191cb5SApple OSS Distributions static
84*1b191cb5SApple OSS Distributions void
darwin_test_setup(void)85*1b191cb5SApple OSS Distributions darwin_test_setup(void)
86*1b191cb5SApple OSS Distributions {
87*1b191cb5SApple OSS Distributions 	T_SETUPBEGIN;
88*1b191cb5SApple OSS Distributions 	int dev_kernel = is_development_kernel();
89*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(dev_kernel, "sysctlbyname kern.development");
90*1b191cb5SApple OSS Distributions 	if (is_development_kernel() != 1) {
91*1b191cb5SApple OSS Distributions 		T_SKIP("Skipping test on non development kernel.");
92*1b191cb5SApple OSS Distributions 	}
93*1b191cb5SApple OSS Distributions 	init_scalable_counter_test();
94*1b191cb5SApple OSS Distributions 	T_SETUPEND;
95*1b191cb5SApple OSS Distributions 	T_ATEND(darwin_test_fini_scalable_counter_test);
96*1b191cb5SApple OSS Distributions }
97*1b191cb5SApple OSS Distributions 
98*1b191cb5SApple OSS Distributions T_DECL(test_scalable_counters_single_threaded, "Test single threaded operations on scalable_counters", T_META_ASROOT(true))
99*1b191cb5SApple OSS Distributions {
100*1b191cb5SApple OSS Distributions 	static int64_t kNumIterations = 100, i, expected_value = 0;
101*1b191cb5SApple OSS Distributions 	darwin_test_setup();
102*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(scalable_counter_load(), 0LL, "Counter starts at zero");
103*1b191cb5SApple OSS Distributions 
104*1b191cb5SApple OSS Distributions 	/* Simple add, subtract, and read */
105*1b191cb5SApple OSS Distributions 	scalable_counter_add(1);
106*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(scalable_counter_load(), 1LL, "0 + 1 == 1");
107*1b191cb5SApple OSS Distributions 	scalable_counter_add(-1);
108*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(scalable_counter_load(), 0LL, "1 - 1 == 0");
109*1b191cb5SApple OSS Distributions 	for (i = 0; i < kNumIterations; i++) {
110*1b191cb5SApple OSS Distributions 		scalable_counter_add(i);
111*1b191cb5SApple OSS Distributions 		expected_value += i;
112*1b191cb5SApple OSS Distributions 	}
113*1b191cb5SApple OSS Distributions 	for (i = 0; i < kNumIterations / 2; i++) {
114*1b191cb5SApple OSS Distributions 		scalable_counter_add(-i);
115*1b191cb5SApple OSS Distributions 		expected_value -= i;
116*1b191cb5SApple OSS Distributions 	}
117*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(scalable_counter_load(), expected_value, "Counter value is correct.");
118*1b191cb5SApple OSS Distributions 	T_END;
119*1b191cb5SApple OSS Distributions }
120*1b191cb5SApple OSS Distributions 
121*1b191cb5SApple OSS Distributions T_DECL(test_static_counter, "Test staticly declared counter", T_META_ASROOT(true))
122*1b191cb5SApple OSS Distributions {
123*1b191cb5SApple OSS Distributions 	static size_t kNumIterations = 100;
124*1b191cb5SApple OSS Distributions 	int64_t start_value;
125*1b191cb5SApple OSS Distributions 	darwin_test_setup();
126*1b191cb5SApple OSS Distributions 	start_value = static_scalable_counter_load();
127*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < kNumIterations; i++) {
128*1b191cb5SApple OSS Distributions 		static_scalable_counter_add(1);
129*1b191cb5SApple OSS Distributions 	}
130*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(static_scalable_counter_load(), (long long) kNumIterations + start_value, "Counter value is correct");
131*1b191cb5SApple OSS Distributions 	T_END;
132*1b191cb5SApple OSS Distributions }
133*1b191cb5SApple OSS Distributions 
134*1b191cb5SApple OSS Distributions T_DECL(test_scalable_counters_multithreaded, "Test multi-threaded operations on scalable_counters", T_META_ASROOT(true))
135*1b191cb5SApple OSS Distributions {
136*1b191cb5SApple OSS Distributions 	unsigned int kNumThreads = ncpu() * 5;
137*1b191cb5SApple OSS Distributions 	int ret;
138*1b191cb5SApple OSS Distributions 	int64_t i;
139*1b191cb5SApple OSS Distributions 	pthread_attr_t pthread_attr;
140*1b191cb5SApple OSS Distributions 	pthread_t *threads;
141*1b191cb5SApple OSS Distributions 
142*1b191cb5SApple OSS Distributions 	darwin_test_setup();
143*1b191cb5SApple OSS Distributions 
144*1b191cb5SApple OSS Distributions 	threads = malloc(sizeof(pthread_t) * kNumThreads);
145*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(threads, "Out of memory");
146*1b191cb5SApple OSS Distributions 
147*1b191cb5SApple OSS Distributions 	ret = pthread_attr_init(&pthread_attr);
148*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_attr_init");
149*1b191cb5SApple OSS Distributions 
150*1b191cb5SApple OSS Distributions 	int64_t expected_value = 0;
151*1b191cb5SApple OSS Distributions 	for (i = 0; i < kNumThreads; i++) {
152*1b191cb5SApple OSS Distributions 		ret = pthread_create(&threads[i], &pthread_attr, background_scalable_counter_thread, (void*)(i));
153*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_create");
154*1b191cb5SApple OSS Distributions 		expected_value += 100 * i;
155*1b191cb5SApple OSS Distributions 	}
156*1b191cb5SApple OSS Distributions 
157*1b191cb5SApple OSS Distributions 	for (i = 0; i < kNumThreads; i++) {
158*1b191cb5SApple OSS Distributions 		void *exit_code;
159*1b191cb5SApple OSS Distributions 		ret = pthread_join(threads[i], &exit_code);
160*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_join");
161*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ((ptrdiff_t) exit_code, (ptrdiff_t) 0, "Background thread exited sucessfully.");
162*1b191cb5SApple OSS Distributions 	}
163*1b191cb5SApple OSS Distributions 	atomic_thread_fence(memory_order_acquire);
164*1b191cb5SApple OSS Distributions 
165*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(scalable_counter_load(), expected_value, "Counter value is correct.");
166*1b191cb5SApple OSS Distributions 
167*1b191cb5SApple OSS Distributions 	ret = pthread_attr_destroy(&pthread_attr);
168*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_attr_destroy");
169*1b191cb5SApple OSS Distributions 	free(threads);
170*1b191cb5SApple OSS Distributions }
171*1b191cb5SApple OSS Distributions 
172*1b191cb5SApple OSS Distributions static unsigned int
ncpu()173*1b191cb5SApple OSS Distributions ncpu()
174*1b191cb5SApple OSS Distributions {
175*1b191cb5SApple OSS Distributions 	kern_return_t result;
176*1b191cb5SApple OSS Distributions 	int ncpu;
177*1b191cb5SApple OSS Distributions 	size_t size = sizeof(ncpu);
178*1b191cb5SApple OSS Distributions 	result = sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0);
179*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(result, "hw.npu");
180*1b191cb5SApple OSS Distributions 	return (unsigned int) ncpu;
181*1b191cb5SApple OSS Distributions }
182