Implement rte_memcmp with AVX/SSE instructions.
PATCH v2 1/1 Implement rte_memcmp with AVX/SSE instructions.
Background:
After preliminary discussion with John (Zhihong) and Tim from Intel it was decided that it would be beneficial to use AVX/SSE instructions for memcmp similar to memcpy being implemeneted. In addition, we decided to use librte_hash as a test candidate to test both functionality and performance.
Currently memcmp in librte_hash is used for key comparisons whose length can vary and max key length is defined to 64 bytes. Preliminary tests on memory comparison alone shows using AVX/SSE instructions takes 1/3rd CPU ticks compared with regular memcmp function. Furthermore, hash_perf_autotest shows better results in all categories. Please note that memory comparison is a small portion in hash functionality and CPU Ticks/Op is for hash operations (Add on Empty, Add update, Lookup). Only hash lookup results are shown below. I can send complete results if interested.
Test was conducted on Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz, Ubuntu 14.04, x86_64, 16GB DDR3 system.
PS: I would like to keep "rte_memcmp" simple with return codes
0 - match
1 - no-match
since usage in DPDK is for equality or inequality and I have not seen any instance where less-than/greater-than comparison is needed. Hence "if (unlikely(...))" portion in the code will probably be removed and it will be made specific to DPDK rather than being generic.
/*************Existing code**********************************/
*** Hash table performance test results ***
Hash Func. , Operation , Key size (bytes), Entries, Entries per bucket, Errors , Avg. bucket entries, Ticks/Op. rte_hash_crc, Lookup , 16 , 1024 , 1 , 10000 , 0.00 , 88.55 rte_hash_crc, Lookup , 16 , 1024 , 2 , 10000 , 0.00 , 99.28 rte_hash_crc, Lookup , 16 , 1024 , 4 , 10000 , 0.00 , 106.73 rte_hash_crc, Lookup , 16 , 1024 , 8 , 10000 , 0.00 , 126.99 rte_hash_crc, Lookup , 16 , 1024 , 16 , 10000 , 0.00 , 159.80
rte_hash_crc, Lookup , 16 , 1048576, 1 , 51 , 0.01 , 175.23 rte_hash_crc, Lookup , 16 , 1048576, 2 , 2 , 0.02 , 171.24 rte_hash_crc, Lookup , 16 , 1048576, 4 , 0 , 0.04 , 145.48 rte_hash_crc, Lookup , 16 , 1048576, 8 , 0 , 0.08 , 162.35 rte_hash_crc, Lookup , 16 , 1048576, 16 , 0 , 0.15 , 182.42
jhash , Lookup , 16 , 1048576, 1 , 33 , 0.01 , 219.71 jhash , Lookup , 16 , 1048576, 2 , 1 , 0.02 , 216.44 jhash , Lookup , 16 , 1048576, 4 , 0 , 0.04 , 188.29 jhash , Lookup , 16 , 1048576, 8 , 0 , 0.08 , 203.70 jhash , Lookup , 16 , 1048576, 16 , 0 , 0.15 , 229.50
/**************New AVX/SSE code******************************/
Hash Func. , Operation , Key size (bytes), Entries, Entries per bucket, Errors , Avg. bucket entries, Ticks/Op. rte_hash_crc, Lookup , 16 , 1024 , 1 , 10000 , 0.00 , 85.69 rte_hash_crc, Lookup , 16 , 1024 , 2 , 10000 , 0.00 , 93.95 rte_hash_crc, Lookup , 16 , 1024 , 4 , 10000 , 0.00 , 102.80 rte_hash_crc, Lookup , 16 , 1024 , 8 , 10000 , 0.00 , 122.60 rte_hash_crc, Lookup , 16 , 1024 , 16 , 10000 , 0.00 , 156.58
rte_hash_crc, Lookup , 16 , 1048576, 1 , 41 , 0.01 , 156.84 rte_hash_crc, Lookup , 16 , 1048576, 2 , 0 , 0.02 , 157.90 rte_hash_crc, Lookup , 16 , 1048576, 4 , 0 , 0.04 , 134.92 rte_hash_crc, Lookup , 16 , 1048576, 8 , 0 , 0.08 , 150.99 rte_hash_crc, Lookup , 16 , 1048576, 16 , 0 , 0.15 , 174.08
jhash , Lookup , 16 , 1048576, 1 , 45 , 0.01 , 212.03 jhash , Lookup , 16 , 1048576, 2 , 2 , 0.02 , 210.65 jhash , Lookup , 16 , 1048576, 4 , 0 , 0.04 , 185.90 jhash , Lookup , 16 , 1048576, 8 , 0 , 0.08 , 201.35 jhash , Lookup , 16 , 1048576, 16 , 0 , 0.15 , 223.54
Ravi Kerur (1):
Implement memcmp using AVX/SSE instructions.
app/test/test_hash_perf.c | 36 +- .../common/include/arch/ppc_64/rte_memcmp.h | 62 +++ .../common/include/arch/x86/rte_memcmp.h | 421 +++++++++++++++++++++ lib/librte_eal/common/include/generic/rte_memcmp.h | 131 +++++++ lib/librte_hash/rte_hash.c | 59 ++- 5 files changed, 675 insertions(+), 34 deletions(-) create mode 100644 lib/librte_eal/common/include/arch/ppc_64/rte_memcmp.h create mode 100644 lib/librte_eal/common/include/arch/x86/rte_memcmp.h create mode 100644 lib/librte_eal/common/include/generic/rte_memcmp.h
Signature · 1 line
1.9.1
PATCH v2 1/1 Implement memcmp using AVX/SSE instructions.
In reply to a message not in the archiveThis patch replaces memcmp in librte_hash with rte_memcmp which is implemented with AVX/SSE instructions.
Preliminary results on Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz, Ubuntu 14.04 x86_64 shows comparisons using AVX/SSE instructions taking 1/3rd CPU ticks for 16, 32, 48 and 64 bytes comparison. In addition, hash_perf_autotest results shows using new comparison function results in faster completion of hash operations than existing memcmp in all categories.
--- app/test/test_hash_perf.c | 36 +- .../common/include/arch/ppc_64/rte_memcmp.h | 62 +++ .../common/include/arch/x86/rte_memcmp.h | 421 +++++++++++++++++++++ lib/librte_eal/common/include/generic/rte_memcmp.h | 131 +++++++ lib/librte_hash/rte_hash.c | 59 ++- 5 files changed, 675 insertions(+), 34 deletions(-) create mode 100644 lib/librte_eal/common/include/arch/ppc_64/rte_memcmp.h create mode 100644 lib/librte_eal/common/include/arch/x86/rte_memcmp.h create mode 100644 lib/librte_eal/common/include/generic/rte_memcmp.h
app/test/test_hash_perf.c +22 -14
@@ -440,7 +440,7 @@ run_single_tbl_perf_test(const struct rte_hash *h, hash_operation func, uint32_t *invalid_pos_count) {uint64_t begin, end, ticks =0 ; -uint8_t *key =NULL ; +uint8_t *volatile key =NULL ;uint32_t *bucket_occupancies =NULL ;uint32_t num_buckets, i, j;int32_t pos;
@@ -547,30 +547,30 @@ run_tbl_perf_test(struct tbl_perf_test_params *params)case ADD_UPDATE : num_iterations = params->num_iterations; params->num_iterations = params->entries; -run_single_tbl_perf_test (handle, rte_hash_add_key, params, - &avg_occupancy, &invalid_pos); - params->num_iterations = num_iterations; ticks =run_single_tbl_perf_test (handle, rte_hash_add_key, params, &avg_occupancy, &invalid_pos); + params->num_iterations = num_iterations; + ticks +=run_single_tbl_perf_test (handle, rte_hash_add_key, + params, &avg_occupancy, &invalid_pos);break ;case DELETE : num_iterations = params->num_iterations; params->num_iterations = params->entries; -run_single_tbl_perf_test (handle, rte_hash_add_key, params, - &avg_occupancy, &invalid_pos); + ticks =run_single_tbl_perf_test (handle, rte_hash_add_key, + params, &avg_occupancy, &invalid_pos); params->num_iterations = num_iterations; - ticks =run_single_tbl_perf_test (handle, rte_hash_del_key, + ticks +=run_single_tbl_perf_test (handle, rte_hash_del_key, params, &avg_occupancy, &invalid_pos);break ;case LOOKUP : num_iterations = params->num_iterations; params->num_iterations = params->entries; -run_single_tbl_perf_test (handle, rte_hash_add_key, params, - &avg_occupancy, &invalid_pos); + ticks =run_single_tbl_perf_test (handle, rte_hash_add_key, + params, &avg_occupancy, &invalid_pos); params->num_iterations = num_iterations; - ticks =run_single_tbl_perf_test (handle, rte_hash_lookup, + ticks +=run_single_tbl_perf_test (handle, rte_hash_lookup, params, &avg_occupancy, &invalid_pos);break ;default :return -1 ;
@@ -623,10 +623,15 @@ static int run_all_tbl_perf_tests(void)static void run_hash_func_test (rte_hash_function f,uint32_t init_val,uint32_t key_len) { -static uint8_t key[RTE_HASH_KEY_LENGTH_MAX ]; +static uint8_t *volatile key;uint64_t ticks =0 , start, end;unsigned i, j; + key =rte_zmalloc ("func hash key" , + key_len *sizeof (uint8_t ),16 ); +if (key ==NULL ) +return ; +for (i =0 ; i <HASHTEST_ITERATIONS ; i++) {for (j =0 ; j < key_len; j++)
@@ -638,8 +643,11 @@ static void run_hash_func_test(rte_hash_function f, uint32_t init_val, ticks += end - start; } -printf ("%-12s, %-18u, %-13u, %.02f\n" ,get_hash_name (f), (unsigned ) key_len, - (unsigned ) init_val, (double )ticks /HASHTEST_ITERATIONS ); +rte_free (key); + +printf ("%-12s, %-18u, %-13u, %.02f\n" , +get_hash_name (f), (unsigned ) key_len, (unsigned ) init_val, + (double )ticks /HASHTEST_ITERATIONS ); }/*
@@ -687,7 +695,7 @@ fbk_hash_perf_test(void) .socket_id = rte_socket_id(), };struct rte_fbk_hash_table *handle =NULL ; -uint32_t *keys =NULL ; +uint32_t *volatile keys =NULL ;unsigned indexes[TEST_SIZE ];uint64_t lookup_time =0 ;unsigned added =0 ;
lib/librte_eal/common/include/arch/ppc_64/rte_memcmp.h new file +62 -0
62 lines not on this page · raw message
lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +421 -0
421 lines not on this page · raw message
lib/librte_eal/common/include/generic/rte_memcmp.h new file +131 -0
131 lines not on this page · raw message
lib/librte_hash/rte_hash.c +39 -20
107 lines not on this page · raw message
Signature · 1 line
1.9.1
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to a message not in the archiveQuoted 14 lines · Ravi Kerur
This patch replaces memcmp in
librte_hashwithrte_memcmpwhich is implemented with AVX/SSE instructions.lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +9
-0+static inline int +rte_memcmp (const void *_src_1,const void *_src_2,size_t n) +{ +const uint8_t *src_1 = (const uint8_t *)_src_1; +const uint8_t *src_2 = (const uint8_t *)_src_2; +int ret =0 ; + +if (n &0x80 ) +return rte_cmp128 (src_1, src_2);
lib/librte_eal/common/include/arch/x86/rte_memcmp.h line 402
Quoted 10 lines
lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +10
-0+ +if (n &0x40 ) +return rte_cmp64 (src_1, src_2); + +if (n &0x20 ) { + ret =rte_cmp32 (src_1, src_2); + n -=0x20 ; + src_1 +=0x20 ; + src_2 +=0x20 ; + }
Pardon me for butting in, but this seems incorrect for the first two cases listed above, as the function as written will only compare the first 128 or 64 bytes of each source and return the result. The pattern expressed in the 32 byte case appears more correct, as it compares the first 32 bytes and then lets later pieces of the function handle the smaller remaining bits of the sources. Also, if this function is to handle arbitrarily large source data, the 128 byte case needs to be in a loop.
What am I missing?
--
Matt Laswell
infinite io, inc.
[email protected]
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to a message not in the archiveQuoted 36 lines · Matt Laswell
Quoted 14 lines · Ravi Kerur
This patch replaces memcmp in
librte_hashwithrte_memcmpwhich is implemented with AVX/SSE instructions.lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +9
-0+static inline int +rte_memcmp (const void *_src_1,const void *_src_2,size_t n) +{ +const uint8_t *src_1 = (const uint8_t *)_src_1; +const uint8_t *src_2 = (const uint8_t *)_src_2; +int ret =0 ; + +if (n &0x80 ) +return rte_cmp128 (src_1, src_2);lib/librte_eal/common/include/arch/x86/rte_memcmp.h line 402
Quoted 10 lines
lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +10
-0+ +if (n &0x40 ) +return rte_cmp64 (src_1, src_2); + +if (n &0x20 ) { + ret =rte_cmp32 (src_1, src_2); + n -=0x20 ; + src_1 +=0x20 ; + src_2 +=0x20 ; + }Pardon me for butting in, but this seems incorrect for the first two cases listed above, as the function as written will only compare the first 128 or 64 bytes of each source and return the result. The pattern expressed in the 32 byte case appears more correct, as it compares the first 32 bytes and then lets later pieces of the function handle the smaller remaining bits of the sources. Also, if this function is to handle arbitrarily large source data, the 128 byte case needs to be in a loop.
Quoted 2 lines
What am I missing?
Current max hash key length supported is 64 bytes, hence no comparison is done after 64 bytes. 128 bytes comparison is added to measure performance only and there is no use-case as of now. With the current use-cases its not required but if there is a need to handle large arbitrary data upto 128 bytes it can be modified.
Quoted 7 lines
--
Matt Laswell
infinite io, inc.
[email protected]
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to a message not in the archiveQuoted 41 lines · Ravi Kerur
Quoted 36 lines · Matt Laswell
Quoted 14 lines · Ravi Kerur
This patch replaces memcmp in
librte_hashwithrte_memcmpwhich is implemented with AVX/SSE instructions.lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +9
-0+static inline int +rte_memcmp (const void *_src_1,const void *_src_2,size_t n) +{ +const uint8_t *src_1 = (const uint8_t *)_src_1; +const uint8_t *src_2 = (const uint8_t *)_src_2; +int ret =0 ; + +if (n &0x80 ) +return rte_cmp128 (src_1, src_2);lib/librte_eal/common/include/arch/x86/rte_memcmp.h line 402
Quoted 10 lines
lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +10
-0+ +if (n &0x40 ) +return rte_cmp64 (src_1, src_2); + +if (n &0x20 ) { + ret =rte_cmp32 (src_1, src_2); + n -=0x20 ; + src_1 +=0x20 ; + src_2 +=0x20 ; + }Pardon me for butting in, but this seems incorrect for the first two cases listed above, as the function as written will only compare the first 128 or 64 bytes of each source and return the result. The pattern expressed in the 32 byte case appears more correct, as it compares the first 32 bytes and then lets later pieces of the function handle the smaller remaining bits of the sources. Also, if this function is to handle arbitrarily large source data, the 128 byte case needs to be in a loop.
Quoted 9 lines
Quoted 2 lines
What am I missing?
Current max hash key length supported is 64 bytes, hence no comparison is done after 64 bytes. 128 bytes comparison is added to measure performance only and there is no use-case as of now. With the current use-cases its not required but if there is a need to handle large arbitrary data upto 128 bytes it can be modified.
Ah, gotcha. I misunderstood and thought that this was meant to be a generic AVX/SSE enabled memcmp() replacement, and that the use of it inrte_hash was meant merely as a test case. If it's more limited than that,
carry on, though you might want to make a note of it in the documentation. I suspect others will misinterpret the name as I did.
--
Matt Laswell
infinite io, inc.
[email protected]
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to Ravi KerurHi Ravi,
Quoted 45 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ravi Kerur
Sent: Friday, May 08, 2015 11:55 PM
To: Matt Laswell
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSE instructions.Quoted 36 lines · Matt Laswell
Quoted 14 lines · Ravi Kerur
This patch replaces memcmp in
librte_hashwithrte_memcmpwhich is implemented with AVX/SSE instructions.lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +9
-0+static inline int +rte_memcmp (const void *_src_1,const void *_src_2,size_t n) +{ +const uint8_t *src_1 = (const uint8_t *)_src_1; +const uint8_t *src_2 = (const uint8_t *)_src_2; +int ret =0 ; + +if (n &0x80 ) +return rte_cmp128 (src_1, src_2);lib/librte_eal/common/include/arch/x86/rte_memcmp.h line 402
Quoted 10 lines
lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +10
-0+ +if (n &0x40 ) +return rte_cmp64 (src_1, src_2); + +if (n &0x20 ) { + ret =rte_cmp32 (src_1, src_2); + n -=0x20 ; + src_1 +=0x20 ; + src_2 +=0x20 ; + }Pardon me for butting in, but this seems incorrect for the first two cases listed above, as the function as written will only compare the first 128 or 64 bytes of each source and return the result. The pattern expressed in the 32 byte case appears more correct, as it compares the first 32 bytes and then lets later pieces of the function handle the smaller remaining bits of the sources. Also, if this function is to handle arbitrarily large source data, the 128 byte case needs to be in a loop.
Quoted 8 lines
Quoted 2 lines
What am I missing?
Current max hash key length supported is 64 bytes, hence no comparison is done after 64 bytes. 128 bytes comparison is added to measure performance only and there is no use-case as of now. With the current use-cases its not required but if there is a need to handle large arbitrary data upto 128 bytes it can be modified.
So on x86 let say rte_memcmp(k1, k2, 65) might produce invalid results, right?
While on PPC will work as expected (as it calls memcpu underneath)?
That looks really weird to me.
If you plan to use rte_memcmp only for hash comparisons, then probably
you should put it somewhere into librte_hash and name it accordingly: rte_hash_key_cmp() or something. And put a big comment around it, that it only works with particular lengths.
If you want it to be a generic function inside EAL, then it probably need to handle different lengths properly on all supported architectures.
Konstantin
Quoted 8 lines
Quoted 8 lines
--
Matt Laswell
infinite io, inc.
[email protected]
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
Hi Konstantin,
Quoted 64 lines · Konstantin
Hi Ravi,
Quoted 6 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ravi Kerur
Sent: Friday, May 08, 2015 11:55 PM
To: Matt Laswell
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSEinstructions.
Quoted 2 lines
On Fri, May 8, 2015 at 3:29 PM, Matt Laswell <[email protected]>
Quoted 22 lines · wrote:
Quoted 22 lines · Ravi Kerur
Quoted 18 lines
This patch replaces memcmp in
librte_hashwithrte_memcmpwhich is implemented with AVX/SSE instructions.lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +14
-0+static inline int +rte_memcmp (const void *_src_1,const void *_src_2,size_t n) +{ +const uint8_t *src_1 = (const uint8_t *)_src_1; +const uint8_t *src_2 = (const uint8_t *)_src_2; +int ret =0 ; + +if (n &0x80 ) +return rte_cmp128 (src_1, src_2); + +if (n &0x40 ) +return rte_cmp64 (src_1, src_2); + +if (n &0x20 ) {Quoted 8 lines
Quoted 8 lines
lib/librte_eal/common/include/arch/x86/rte_memcmp.h line 402
Quoted 7 lines
lib/librte_eal/common/include/arch/x86/rte_memcmp.h new file +5
-0+ ret =rte_cmp32 (src_1, src_2); + n -=0x20 ; + src_1 +=0x20 ; + src_2 +=0x20 ; + }Pardon me for butting in, but this seems incorrect for the first two
cases
Quoted 1 line
Quoted 1 line
listed above, as the function as written will only compare the first
128 or
Quoted 1 line
Quoted 1 line
64 bytes of each source and return the result. The pattern expressed
in
Quoted 1 line
Quoted 1 line
the 32 byte case appears more correct, as it compares the first 32
bytes
Quoted 2 lines
Quoted 2 lines
and then lets later pieces of the function handle the smaller remaining bits of the sources. Also, if this function is to handle arbitrarily
large
Quoted 8 lines
Quoted 4 lines
source data, the 128 byte case needs to be in a loop.
What am I missing?
Current max hash key length supported is 64 bytes, hence no comparison is done after 64 bytes. 128 bytes comparison is added to measure performance only and there is no use-case as of now. With the current use-cases its
not
Quoted 2 lines
required but if there is a need to handle large arbitrary data upto 128 bytes it can be modified.
Quoted 15 lines
So on x86 let say
rte_memcmp(k1, k2, 65)might produce invalid results, right?
While on PPC will work as expected (as it calls memcpu underneath)? That looks really weird to me.
If you plan to userte_memcmponly for hash comparisons, then probably you should put it somewhere intolibrte_hashand name it accordingly:rte_hash_key_cmp()or something.
And put a big comment around it, that it only works with particular lengths.
If you want it to be a generic function inside EAL, then it probably need to handle different lengths properly
on all supported architectures.
Konstantin
Let me just explain it here and probably add it to document as well.
rte_memcmp is not
1. a replacement to memcmp
2. restricted to hash key comparison
rte_memcmp is
1. optimized comparison for 16 to 128 bytes, v1 patch series had this support. Changed some of the logic in v2 due to concerns raised for unavailable use-cases beyond 64 bytes comparison. With minor tuning over the weekend I am able to get better performance for anything between 16 to 128 bytes comparison.
2. will be specific to DPDK i.e. currently all memcmp usage in DPDK are for equality or inequality hence "less than" or "greater than" implementation in rte_memcmp doesn't make sense and will be removed in subsequent patches, it will return 0 or 1 for equal/unequal cases.
rte_hash will be the first candidate to move to rte_memcmp and subsequently rte_lpm6 which uses 16 bytes comparison will be moved
Later on RING_SIZE which uses large size for comparison will be moved. I am currently studying/understanding that logic and will make changes to rte_memcmp to support that.
I don't want to make lot of changes in one shot and see that patch series die a slow death with no takers.
Thanks,
Ravi
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
--
Matt Laswell
infinite io, inc.
[email protected]
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to a message not in the archiveHi Ravi,
Quoted 83 lines
From: Ravi Kerur [mailto:[email protected]]
Sent: Monday, May 11, 2015 6:43 PM
To: Ananyev, Konstantin
Cc: Matt Laswell; [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSE instructions.Hi Konstantin,
On Mon, May 11, 2015 at 2:51 AM, Ananyev, Konstantin <[email protected]> wrote:
Hi Ravi,Quoted 45 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ravi Kerur
Sent: Friday, May 08, 2015 11:55 PM
To: Matt Laswell
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSE instructions.Quoted 36 lines · Matt Laswell
Quoted 14 lines · Ravi Kerur
This patch replaces memcmp in
librte_hashwithrte_memcmpwhich is implemented with AVX/SSE instructions.(quoted diff) +9
-0+static inline int +rte_memcmp(const void *_src_1, const void *_src_2, size_t n) +{ + const uint8_t *src_1 = (const uint8_t *)_src_1; + const uint8_t *src_2 = (const uint8_t *)_src_2; + int ret = 0; + + if (n & 0x80) + return rte_cmp128(src_1, src_2);Review
Quoted 10 lines
(quoted diff) +10
-0+ + if (n & 0x40) + return rte_cmp64(src_1, src_2); + + if (n & 0x20) { + ret = rte_cmp32(src_1, src_2); + n -= 0x20; + src_1 += 0x20; + src_2 += 0x20; + }Pardon me for butting in, but this seems incorrect for the first two cases listed above, as the function as written will only compare the first 128 or 64 bytes of each source and return the result. The pattern expressed in the 32 byte case appears more correct, as it compares the first 32 bytes and then lets later pieces of the function handle the smaller remaining bits of the sources. Also, if this function is to handle arbitrarily large source data, the 128 byte case needs to be in a loop.
Quoted 8 lines
Quoted 2 lines
What am I missing?
Current max hash key length supported is 64 bytes, hence no comparison is done after 64 bytes. 128 bytes comparison is added to measure performance only and there is no use-case as of now. With the current use-cases its not required but if there is a need to handle large arbitrary data upto 128 bytes it can be modified.
So on x86 let say
rte_memcmp(k1, k2, 65)might produce invalid results, right?
While on PPC will work as expected (as it calls memcpu underneath)?
That looks really weird to me.
If you plan to userte_memcmponly for hash comparisons, then probably
you should put it somewhere intolibrte_hashand name it accordingly:rte_hash_key_cmp()or something. And put a big comment around it, that it only works with particular lengths.
If you want it to be a generic function inside EAL, then it probably need to handle different lengths properly on all supported architectures.
KonstantinLet me just explain it here and probably add it to document as well.
rte_memcmpis not1. a replacement to memcmp
Quoted 8 lines
2. restricted to hash key comparison
rte_memcmpis1. optimized comparison for 16 to 128 bytes, v1 patch series had this support. Changed some of the logic in v2 due to concerns raised for unavailable use-cases beyond 64 bytes comparison.
From what I see in v2 it supposed to work correctly for len in [0,64] and len=128, right?
Not sure I get it: so for v1 it was able to handle any length correctly, but then you removed it? If so, I wonder what was the reason? Make it faster?
Another thing that looks strange to me:
While all rte_cmp*() uses actual data values for comparison results, rte_memcmp_remainder() return value depends not only on data values but also on data locations:
+static inline int
+rte_memcmp_remainder(const uint8_t *src_1u, const uint8_t *src_2u, size_t n) +{
...
exit:
+
+ return src_1u < src_2u ? -1 : 1;
+}
If you just test for equal/not equal that doesn't really matter.
If this is supposed to be a 'proper' comparison function, then the result is sort of unpredictable.
Quoted 6 lines
With minor tuning over the weekend I am able to get better performance for anything between 16 to 128 bytes comparison.
2. will be specific to DPDK i.e. currently all memcmp usage in DPDK are for equality or inequality hence "less than" or "greater than" implementation in
rte_memcmpdoesn't make sense and will be removed in subsequent patches, it will return 0 or 1 for equal/unequal cases.
If you don't plan your function to follow memcmp() semantics and syntax, why to name it rte_memcmp()? I think that will make a lot of confusion around.
Why not to name it differently(and put a clear comment in the declaration of course)?
Quoted 6 lines
rte_hashwill be the first candidate to move torte_memcmpand subsequentlyrte_lpm6which uses 16 bytes comparison will be movedLater on
RING_SIZEwhich uses large size for comparison will be moved. I am currently studying/understanding that logic and will make changes torte_memcmpto support that.
Sorry, didn't get you here.
Konstantin
Quoted 14 lines
I don't want to make lot of changes in one shot and see that patch series die a slow death with no takers.
Thanks,
RaviQuoted 8 lines
Quoted 8 lines
--
Matt Laswell
infinite io, inc.
[email protected]
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
Hi Konstantin,
Quoted 108 lines · Konstantin
Hi Ravi,
Quoted 6 lines
From: Ravi Kerur [mailto:[email protected]]
Sent: Monday, May 11, 2015 6:43 PM
To: Ananyev, Konstantin
Cc: Matt Laswell; [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSEinstructions.
Quoted 5 lines
Hi Konstantin,
On Mon, May 11, 2015 at 2:51 AM, Ananyev, Konstantin <
Quoted 8 lines · [email protected]> wrote:
Hi Ravi,
Quoted 6 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ravi Kerur
Sent: Friday, May 08, 2015 11:55 PM
To: Matt Laswell
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSEinstructions.
Quoted 2 lines
Quoted 2 lines
On Fri, May 8, 2015 at 3:29 PM, Matt Laswell <[email protected]>
Quoted 22 lines · wrote:
Quoted 22 lines
Quoted 22 lines · Ravi Kerur
Quoted 18 lines
This patch replaces memcmp in
librte_hashwithrte_memcmpwhich is implemented with AVX/SSE instructions.(quoted diff) +14
-0+static inline int +rte_memcmp(const void *_src_1, const void *_src_2, size_t n) +{ + const uint8_t *src_1 = (const uint8_t *)_src_1; + const uint8_t *src_2 = (const uint8_t *)_src_2; + int ret = 0; + + if (n & 0x80) + return rte_cmp128(src_1, src_2); + + if (n & 0x40) + return rte_cmp64(src_1, src_2); + + if (n & 0x20) {Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Review
Quoted 7 lines
(quoted diff) +5
-0+ ret = rte_cmp32(src_1, src_2); + n -= 0x20; + src_1 += 0x20; + src_2 += 0x20; + }Pardon me for butting in, but this seems incorrect for the first two
cases
Quoted 1 line
Quoted 1 line
Quoted 1 line
listed above, as the function as written will only compare the first
128 or
Quoted 1 line
Quoted 1 line
Quoted 1 line
64 bytes of each source and return the result. The pattern
expressed in
Quoted 1 line
Quoted 1 line
Quoted 1 line
the 32 byte case appears more correct, as it compares the first 32
bytes
Quoted 1 line
Quoted 1 line
Quoted 1 line
and then lets later pieces of the function handle the smaller
remaining
Quoted 1 line
Quoted 1 line
Quoted 1 line
bits of the sources. Also, if this function is to handle arbitrarily
large
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
source data, the 128 byte case needs to be in a loop.
What am I missing?
Current max hash key length supported is 64 bytes, hence no comparison
is
Quoted 1 line
Quoted 1 line
done after 64 bytes. 128 bytes comparison is added to measure
performance
Quoted 1 line
Quoted 1 line
only and there is no use-case as of now. With the current use-cases
its not
Quoted 3 lines
Quoted 2 lines
required but if there is a need to handle large arbitrary data upto 128 bytes it can be modified.
So on x86 let say
rte_memcmp(k1, k2, 65)might produce invalid results,right?
Quoted 4 lines
While on PPC will work as expected (as it calls memcpu underneath)? That looks really weird to me.
If you plan to userte_memcmponly for hash comparisons, then probably you should put it somewhere intolibrte_hashand name it accordingly:
rte_hash_key_cmp()or something.Quoted 1 line
And put a big comment around it, that it only works with particular
lengths.
Quoted 1 line
If you want it to be a generic function inside EAL, then it probably
need to handle different lengths properly
Quoted 8 lines
on all supported architectures.
KonstantinLet me just explain it here and probably add it to document as well.
rte_memcmpis notQuoted 7 lines
1. a replacement to memcmp
2. restricted to hash key comparison
rte_memcmpis1. optimized comparison for 16 to 128 bytes, v1 patch series had this
Quoted 9 lines
support. Changed some of the logic in v2 due to concerns raised
Quoted 1 line
for unavailable use-cases beyond 64 bytes comparison.
From what I see in v2 it supposed to work correctly for len in [0,64] and len=128, right?
Not sure I get it: so for v1 it was able to handle any length correctly, but then you removed it?
If so, I wonder what was the reason? Make it faster?
My initial discussion was with Zhilong(John) from Intel and we decided to implement up to 128 bytes comparison and use rte_hash and rte_lpm6 as a candidate for testing. When I sent out v1 patch, Bruce comments were on use-case for 128 bytes comparison and was it really required? Hence I decided in v2 to support only up to 64 bytes and added 128 bytes only for performance measurement.
Personally I think support for up to 128 bytes comparison is required, there might not be use-cases today but it will definitely be useful.
Quoted 13 lines
Another thing that looks strange to me:
While all rte_cmp*() uses actual data values for comparison results,rte_memcmp_remainder()return value depends not only on data values but also on data locations:(quoted diff) +2
-0+static inline int +rte_memcmp_remainder(const uint8_t *src_1u, const uint8_t *src_2u, size_tn)
+{
...
exit:
Review
Quoted 3 lines
(quoted diff) +3
-0+ + return src_1u < src_2u ? -1 : 1; +}
This is a bug and its not supposed to be there. I will fix it. Thanks for catching it.
Quoted 11 lines
If you just test for equal/not equal that doesn't really matter. If this is supposed to be a 'proper' comparison function, then the result is sort of unpredictable.
Quoted 1 line
With minor tuning over the weekend I am able to get better performance
for
Quoted 3 lines
anything between 16 to 128 bytes comparison.
2. will be specific to DPDK i.e. currently all memcmp usage in DPDK are
for equality or inequality hence "less than" or "greater than"
Quoted 1 line
implementation in
rte_memcmpdoesn't make sense and will be removed in
Quoted 9 lines
subsequent patches, it will return 0 or 1 for
Quoted 1 line
equal/unequal cases.
If you don't plan your function to follow
memcmp()semantics and syntax, why to name itrte_memcmp()?
I think that will make a lot of confusion around.
Why not to name it differently(and put a clear comment in the declaration of course)?
Following memcmp semantics is not hard but there are no use-cases for it in DPDK currently. Keeping it specific to DPDK usage simplifies code as well. I can change the name to "rte_compare" and add comments to the function. Will it work?
Quoted 12 lines
Quoted 3 lines
rte_hashwill be the first candidate to move torte_memcmpandsubsequently
rte_lpm6which uses 16 bytes comparison will beQuoted 3 lines
moved
Later on
RING_SIZEwhich uses large size for comparison will be moved. Iam currently studying/understanding that logic and will make
Quoted 1 line
changes to
rte_memcmpto support that.Sorry, didn't get you here.
Once rte_hash, rte_lpm6 changes and new compare function code are reviewed and accepted I plan to move to different components (RING_SIZE is currently defined to be from 256 to 16384 bytes) and memcmp function being used in test_ring, test_pmd_ring and other functions. I did not want to add all component changes into one patch series as it causes high review latency or patch series just dies down silently. Instead make patches small and incremental in every series, hope this clarifies.
Thanks,
Ravi
Konstantin
Quoted 18 lines
Quoted 3 lines
I don't want to make lot of changes in one shot and see that patch
series die a slow death with no takers.
Quoted 12 lines
Thanks,
RaviQuoted 8 lines
Quoted 8 lines
--
Matt Laswell
infinite io, inc.
[email protected]
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
I probably shouldn't stick my nose into this, but I can't help myself.
An experienced programmer will tend to ignore the documentation for a routine named "blahblah_memcmp" and just assume it functions like memcmp. Whether or not there's currently a use case in DPDK is completely irrelevant because as soon as there *is* a use case, some poor DPDK developer will try to use rte_memcmp for that and may or may not have a test case that reveals their mistake.
The term "compare" suggests checking for larger or smaller. If you want to check for equality, use "equal" or "eq" in the name and return true if they're equal. But personally, I'd compare unless there was a good reason not to. Indeed, I would just implement full memcmp functionality and be done with it, even if that meant using my fancy new assembly code for the cases I handle and then calling memcmp itself for the cases I didn't.
If a routine that appears to take an arbitrary size doesn't, the name should in some manner reflect what sizes it takes. Better would be for a routine that only handles specific sizes to be split into versions that only take fixed sizes, but I don't know enough about your use cases to say whether that makes sense here.
-don provan
[email protected]
-----Original Message-----
From: Ravi Kerur [mailto:[email protected]]
Sent: Monday, May 11, 2015 1:47 PM
To: Ananyev, Konstantin
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSE instructions.
...
Following memcmp semantics is not hard but there are no use-cases for it in DPDK currently. Keeping it specific to DPDK usage simplifies code as well. I can change the name to "rte_compare" and add comments to the function.
Will it work?
...
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to Ravi KerurHi, Ravi Kerur
Quoted 3 lines · Ravi Kerur
Preliminary results on Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz, Ubuntu 14.04
x86_64shows comparisons using AVX/SSE instructions taking 1/3rd CPU ticks for 16, 32, 48 and 64 bytes comparison. In addition,
I had write a program to test rte_memcmp and I have a question about the result. Why cost same CPU ticks for 128 256 512 1024 1500 bytes? Is there any problem in my test?
[root@localhost test]# gcc avx_test.c -O3 -I /data/linhf/v2r2c00/open-source/dpdk/dpdk-2.0.0/x86_64-native-linuxapp-gcc/include/ -mavx2 -DRTE_MACHINE_CPUFLAG_AVX2 [root@localhost test]# ./a.out 0
each test run 100000000 times
copy 16 bytes costs average 7(rte_memcmp) 10(memcmp) ticks
copy 32 bytes costs average 9(rte_memcmp) 11(memcmp) ticks
copy 64 bytes costs average 6(rte_memcmp) 13(memcmp) ticks
copy 128 bytes costs average 11(rte_memcmp) 14(memcmp) ticks
copy 256 bytes costs average 9(rte_memcmp) 14(memcmp) ticks
copy 512 bytes costs average 9(rte_memcmp) 14(memcmp) ticks
copy 1024 bytes costs average 9(rte_memcmp) 14(memcmp) ticks
copy 1500 bytes costs average 11(rte_memcmp) 14(memcmp) ticks
[root@localhost test]# ./a.out 1
each test run 100000000 times
copy 16 bytes costs average 2(rte_memcpy) 10(memcpy) ticks
copy 32 bytes costs average 2(rte_memcpy) 10(memcpy) ticks
copy 64 bytes costs average 3(rte_memcpy) 10(memcpy) ticks
copy 128 bytes costs average 7(rte_memcpy) 12(memcpy) ticks
copy 256 bytes costs average 9(rte_memcpy) 23(memcpy) ticks
copy 512 bytes costs average 14(rte_memcpy) 34(memcpy) ticks
copy 1024 bytes costs average 37(rte_memcpy) 61(memcpy) ticks
copy 1500 bytes costs average 62(rte_memcpy) 87(memcpy) ticks
Here is my program:
#include <stdio.h>
#include <rte_cycles.h>
#include <smmintrin.h>
#include <rte_memcpy.h>
#include <rte_memcmp.h>
#define TIMES 100000000L
void test_memcpy(size_t n)
{
uint64_t start, end, i, start2, end2;
uint8_t *src, *dst;
src = (uint8_t*)malloc(n * sizeof(uint8_t));
dst = (uint8_t*)malloc(n * sizeof(uint8_t));
start = rte_rdtsc();
for (i = 0; i < TIMES; i++) {
rte_memcpy(dst, src, n);
}
end = rte_rdtsc();
start2 = rte_rdtsc();
for (i = 0; i < TIMES; i++) {
memcpy(dst, src, n);
}
end2 = rte_rdtsc();
free(src);
free(dst);
printf("copy %u bytes costs average %llu(rte_memcpy) %llu(memcpy) ticks\n", n, (end - start)/TIMES, (end2 - start2)/TIMES);
}int test_memcmp(size_t n)
{
uint64_t start, end, i, start2, end2, j;
uint8_t *src, *dst;
int *ret;
src = (uint8_t*)malloc(n * sizeof(uint8_t));
dst = (uint8_t*)malloc(n * sizeof(uint8_t));
ret = (int*)malloc(TIMES * sizeof(int));
start = rte_rdtsc();
for (i = 0; i < TIMES; i++) {
ret[i] = rte_memcmp(dst, src, n);
}
end = rte_rdtsc();
start2 = rte_rdtsc();
for (i = 0; i < TIMES; i++) {
ret[i] = memcmp(dst, src, n);
}
end2 = rte_rdtsc();
// avoid gcc to optimize memcmp
for (i = 0; i < TIMES; i++) {
t += ret[i];
}
free(src);
free(dst);
printf("copy %u bytes costs average %llu(rte_memcmp) %llu(memcmp) ticks\n", n, (end - start)/TIMES, (end2 - start2)/TIMES);
return t;
}int main(int narg, char** args)
{
printf("each test run %llu times\n", TIMES);
if (narg < 2) {
printf("usage:./avx_test 0/1 1:test memcpy 0:test memcmp\n");
return -1;
}
if (atoi(args[1])) {
test_memcpy(16);
test_memcpy(32);
test_memcpy(64);
test_memcpy(128);
test_memcpy(256);
test_memcpy(512);
test_memcpy(1024);
test_memcpy(1500);
} else {
test_memcmp(16);
test_memcmp(32);
test_memcmp(64);
test_memcmp(128);
test_memcmp(256);
test_memcmp(512);
test_memcmp(1024);
test_memcmp(1500);
}
}Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to Don ProvanQuoted 9 lines · Don Provan
I probably shouldn't stick my nose into this, but I can't help myself.
An experienced programmer will tend to ignore the documentation for a routine named "
blahblah_memcmp" and just assume it functions like memcmp. Whether or not there's currently a use case in DPDK is completely irrelevant because as soon as there *is* a use case, some poor DPDK developer will try to userte_memcmpfor that and may or may not have a test case that reveals their mistake.
In general I agree with you. However, comparison is a hit(equal) or miss(unequal) is generally the case in networking. I haven't seen cases where "less than" or "greater than" has mattered.
Quoted 9 lines
The term "compare" suggests checking for larger or smaller. If you want to check for equality, use "equal" or "eq" in the name and return true if they're equal. But personally, I'd compare unless there was a good reason not to. Indeed, I would just implement full memcmp functionality and be done with it, even if that meant using my fancy new assembly code for the cases I handle and then calling memcmp itself for the cases I didn't.
Quoted 10 lines
Agreed, I will look into implementing full functionality.
If a routine that appears to take an arbitrary size doesn't, the name should in some manner reflect what sizes it takes. Better would be for a routine that only handles specific sizes to be split into versions that only take fixed sizes, but I don't know enough about your use cases to say whether that makes sense here.
Users of rte_memcmp will be existing dpdk test and library code.
-don provan
Quoted 19 lines
-----Original Message-----
From: Ravi Kerur [mailto:[email protected]]
Sent: Monday, May 11, 2015 1:47 PM
To: Ananyev, Konstantin
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSE instructions....
Following memcmp semantics is not hard but there are no use-cases for it in DPDK currently. Keeping it specific to DPDK usage simplifies code as well.
I can change the name to "rte_compare" and add comments to the function. Will it work?
...
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to LinhaifengHi Linhaifeng,
Quoted 7 lines · Linhaifeng
Hi, Ravi Kerur
Quoted 3 lines · Ravi Kerur
Preliminary results on Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz, Ubuntu 14.04
x86_64shows comparisons using AVX/SSE instructions taking 1/3rd CPU ticks for 16, 32, 48 and 64 bytes comparison. In addition,
Quoted 7 lines
I had write a program to test
rte_memcmpand I have a question about the result.
Why cost same CPU ticks for 128 256 512 1024 1500 bytes? Is there any problem in
my test?
If you can wait until Thursday I will probably send v3 patch which will have full memcmp support.
In your program try with volatile pointer and see if it helps.
Quoted 139 lines
[root@localhost test]# gcc
avx_test.c-O3 -I /data/linhf/v2r2c00/open-source/dpdk/dpdk-2.0.0/x86_64-native-linuxapp-gcc/include/ -mavx2 -DRTE_MACHINE_CPUFLAG_AVX2
[root@localhost test]# ./a.out 0
each test run 100000000 times
copy 16 bytes costs average 7(rte_memcmp) 10(memcmp) ticks
copy 32 bytes costs average 9(rte_memcmp) 11(memcmp) ticks
copy 64 bytes costs average 6(rte_memcmp) 13(memcmp) ticks
copy 128 bytes costs average 11(rte_memcmp) 14(memcmp) ticks
copy 256 bytes costs average 9(rte_memcmp) 14(memcmp) ticks
copy 512 bytes costs average 9(rte_memcmp) 14(memcmp) ticks
copy 1024 bytes costs average 9(rte_memcmp) 14(memcmp) ticks
copy 1500 bytes costs average 11(rte_memcmp) 14(memcmp) ticks [root@localhost test]# ./a.out 1
each test run 100000000 times
copy 16 bytes costs average 2(rte_memcpy) 10(memcpy) ticks
copy 32 bytes costs average 2(rte_memcpy) 10(memcpy) ticks
copy 64 bytes costs average 3(rte_memcpy) 10(memcpy) ticks
copy 128 bytes costs average 7(rte_memcpy) 12(memcpy) ticks
copy 256 bytes costs average 9(rte_memcpy) 23(memcpy) ticks
copy 512 bytes costs average 14(rte_memcpy) 34(memcpy) ticks
copy 1024 bytes costs average 37(rte_memcpy) 61(memcpy) ticks
copy 1500 bytes costs average 62(rte_memcpy) 87(memcpy) ticksHere is my program:
#include <
stdio.h>
#include <rte_cycles.h>
#include <smmintrin.h>
#include <rte_memcpy.h>
#include <rte_memcmp.h>#define TIMES 100000000L
void
test_memcpy(size_t n){ uint64_t start, end, i, start2, end2; uint8_t *src, *dst; src = (uint8_t*)malloc(n * sizeof(uint8_t)); dst = (uint8_t*)malloc(n * sizeof(uint8_t)); start = rte_rdtsc(); for (i = 0; i < TIMES; i++) { rte_memcpy(dst, src, n); } end = rte_rdtsc(); start2 = rte_rdtsc(); for (i = 0; i < TIMES; i++) { memcpy(dst, src, n); } end2 = rte_rdtsc(); free(src); free(dst); printf("copy %u bytes costs average %llu(rte_memcpy) %llu(memcpy)ticks\n", n, (end - start)/TIMES, (end2 - start2)/TIMES); }
int
test_memcmp(size_t n){ uint64_t start, end, i, start2, end2, j; uint8_t *src, *dst; int *ret; src = (uint8_t*)malloc(n * sizeof(uint8_t)); dst = (uint8_t*)malloc(n * sizeof(uint8_t)); ret = (int*)malloc(TIMES * sizeof(int)); start = rte_rdtsc(); for (i = 0; i < TIMES; i++) { ret[i] = rte_memcmp(dst, src, n); } end = rte_rdtsc(); start2 = rte_rdtsc(); for (i = 0; i < TIMES; i++) { ret[i] = memcmp(dst, src, n); } end2 = rte_rdtsc(); // avoid gcc to optimize memcmp for (i = 0; i < TIMES; i++) { t += ret[i]; } free(src); free(dst); printf("copy %u bytes costs average %llu(rte_memcmp) %llu(memcmp) ticks\n", n, (end - start)/TIMES, (end2 - start2)/TIMES); return t; }int
main(int narg, char** args){ printf("each test run %llu times\n", TIMES); if (narg < 2) { printf("usage:./avx_test 0/1 1:test memcpy 0:test memcmp\n"); return -1; } if (atoi(args[1])) { test_memcpy(16); test_memcpy(32); test_memcpy(64); test_memcpy(128); test_memcpy(256); test_memcpy(512); test_memcpy(1024); test_memcpy(1500); } else { test_memcmp(16); test_memcmp(32); test_memcmp(64); test_memcmp(128); test_memcmp(256); test_memcmp(512); test_memcmp(1024); test_memcmp(1500); } }
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
Quoted 2 lines · Ravi Kerur
If you can wait until Thursday I will probably send v3 patch which will have full memcmp support.
Ok, I'd like to test it:)
Quoted 2 lines
In your program try with volatile pointer and see if it helps.
like "volatile uint8_t *src, *dst" ?
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to Ravi KerurQuoted 12 lines · Ravi Kerur
Quoted 9 lines · Don Provan
I probably shouldn't stick my nose into this, but I can't help myself.
An experienced programmer will tend to ignore the documentation for a routine named "
blahblah_memcmp" and just assume it functions like memcmp. Whether or not there's currently a use case in DPDK is completely irrelevant because as soon as there *is* a use case, some poor DPDK developer will try to userte_memcmpfor that and may or may not have a test case that reveals their mistake.
Quoted 5 lines
In general I agree with you. However, comparison is a hit(equal) or miss(unequal) is generally the case in networking. I haven't seen cases where "less than" or "greater than" has mattered.
Agreed that == and != are the common operations. However, if that is what is returned from the function - and given other limitations on parameter sizes - I agree with previous posters that this function needs to have a different name to rte_memcmp so as to avoid confusion.
/Bruce
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to a message not in the archiveHi Ravi,
Quoted 113 lines
-----Original Message-----
From: Ananyev, Konstantin
Sent: Wednesday, May 13, 2015 11:02 AM
To: Ananyev, Konstantin
Subject: FW: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSE instructions.From: Ravi Kerur [mailto:[email protected]]
Sent: Monday, May 11, 2015 9:47 PM
To: Ananyev, Konstantin
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSE instructions.Hi Konstantin,
On Mon, May 11, 2015 at 12:35 PM, Ananyev, Konstantin <[email protected]> wrote:
Hi Ravi,
Quoted 81 lines
From: Ravi Kerur [mailto:[email protected]]
Sent: Monday, May 11, 2015 6:43 PM
To: Ananyev, Konstantin
Cc: Matt Laswell; [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSE instructions.Hi Konstantin,
On Mon, May 11, 2015 at 2:51 AM, Ananyev, Konstantin <[email protected]> wrote:
Hi Ravi,Quoted 45 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ravi Kerur
Sent: Friday, May 08, 2015 11:55 PM
To: Matt Laswell
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSE instructions.Quoted 36 lines · Matt Laswell
Quoted 14 lines · Ravi Kerur
This patch replaces memcmp in
librte_hashwithrte_memcmpwhich is implemented with AVX/SSE instructions.(quoted diff) +9
-0+static inline int +rte_memcmp(const void *_src_1, const void *_src_2, size_t n) +{ + const uint8_t *src_1 = (const uint8_t *)_src_1; + const uint8_t *src_2 = (const uint8_t *)_src_2; + int ret = 0; + + if (n & 0x80) + return rte_cmp128(src_1, src_2);Review
Quoted 10 lines
(quoted diff) +10
-0+ + if (n & 0x40) + return rte_cmp64(src_1, src_2); + + if (n & 0x20) { + ret = rte_cmp32(src_1, src_2); + n -= 0x20; + src_1 += 0x20; + src_2 += 0x20; + }Pardon me for butting in, but this seems incorrect for the first two cases listed above, as the function as written will only compare the first 128 or 64 bytes of each source and return the result. The pattern expressed in the 32 byte case appears more correct, as it compares the first 32 bytes and then lets later pieces of the function handle the smaller remaining bits of the sources. Also, if this function is to handle arbitrarily large source data, the 128 byte case needs to be in a loop.
Quoted 8 lines
Quoted 2 lines
What am I missing?
Current max hash key length supported is 64 bytes, hence no comparison is done after 64 bytes. 128 bytes comparison is added to measure performance only and there is no use-case as of now. With the current use-cases its not required but if there is a need to handle large arbitrary data upto 128 bytes it can be modified.
So on x86 let say
rte_memcmp(k1, k2, 65)might produce invalid results, right?
While on PPC will work as expected (as it calls memcpu underneath)?
That looks really weird to me.
If you plan to userte_memcmponly for hash comparisons, then probably
you should put it somewhere intolibrte_hashand name it accordingly:rte_hash_key_cmp()or something. And put a big comment around it, that it only works with particular lengths.
If you want it to be a generic function inside EAL, then it probably need to handle different lengths properly on all supported architectures.
KonstantinLet me just explain it here and probably add it to document as well.
rte_memcmpis notQuoted 8 lines
1. a replacement to memcmp
2. restricted to hash key comparison
rte_memcmpis1. optimized comparison for 16 to 128 bytes, v1 patch series had this support. Changed some of the logic in v2 due to concerns raised for unavailable use-cases beyond 64 bytes comparison.
From what I see in v2 it supposed to work correctly for len in [0,64] and len=128, right? Not sure I get it: so for v1 it was able to handle any length correctly, but then you removed it? If so, I wonder what was the reason? Make it faster?
Quoted 5 lines
My initial discussion was with Zhilong(John) from Intel and we decided to implement up to 128 bytes comparison and use
rte_hashandrte_lpm6as a candidate for testing. When I sent out v1 patch, Bruce comments were on use-case for 128 bytes comparison and was it really required? Hence I decided in v2 to support only up to 64 bytes and added 128 bytes only for performance measurement. Personally I think support for up to 128 bytes comparison is required, there might not be use-cases today but it will definitely be useful.
Ok, we don't have a real usage case for it now, but it still probably good to have it work with arbitrary key-length. Again, as Don suggested in another mail, we can have an optimised implementation
for particular sizes and fall back to slow-path (memcp) for all other cases.
Even if you'll decide to limit len to particular value (64/128), it is probably not very good to have a gap in between, as it exists now [65-127].
Quoted 25 lines
Another thing that looks strange to me:
While all rte_cmp*() uses actual data values for comparison results,rte_memcmp_remainder()return value depends not only on data values but also on data locations:(quoted diff) +3
-0+static inline int +rte_memcmp_remainder(const uint8_t *src_1u, const uint8_t *src_2u, size_t n) +{exit:
(quoted diff) +3
-0+ + return src_1u < src_2u ? -1 : 1; +}This is a bug and its not supposed to be there. I will fix it. Thanks for catching it.
If you just test for equal/not equal that doesn't really matter.
If this is supposed to be a 'proper' comparison function, then the result is sort of unpredictable.Quoted 6 lines
With minor tuning over the weekend I am able to get better performance for anything between 16 to 128 bytes comparison.
2. will be specific to DPDK i.e. currently all memcmp usage in DPDK are for equality or inequality hence "less than" or "greater than" implementation in
rte_memcmpdoesn't make sense and will be removed in subsequent patches, it will return 0 or 1 for equal/unequal cases.
Quoted 6 lines
If you don't plan your function to follow
memcmp()semantics and syntax, why to name itrte_memcmp()? I think that will make a lot of confusion around.
Why not to name it differently(and put a clear comment in the declaration of course)?Following memcmp semantics is not hard but there are no use-cases for it in DPDK currently. Keeping it specific to DPDK usage simplifies code as well. I can change the name to "
rte_compare" and add comments to the function. Will it work?
Yep, either rte_compare(), or as Don suggested rte_testequal() - both seems good to me.
Konstantin
Quoted 34 lines
Quoted 7 lines
rte_hashwill be the first candidate to move torte_memcmpand subsequentlyrte_lpm6which uses 16 bytes comparison will be movedLater on
RING_SIZEwhich uses large size for comparison will be moved. I am currently studying/understanding that logic and willmake
Quoted 1 line
changes to
rte_memcmpto support that.Sorry, didn't get you here.
Once
rte_hash,rte_lpm6changes and new compare function code are reviewed and accepted I plan to move to different components (RING_SIZEis currently defined to be from 256 to 16384 bytes) and memcmp function being used intest_ring,test_pmd_ringand other functions. I did not want to add all component changes into one patch series as it causes high review latency or patch series just dies down silently. Instead make patches small and incremental in every series, hope this clarifies. Thanks,
Ravi
KonstantinQuoted 14 lines
I don't want to make lot of changes in one shot and see that patch series die a slow death with no takers.
Thanks,
RaviQuoted 8 lines
Quoted 8 lines
--
Matt Laswell
infinite io, inc.
[email protected]
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to Ravi KerurQuoted 12 lines · Ravi Kerur
Quoted 9 lines · Don Provan
I probably shouldn't stick my nose into this, but I can't help myself.
An experienced programmer will tend to ignore the documentation for a routine named "
blahblah_memcmp" and just assume it functions like memcmp. Whether or not there's currently a use case in DPDK is completely irrelevant because as soon as there *is* a use case, some poor DPDK developer will try to userte_memcmpfor that and may or may not have a test case that reveals their mistake.
Quoted 4 lines
In general I agree with you. However, comparison is a hit(equal) or miss(unequal) is generally the case in networking. I haven't seen cases where "less than" or "greater than" has mattered.
It's useful when you need to make sure packets from both sides of a conversation go to the same processing queue/thread. Instead of hashing the 5-tuple from the packet as src.ip, dst.ip, src.dport, dst.dport, etc., you can use lesser.ip, higher.ip, lesser.sport, higher.dport, etc.
Very common when you are doing deep packet inspection.
Jay
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to LinhaifengQuoted 6 lines · Linhaifeng
Quoted 2 lines · Ravi Kerur
If you can wait until Thursday I will probably send v3 patch which will have full memcmp support.
Quoted 7 lines
Ok, I'd like to test it:)
Quoted 2 lines
In your program try with volatile pointer and see if it helps.
like "volatile
uint8_t*src, *dst" ?
uint8_t * volatile src
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to Ananyev, KonstantinHi Konstanin,
Quoted 160 lines · Konstantin
Hi Ravi,
Quoted 5 lines
-----Original Message-----
From: Ananyev, Konstantin
Sent: Wednesday, May 13, 2015 11:02 AM
To: Ananyev, Konstantin
Subject: FW: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSEinstructions.
Quoted 8 lines
From: Ravi Kerur [mailto:[email protected]]
Sent: Monday, May 11, 2015 9:47 PM
To: Ananyev, Konstantin
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSEinstructions.
Quoted 4 lines
Hi Konstantin,
On Mon, May 11, 2015 at 12:35 PM, Ananyev, Konstantin <
Quoted 9 lines · [email protected]> wrote:
Hi Ravi,
Quoted 6 lines
From: Ravi Kerur [mailto:[email protected]]
Sent: Monday, May 11, 2015 6:43 PM
To: Ananyev, Konstantin
Cc: Matt Laswell; [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSEinstructions.
Quoted 5 lines
Quoted 5 lines
Hi Konstantin,
On Mon, May 11, 2015 at 2:51 AM, Ananyev, Konstantin <
Quoted 8 lines · [email protected]> wrote:
Quoted 8 lines
Hi Ravi,
Quoted 6 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ravi Kerur
Sent: Friday, May 08, 2015 11:55 PM
To: Matt Laswell
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] Implement memcmp using AVX/SSEinstructions.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
On Fri, May 8, 2015 at 3:29 PM, Matt Laswell <[email protected]>
Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
On Fri, May 8, 2015 at 4:19 PM, Ravi Kerur <[email protected]>
Quoted 18 lines · wrote:
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
This patch replaces memcmp in
librte_hashwithrte_memcmpwhich is implemented with AVX/SSE instructions.(quoted diff) +14
-0+static inline int +rte_memcmp(const void *_src_1, const void *_src_2, size_t n) +{ + const uint8_t *src_1 = (const uint8_t *)_src_1; + const uint8_t *src_2 = (const uint8_t *)_src_2; + int ret = 0; + + if (n & 0x80) + return rte_cmp128(src_1, src_2); + + if (n & 0x40) + return rte_cmp64(src_1, src_2); + + if (n & 0x20) {Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Review
Quoted 7 lines
(quoted diff) +5
-0+ ret = rte_cmp32(src_1, src_2); + n -= 0x20; + src_1 += 0x20; + src_2 += 0x20; + }Pardon me for butting in, but this seems incorrect for the first
two cases
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
listed above, as the function as written will only compare the
first 128 or
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
64 bytes of each source and return the result. The pattern
expressed in
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
the 32 byte case appears more correct, as it compares the first 32
bytes
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
and then lets later pieces of the function handle the smaller
remaining
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
bits of the sources. Also, if this function is to handle
arbitrarily large
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
source data, the 128 byte case needs to be in a loop.
What am I missing?
Current max hash key length supported is 64 bytes, hence no
comparison is
Quoted 1 line
Quoted 1 line
Quoted 1 line
done after 64 bytes. 128 bytes comparison is added to measure
performance
Quoted 1 line
Quoted 1 line
Quoted 1 line
only and there is no use-case as of now. With the current use-cases
its not
Quoted 1 line
Quoted 1 line
Quoted 1 line
required but if there is a need to handle large arbitrary data upto
128
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
bytes it can be modified.
So on x86 let say
rte_memcmp(k1, k2, 65)might produce invalidresults, right?
Quoted 4 lines
Quoted 4 lines
While on PPC will work as expected (as it calls memcpu underneath)? That looks really weird to me.
If you plan to userte_memcmponly for hash comparisons, then probably you should put it somewhere intolibrte_hashand name it accordingly:
rte_hash_key_cmp()or something.Quoted 1 line
Quoted 1 line
And put a big comment around it, that it only works with particular
lengths.
Quoted 1 line
Quoted 1 line
If you want it to be a generic function inside EAL, then it probably
need to handle different lengths properly
Quoted 8 lines
Quoted 8 lines
on all supported architectures.
KonstantinLet me just explain it here and probably add it to document as well.
rte_memcmpis notQuoted 7 lines
Quoted 7 lines
1. a replacement to memcmp
2. restricted to hash key comparison
rte_memcmpis1. optimized comparison for 16 to 128 bytes, v1 patch series had this
support. Changed some of the logic in v2 due to concerns raised
Quoted 2 lines
Quoted 1 line
for unavailable use-cases beyond 64 bytes comparison.
From what I see in v2 it supposed to work correctly for len in [0,64]
and len=128, right?
Quoted 1 line
Not sure I get it: so for v1 it was able to handle any length correctly,
but then you removed it?
Quoted 3 lines
If so, I wonder what was the reason? Make it faster?
My initial discussion was with Zhilong(John) from Intel and we decided
to implement up to 128 bytes comparison and use
rte_hashQuoted 1 line
and
rte_lpm6as a candidate for testing. When I sent out v1 patch, Brucecomments were on use-case for 128 bytes comparison and
Quoted 1 line
was it really required? Hence I decided in v2 to support only up to 64
bytes and added 128 bytes only for performance measurement.
Quoted 1 line
Personally I think support for up to 128 bytes comparison is required,
there might not be use-cases today but it will definitely be
Quoted 1 line
useful.
Ok, we don't have a real usage case for it now, but it still probably good to have it work with arbitrary key-length.
Again, as Don suggested in another mail, we can have an optimised implementation
for particular sizes and fall back to slow-path (memcp) for all other cases.
Even if you'll decide to limit len to particular value (64/128), it is probably not very good to have a gap in between,
as it exists now [65-127].
Quoted 1 line
Agreed. I am almost done with
rte_memcmpmimicing memcmp and results look
ok to me. I am testing out on bsd and linux and will send out updated patch once i am done with testing.
Quoted 91 lines
Quoted 4 lines
Another thing that looks strange to me:
While all rte_cmp*() uses actual data values for comparison results,rte_memcmp_remainder()return value depends not only on data values butalso on data locations:
Review
Quoted 3 lines
(quoted diff) +2
-0+static inline int +rte_memcmp_remainder(const uint8_t *src_1u, const uint8_t *src_2u,
size_tn)Quoted 8 lines
+{
...
exit:(quoted diff) +3
-0+ + return src_1u < src_2u ? -1 : 1; +}This is a bug and its not supposed to be there. I will fix it. Thanks
for catching it.
Quoted 3 lines
If you just test for equal/not equal that doesn't really matter. If this is supposed to be a 'proper' comparison function, then the
result is sort of unpredictable.
Quoted 1 line
Quoted 1 line
With minor tuning over the weekend I am able to get better performance
for
Quoted 3 lines
Quoted 3 lines
anything between 16 to 128 bytes comparison.
2. will be specific to DPDK i.e. currently all memcmp usage in DPDK
are for equality or inequality hence "less than" or "greater than"
Quoted 1 line
Quoted 1 line
implementation in
rte_memcmpdoesn't make sense and will be removed insubsequent patches, it will return 0 or 1 for
Quoted 3 lines
Quoted 1 line
equal/unequal cases.
If you don't plan your function to follow
memcmp()semantics and syntax,why to name it
rte_memcmp()?Quoted 2 lines
I think that will make a lot of confusion around. Why not to name it differently(and put a clear comment in the
declaration of course)?
Quoted 2 lines
Following memcmp semantics is not hard but there are no use-cases for it
in DPDK currently. Keeping it specific to DPDK usage
Quoted 1 line
simplifies code as well. I can change the name to "
rte_compare" and addcomments to the function. Will it work?
Yep, either
rte_compare(), or as Don suggestedrte_testequal()- both seems good to me.Konstantin
Quoted 4 lines
Quoted 4 lines
rte_hashwill be the first candidate to move torte_memcmpandsubsequently
rte_lpm6which uses 16 bytes comparison will beQuoted 3 lines
Quoted 3 lines
moved
Later on
RING_SIZEwhich uses large size for comparison will be moved.I am currently studying/understanding that logic and will
Quoted 6 lines
make
Quoted 1 line
changes to
rte_memcmpto support that.Sorry, didn't get you here.
Once
rte_hash,rte_lpm6changes and new compare function code arereviewed and accepted I plan to move to different
Quoted 1 line
components (
RING_SIZEis currently defined to be from 256 to 16384bytes) and memcmp function being used in
test_ring,Quoted 1 line
test_pmd_ringand other functions. I did not want to add all componentchanges into one patch series as it causes high review latency
Quoted 1 line
or patch series just dies down silently. Instead make patches small and
incremental in every series, hope this clarifies.
Quoted 6 lines
Thanks,
Ravi
KonstantinQuoted 2 lines
I don't want to make lot of changes in one shot and see that patch
series die a slow death with no takers.
Quoted 12 lines
Quoted 12 lines
Thanks,
RaviQuoted 8 lines
Quoted 8 lines
--
Matt Laswell
infinite io, inc.
[email protected]
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to Jay RoletteQuoted 19 lines · Jay Rolette
Quoted 12 lines · Ravi Kerur
Quoted 9 lines · Don Provan
I probably shouldn't stick my nose into this, but I can't help myself.
An experienced programmer will tend to ignore the documentation for a routine named "
blahblah_memcmp" and just assume it functions like memcmp. Whether or not there's currently a use case in DPDK is completely irrelevant because as soon as there *is* a use case, some poor DPDK developer will try to userte_memcmpfor that and may or may not have a test case that reveals their mistake.Quoted 4 lines
In general I agree with you. However, comparison is a hit(equal) or miss(unequal) is generally the case in networking. I haven't seen cases where "less than" or "greater than" has mattered.
Quoted 7 lines
It's useful when you need to make sure packets from both sides of a conversation go to the same processing queue/thread. Instead of hashing the 5-tuple from the packet as src.ip, dst.ip, src.dport, dst.dport, etc., you can use lesser.ip, higher.ip, lesser.sport, higher.dport, etc.
Very common when you are doing deep packet inspection.
Thanks for sharing this information.
Quoted 3 lines
Jay
Re: [PATCH v2] Implement memcmp using AVX/SSE instructions.
In reply to Bruce RichardsonQuoted 18 lines · Bruce Richardson
Quoted 12 lines · Ravi Kerur
Quoted 9 lines · Don Provan
I probably shouldn't stick my nose into this, but I can't help myself.
An experienced programmer will tend to ignore the documentation for a routine named "
blahblah_memcmp" and just assume it functions like memcmp. Whether or not there's currently a use case in DPDK is completely irrelevant because as soon as there *is* a use case, some poor DPDK developer will try to userte_memcmpfor that and may or may not have a test case that reveals their mistake.Quoted 5 lines
In general I agree with you. However, comparison is a hit(equal) or miss(unequal) is generally the case in networking. I haven't seen cases where "less than" or "greater than" has mattered.
Quoted 7 lines
Agreed that == and != are the common operations. However, if that is what is returned from the function - and given other limitations on parameter sizes -
I agree with previous posters that this function needs to have a different name
torte_memcmpso as to avoid confusion.
I will be implementing complete memcmp itself, so probably I will retain same name.
Quoted 4 lines
/Bruce