| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "test.h" |
| 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 |
#if INCLUDE_ATOMIC_SLIST_TEST |
| 50 |
|
| 51 |
#include <pjlib.h> |
| 52 |
|
| 53 |
#if defined(PJ_WIN32) && PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32 |
| 54 |
|
| 55 |
#ifndef MEMORY_ALLOCATION_ALIGNMENT |
| 56 |
# include <windows.h> |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifndef PJ_IS_ALIGNED |
| 60 |
# define PJ_IS_ALIGNED(PTR, ALIGNMENT) (!((pj_ssize_t)(PTR) & ((ALIGNMENT)-1))) |
| 61 |
#endif |
| 62 |
|
| 63 |
#endif |
| 64 |
|
| 65 |
#define THIS_FILE "atomic_slist.c" |
| 66 |
#define MAX_RESERVED 16 |
| 67 |
#define MAX_SLOTS 100 |
| 68 |
|
| 69 |
#define MAX_WORKER_THREADS 3 |
| 70 |
#define MAX_REPEATS 100000 |
| 71 |
|
| 72 |
#define TRACE(log) PJ_LOG(3,log) |
| 73 |
|
| 74 |
typedef struct PJ_ATOMIC_SLIST_ALIGN_PREFIX slist_node { |
| 75 |
PJ_DECL_ATOMIC_SLIST_MEMBER(struct slist_node); |
| 76 |
int value; |
| 77 |
char unaligner; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
} PJ_ATOMIC_SLIST_ALIGN_SUFFIX slist_node; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
typedef struct PJ_ATOMIC_SLIST_ALIGN_PREFIX slot_data { |
| 89 |
PJ_DECL_ATOMIC_SLIST_MEMBER(struct slot_data); |
| 90 |
|
| 91 |
|
| 92 |
pj_thread_t *owner; |
| 93 |
} PJ_ATOMIC_SLIST_ALIGN_SUFFIX slot_data; |
| 94 |
|
| 95 |
typedef struct slist_test_desc { |
| 96 |
struct { |
| 97 |
const char *title; |
| 98 |
int n_threads; |
| 99 |
unsigned repeat; |
| 100 |
} cfg; |
| 101 |
|
| 102 |
struct { |
| 103 |
pj_pool_t *pool; |
| 104 |
pj_atomic_slist *empty_slot_slist; |
| 105 |
|
| 106 |
|
| 107 |
char unaligner2; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
# if defined(_MSC_VER) |
| 118 |
# pragma warning(push) |
| 119 |
# pragma warning(disable:4324) // structure padded due to align() |
| 120 |
# endif |
| 121 |
slot_data slots[MAX_SLOTS]; |
| 122 |
|
| 123 |
# if defined(_MSC_VER) |
| 124 |
# pragma warning(pop) |
| 125 |
# endif |
| 126 |
|
| 127 |
int retcode; |
| 128 |
} state; |
| 129 |
} slist_test_desc; |
| 130 |
|
| 131 |
static int slist_stress_test_init(slist_test_desc* test); |
| 132 |
static int slist_stress_test_destroy(slist_test_desc* test); |
| 133 |
|
| 134 |
static int slist_stress_test(slist_test_desc* test); |
| 135 |
static int worker_thread(void* p); |
| 136 |
|
| 137 |
static slist_test_desc tests[] = { |
| 138 |
{ |
| 139 |
.cfg.title = "slist (single thread)", |
| 140 |
.cfg.n_threads = 0, |
| 141 |
.cfg.repeat = MAX_REPEATS |
| 142 |
}, |
| 143 |
{ |
| 144 |
.cfg.title = "slist (multi threads)", |
| 145 |
.cfg.n_threads = MAX_WORKER_THREADS, |
| 146 |
.cfg.repeat = MAX_REPEATS |
| 147 |
} |
| 148 |
}; |
| 149 |
|
| 150 |
int atomic_slist_test() |
| 151 |
{ |
| 152 |
pj_pool_t *pool = NULL; |
| 153 |
pj_atomic_slist *slist = NULL; |
| 154 |
const int sz = 15; |
| 155 |
slist_node *nodes; |
| 156 |
slist_node *p; |
| 157 |
int i; |
| 158 |
int rc = 0; |
| 159 |
|
| 160 |
pool = pj_pool_create(mem, NULL, 4096, 0, NULL); |
| 161 |
PJ_TEST_NOT_NULL(pool, NULL, { rc=-10; goto error; }); |
| 162 |
|
| 163 |
nodes = pj_atomic_slist_calloc(pool, sz, sizeof(slist_node)); |
| 164 |
PJ_TEST_NOT_NULL(nodes, NULL, { rc=-15; goto error; }); |
| 165 |
|
| 166 |
#if defined(PJ_WIN32) && PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32 |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
# if defined(_MSC_VER) |
| 171 |
# pragma warning(push) |
| 172 |
# pragma warning(disable:4324) // structure padded due to align() |
| 173 |
# endif |
| 174 |
|
| 175 |
PJ_TEST_GTE(TYPE_ALIGNMENT(slist_node), MEMORY_ALLOCATION_ALIGNMENT, NULL, { rc=-3; goto error; }); |
| 176 |
|
| 177 |
# if defined(_MSC_VER) |
| 178 |
# pragma warning(pop) |
| 179 |
# endif |
| 180 |
|
| 181 |
for (i = 0; i < sz; ++i) |
| 182 |
PJ_TEST_TRUE(PJ_IS_ALIGNED(&nodes[i], MEMORY_ALLOCATION_ALIGNMENT), NULL, { rc=-7; goto error; }); |
| 183 |
|
| 184 |
#endif |
| 185 |
|
| 186 |
|
| 187 |
PJ_TEST_SUCCESS(pj_atomic_slist_create(pool, &slist), NULL, { rc=-20; goto error; }); |
| 188 |
|
| 189 |
|
| 190 |
PJ_TEST_EQ(pj_atomic_slist_pop(slist), NULL, NULL, { rc=-25; goto error; }); |
| 191 |
PJ_TEST_EQ(pj_atomic_slist_size(slist), 0, NULL, { rc = -27; goto error; }); |
| 192 |
|
| 193 |
|
| 194 |
for (i = 0; i < sz; ++i) { |
| 195 |
nodes[i].value = i; |
| 196 |
PJ_TEST_SUCCESS(pj_atomic_slist_push(slist, &nodes[i]), NULL, { rc=-30; goto error; }); |
| 197 |
PJ_TEST_EQ(pj_atomic_slist_size(slist), i+1, NULL, { rc = -35; goto error; }); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
while((p = pj_atomic_slist_pop(slist)) != NULL) { |
| 202 |
--i; |
| 203 |
pj_assert(p->value == i); |
| 204 |
PJ_TEST_TRUE(p->value == i && p == nodes+i, NULL, { rc=-40; goto error; }); |
| 205 |
PJ_TEST_EQ(pj_atomic_slist_size(slist), i, NULL, { rc = -45; goto error; }); |
| 206 |
} |
| 207 |
PJ_TEST_EQ(i, 0, NULL, { rc=-50; goto error; }); |
| 208 |
PJ_TEST_EQ(pj_atomic_slist_size(slist), 0, NULL, { rc = -55; goto error; }); |
| 209 |
|
| 210 |
error: |
| 211 |
if (slist != NULL) |
| 212 |
PJ_TEST_SUCCESS(pj_atomic_slist_destroy(slist), NULL, if (!rc) rc = -60); |
| 213 |
|
| 214 |
if (pool) |
| 215 |
pj_pool_release(pool); |
| 216 |
|
| 217 |
return rc; |
| 218 |
} |
| 219 |
|
| 220 |
int atomic_slist_mt_test() |
| 221 |
{ |
| 222 |
pj_pool_t *pool = NULL; |
| 223 |
int rc = 0; |
| 224 |
int i; |
| 225 |
|
| 226 |
pool = pj_pool_create(mem, NULL, 4096, 0, NULL); |
| 227 |
PJ_TEST_NOT_NULL(pool, NULL, return -100); |
| 228 |
|
| 229 |
for (i = 0; !rc && i < PJ_ARRAY_SIZE(tests); ++i) { |
| 230 |
tests[i].state.pool = pool; |
| 231 |
rc = slist_stress_test(&tests[i]); |
| 232 |
} |
| 233 |
|
| 234 |
if (pool) |
| 235 |
pj_pool_release(pool); |
| 236 |
|
| 237 |
return rc; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
static int slist_stress_test(slist_test_desc* test) { |
| 246 |
unsigned i; |
| 247 |
pj_timestamp t1, t2; |
| 248 |
int rc; |
| 249 |
int ident; |
| 250 |
|
| 251 |
TRACE((THIS_FILE, "%s", test->cfg.title)); |
| 252 |
ident = pj_log_get_indent(); |
| 253 |
pj_log_push_indent(); |
| 254 |
|
| 255 |
rc = slist_stress_test_init(test); |
| 256 |
if (rc) |
| 257 |
return rc; |
| 258 |
|
| 259 |
pj_get_timestamp(&t1); |
| 260 |
|
| 261 |
if (test->cfg.n_threads == 0) |
| 262 |
worker_thread(test); |
| 263 |
else { |
| 264 |
unsigned n_threads = test->cfg.n_threads; |
| 265 |
pj_thread_t* threads[MAX_WORKER_THREADS]; |
| 266 |
|
| 267 |
for (i = 0; i < n_threads; ++i) |
| 268 |
PJ_TEST_SUCCESS((pj_thread_create(test->state.pool, "slist_stress_test", |
| 269 |
&worker_thread, test, |
| 270 |
0, PJ_THREAD_SUSPENDED, |
| 271 |
&threads[i])), |
| 272 |
"Unable to create thread", return -105); |
| 273 |
|
| 274 |
for (i = 0; i < n_threads; ++i) |
| 275 |
PJ_TEST_SUCCESS(pj_thread_resume(threads[i]), "Unable to resume thread", return -110); |
| 276 |
|
| 277 |
worker_thread(test); |
| 278 |
|
| 279 |
for (i = 0; i < n_threads; ++i) { |
| 280 |
pj_thread_join(threads[i]); |
| 281 |
pj_thread_destroy(threads[i]); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
pj_get_timestamp(&t2); |
| 286 |
|
| 287 |
rc = slist_stress_test_destroy(test); |
| 288 |
if (rc) |
| 289 |
return rc; |
| 290 |
|
| 291 |
TRACE((THIS_FILE, "%s time: %d ms (total), %d nanosec/op", test->cfg.title, |
| 292 |
pj_elapsed_msec(&t1, &t2), |
| 293 |
pj_elapsed_nanosec(&t1, &t2) / ((test->cfg.n_threads+1)*test->cfg.repeat))); |
| 294 |
|
| 295 |
pj_log_set_indent(ident); |
| 296 |
return test->state.retcode; |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
static int slist_stress_test_init(slist_test_desc* test) { |
| 301 |
slot_data* p; |
| 302 |
PJ_TEST_SUCCESS(pj_atomic_slist_create(test->state.pool, &test->state.empty_slot_slist), "Unable to create slist", return -115); |
| 303 |
|
| 304 |
for (p = test->state.slots + PJ_ARRAY_SIZE(test->state.slots) - 1; p > test->state.slots - 1; --p) |
| 305 |
PJ_TEST_SUCCESS(pj_atomic_slist_push(test->state.empty_slot_slist, p), "Unable to init slist", return -120); |
| 306 |
|
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
static int slist_stress_test_destroy(slist_test_desc* test) { |
| 311 |
PJ_TEST_SUCCESS(pj_atomic_slist_destroy(test->state.empty_slot_slist), "Unable to destroy slist", return -125); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
static int worker_thread(void* p) { |
| 317 |
slist_test_desc* test = (slist_test_desc*)p; |
| 318 |
|
| 319 |
unsigned n_events = 0; |
| 320 |
unsigned reserved_slots[MAX_RESERVED]; |
| 321 |
unsigned reserved_count = 0; |
| 322 |
unsigned slot_id; |
| 323 |
|
| 324 |
pj_bzero(reserved_slots, sizeof(reserved_slots)); |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
pj_log_set_indent(3); |
| 330 |
|
| 331 |
while (test->state.retcode == 0 && n_events < test->cfg.repeat) { |
| 332 |
slot_data* slot = pj_atomic_slist_pop(test->state.empty_slot_slist); |
| 333 |
if (slot != NULL) { |
| 334 |
PJ_TEST_EQ(slot->owner, NULL, "Reserved slot is not empty", { |
| 335 |
test->state.retcode = -130; |
| 336 |
break; |
| 337 |
}); |
| 338 |
slot->owner = pj_thread_this(); |
| 339 |
slot_id = (unsigned)(slot - test->state.slots); |
| 340 |
reserved_slots[reserved_count++] = slot_id; |
| 341 |
++n_events; |
| 342 |
} |
| 343 |
if (slot == NULL || |
| 344 |
reserved_count >= MAX_RESERVED || |
| 345 |
n_events >= test->cfg.repeat) { |
| 346 |
while (reserved_count) { |
| 347 |
slot_id = reserved_slots[--reserved_count]; |
| 348 |
slot = &test->state.slots[slot_id]; |
| 349 |
if (slot->owner == pj_thread_this()) { |
| 350 |
slot->owner = NULL; |
| 351 |
pj_atomic_slist_push(test->state.empty_slot_slist, slot); |
| 352 |
} else if (slot->owner == NULL) { |
| 353 |
PJ_LOG(1, (THIS_FILE, "Anothed thread has freed up this thread's slot")); |
| 354 |
test->state.retcode = -135; |
| 355 |
} else { |
| 356 |
PJ_LOG(1, (THIS_FILE, "Anothed thread has reserved this thread's slot")); |
| 357 |
test->state.retcode = -140; |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
} |
| 363 |
|
| 364 |
TRACE((THIS_FILE, "thread exiting %s, n_events=%d", test->cfg.title, n_events)); |
| 365 |
return 0; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
#else |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
int dummy_slist_test; |
| 374 |
#endif |
| 375 |
|
| 376 |
|