4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <rte_cycles.h>
38 #include <rte_hash_crc.h>
39 #include <rte_launch.h>
40 #include <rte_malloc.h>
41 #include <rte_random.h>
42 #include <rte_spinlock.h>
47 * Check condition and return an error if true. Assumes that "handle" is the
48 * name of the hash structure pointer to be freed.
50 #define RETURN_IF_ERROR(cond, str, ...) do { \
52 printf("ERROR line %d: " str "\n", __LINE__, \
55 rte_hash_free(handle); \
60 #define RTE_APP_TEST_HASH_MULTIWRITER_FAILED 0
65 uint32_t nb_tsx_insertion
;
67 } tbl_multiwriter_test_params
;
69 const uint32_t nb_entries
= 16*1024*1024;
70 const uint32_t nb_total_tsx_insertion
= 15*1024*1024;
71 uint32_t rounded_nb_total_tsx_insertion
;
73 static rte_atomic64_t gcycles
;
74 static rte_atomic64_t ginsertions
;
79 test_hash_multiwriter_worker(__attribute__((unused
)) void *arg
)
82 uint32_t lcore_id
= rte_lcore_id();
83 uint64_t begin
, cycles
;
85 offset
= (lcore_id
- rte_get_master_lcore())
86 * tbl_multiwriter_test_params
.nb_tsx_insertion
;
88 printf("Core #%d inserting %d: %'"PRId64
" - %'"PRId64
"\n",
89 lcore_id
, tbl_multiwriter_test_params
.nb_tsx_insertion
,
90 offset
, offset
+ tbl_multiwriter_test_params
.nb_tsx_insertion
);
92 begin
= rte_rdtsc_precise();
95 i
< offset
+ tbl_multiwriter_test_params
.nb_tsx_insertion
;
97 if (rte_hash_add_key(tbl_multiwriter_test_params
.h
,
98 tbl_multiwriter_test_params
.keys
+ i
) < 0)
102 cycles
= rte_rdtsc_precise() - begin
;
103 rte_atomic64_add(&gcycles
, cycles
);
104 rte_atomic64_add(&ginsertions
, i
- offset
);
106 for (; i
< offset
+ tbl_multiwriter_test_params
.nb_tsx_insertion
; i
++)
107 tbl_multiwriter_test_params
.keys
[i
]
108 = RTE_APP_TEST_HASH_MULTIWRITER_FAILED
;
115 test_hash_multiwriter(void)
117 unsigned int i
, rounded_nb_total_tsx_insertion
;
118 static unsigned calledCount
= 1;
123 struct rte_hash_parameters hash_params
= {
124 .entries
= nb_entries
,
125 .key_len
= sizeof(uint32_t),
126 .hash_func
= rte_hash_crc
,
127 .hash_func_init_val
= 0,
128 .socket_id
= rte_socket_id(),
131 hash_params
.extra_flag
=
132 RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT
133 | RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD
;
135 hash_params
.extra_flag
=
136 RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD
;
138 struct rte_hash
*handle
;
139 char name
[RTE_HASH_NAMESIZE
];
141 const void *next_key
;
145 uint32_t duplicated_keys
= 0;
146 uint32_t lost_keys
= 0;
148 snprintf(name
, 32, "test%u", calledCount
++);
149 hash_params
.name
= name
;
151 handle
= rte_hash_create(&hash_params
);
152 RETURN_IF_ERROR(handle
== NULL
, "hash creation failed");
154 tbl_multiwriter_test_params
.h
= handle
;
155 tbl_multiwriter_test_params
.nb_tsx_insertion
=
156 nb_total_tsx_insertion
/ rte_lcore_count();
158 rounded_nb_total_tsx_insertion
= (nb_total_tsx_insertion
/
159 tbl_multiwriter_test_params
.nb_tsx_insertion
)
160 * tbl_multiwriter_test_params
.nb_tsx_insertion
;
162 rte_srand(rte_rdtsc());
164 keys
= rte_malloc(NULL
, sizeof(uint32_t) * nb_entries
, 0);
167 printf("RTE_MALLOC failed\n");
171 found
= rte_zmalloc(NULL
, sizeof(uint32_t) * nb_entries
, 0);
173 printf("RTE_ZMALLOC failed\n");
177 for (i
= 0; i
< nb_entries
; i
++)
180 tbl_multiwriter_test_params
.keys
= keys
;
181 tbl_multiwriter_test_params
.found
= found
;
183 rte_atomic64_init(&gcycles
);
184 rte_atomic64_clear(&gcycles
);
186 rte_atomic64_init(&ginsertions
);
187 rte_atomic64_clear(&ginsertions
);
189 /* Fire all threads. */
190 rte_eal_mp_remote_launch(test_hash_multiwriter_worker
,
192 rte_eal_mp_wait_lcore();
194 while (rte_hash_iterate(handle
, &next_key
, &next_data
, &iter
) >= 0) {
195 /* Search for the key in the list of keys added .*/
196 i
= *(const uint32_t *)next_key
;
197 tbl_multiwriter_test_params
.found
[i
]++;
200 for (i
= 0; i
< rounded_nb_total_tsx_insertion
; i
++) {
201 if (tbl_multiwriter_test_params
.keys
[i
]
202 != RTE_APP_TEST_HASH_MULTIWRITER_FAILED
) {
203 if (tbl_multiwriter_test_params
.found
[i
] > 1) {
207 if (tbl_multiwriter_test_params
.found
[i
] == 0) {
209 printf("key %d is lost\n", i
);
215 if (duplicated_keys
> 0) {
216 printf("%d key duplicated\n", duplicated_keys
);
221 printf("%d key lost\n", lost_keys
);
225 printf("No key corrupted during multiwriter insertion.\n");
227 unsigned long long int cycles_per_insertion
=
228 rte_atomic64_read(&gcycles
)/
229 rte_atomic64_read(&ginsertions
);
231 printf(" cycles per insertion: %llu\n", cycles_per_insertion
);
233 rte_free(tbl_multiwriter_test_params
.found
);
234 rte_free(tbl_multiwriter_test_params
.keys
);
235 rte_hash_free(handle
);
239 rte_free(tbl_multiwriter_test_params
.found
);
241 rte_free(tbl_multiwriter_test_params
.keys
);
243 rte_hash_free(handle
);
248 test_hash_multiwriter_main(void)
250 if (rte_lcore_count() == 1) {
251 printf("More than one lcore is required to do multiwriter test\n");
256 setlocale(LC_NUMERIC
, "");
259 if (!rte_tm_supported()) {
260 printf("Hardware transactional memory (lock elision) "
261 "is NOT supported\n");
263 printf("Hardware transactional memory (lock elision) "
266 printf("Test multi-writer with Hardware transactional memory\n");
269 if (test_hash_multiwriter() < 0)
273 printf("Test multi-writer without Hardware transactional memory\n");
275 if (test_hash_multiwriter() < 0)
281 REGISTER_TEST_COMMAND(hash_multiwriter_autotest
, test_hash_multiwriter_main
);