* [dpdk-dev] [PATCH v4] app/test: add tests for atomic exchanges
[not found] <20191018182112.115448-1-drc@ibm.com>
@ 2019-10-18 20:30 ` David Christensen
2019-11-06 14:18 ` David Marchand
2019-11-06 19:57 ` [dpdk-dev] [PATCH v5] " David Christensen
0 siblings, 2 replies; 6+ messages in thread
From: David Christensen @ 2019-10-18 20:30 UTC (permalink / raw)
To: dev; +Cc: David Christensen
The test works by creating a token comprised of random data
and a CRC8 value, using the rte_atomicXX_exchange to exchange
the new token for a previously generated token, and then
verifying that the exchanged data is intact (i.e. the CRC8
is still correct for the data).
Signed-off-by: David Christensen <drc@linux.vnet.ibm.com>
---
v4:
* Fix build error due to use of variable initialization
in "for" statement.
v3:
* Actually fixed build issue on all platforms caused by
misspelling of rte_atomic64_inc
v2:
* Fixed build issue on all platforms caused by misspelling
of rte_atomic64_inc
app/test/test_atomic.c | 176 ++++++++++++++++++++++++++++++++++++++++-
1 file changed, 174 insertions(+), 2 deletions(-)
diff --git a/app/test/test_atomic.c b/app/test/test_atomic.c
index 43be30ec0..858c6d7f9 100644
--- a/app/test/test_atomic.c
+++ b/app/test/test_atomic.c
@@ -1,10 +1,12 @@
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2019 Arm Limited
*/
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
+#include <inttypes.h>
#include <sys/queue.h>
#include <rte_memory.h>
@@ -13,6 +15,8 @@
#include <rte_atomic.h>
#include <rte_eal.h>
#include <rte_lcore.h>
+#include <rte_random.h>
+#include <rte_hash_crc.h>
#include "test.h"
@@ -20,7 +24,7 @@
* Atomic Variables
* ================
*
- * - The main test function performs three subtests. The first test
+ * - The main test function performs four subtests. The first test
* checks that the usual inc/dec/add/sub functions are working
* correctly:
*
@@ -61,11 +65,26 @@
* atomic_sub(&count, tmp+1);
*
* - At the end of the test, the *count* value must be 0.
+ *
+ * - Test "atomic exchange"
+ *
+ * - Create a 64 bit token that can be tested for data integrity
+ *
+ * - Invoke ``test_atomic_exchange`` on each lcore. Before doing
+ * anything else, the cores wait for a synchronization event.
+ * Each core then does the follwoing for N iterations:
+ *
+ * Generate a new token with a data integrity check
+ * Exchange the new token for previously generated token
+ * Increment a counter if a corrupt token was received
+ *
+ * - At the end of the test, the number of corrupted tokens must be 0.
+ *
*/
#define NUM_ATOMIC_TYPES 3
-#define N 10000
+#define N 1000000
static rte_atomic16_t a16;
static rte_atomic32_t a32;
@@ -216,6 +235,127 @@ test_atomic_dec_and_test(__attribute__((unused)) void *arg)
return 0;
}
+/*
+ * Helper definitions/variables/functions for
+ * atomic exchange tests
+ */
+typedef union {
+ uint16_t u16;
+ uint8_t u8[2];
+} rte_u16_t;
+
+typedef union {
+ uint32_t u32;
+ uint16_t u16[2];
+ uint8_t u8[4];
+} rte_u32_t;
+
+typedef union {
+ uint64_t u64;
+ uint32_t u32[2];
+ uint16_t u16[4];
+ uint8_t u8[8];
+} rte_u64_t;
+
+const uint8_t CRC8_POLY = 0x91;
+uint8_t crc8_table[256];
+
+volatile uint16_t token16;
+volatile uint32_t token32;
+volatile uint64_t token64;
+
+static void
+build_crc8_table(void)
+{
+ uint8_t val;
+ int i, j;
+
+ for (i = 0; i < 256; i++) {
+ val = i;
+ for (j = 0; j < 8; j++) {
+ if (val & 1)
+ val ^= CRC8_POLY;
+ val >>= 1;
+ }
+ crc8_table[i] = val;
+ }
+}
+
+static uint8_t
+get_crc8(uint8_t *message, int length)
+{
+ uint8_t crc = 0;
+ int i;
+
+ for (i = 0; i < length; i++)
+ crc = crc8_table[crc ^ message[i]];
+ return crc;
+}
+
+/*
+ * The atomic exchange test sets up a token in memory and
+ * then spins up multiple lcores whose job is to generate
+ * new tokens, exchange that new token for the old one held
+ * in memory, and then verify that the old token is still
+ * valid (i.e. the exchange did not corrupt the token).
+ *
+ * A token is made up of random data and 8 bits of crc
+ * covering that random data. The following is an example
+ * of a 64bit token.
+ *
+ * +------------+------------+
+ * | 63 56 | 55 0 |
+ * +------------+------------+
+ * | CRC8 | Data |
+ * +------------+------------+
+ */
+static int
+test_atomic_exchange(__attribute__((unused)) void *arg)
+{
+ int i;
+ rte_u16_t nt16, ot16; /* new token, old token */
+ rte_u32_t nt32, ot32;
+ rte_u64_t nt64, ot64;
+
+ /* Wait until all of the other threads have been dispatched */
+ while (rte_atomic32_read(&synchro) == 0)
+ ;
+
+ /*
+ * Let the battle begin! Every thread attempts to steal the current
+ * token with an atomic exchange operation and install its own newly
+ * generated token. If the old token is valid (i.e. it has the
+ * appropriate crc32 hash for the data) then the test iteration has
+ * passed. If the token is invalid, increment the counter.
+ */
+ for (i = 0; i < N; i++) {
+
+ /* Test 64bit Atomic Exchange */
+ nt64.u64 = rte_rand();
+ nt64.u8[7] = get_crc8(&nt64.u8[0], sizeof(nt64) - 1);
+ ot64.u64 = rte_atomic64_exchange(&token64, nt64.u64);
+ if (ot64.u8[7] != get_crc8(&ot64.u8[0], sizeof(ot64) - 1))
+ rte_atomic64_inc(&count);
+
+ /* Test 32bit Atomic Exchange */
+ nt32.u32 = (uint32_t)rte_rand();
+ nt32.u8[3] = get_crc8(&nt32.u8[0], sizeof(nt32) - 1);
+ ot32.u32 = rte_atomic32_exchange(&token32, nt32.u32);
+ if (ot32.u8[3] != get_crc8(&ot32.u8[0], sizeof(ot32) - 1))
+ rte_atomic64_inc(&count);
+
+ /* Test 16bit Atomic Exchange */
+ nt16.u16 = (uint16_t)rte_rand();
+ nt16.u8[1] = get_crc8(&nt16.u8[0], sizeof(nt16) - 1);
+ ot16.u16 = rte_atomic16_exchange(&token16, nt16.u16);
+ if (ot16.u8[1] != get_crc8(&ot16.u8[0], sizeof(ot16) - 1))
+ rte_atomic64_inc(&count);
+ }
+
+ return 0;
+}
+
+
static int
test_atomic(void)
{
@@ -340,6 +480,38 @@ test_atomic(void)
return -1;
}
+ /*
+ * Test 16/32/64bit atomic exchange.
+ */
+ rte_u64_t t;
+
+ printf("exchange test\n");
+
+ rte_atomic32_clear(&synchro);
+ rte_atomic64_clear(&count);
+
+ /* Generate the CRC8 lookup table */
+ build_crc8_table();
+
+ /* Create the initial tokens used by the test */
+ t.u64 = rte_rand();
+ token16 = (get_crc8(&t.u8[0], sizeof(token16) - 1) << 8)
+ | (t.u16[0] & 0x00ff);
+ token32 = ((uint32_t)get_crc8(&t.u8[0], sizeof(token32) - 1) << 24)
+ | (t.u32[0] & 0x00ffffff);
+ token64 = ((uint64_t)get_crc8(&t.u8[0], sizeof(token64) - 1) << 56)
+ | (t.u64 & 0x00ffffffffffffff);
+
+ rte_eal_mp_remote_launch(test_atomic_exchange, NULL, SKIP_MASTER);
+ rte_atomic32_set(&synchro, 1);
+ rte_eal_mp_wait_lcore();
+ rte_atomic32_clear(&synchro);
+
+ if (rte_atomic64_read(&count) > 0) {
+ printf("Atomic exchange test failed\n");
+ return -1;
+ }
+
return 0;
}
--
2.18.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [dpdk-dev] [PATCH v4] app/test: add tests for atomic exchanges
2019-10-18 20:30 ` [dpdk-dev] [PATCH v4] app/test: add tests for atomic exchanges David Christensen
@ 2019-11-06 14:18 ` David Marchand
2019-11-06 19:57 ` [dpdk-dev] [PATCH v5] " David Christensen
1 sibling, 0 replies; 6+ messages in thread
From: David Marchand @ 2019-11-06 14:18 UTC (permalink / raw)
To: David Christensen; +Cc: dev
On Fri, Oct 18, 2019 at 10:31 PM David Christensen
<drc@linux.vnet.ibm.com> wrote:
>
> The test works by creating a token comprised of random data
> and a CRC8 value, using the rte_atomicXX_exchange to exchange
> the new token for a previously generated token, and then
> verifying that the exchanged data is intact (i.e. the CRC8
> is still correct for the data).
Thanks, can you rebase this on master?
>
> Signed-off-by: David Christensen <drc@linux.vnet.ibm.com>
> ---
> v4:
> * Fix build error due to use of variable initialization
> in "for" statement.
>
> v3:
> * Actually fixed build issue on all platforms caused by
> misspelling of rte_atomic64_inc
>
> v2:
> * Fixed build issue on all platforms caused by misspelling
> of rte_atomic64_inc
>
> app/test/test_atomic.c | 176 ++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 174 insertions(+), 2 deletions(-)
>
> diff --git a/app/test/test_atomic.c b/app/test/test_atomic.c
> index 43be30ec0..858c6d7f9 100644
> --- a/app/test/test_atomic.c
> +++ b/app/test/test_atomic.c
> @@ -1,10 +1,12 @@
> /* SPDX-License-Identifier: BSD-3-Clause
> * Copyright(c) 2010-2014 Intel Corporation
> + * Copyright(c) 2019 Arm Limited
> */
>
> #include <stdio.h>
> #include <stdint.h>
> #include <unistd.h>
> +#include <inttypes.h>
> #include <sys/queue.h>
>
> #include <rte_memory.h>
> @@ -13,6 +15,8 @@
> #include <rte_atomic.h>
> #include <rte_eal.h>
> #include <rte_lcore.h>
> +#include <rte_random.h>
> +#include <rte_hash_crc.h>
>
> #include "test.h"
>
> @@ -20,7 +24,7 @@
> * Atomic Variables
> * ================
> *
> - * - The main test function performs three subtests. The first test
> + * - The main test function performs four subtests. The first test
Let's drop this sentence.
No point in maintaining the number of subtests.
> * checks that the usual inc/dec/add/sub functions are working
> * correctly:
> *
> @@ -61,11 +65,26 @@
> * atomic_sub(&count, tmp+1);
> *
> * - At the end of the test, the *count* value must be 0.
> + *
> + * - Test "atomic exchange"
> + *
> + * - Create a 64 bit token that can be tested for data integrity
> + *
> + * - Invoke ``test_atomic_exchange`` on each lcore. Before doing
> + * anything else, the cores wait for a synchronization event.
> + * Each core then does the follwoing for N iterations:
> + *
> + * Generate a new token with a data integrity check
> + * Exchange the new token for previously generated token
> + * Increment a counter if a corrupt token was received
> + *
> + * - At the end of the test, the number of corrupted tokens must be 0.
> + *
> */
>
> #define NUM_ATOMIC_TYPES 3
>
> -#define N 10000
> +#define N 1000000
>
> static rte_atomic16_t a16;
> static rte_atomic32_t a32;
> @@ -216,6 +235,127 @@ test_atomic_dec_and_test(__attribute__((unused)) void *arg)
> return 0;
> }
>
> +/*
> + * Helper definitions/variables/functions for
> + * atomic exchange tests
> + */
> +typedef union {
> + uint16_t u16;
> + uint8_t u8[2];
> +} rte_u16_t;
> +
> +typedef union {
> + uint32_t u32;
> + uint16_t u16[2];
> + uint8_t u8[4];
> +} rte_u32_t;
> +
> +typedef union {
> + uint64_t u64;
> + uint32_t u32[2];
> + uint16_t u16[4];
> + uint8_t u8[8];
> +} rte_u64_t;
Please, don't use such names for internal types.
It gives the impression those are EAL types.. but I don't feel like we
need them, so let's just avoid adding them in EAL, now.
--
David Marchand
^ permalink raw reply [flat|nested] 6+ messages in thread
* [dpdk-dev] [PATCH v5] app/test: add tests for atomic exchanges
2019-10-18 20:30 ` [dpdk-dev] [PATCH v4] app/test: add tests for atomic exchanges David Christensen
2019-11-06 14:18 ` David Marchand
@ 2019-11-06 19:57 ` David Christensen
2019-11-06 20:21 ` David Marchand
2019-11-06 21:18 ` [dpdk-dev] [PATCH v6] " David Christensen
1 sibling, 2 replies; 6+ messages in thread
From: David Christensen @ 2019-11-06 19:57 UTC (permalink / raw)
To: dev; +Cc: David Christensen
The test works by creating a token comprised of random data
and a CRC8 value, using the rte_atomicXX_exchange to exchange
the new token for a previously generated token, and then
verifying that the exchanged data is intact (i.e. the CRC8
is still correct for the data).
Signed-off-by: David Christensen <drc@linux.vnet.ibm.com>
---
v5:
* Rebase to master
* Removed comment reference to the number of sub-tests
* Rename rte_u16/32/64_t typedefs to test16/32/64_t
v4:
* Fix build error due to use of variable initialization
in "for" statement.
v3:
* Actually fixed build issue on all platforms caused by
misspelling of rte_atomic64_inc
v2:
* Fixed build issue on all platforms caused by misspelling
of rte_atomic64_inc
---
app/test/test_atomic.c | 178 ++++++++++++++++++++++++++++++++++++++++-
1 file changed, 174 insertions(+), 4 deletions(-)
diff --git a/app/test/test_atomic.c b/app/test/test_atomic.c
index 2df950694..de372a13f 100644
--- a/app/test/test_atomic.c
+++ b/app/test/test_atomic.c
@@ -6,6 +6,7 @@
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
+#include <inttypes.h>
#include <sys/queue.h>
#include <rte_memory.h>
@@ -14,6 +15,8 @@
#include <rte_atomic.h>
#include <rte_eal.h>
#include <rte_lcore.h>
+#include <rte_random.h>
+#include <rte_hash_crc.h>
#include "test.h"
@@ -21,7 +24,7 @@
* Atomic Variables
* ================
*
- * - The main test function performs four subtests. The first test
+ * - The main test function performs several subtests. The first
* checks that the usual inc/dec/add/sub functions are working
* correctly:
*
@@ -34,7 +37,7 @@
* - The function checks that once all lcores finish their function,
* the value of the atomic variables are still the same.
*
- * - The second test verifies the behavior of "test and set" functions.
+ * - Test "test and set" functions.
*
* - Initialize 16-bit, 32-bit and 64-bit atomic variables to zero.
*
@@ -48,7 +51,7 @@
* - The main function checks that the atomic counter was incremented
* twice only (one for 16-bit, one for 32-bit and one for 64-bit values).
*
- * - Test "add/sub and return"
+ * - Test "add/sub and return" functions
*
* - Initialize 16-bit, 32-bit and 64-bit atomic variables to zero.
*
@@ -78,6 +81,20 @@
*
* - At the end of the test, the *count128* first 64-bit value and
* second 64-bit value differ by the total iterations.
+ *
+ * - Test "atomic exchange" functions
+ *
+ * - Create a 64 bit token that can be tested for data integrity
+ *
+ * - Invoke ``test_atomic_exchange`` on each lcore. Before doing
+ * anything else, the cores wait for a synchronization event.
+ * Each core then does the follwoing for N iterations:
+ *
+ * Generate a new token with a data integrity check
+ * Exchange the new token for previously generated token
+ * Increment a counter if a corrupt token was received
+ *
+ * - At the end of the test, the number of corrupted tokens must be 0.
*/
#define NUM_ATOMIC_TYPES 3
@@ -305,6 +322,125 @@ test_atomic128_cmp_exchange(__attribute__((unused)) void *arg)
}
#endif
+/*
+ * Helper definitions/variables/functions for
+ * atomic exchange tests
+ */
+typedef union {
+ uint16_t u16;
+ uint8_t u8[2];
+} test16_t;
+
+typedef union {
+ uint32_t u32;
+ uint16_t u16[2];
+ uint8_t u8[4];
+} test32_t;
+
+typedef union {
+ uint64_t u64;
+ uint32_t u32[2];
+ uint16_t u16[4];
+ uint8_t u8[8];
+} test64_t;
+
+const uint8_t CRC8_POLY = 0x91;
+uint8_t crc8_table[256];
+
+volatile uint16_t token16;
+volatile uint32_t token32;
+volatile uint64_t token64;
+
+static void
+build_crc8_table(void)
+{
+ uint8_t val;
+ int i, j;
+
+ for (i = 0; i < 256; i++) {
+ val = i;
+ for (j = 0; j < 8; j++) {
+ if (val & 1)
+ val ^= CRC8_POLY;
+ val >>= 1;
+ }
+ crc8_table[i] = val;
+ }
+}
+
+static uint8_t
+get_crc8(uint8_t *message, int length)
+{
+ uint8_t crc = 0;
+ int i;
+
+ for (i = 0; i < length; i++)
+ crc = crc8_table[crc ^ message[i]];
+ return crc;
+}
+
+/*
+ * The atomic exchange test sets up a token in memory and
+ * then spins up multiple lcores whose job is to generate
+ * new tokens, exchange that new token for the old one held
+ * in memory, and then verify that the old token is still
+ * valid (i.e. the exchange did not corrupt the token).
+ *
+ * A token is made up of random data and 8 bits of crc
+ * covering that random data. The following is an example
+ * of a 64bit token.
+ *
+ * +------------+------------+
+ * | 63 56 | 55 0 |
+ * +------------+------------+
+ * | CRC8 | Data |
+ * +------------+------------+
+ */
+static int
+test_atomic_exchange(__attribute__((unused)) void *arg)
+{
+ int i;
+ test16_t nt16, ot16; /* new token, old token */
+ test32_t nt32, ot32;
+ test64_t nt64, ot64;
+
+ /* Wait until all of the other threads have been dispatched */
+ while (rte_atomic32_read(&synchro) == 0)
+ ;
+
+ /*
+ * Let the battle begin! Every thread attempts to steal the current
+ * token with an atomic exchange operation and install its own newly
+ * generated token. If the old token is valid (i.e. it has the
+ * appropriate crc32 hash for the data) then the test iteration has
+ * passed. If the token is invalid, increment the counter.
+ */
+ for (i = 0; i < N; i++) {
+
+ /* Test 64bit Atomic Exchange */
+ nt64.u64 = rte_rand();
+ nt64.u8[7] = get_crc8(&nt64.u8[0], sizeof(nt64) - 1);
+ ot64.u64 = rte_atomic64_exchange(&token64, nt64.u64);
+ if (ot64.u8[7] != get_crc8(&ot64.u8[0], sizeof(ot64) - 1))
+ rte_atomic64_inc(&count);
+
+ /* Test 32bit Atomic Exchange */
+ nt32.u32 = (uint32_t)rte_rand();
+ nt32.u8[3] = get_crc8(&nt32.u8[0], sizeof(nt32) - 1);
+ ot32.u32 = rte_atomic32_exchange(&token32, nt32.u32);
+ if (ot32.u8[3] != get_crc8(&ot32.u8[0], sizeof(ot32) - 1))
+ rte_atomic64_inc(&count);
+
+ /* Test 16bit Atomic Exchange */
+ nt16.u16 = (uint16_t)rte_rand();
+ nt16.u8[1] = get_crc8(&nt16.u8[0], sizeof(nt16) - 1);
+ ot16.u16 = rte_atomic16_exchange(&token16, nt16.u16);
+ if (ot16.u8[1] != get_crc8(&ot16.u8[0], sizeof(ot16) - 1))
+ rte_atomic64_inc(&count);
+ }
+
+ return 0;
+}
static int
test_atomic(void)
{
@@ -459,9 +595,43 @@ test_atomic(void)
printf("128-bit compare and swap failed\n");
return -1;
}
-#endif
return 0;
}
+#endif
+
+ /*
+ * Test 16/32/64bit atomic exchange.
+ */
+ test64_t t;
+
+ printf("exchange test\n");
+
+ rte_atomic32_clear(&synchro);
+ rte_atomic64_clear(&count);
+
+ /* Generate the CRC8 lookup table */
+ build_crc8_table();
+
+ /* Create the initial tokens used by the test */
+ t.u64 = rte_rand();
+ token16 = (get_crc8(&t.u8[0], sizeof(token16) - 1) << 8)
+ | (t.u16[0] & 0x00ff);
+ token32 = ((uint32_t)get_crc8(&t.u8[0], sizeof(token32) - 1) << 24)
+ | (t.u32[0] & 0x00ffffff);
+ token64 = ((uint64_t)get_crc8(&t.u8[0], sizeof(token64) - 1) << 56)
+ | (t.u64 & 0x00ffffffffffffff);
+ rte_eal_mp_remote_launch(test_atomic_exchange, NULL, SKIP_MASTER);
+ rte_atomic32_set(&synchro, 1);
+ rte_eal_mp_wait_lcore();
+ rte_atomic32_clear(&synchro);
+
+ if (rte_atomic64_read(&count) > 0) {
+ printf("Atomic exchange test failed\n");
+ return -1;
+ }
+
+ return 0;
+}
REGISTER_TEST_COMMAND(atomic_autotest, test_atomic);
--
2.18.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [dpdk-dev] [PATCH v5] app/test: add tests for atomic exchanges
2019-11-06 19:57 ` [dpdk-dev] [PATCH v5] " David Christensen
@ 2019-11-06 20:21 ` David Marchand
2019-11-06 21:18 ` [dpdk-dev] [PATCH v6] " David Christensen
1 sibling, 0 replies; 6+ messages in thread
From: David Marchand @ 2019-11-06 20:21 UTC (permalink / raw)
To: David Christensen; +Cc: dev
On Wed, Nov 6, 2019 at 8:57 PM David Christensen <drc@linux.vnet.ibm.com> wrote:
> @@ -459,9 +595,43 @@ test_atomic(void)
> printf("128-bit compare and swap failed\n");
> return -1;
> }
> -#endif
>
> return 0;
> }
> +#endif
> +
> + /*
> + * Test 16/32/64bit atomic exchange.
> + */
> + test64_t t;
We will have a build error here for arm and x86.
--
David Marchand
^ permalink raw reply [flat|nested] 6+ messages in thread
* [dpdk-dev] [PATCH v6] app/test: add tests for atomic exchanges
2019-11-06 19:57 ` [dpdk-dev] [PATCH v5] " David Christensen
2019-11-06 20:21 ` David Marchand
@ 2019-11-06 21:18 ` David Christensen
2019-11-07 16:42 ` David Marchand
1 sibling, 1 reply; 6+ messages in thread
From: David Christensen @ 2019-11-06 21:18 UTC (permalink / raw)
To: dev; +Cc: David Christensen
The test works by creating a token comprised of random data
and a CRC8 value, using the rte_atomicXX_exchange to exchange
the new token for a previously generated token, and then
verifying that the exchanged data is intact (i.e. the CRC8
is still correct for the data).
Signed-off-by: David Christensen <drc@linux.vnet.ibm.com>
---
v6:
* Fixed build error on x86/ARM systems
v5:
* Rebase to master
* Removed comment reference to the number of sub-tests
* Rename rte_u16/32/64_t typedefs to test16/32/64_t
v4:
* Fix build error due to use of variable initialization
in "for" statement.
v3:
* Actually fixed build issue on all platforms caused by
misspelling of rte_atomic64_inc
v2:
* Fixed build issue on all platforms caused by misspelling
of rte_atomic64_inc
---
app/test/test_atomic.c | 175 ++++++++++++++++++++++++++++++++++++++++-
1 file changed, 171 insertions(+), 4 deletions(-)
diff --git a/app/test/test_atomic.c b/app/test/test_atomic.c
index 2df950694..de3030d22 100644
--- a/app/test/test_atomic.c
+++ b/app/test/test_atomic.c
@@ -6,6 +6,7 @@
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
+#include <inttypes.h>
#include <sys/queue.h>
#include <rte_memory.h>
@@ -14,6 +15,8 @@
#include <rte_atomic.h>
#include <rte_eal.h>
#include <rte_lcore.h>
+#include <rte_random.h>
+#include <rte_hash_crc.h>
#include "test.h"
@@ -21,7 +24,7 @@
* Atomic Variables
* ================
*
- * - The main test function performs four subtests. The first test
+ * - The main test function performs several subtests. The first
* checks that the usual inc/dec/add/sub functions are working
* correctly:
*
@@ -34,7 +37,7 @@
* - The function checks that once all lcores finish their function,
* the value of the atomic variables are still the same.
*
- * - The second test verifies the behavior of "test and set" functions.
+ * - Test "test and set" functions.
*
* - Initialize 16-bit, 32-bit and 64-bit atomic variables to zero.
*
@@ -48,7 +51,7 @@
* - The main function checks that the atomic counter was incremented
* twice only (one for 16-bit, one for 32-bit and one for 64-bit values).
*
- * - Test "add/sub and return"
+ * - Test "add/sub and return" functions
*
* - Initialize 16-bit, 32-bit and 64-bit atomic variables to zero.
*
@@ -78,6 +81,20 @@
*
* - At the end of the test, the *count128* first 64-bit value and
* second 64-bit value differ by the total iterations.
+ *
+ * - Test "atomic exchange" functions
+ *
+ * - Create a 64 bit token that can be tested for data integrity
+ *
+ * - Invoke ``test_atomic_exchange`` on each lcore. Before doing
+ * anything else, the cores wait for a synchronization event.
+ * Each core then does the follwoing for N iterations:
+ *
+ * Generate a new token with a data integrity check
+ * Exchange the new token for previously generated token
+ * Increment a counter if a corrupt token was received
+ *
+ * - At the end of the test, the number of corrupted tokens must be 0.
*/
#define NUM_ATOMIC_TYPES 3
@@ -305,6 +322,125 @@ test_atomic128_cmp_exchange(__attribute__((unused)) void *arg)
}
#endif
+/*
+ * Helper definitions/variables/functions for
+ * atomic exchange tests
+ */
+typedef union {
+ uint16_t u16;
+ uint8_t u8[2];
+} test16_t;
+
+typedef union {
+ uint32_t u32;
+ uint16_t u16[2];
+ uint8_t u8[4];
+} test32_t;
+
+typedef union {
+ uint64_t u64;
+ uint32_t u32[2];
+ uint16_t u16[4];
+ uint8_t u8[8];
+} test64_t;
+
+const uint8_t CRC8_POLY = 0x91;
+uint8_t crc8_table[256];
+
+volatile uint16_t token16;
+volatile uint32_t token32;
+volatile uint64_t token64;
+
+static void
+build_crc8_table(void)
+{
+ uint8_t val;
+ int i, j;
+
+ for (i = 0; i < 256; i++) {
+ val = i;
+ for (j = 0; j < 8; j++) {
+ if (val & 1)
+ val ^= CRC8_POLY;
+ val >>= 1;
+ }
+ crc8_table[i] = val;
+ }
+}
+
+static uint8_t
+get_crc8(uint8_t *message, int length)
+{
+ uint8_t crc = 0;
+ int i;
+
+ for (i = 0; i < length; i++)
+ crc = crc8_table[crc ^ message[i]];
+ return crc;
+}
+
+/*
+ * The atomic exchange test sets up a token in memory and
+ * then spins up multiple lcores whose job is to generate
+ * new tokens, exchange that new token for the old one held
+ * in memory, and then verify that the old token is still
+ * valid (i.e. the exchange did not corrupt the token).
+ *
+ * A token is made up of random data and 8 bits of crc
+ * covering that random data. The following is an example
+ * of a 64bit token.
+ *
+ * +------------+------------+
+ * | 63 56 | 55 0 |
+ * +------------+------------+
+ * | CRC8 | Data |
+ * +------------+------------+
+ */
+static int
+test_atomic_exchange(__attribute__((unused)) void *arg)
+{
+ int i;
+ test16_t nt16, ot16; /* new token, old token */
+ test32_t nt32, ot32;
+ test64_t nt64, ot64;
+
+ /* Wait until all of the other threads have been dispatched */
+ while (rte_atomic32_read(&synchro) == 0)
+ ;
+
+ /*
+ * Let the battle begin! Every thread attempts to steal the current
+ * token with an atomic exchange operation and install its own newly
+ * generated token. If the old token is valid (i.e. it has the
+ * appropriate crc32 hash for the data) then the test iteration has
+ * passed. If the token is invalid, increment the counter.
+ */
+ for (i = 0; i < N; i++) {
+
+ /* Test 64bit Atomic Exchange */
+ nt64.u64 = rte_rand();
+ nt64.u8[7] = get_crc8(&nt64.u8[0], sizeof(nt64) - 1);
+ ot64.u64 = rte_atomic64_exchange(&token64, nt64.u64);
+ if (ot64.u8[7] != get_crc8(&ot64.u8[0], sizeof(ot64) - 1))
+ rte_atomic64_inc(&count);
+
+ /* Test 32bit Atomic Exchange */
+ nt32.u32 = (uint32_t)rte_rand();
+ nt32.u8[3] = get_crc8(&nt32.u8[0], sizeof(nt32) - 1);
+ ot32.u32 = rte_atomic32_exchange(&token32, nt32.u32);
+ if (ot32.u8[3] != get_crc8(&ot32.u8[0], sizeof(ot32) - 1))
+ rte_atomic64_inc(&count);
+
+ /* Test 16bit Atomic Exchange */
+ nt16.u16 = (uint16_t)rte_rand();
+ nt16.u8[1] = get_crc8(&nt16.u8[0], sizeof(nt16) - 1);
+ ot16.u16 = rte_atomic16_exchange(&token16, nt16.u16);
+ if (ot16.u8[1] != get_crc8(&ot16.u8[0], sizeof(ot16) - 1))
+ rte_atomic64_inc(&count);
+ }
+
+ return 0;
+}
static int
test_atomic(void)
{
@@ -461,7 +597,38 @@ test_atomic(void)
}
#endif
+ /*
+ * Test 16/32/64bit atomic exchange.
+ */
+ test64_t t;
+
+ printf("exchange test\n");
+
+ rte_atomic32_clear(&synchro);
+ rte_atomic64_clear(&count);
+
+ /* Generate the CRC8 lookup table */
+ build_crc8_table();
+
+ /* Create the initial tokens used by the test */
+ t.u64 = rte_rand();
+ token16 = (get_crc8(&t.u8[0], sizeof(token16) - 1) << 8)
+ | (t.u16[0] & 0x00ff);
+ token32 = ((uint32_t)get_crc8(&t.u8[0], sizeof(token32) - 1) << 24)
+ | (t.u32[0] & 0x00ffffff);
+ token64 = ((uint64_t)get_crc8(&t.u8[0], sizeof(token64) - 1) << 56)
+ | (t.u64 & 0x00ffffffffffffff);
+
+ rte_eal_mp_remote_launch(test_atomic_exchange, NULL, SKIP_MASTER);
+ rte_atomic32_set(&synchro, 1);
+ rte_eal_mp_wait_lcore();
+ rte_atomic32_clear(&synchro);
+
+ if (rte_atomic64_read(&count) > 0) {
+ printf("Atomic exchange test failed\n");
+ return -1;
+ }
+
return 0;
}
-
REGISTER_TEST_COMMAND(atomic_autotest, test_atomic);
--
2.18.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [dpdk-dev] [PATCH v6] app/test: add tests for atomic exchanges
2019-11-06 21:18 ` [dpdk-dev] [PATCH v6] " David Christensen
@ 2019-11-07 16:42 ` David Marchand
0 siblings, 0 replies; 6+ messages in thread
From: David Marchand @ 2019-11-07 16:42 UTC (permalink / raw)
To: David Christensen; +Cc: dev
On Wed, Nov 6, 2019 at 10:18 PM David Christensen
<drc@linux.vnet.ibm.com> wrote:
>
> The test works by creating a token comprised of random data
> and a CRC8 value, using the rte_atomicXX_exchange to exchange
> the new token for a previously generated token, and then
> verifying that the exchanged data is intact (i.e. the CRC8
> is still correct for the data).
>
> Signed-off-by: David Christensen <drc@linux.vnet.ibm.com>
Acked-by: David Marchand <david.marchand@redhat.com>
Applied, thanks.
--
David Marchand
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2019-11-07 16:43 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20191018182112.115448-1-drc@ibm.com>
2019-10-18 20:30 ` [dpdk-dev] [PATCH v4] app/test: add tests for atomic exchanges David Christensen
2019-11-06 14:18 ` David Marchand
2019-11-06 19:57 ` [dpdk-dev] [PATCH v5] " David Christensen
2019-11-06 20:21 ` David Marchand
2019-11-06 21:18 ` [dpdk-dev] [PATCH v6] " David Christensen
2019-11-07 16:42 ` David Marchand
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).