Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | /* * Copyright (c) 2020 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* sysctl interface for testing percpu counters in DEBUG or DEVELOPMENT kernel only. */ #if !(DEVELOPMENT || DEBUG) #error "Counter testing is not enabled on RELEASE configurations" #endif #include <sys/sysctl.h> #include <kern/counter.h> #include <machine/atomic.h> #include <libkern/libkern.h> #include <machine/machine_routines.h> #include <kern/cpu_data.h> #include <os/log.h> #ifdef CONFIG_XNUPOST #include <tests/xnupost.h> #endif /* CONFIG_XNUPOST */ static _Atomic boolean_t scalable_counter_test_running = FALSE; scalable_counter_t test_scalable_counter; SCALABLE_COUNTER_DEFINE(test_static_scalable_counter); #ifdef CONFIG_XNUPOST kern_return_t counter_tests(void); /* * Sanity test that a counter can be modified before zalloc is initialized. */ static void bump_static_counter(void* arg) { (void) arg; counter_inc(&test_static_scalable_counter); } STARTUP_ARG(PMAP_STEAL, STARTUP_RANK_MIDDLE, bump_static_counter, NULL); kern_return_t counter_tests() { T_ASSERT_EQ_ULLONG(counter_load(&test_static_scalable_counter), 1, "Counter was incremented"); return KERN_SUCCESS; } #endif /* CONFIG_XNUPOST */ static int sysctl_scalable_counter_test_start SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg1, arg2) int ret_val = 1; int error = 0; boolean_t exclusive; error = sysctl_io_number(req, ret_val, sizeof(int), &ret_val, NULL); if (error || !req->newptr) { return error; } /* The test doesn't support being run multiple times in parallel. */ exclusive = os_atomic_cmpxchg(&scalable_counter_test_running, FALSE, TRUE, seq_cst); if (!exclusive) { os_log(OS_LOG_DEFAULT, "scalable_counter_test: Caught attempt to run the test in parallel."); return EINVAL; } counter_alloc(&test_scalable_counter); return 0; } static int sysctl_scalable_counter_test_finish SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg1, arg2) boolean_t exclusive; int ret_val = 0; int error = 0; error = sysctl_io_number(req, ret_val, sizeof(int), &ret_val, NULL); if (error || !req->newptr) { return error; } /* The test doesn't support being run multiple times in parallel. */ exclusive = os_atomic_cmpxchg(&scalable_counter_test_running, TRUE, FALSE, seq_cst); if (!exclusive) { /* Finish called without start. */ return EINVAL; } return 0; } static int sysctl_scalable_counter_add SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg1, arg2) int64_t value = 0; int error = 0; if (!os_atomic_load(&scalable_counter_test_running, seq_cst)) { /* Must call start */ return EINVAL; } error = sysctl_io_number(req, value, sizeof(int64_t), &value, NULL); if (error || !req->newptr) { return error; } counter_add(&test_scalable_counter, value); return 0; } static int sysctl_static_scalable_counter_add SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg1, arg2) int64_t value = 0; int error = 0; if (!os_atomic_load(&scalable_counter_test_running, seq_cst)) { /* Must call start */ return EINVAL; } error = sysctl_io_number(req, value, sizeof(int64_t), &value, NULL); if (error || !req->newptr) { return error; } counter_add(&test_static_scalable_counter, value); return 0; } static int sysctl_scalable_counter_load SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg1, arg2) uint64_t value; if (!os_atomic_load(&scalable_counter_test_running, seq_cst)) { /* Must call start */ return EINVAL; } value = counter_load(&test_scalable_counter); return SYSCTL_OUT(req, &value, sizeof(value)); } static int sysctl_scalable_counter_write_benchmark SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg1, arg2) int error; int64_t iterations; int ret_val = 0; if (!os_atomic_load(&scalable_counter_test_running, seq_cst)) { /* Must call start */ return EINVAL; } error = sysctl_io_number(req, ret_val, sizeof(int), &iterations, NULL); if (error || !req->newptr) { return error; } for (int64_t i = 0; i < iterations; i++) { counter_inc(&test_scalable_counter); } return 0; } static volatile uint64_t racy_counter; static int sysctl_racy_counter_write_benchmark SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg1, arg2) int error; int64_t iterations; int ret_val = 0; error = sysctl_io_number(req, ret_val, sizeof(int), &iterations, NULL); if (error || !req->newptr) { return error; } for (int64_t i = 0; i < iterations; i++) { racy_counter++; } return 0; } static int sysctl_racy_counter_load SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg1, arg2) uint64_t value = racy_counter; return SYSCTL_OUT(req, &value, sizeof(value)); } static _Atomic uint64_t atomic_counter; static int sysctl_atomic_counter_write_benchmark SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg1, arg2) int error; int64_t iterations; int ret_val = 0; error = sysctl_io_number(req, ret_val, sizeof(int), &iterations, NULL); if (error || !req->newptr) { return error; } for (int64_t i = 0; i < iterations; i++) { os_atomic_add(&atomic_counter, 1, relaxed); } return 0; } static int sysctl_atomic_counter_load SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg1, arg2) uint64_t value = os_atomic_load_wide(&atomic_counter, relaxed); return SYSCTL_OUT(req, &value, sizeof(value)); } SYSCTL_PROC(_kern, OID_AUTO, scalable_counter_test_start, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MASKED | CTLFLAG_LOCKED, 0, 0, sysctl_scalable_counter_test_start, "I", "Setup per-cpu counter test"); SYSCTL_PROC(_kern, OID_AUTO, scalable_counter_test_finish, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MASKED | CTLFLAG_LOCKED, 0, 0, sysctl_scalable_counter_test_finish, "I", "Finish per-cpu counter test"); SYSCTL_PROC(_kern, OID_AUTO, scalable_counter_test_add, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_MASKED | CTLFLAG_LOCKED, 0, 0, sysctl_scalable_counter_add, "I", "Perform an add on the per-cpu counter"); SYSCTL_PROC(_kern, OID_AUTO, static_scalable_counter_test_add, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_MASKED | CTLFLAG_LOCKED, 0, 0, sysctl_static_scalable_counter_add, "I", "Perform an add on the static per-cpu counter"); SYSCTL_PROC(_kern, OID_AUTO, scalable_counter_test_load, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_MASKED | CTLFLAG_LOCKED, 0, 0, sysctl_scalable_counter_load, "I", "Load the current per-cpu counter value."); SYSCTL_SCALABLE_COUNTER(_kern, static_scalable_counter_test_load, test_static_scalable_counter, "Load the current static per-cpu counter value."); SYSCTL_PROC(_kern, OID_AUTO, scalable_counter_write_benchmark, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MASKED | CTLFLAG_LOCKED, 0, 0, sysctl_scalable_counter_write_benchmark, "I", "Per-cpu counter write benchmark"); SYSCTL_PROC(_kern, OID_AUTO, scalable_counter_racy_counter_benchmark, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MASKED | CTLFLAG_LOCKED, 0, 0, sysctl_racy_counter_write_benchmark, "I", "Global counter racy benchmark"); SYSCTL_PROC(_kern, OID_AUTO, scalable_counter_racy_counter_load, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MASKED | CTLFLAG_LOCKED, 0, 0, sysctl_racy_counter_load, "I", "Global counter racy load"); SYSCTL_PROC(_kern, OID_AUTO, scalable_counter_atomic_counter_write_benchmark, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MASKED | CTLFLAG_LOCKED, 0, 0, sysctl_atomic_counter_write_benchmark, "I", "Atomic counter write benchmark"); SYSCTL_PROC(_kern, OID_AUTO, scalable_counter_atomic_counter_load, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MASKED | CTLFLAG_LOCKED, 0, 0, sysctl_atomic_counter_load, "I", "Atomic counter load"); |