Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright(c) 2010-2014 Intel Corporation 3 : : * Copyright(c) 2019 Arm Limited 4 : : */ 5 : : 6 : : #include <stdio.h> 7 : : #include <stdint.h> 8 : : #include <unistd.h> 9 : : #include <inttypes.h> 10 : : #include <sys/queue.h> 11 : : 12 : : #include <rte_memory.h> 13 : : #include <rte_common.h> 14 : : #include <rte_per_lcore.h> 15 : : #include <rte_launch.h> 16 : : #include <rte_atomic.h> 17 : : #include <rte_stdatomic.h> 18 : : #include <rte_eal.h> 19 : : #include <rte_lcore.h> 20 : : #include <rte_pause.h> 21 : : 22 : : #include "test.h" 23 : : 24 : : #ifndef RTE_TOOLCHAIN_MSVC 25 : : /* 26 : : * The 128-bit compare and exchange is only implemented for 27 : : * x86_64 and aarch64 with GCC/Clang 28 : : */ 29 : : #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_ARM64) 30 : : /* 31 : : * Atomic Variables 32 : : * ================ 33 : : * 34 : : * - Test "128-bit compare and swap" (aarch64 and x86_64 only) 35 : : * 36 : : * - Initialize 128-bit atomic variables to zero. 37 : : * 38 : : * - Invoke ``test_atomic128_cmp_exchange()`` on each lcore. Before doing 39 : : * anything else, the cores are waiting a synchro. Each lcore does 40 : : * these compare and swap (CAS) operations several times:: 41 : : * 42 : : * Acquired CAS update counter.val[0] + 2; counter.val[1] + 1; 43 : : * Released CAS update counter.val[0] + 2; counter.val[1] + 1; 44 : : * Acquired_Released CAS update counter.val[0] + 2; counter.val[1] + 1; 45 : : * Relaxed CAS update counter.val[0] + 2; counter.val[1] + 1; 46 : : * 47 : : * - At the end of the test, the *count128* first 64-bit value and 48 : : * second 64-bit value differ by the total iterations. 49 : : */ 50 : : 51 : : #define N_BASE 1000000u 52 : : #define N_MIN 10000u 53 : : 54 : : /* 55 : : * Number of iterations for each test, scaled inversely with core count. 56 : : * More cores means more contention which increases time per operation. 57 : : * Calculated once at test start to avoid repeated computation in workers. 58 : : */ 59 : : static unsigned int num_iterations; 60 : : 61 : : static RTE_ATOMIC(uint32_t) synchro; 62 : : 63 : : static rte_int128_t count128; 64 : : 65 : : /* 66 : : * rte_atomic128_cmp_exchange() should update a 128 bits counter's first 64 67 : : * bits by 2 and the second 64 bits by 1 in this test. It should return true 68 : : * if the compare exchange operation is successful. 69 : : * This test repeats 128 bits compare and swap operations N rounds. In each 70 : : * iteration it runs compare and swap operation with different memory models. 71 : : */ 72 : : static int 73 : 1 : test_atomic128_cmp_exchange(__rte_unused void *arg) 74 : : { 75 : : rte_int128_t expected; 76 : : int success; 77 : : unsigned int i; 78 : : 79 [ + + ]: 36937 : while (rte_atomic_load_explicit(&synchro, rte_memory_order_relaxed) == 0) 80 : : rte_pause(); 81 : : 82 : 1 : expected = count128; 83 : : 84 [ + + ]: 500001 : for (i = 0; i < num_iterations; i++) { 85 : : do { 86 : : rte_int128_t desired; 87 : : 88 : 999999 : desired.val[0] = expected.val[0] + 2; 89 : 999999 : desired.val[1] = expected.val[1] + 1; 90 : : 91 : : success = rte_atomic128_cmp_exchange(&count128, 92 : : &expected, &desired, 1, 93 : : rte_memory_order_acquire, rte_memory_order_relaxed); 94 [ + + ]: 999999 : } while (success == 0); 95 : : 96 : : do { 97 : : rte_int128_t desired; 98 : : 99 : 1000000 : desired.val[0] = expected.val[0] + 2; 100 : 1000000 : desired.val[1] = expected.val[1] + 1; 101 : : 102 : : success = rte_atomic128_cmp_exchange(&count128, 103 : : &expected, &desired, 1, 104 : : rte_memory_order_release, rte_memory_order_relaxed); 105 [ + + ]: 1000000 : } while (success == 0); 106 : : 107 : : do { 108 : : rte_int128_t desired; 109 : : 110 : 1000000 : desired.val[0] = expected.val[0] + 2; 111 : 1000000 : desired.val[1] = expected.val[1] + 1; 112 : : 113 : : success = rte_atomic128_cmp_exchange(&count128, 114 : : &expected, &desired, 1, 115 : : rte_memory_order_acq_rel, rte_memory_order_relaxed); 116 [ + + ]: 1000000 : } while (success == 0); 117 : : 118 : : do { 119 : : rte_int128_t desired; 120 : : 121 : 1000000 : desired.val[0] = expected.val[0] + 2; 122 : 1000000 : desired.val[1] = expected.val[1] + 1; 123 : : 124 : : success = rte_atomic128_cmp_exchange(&count128, 125 : : &expected, &desired, 1, 126 : : rte_memory_order_relaxed, rte_memory_order_relaxed); 127 [ + + ]: 1000000 : } while (success == 0); 128 : : } 129 : : 130 : 1 : return 0; 131 : : } 132 : : 133 : : static int 134 : 1 : test_atomic(void) 135 : : { 136 : : uint64_t iterations; 137 : : 138 : 1 : num_iterations = test_scale_iterations(N_BASE, N_MIN); 139 : : 140 : : /* 141 : : * This case tests the functionality of rte_atomic128_cmp_exchange 142 : : * API. It calls rte_atomic128_cmp_exchange with four kinds of memory 143 : : * models successively on each worker core. Once each 128-bit atomic 144 : : * compare and swap operation is successful, it updates the global 145 : : * 128-bit counter by 2 for the first 64-bit and 1 for the second 146 : : * 64-bit. Each worker core iterates this test N times. 147 : : * At the end of test, verify whether the first 64-bits of the 128-bit 148 : : * counter and the second 64bits is differ by the total iterations. If 149 : : * it is, the test passes. 150 : : */ 151 : : printf("128-bit compare and swap test\n"); 152 : : 153 : 1 : rte_atomic_store_explicit(&synchro, 0, rte_memory_order_relaxed); 154 : 1 : count128.val[0] = 0; 155 : 1 : count128.val[1] = 0; 156 : : 157 : 1 : rte_eal_mp_remote_launch(test_atomic128_cmp_exchange, NULL, SKIP_MAIN); 158 : 1 : rte_atomic_store_explicit(&synchro, 1, rte_memory_order_relaxed); 159 : 1 : rte_eal_mp_wait_lcore(); 160 : 1 : rte_atomic_store_explicit(&synchro, 0, rte_memory_order_relaxed); 161 : : 162 : 1 : iterations = count128.val[0] - count128.val[1]; 163 [ - + ]: 1 : if (iterations != (uint64_t)4 * num_iterations * (rte_lcore_count() - 1)) { 164 : : printf("128-bit compare and swap failed\n"); 165 : 0 : return -1; 166 : : } 167 : : 168 : : return 0; 169 : : } 170 : : 171 : : #else 172 : : 173 : : static int 174 : : test_atomic(void) 175 : : { 176 : : printf("128 bit exchange not implemented\n"); 177 : : return TEST_SKIPPED; 178 : : } 179 : : 180 : : #endif 181 : : 182 : 305 : REGISTER_FAST_TEST(atomic_autotest, NOHUGE_SKIP, ASAN_OK, test_atomic); 183 : : #endif /* RTE_TOOLCHAIN_MSVC */