| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
#include <sched.h> /* IWYU pragma: keep */ |
| 16 |
#include <setjmp.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <stddef.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#define UNIT_TESTING |
| 25 |
#include <cmocka.h> |
| 26 |
|
| 27 |
#include <isc/job.h> |
| 28 |
#include <isc/quota.h> |
| 29 |
#include <isc/result.h> |
| 30 |
#include <isc/thread.h> |
| 31 |
#include <isc/util.h> |
| 32 |
#include <isc/uv.h> |
| 33 |
|
| 34 |
#include <tests/isc.h> |
| 35 |
|
| 36 |
isc_quota_t quota; |
| 37 |
|
| 38 |
ISC_RUN_TEST_IMPL(isc_quota_get_set) { |
| 39 |
UNUSED(state); |
| 40 |
isc_quota_init("a, 100); |
| 41 |
|
| 42 |
assert_int_equal(isc_quota_getmax("a), 100); |
| 43 |
assert_int_equal(isc_quota_getsoft("a), 0); |
| 44 |
|
| 45 |
isc_quota_max("a, 50); |
| 46 |
isc_quota_soft("a, 30); |
| 47 |
|
| 48 |
assert_int_equal(isc_quota_getmax("a), 50); |
| 49 |
assert_int_equal(isc_quota_getsoft("a), 30); |
| 50 |
|
| 51 |
assert_int_equal(isc_quota_getused("a), 0); |
| 52 |
isc_quota_acquire("a); |
| 53 |
assert_int_equal(isc_quota_getused("a), 1); |
| 54 |
isc_quota_release("a); |
| 55 |
assert_int_equal(isc_quota_getused("a), 0); |
| 56 |
|
| 57 |
/* Unlimited */ |
| 58 |
isc_quota_max("a, 0); |
| 59 |
isc_quota_soft("a, 0); |
| 60 |
|
| 61 |
isc_quota_destroy("a); |
| 62 |
} |
| 63 |
|
| 64 |
static void |
| 65 |
add_quota(isc_quota_t *source, isc_result_t expected_result, |
| 66 |
int expected_used) { |
| 67 |
isc_result_t result; |
| 68 |
|
| 69 |
result = isc_quota_acquire(source); |
| 70 |
assert_int_equal(result, expected_result); |
| 71 |
|
| 72 |
switch (expected_result) { |
| 73 |
case ISC_R_SUCCESS: |
| 74 |
case ISC_R_SOFTQUOTA: |
| 75 |
break; |
| 76 |
default: |
| 77 |
break; |
| 78 |
} |
| 79 |
|
| 80 |
assert_int_equal(isc_quota_getused(source), expected_used); |
| 81 |
} |
| 82 |
|
| 83 |
ISC_RUN_TEST_IMPL(isc_quota_hard) { |
| 84 |
int i; |
| 85 |
UNUSED(state); |
| 86 |
|
| 87 |
isc_quota_init("a, 100); |
| 88 |
|
| 89 |
for (i = 0; i < 100; i++) { |
| 90 |
add_quota("a, ISC_R_SUCCESS, i + 1); |
| 91 |
} |
| 92 |
|
| 93 |
add_quota("a, ISC_R_QUOTA, 100); |
| 94 |
|
| 95 |
assert_int_equal(isc_quota_getused("a), 100); |
| 96 |
|
| 97 |
isc_quota_release("a); |
| 98 |
|
| 99 |
add_quota("a, ISC_R_SUCCESS, 100); |
| 100 |
add_quota("a, ISC_R_QUOTA, 100); |
| 101 |
|
| 102 |
for (i = 100; i > 0; i--) { |
| 103 |
isc_quota_release("a); |
| 104 |
assert_int_equal(isc_quota_getused("a), i - 1); |
| 105 |
} |
| 106 |
assert_int_equal(isc_quota_getused("a), 0); |
| 107 |
isc_quota_destroy("a); |
| 108 |
} |
| 109 |
|
| 110 |
ISC_RUN_TEST_IMPL(isc_quota_soft) { |
| 111 |
int i; |
| 112 |
UNUSED(state); |
| 113 |
|
| 114 |
isc_quota_init("a, 100); |
| 115 |
isc_quota_soft("a, 50); |
| 116 |
|
| 117 |
for (i = 0; i < 50; i++) { |
| 118 |
add_quota("a, ISC_R_SUCCESS, i + 1); |
| 119 |
} |
| 120 |
for (i = 50; i < 100; i++) { |
| 121 |
add_quota("a, ISC_R_SOFTQUOTA, i + 1); |
| 122 |
} |
| 123 |
|
| 124 |
add_quota("a, ISC_R_QUOTA, 100); |
| 125 |
|
| 126 |
for (i = 99; i >= 0; i--) { |
| 127 |
isc_quota_release("a); |
| 128 |
assert_int_equal(isc_quota_getused("a), i); |
| 129 |
} |
| 130 |
assert_int_equal(isc_quota_getused("a), 0); |
| 131 |
isc_quota_destroy("a); |
| 132 |
} |
| 133 |
|
| 134 |
static atomic_uint_fast32_t cb_calls = 0; |
| 135 |
static isc_job_t cbs[30]; |
| 136 |
|
| 137 |
static void |
| 138 |
callback(void *data) { |
| 139 |
int val = *(int *)data; |
| 140 |
/* Callback is not called if we get the quota directly */ |
| 141 |
assert_int_not_equal(val, -1); |
| 142 |
|
| 143 |
/* Verify that the callbacks are called in order */ |
| 144 |
int v = atomic_fetch_add_relaxed(&cb_calls, 1); |
| 145 |
assert_int_equal(v, val); |
| 146 |
|
| 147 |
/* |
| 148 |
* First 5 will be detached by the test function, |
| 149 |
* for the last 5 - do a 'chain detach'. |
| 150 |
*/ |
| 151 |
if (v >= 5) { |
| 152 |
isc_quota_release("a); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
ISC_RUN_TEST_IMPL(isc_quota_callback) { |
| 157 |
isc_result_t result; |
| 158 |
/* |
| 159 |
* - 10 calls that end with SUCCESS |
| 160 |
* - 10 calls that end with SOFTQUOTA |
| 161 |
* - 10 callbacks |
| 162 |
*/ |
| 163 |
int ints[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 164 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 165 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 166 |
int i; |
| 167 |
UNUSED(state); |
| 168 |
|
| 169 |
isc_quota_init("a, 20); |
| 170 |
isc_quota_soft("a, 10); |
| 171 |
|
| 172 |
for (i = 0; i < 10; i++) { |
| 173 |
cbs[i] = (isc_job_t)ISC_JOB_INITIALIZER; |
| 174 |
result = isc_quota_acquire_cb("a, &cbs[i], callback, |
| 175 |
&ints[i]); |
| 176 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 177 |
assert_int_equal(isc_quota_getused("a), i + 1); |
| 178 |
} |
| 179 |
for (i = 10; i < 20; i++) { |
| 180 |
cbs[i] = (isc_job_t)ISC_JOB_INITIALIZER; |
| 181 |
result = isc_quota_acquire_cb("a, &cbs[i], callback, |
| 182 |
&ints[i]); |
| 183 |
assert_int_equal(result, ISC_R_SOFTQUOTA); |
| 184 |
assert_int_equal(isc_quota_getused("a), i + 1); |
| 185 |
} |
| 186 |
|
| 187 |
for (i = 20; i < 30; i++) { |
| 188 |
cbs[i] = (isc_job_t)ISC_JOB_INITIALIZER; |
| 189 |
result = isc_quota_acquire_cb("a, &cbs[i], callback, |
| 190 |
&ints[i]); |
| 191 |
assert_int_equal(result, ISC_R_QUOTA); |
| 192 |
assert_int_equal(isc_quota_getused("a), 20); |
| 193 |
} |
| 194 |
assert_int_equal(atomic_load(&cb_calls), 0); |
| 195 |
|
| 196 |
for (i = 0; i < 5; i++) { |
| 197 |
isc_quota_release("a); |
| 198 |
assert_int_equal(isc_quota_getused("a), 20); |
| 199 |
assert_int_equal(atomic_load(&cb_calls), i + 1); |
| 200 |
} |
| 201 |
/* That should cause a chain reaction */ |
| 202 |
isc_quota_release("a); |
| 203 |
assert_int_equal(atomic_load(&cb_calls), 10); |
| 204 |
|
| 205 |
/* Release the quotas that we did not released in the callback */ |
| 206 |
for (i = 0; i < 5; i++) { |
| 207 |
isc_quota_release("a); |
| 208 |
} |
| 209 |
|
| 210 |
for (i = 6; i < 20; i++) { |
| 211 |
isc_quota_release("a); |
| 212 |
assert_int_equal(isc_quota_getused("a), 19 - i); |
| 213 |
} |
| 214 |
assert_int_equal(atomic_load(&cb_calls), 10); |
| 215 |
|
| 216 |
assert_int_equal(isc_quota_getused("a), 0); |
| 217 |
isc_quota_destroy("a); |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* Multithreaded quota callback test: |
| 222 |
* - quota set to 100 |
| 223 |
* - 10 threads, each trying to get 100 quotas. |
| 224 |
* - creates a separate thread to release it after 10ms |
| 225 |
*/ |
| 226 |
|
| 227 |
typedef struct qthreadinfo { |
| 228 |
atomic_uint_fast32_t direct; |
| 229 |
atomic_uint_fast32_t callback; |
| 230 |
isc_job_t callbacks[100]; |
| 231 |
} qthreadinfo_t; |
| 232 |
|
| 233 |
static atomic_uint_fast32_t g_tnum = 0; |
| 234 |
/* at most 10 * 100 quota_detach threads */ |
| 235 |
isc_thread_t g_threads[10 * 100]; |
| 236 |
|
| 237 |
static void * |
| 238 |
quota_release(void *arg) { |
| 239 |
uv_sleep(10); |
| 240 |
isc_quota_release((isc_quota_t *)arg); |
| 241 |
return NULL; |
| 242 |
} |
| 243 |
|
| 244 |
static void |
| 245 |
quota_callback(void *data) { |
| 246 |
qthreadinfo_t *qti = (qthreadinfo_t *)data; |
| 247 |
atomic_fetch_add_relaxed(&qti->callback, 1); |
| 248 |
int tnum = atomic_fetch_add_relaxed(&g_tnum, 1); |
| 249 |
isc_thread_create(quota_release, "a, &g_threads[tnum]); |
| 250 |
} |
| 251 |
|
| 252 |
static void * |
| 253 |
quota_thread(void *qtip) { |
| 254 |
qthreadinfo_t *qti = (qthreadinfo_t *)qtip; |
| 255 |
for (int i = 0; i < 100; i++) { |
| 256 |
qti->callbacks[i] = (isc_job_t)ISC_JOB_INITIALIZER; |
| 257 |
isc_result_t result = isc_quota_acquire_cb( |
| 258 |
"a, &qti->callbacks[i], quota_callback, qti); |
| 259 |
if (result == ISC_R_SUCCESS) { |
| 260 |
atomic_fetch_add_relaxed(&qti->direct, 1); |
| 261 |
int tnum = atomic_fetch_add_relaxed(&g_tnum, 1); |
| 262 |
isc_thread_create(quota_release, "a, |
| 263 |
&g_threads[tnum]); |
| 264 |
} |
| 265 |
} |
| 266 |
return NULL; |
| 267 |
} |
| 268 |
|
| 269 |
ISC_RUN_TEST_IMPL(isc_quota_callback_mt) { |
| 270 |
UNUSED(state); |
| 271 |
int i; |
| 272 |
|
| 273 |
isc_quota_init("a, 100); |
| 274 |
static qthreadinfo_t qtis[10]; |
| 275 |
isc_thread_t threads[10]; |
| 276 |
for (i = 0; i < 10; i++) { |
| 277 |
atomic_init(&qtis[i].direct, 0); |
| 278 |
atomic_init(&qtis[i].callback, 0); |
| 279 |
isc_thread_create(quota_thread, &qtis[i], &threads[i]); |
| 280 |
} |
| 281 |
for (i = 0; i < 10; i++) { |
| 282 |
isc_thread_join(threads[i], NULL); |
| 283 |
} |
| 284 |
|
| 285 |
for (i = 0; i < (int)atomic_load(&g_tnum); i++) { |
| 286 |
isc_thread_join(g_threads[i], NULL); |
| 287 |
} |
| 288 |
int direct = 0, ncallback = 0; |
| 289 |
|
| 290 |
for (i = 0; i < 10; i++) { |
| 291 |
direct += atomic_load(&qtis[i].direct); |
| 292 |
ncallback += atomic_load(&qtis[i].callback); |
| 293 |
} |
| 294 |
/* Total quota gained must be 10 threads * 100 tries */ |
| 295 |
assert_int_equal(direct + ncallback, 10 * 100); |
| 296 |
/* |
| 297 |
* At least 100 must be direct, the rest is virtually random: |
| 298 |
* - in a regular run I'm constantly getting 100:900 ratio |
| 299 |
* - under rr - usually around ~120:880 |
| 300 |
* - under rr -h - 1000:0 |
| 301 |
*/ |
| 302 |
assert_true(direct >= 100); |
| 303 |
|
| 304 |
isc_quota_destroy("a); |
| 305 |
} |
| 306 |
|
| 307 |
ISC_TEST_LIST_START |
| 308 |
|
| 309 |
ISC_TEST_ENTRY(isc_quota_get_set) |
| 310 |
ISC_TEST_ENTRY(isc_quota_hard) |
| 311 |
ISC_TEST_ENTRY(isc_quota_soft) |
| 312 |
ISC_TEST_ENTRY(isc_quota_callback) |
| 313 |
ISC_TEST_ENTRY(isc_quota_callback_mt) |
| 314 |
|
| 315 |
ISC_TEST_LIST_END |
| 316 |
|
| 317 |
ISC_TEST_MAIN |