1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <rte_common.h>
13 #include <rte_memory.h>
14 #include <rte_per_lcore.h>
15 #include <rte_launch.h>
16 #include <rte_atomic.h>
17 #include <rte_rwlock.h>
19 #include <rte_lcore.h>
20 #include <rte_cycles.h>
27 * Provides UT for rte_rwlock API.
28 * Main concern is on functional testing, but also provides some
29 * performance measurements.
30 * Obviously for proper testing need to be executed with more than one lcore.
37 static rte_rwlock_t sl;
38 static rte_rwlock_t sl_tab[RTE_MAX_LCORE];
39 static rte_atomic32_t synchro;
50 uint8_t u8[RTE_CACHE_LINE_SIZE];
51 uint64_t u64[RTE_CACHE_LINE_SIZE / sizeof(uint64_t)];
53 } __rte_cache_aligned try_rwlock_data;
55 struct try_rwlock_lcore {
63 } __rte_cache_aligned;
65 static struct try_rwlock_lcore try_lcore_data[RTE_MAX_LCORE];
68 test_rwlock_per_core(__rte_unused void *arg)
70 rte_rwlock_write_lock(&sl);
71 printf("Global write lock taken on core %u\n", rte_lcore_id());
72 rte_rwlock_write_unlock(&sl);
74 rte_rwlock_write_lock(&sl_tab[rte_lcore_id()]);
75 printf("Hello from core %u !\n", rte_lcore_id());
76 rte_rwlock_write_unlock(&sl_tab[rte_lcore_id()]);
78 rte_rwlock_read_lock(&sl);
79 printf("Global read lock taken on core %u\n", rte_lcore_id());
81 printf("Release global read lock on core %u\n", rte_lcore_id());
82 rte_rwlock_read_unlock(&sl);
87 static rte_rwlock_t lk = RTE_RWLOCK_INITIALIZER;
88 static volatile uint64_t rwlock_data;
89 static uint64_t time_count[RTE_MAX_LCORE] = {0};
91 #define MAX_LOOP 10000
92 #define TEST_RWLOCK_DEBUG 0
95 load_loop_fn(__rte_unused void *arg)
97 uint64_t time_diff = 0, begin;
98 uint64_t hz = rte_get_timer_hz();
100 const unsigned int lcore = rte_lcore_id();
102 /* wait synchro for slaves */
103 if (lcore != rte_get_master_lcore())
104 while (rte_atomic32_read(&synchro) == 0)
107 begin = rte_rdtsc_precise();
108 while (lcount < MAX_LOOP) {
109 rte_rwlock_write_lock(&lk);
111 rte_rwlock_write_unlock(&lk);
113 rte_rwlock_read_lock(&lk);
114 if (TEST_RWLOCK_DEBUG && !(lcount % 100))
115 printf("Core [%u] rwlock_data = %"PRIu64"\n",
117 rte_rwlock_read_unlock(&lk);
120 /* delay to make lock duty cycle slightly realistic */
124 time_diff = rte_rdtsc_precise() - begin;
125 time_count[lcore] = time_diff * 1000000 / hz;
130 test_rwlock_perf(void)
135 printf("\nRwlock Perf Test on %u cores...\n", rte_lcore_count());
137 /* clear synchro and start slaves */
138 rte_atomic32_set(&synchro, 0);
139 if (rte_eal_mp_remote_launch(load_loop_fn, NULL, SKIP_MASTER) < 0)
142 /* start synchro and launch test on master */
143 rte_atomic32_set(&synchro, 1);
146 rte_eal_mp_wait_lcore();
148 RTE_LCORE_FOREACH(i) {
149 printf("Core [%u] cost time = %"PRIu64" us\n",
151 total += time_count[i];
154 printf("Total cost time = %"PRIu64" us\n", total);
155 memset(time_count, 0, sizeof(time_count));
161 * - There is a global rwlock and a table of rwlocks (one per lcore).
163 * - The test function takes all of these locks and launches the
164 * ``test_rwlock_per_core()`` function on each core (except the master).
166 * - The function takes the global write lock, display something,
167 * then releases the global lock.
168 * - Then, it takes the per-lcore write lock, display something, and
169 * releases the per-core lock.
170 * - Finally, a read lock is taken during 100 ms, then released.
172 * - The main function unlocks the per-lcore locks sequentially and
173 * waits between each lock. This triggers the display of a message
174 * for each core, in the correct order.
176 * Then, it tries to take the global write lock and display the last
177 * message. The autotest script checks that the message order is correct.
184 rte_rwlock_init(&sl);
185 for (i=0; i<RTE_MAX_LCORE; i++)
186 rte_rwlock_init(&sl_tab[i]);
188 rte_rwlock_write_lock(&sl);
190 RTE_LCORE_FOREACH_SLAVE(i) {
191 rte_rwlock_write_lock(&sl_tab[i]);
192 rte_eal_remote_launch(test_rwlock_per_core, NULL, i);
195 rte_rwlock_write_unlock(&sl);
197 RTE_LCORE_FOREACH_SLAVE(i) {
198 rte_rwlock_write_unlock(&sl_tab[i]);
202 rte_rwlock_write_lock(&sl);
203 /* this message should be the last message of test */
204 printf("Global write lock taken on master core %u\n", rte_lcore_id());
205 rte_rwlock_write_unlock(&sl);
207 rte_eal_mp_wait_lcore();
209 if (test_rwlock_perf() < 0)
216 try_read(uint32_t lc)
221 rc = rte_rwlock_read_trylock(&try_rwlock_data.lock);
225 for (i = 0; i != RTE_DIM(try_rwlock_data.data.u64); i++) {
227 /* race condition occurred, lock doesn't work properly */
228 if (try_rwlock_data.data.u64[i] != 0) {
229 printf("%s(%u) error: unexpected data pattern\n",
231 rte_memdump(stdout, NULL,
232 (void *)(uintptr_t)&try_rwlock_data.data,
233 sizeof(try_rwlock_data.data));
239 rte_rwlock_read_unlock(&try_rwlock_data.lock);
244 try_write(uint32_t lc)
249 v = RTE_MAX(lc % UINT8_MAX, 1U);
251 rc = rte_rwlock_write_trylock(&try_rwlock_data.lock);
255 /* update by bytes in reverese order */
256 for (i = RTE_DIM(try_rwlock_data.data.u8); i-- != 0; ) {
258 /* race condition occurred, lock doesn't work properly */
259 if (try_rwlock_data.data.u8[i] != 0) {
260 printf("%s:%d(%u) error: unexpected data pattern\n",
261 __func__, __LINE__, lc);
262 rte_memdump(stdout, NULL,
263 (void *)(uintptr_t)&try_rwlock_data.data,
264 sizeof(try_rwlock_data.data));
269 try_rwlock_data.data.u8[i] = v;
272 /* restore by bytes in reverese order */
273 for (i = RTE_DIM(try_rwlock_data.data.u8); i-- != 0; ) {
275 /* race condition occurred, lock doesn't work properly */
276 if (try_rwlock_data.data.u8[i] != v) {
277 printf("%s:%d(%u) error: unexpected data pattern\n",
278 __func__, __LINE__, lc);
279 rte_memdump(stdout, NULL,
280 (void *)(uintptr_t)&try_rwlock_data.data,
281 sizeof(try_rwlock_data.data));
286 try_rwlock_data.data.u8[i] = 0;
289 rte_rwlock_write_unlock(&try_rwlock_data.lock);
294 try_read_lcore(__rte_unused void *data)
298 uint64_t ftm, stm, tm;
299 struct try_rwlock_lcore *lcd;
302 lcd = try_lcore_data + lc;
303 lcd->type = LC_TYPE_RDLOCK;
305 ftm = try_rwlock_data.tick;
306 stm = rte_get_timer_cycles();
309 for (i = 0; i != ITER_NUM; i++) {
313 else if (rc == -EBUSY)
319 tm = rte_get_timer_cycles() - stm;
320 } while (tm < ftm && rc == 0);
328 try_write_lcore(__rte_unused void *data)
332 uint64_t ftm, stm, tm;
333 struct try_rwlock_lcore *lcd;
336 lcd = try_lcore_data + lc;
337 lcd->type = LC_TYPE_WRLOCK;
339 ftm = try_rwlock_data.tick;
340 stm = rte_get_timer_cycles();
343 for (i = 0; i != ITER_NUM; i++) {
347 else if (rc == -EBUSY)
353 tm = rte_get_timer_cycles() - stm;
354 } while (tm < ftm && rc == 0);
362 print_try_lcore_stats(const struct try_rwlock_lcore *tlc, uint32_t lc)
366 f = RTE_MAX(tlc->stat.fail, 1ULL);
367 s = RTE_MAX(tlc->stat.success, 1ULL);
369 printf("try_lcore_data[%u]={\n"
372 "\tfail=%" PRIu64 ",\n"
373 "\tsuccess=%" PRIu64 ",\n"
374 "\tcycles=%" PRIu64 ",\n"
375 "\tcycles/op=%#Lf,\n"
376 "\tcycles/success=%#Lf,\n"
377 "\tsuccess/fail=%#Lf,\n"
381 tlc->type == LC_TYPE_RDLOCK ? "RDLOCK" : "WRLOCK",
385 (long double)tlc->stat.tick /
386 (tlc->stat.fail + tlc->stat.success),
387 (long double)tlc->stat.tick / s,
388 (long double)tlc->stat.success / f);
392 collect_try_lcore_stats(struct try_rwlock_lcore *tlc,
393 const struct try_rwlock_lcore *lc)
395 tlc->stat.tick += lc->stat.tick;
396 tlc->stat.fail += lc->stat.fail;
397 tlc->stat.success += lc->stat.success;
401 * Process collected results:
403 * - collect and print statistics
406 process_try_lcore_stats(void)
410 struct try_rwlock_lcore rlc, wlc;
412 memset(&rlc, 0, sizeof(rlc));
413 memset(&wlc, 0, sizeof(wlc));
415 rlc.type = LC_TYPE_RDLOCK;
416 wlc.type = LC_TYPE_WRLOCK;
421 RTE_LCORE_FOREACH(lc) {
422 rc |= try_lcore_data[lc].rc;
423 if (try_lcore_data[lc].type == LC_TYPE_RDLOCK) {
424 collect_try_lcore_stats(&rlc, try_lcore_data + lc);
427 collect_try_lcore_stats(&wlc, try_lcore_data + lc);
433 RTE_LCORE_FOREACH(lc)
434 print_try_lcore_stats(try_lcore_data + lc, lc);
437 printf("aggregated stats for %u RDLOCK cores:\n", rd);
438 print_try_lcore_stats(&rlc, rd);
442 printf("aggregated stats for %u WRLOCK cores:\n", wr);
443 print_try_lcore_stats(&wlc, wr);
453 memset(&try_lcore_data, 0, sizeof(try_lcore_data));
454 memset(&try_rwlock_data, 0, sizeof(try_rwlock_data));
455 try_rwlock_data.tick = TEST_SEC * rte_get_tsc_hz();
458 /* all lcores grab RDLOCK */
460 try_rwlock_test_rda(void)
464 /* start read test on all avaialble lcores */
465 rte_eal_mp_remote_launch(try_read_lcore, NULL, CALL_MASTER);
466 rte_eal_mp_wait_lcore();
468 return process_try_lcore_stats();
471 /* all slave lcores grab RDLOCK, master one grabs WRLOCK */
473 try_rwlock_test_rds_wrm(void)
477 rte_eal_mp_remote_launch(try_read_lcore, NULL, SKIP_MASTER);
478 try_write_lcore(NULL);
479 rte_eal_mp_wait_lcore();
481 return process_try_lcore_stats();
484 /* master and even slave lcores grab RDLOCK, odd lcores grab WRLOCK */
486 try_rwlock_test_rde_wro(void)
492 mlc = rte_get_master_lcore();
494 RTE_LCORE_FOREACH(lc) {
497 rte_eal_remote_launch(try_read_lcore,
500 rte_eal_remote_launch(try_write_lcore,
504 try_read_lcore(NULL);
505 rte_eal_mp_wait_lcore();
507 return process_try_lcore_stats();
516 static const struct {
521 .name = "rwlock_test1",
522 .ftst = rwlock_test1,
525 .name = "try_rwlock_test_rda",
526 .ftst = try_rwlock_test_rda,
529 .name = "try_rwlock_test_rds_wrm",
530 .ftst = try_rwlock_test_rds_wrm,
533 .name = "try_rwlock_test_rde_wro",
534 .ftst = try_rwlock_test_rde_wro,
539 for (i = 0; i != RTE_DIM(test); i++) {
540 printf("starting test %s;\n", test[i].name);
542 printf("test %s completed with status %d\n", test[i].name, rc);
549 REGISTER_TEST_COMMAND(rwlock_autotest, test_rwlock);
551 /* subtests used in meson for CI */
552 REGISTER_TEST_COMMAND(rwlock_test1_autotest, rwlock_test1);
553 REGISTER_TEST_COMMAND(rwlock_rda_autotest, try_rwlock_test_rda);
554 REGISTER_TEST_COMMAND(rwlock_rds_wrm_autotest, try_rwlock_test_rds_wrm);
555 REGISTER_TEST_COMMAND(rwlock_rde_wro_autotest, try_rwlock_test_rde_wro);