{
struct cds_ja_shadow_node *shadow_node =
caa_container_of(head, struct cds_ja_shadow_node, head);
+ assert(!shadow_node->is_root);
free(shadow_node->node);
free(shadow_node->lock);
free(shadow_node);
*/
ret = cds_lfht_del(ht, lookup_node);
if (!ret) {
- if (flags & RCUJA_SHADOW_CLEAR_FREE_NODE) {
+ if ((flags & RCUJA_SHADOW_CLEAR_FREE_NODE)
+ && !shadow_node->is_root) {
if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) {
flavor->update_call_rcu(&shadow_node->head,
free_shadow_node_and_node_and_lock);
ret = cds_lfht_del(ht, &shadow_node->ht_node);
if (!ret) {
- assert((flags & RCUJA_SHADOW_CLEAR_FREE_NODE)
- && (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK));
- flavor->update_call_rcu(&shadow_node->head,
- free_shadow_node_and_node_and_lock);
+ if ((flags & RCUJA_SHADOW_CLEAR_FREE_NODE)
+ && !shadow_node->is_root) {
+ if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node_and_node_and_lock);
+ } else {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node_and_node);
+ }
+ } else {
+ if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node_and_lock);
+ } else {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node);
+ }
+ }
}
lockret = pthread_mutex_unlock(shadow_node->lock);
assert(!lockret);
#define JA_PTR_MASK (~JA_TYPE_MASK)
#define JA_ENTRY_PER_NODE 256UL
-#define JA_BITS_PER_BYTE 3
+#define JA_LOG2_BITS_PER_BYTE 3U
+#define JA_BITS_PER_BYTE (1U << JA_LOG2_BITS_PER_BYTE)
#define JA_MAX_DEPTH 9 /* Maximum depth, including leafs */
assert(type->type_class == RCU_JA_PIGEON);
child_node_flag = &((struct cds_ja_inode_flag **) node->u.data)[n];
- if (caa_unlikely(child_node_flag_ptr))
+ dbg_printf("ja_pigeon_node_get_nth child_node_flag_ptr %p\n",
+ child_node_flag);
+ if (caa_unlikely(child_node_flag_ptr) && *child_node_flag)
*child_node_flag_ptr = child_node_flag;
return rcu_dereference(*child_node_flag);
}
}
retry: /* for fallback */
- dbg_printf("Recompact to type %d\n", new_type_index);
+ dbg_printf("Recompact from type %d to type %d\n",
+ old_type_index, new_type_index);
new_type = &ja_types[new_type_index];
new_node = alloc_cds_ja_node(new_type);
if (!new_node)
iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1)));
node_flag = ja_node_get_nth(node_flag, NULL,
iter_key);
+ dbg_printf("cds_ja_lookup iter key lookup %u finds node_flag %p\n",
+ (unsigned int) iter_key, node_flag);
if (!ja_node_ptr(node_flag))
return NULL;
}
struct cds_ja_inode_flag *created_nodes[JA_MAX_DEPTH];
int nr_created_nodes = 0;
- dbg_printf("Attach node at level %u\n", level);
+ dbg_printf("Attach node at level %u (node %p, node_flag %p)\n",
+ level, node, node_flag);
assert(node);
shadow_node = rcuja_shadow_lookup_lock(ja->ht, node);
for (i = 1; i < tree_depth; i++) {
uint8_t iter_key;
+ dbg_printf("cds_ja_add iter node_flag_ptr %p node_flag %p\n",
+ *node_flag_ptr, node_flag);
if (!ja_node_ptr(node_flag)) {
ret = ja_attach_node(ja, node_flag_ptr,
parent_node_flag, parent2_node_flag,
node_flag = ja_node_get_nth(node_flag,
&node_flag_ptr,
iter_key);
+ dbg_printf("cds_ja_add iter key lookup %u finds node_flag %p node_flag_ptr %p\n",
+ (unsigned int) iter_key, node_flag, *node_flag_ptr);
}
/*
* level, or chain it if key is already present.
*/
if (!ja_node_ptr(node_flag)) {
+ dbg_printf("cds_ja_add last node_flag_ptr %p node_flag %p\n",
+ *node_flag_ptr, node_flag);
ret = ja_attach_node(ja, node_flag_ptr, parent_node_flag,
parent2_node_flag, key, i, new_node);
} else {
/* ja->root is NULL */
/* tree_depth 0 is for pointer to root node */
- ja->tree_depth = (key_bits >> JA_BITS_PER_BYTE) + 1;
+ ja->tree_depth = (key_bits >> JA_LOG2_BITS_PER_BYTE) + 1;
assert(ja->tree_depth <= JA_MAX_DEPTH);
ja->ht = rcuja_create_ht(flavor);
if (!ja->ht)
ret = -ENOMEM;
goto ht_node_error;
}
+ root_shadow_node->is_root = 1;
return ja;
#define _GNU_SOURCE
#include "test_urcu_ja.h"
#include <inttypes.h>
+#include <stdint.h>
DEFINE_URCU_TLS(unsigned int, rand_lookup);
DEFINE_URCU_TLS(unsigned long, nr_add);
printf("\n\n");
}
+
+static
+int test_8bit_key(void)
+{
+ int ret;
+ uint64_t key;
+
+ /* Test with 8-bit key */
+ test_ja = cds_ja_new(8);
+ if (!test_ja) {
+ printf("Error allocating judy array.\n");
+ return -1;
+ }
+
+ /* Add keys */
+ printf("Test #1: add keys (8-bit).\n");
+ for (key = 0; key < 200; key++) {
+ struct ja_test_node *node =
+ calloc(sizeof(*node), 1);
+
+ ja_test_node_init(node, key);
+ rcu_read_lock();
+ ret = cds_ja_add(test_ja, key, &node->node);
+ rcu_read_unlock();
+ if (ret) {
+ fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
+ ret, key);
+ assert(0);
+ }
+ }
+ printf("OK\n");
+
+ printf("Test #2: successful key lookup (8-bit).\n");
+ for (key = 0; key < 200; key++) {
+ struct cds_hlist_head *head;
+
+ rcu_read_lock();
+ head = cds_ja_lookup(test_ja, key);
+ if (!head) {
+ fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
+ assert(0);
+ }
+ rcu_read_unlock();
+ }
+ printf("OK\n");
+ printf("Test #3: unsuccessful key lookup (8-bit).\n");
+ for (key = 200; key < 240; key++) {
+ struct cds_hlist_head *head;
+
+ rcu_read_lock();
+ head = cds_ja_lookup(test_ja, key);
+ if (head) {
+ fprintf(stderr,
+ "Error unexpected lookup node %" PRIu64 "\n",
+ key);
+ assert(0);
+ }
+ rcu_read_unlock();
+ }
+ printf("OK\n");
+
+ ret = cds_ja_destroy(test_ja);
+ if (ret) {
+ fprintf(stderr, "Error destroying judy array\n");
+ return -1;
+ }
+ return 0;
+}
+
+static
+int test_16bit_key(void)
+{
+ int ret;
+ uint64_t key;
+
+ /* Test with 16-bit key */
+ test_ja = cds_ja_new(16);
+ if (!test_ja) {
+ printf("Error allocating judy array.\n");
+ return -1;
+ }
+
+ /* Add keys */
+ printf("Test #1: add keys (16-bit).\n");
+ //for (key = 0; key < 10000; key++) {
+ for (key = 0; key < 65536; key+=256) {
+ struct ja_test_node *node =
+ calloc(sizeof(*node), 1);
+
+ ja_test_node_init(node, key);
+ rcu_read_lock();
+ ret = cds_ja_add(test_ja, key, &node->node);
+ rcu_read_unlock();
+ if (ret) {
+ fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
+ ret, key);
+ assert(0);
+ }
+ }
+ printf("OK\n");
+
+ printf("Test #2: successful key lookup (16-bit).\n");
+ //for (key = 0; key < 10000; key++) {
+ for (key = 0; key < 65536; key+=256) {
+ struct cds_hlist_head *head;
+
+ rcu_read_lock();
+ head = cds_ja_lookup(test_ja, key);
+ if (!head) {
+ fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
+ assert(0);
+ }
+ rcu_read_unlock();
+ }
+ printf("OK\n");
+ printf("Test #3: unsuccessful key lookup (16-bit).\n");
+ for (key = 11000; key <= 11002; key++) {
+ struct cds_hlist_head *head;
+
+ rcu_read_lock();
+ head = cds_ja_lookup(test_ja, key);
+ if (head) {
+ fprintf(stderr,
+ "Error unexpected lookup node %" PRIu64 "\n",
+ key);
+ assert(0);
+ }
+ rcu_read_unlock();
+ }
+ printf("OK\n");
+
+ ret = cds_ja_destroy(test_ja);
+ if (ret) {
+ fprintf(stderr, "Error destroying judy array\n");
+ return -1;
+ }
+ return 0;
+}
+
+static
+int test_sparse_key(unsigned int bits)
+{
+ int ret;
+ uint64_t key, max_key;
+ int zerocount;
+
+ if (bits == 64)
+ max_key = UINT64_MAX;
+ else
+ max_key = (1ULL << bits) - 1;
+
+ printf("Sparse key test begins for %u-bit keys\n", bits);
+ /* Test with 16-bit key */
+ test_ja = cds_ja_new(bits);
+ if (!test_ja) {
+ printf("Error allocating judy array.\n");
+ return -1;
+ }
+
+ /* Add keys */
+ printf("Test #1: add keys (%u-bit).\n", bits);
+ zerocount = 0;
+ for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
+ struct ja_test_node *node =
+ calloc(sizeof(*node), 1);
+
+ ja_test_node_init(node, key);
+ rcu_read_lock();
+ ret = cds_ja_add(test_ja, key, &node->node);
+ rcu_read_unlock();
+ if (ret) {
+ fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
+ ret, key);
+ assert(0);
+ }
+ if (key == 0)
+ zerocount++;
+ }
+ printf("OK\n");
+
+ printf("Test #2: successful key lookup (%u-bit).\n", bits);
+ zerocount = 0;
+ for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
+ struct cds_hlist_head *head;
+
+ rcu_read_lock();
+ head = cds_ja_lookup(test_ja, key);
+ if (!head) {
+ fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
+ assert(0);
+ }
+ rcu_read_unlock();
+ if (key == 0)
+ zerocount++;
+ }
+ printf("OK\n");
+ if (bits > 8) {
+ printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits);
+ zerocount = 0;
+ for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
+ struct cds_hlist_head *head;
+
+ rcu_read_lock();
+ head = cds_ja_lookup(test_ja, key + 42);
+ if (head) {
+ fprintf(stderr,
+ "Error unexpected lookup node %" PRIu64 "\n",
+ key + 42);
+ assert(0);
+ }
+ rcu_read_unlock();
+ if (key == 0)
+ zerocount++;
+ }
+ printf("OK\n");
+ }
+
+ ret = cds_ja_destroy(test_ja);
+ if (ret) {
+ fprintf(stderr, "Error destroying judy array\n");
+ return -1;
+ }
+ printf("Test ends\n");
+
+ return 0;
+}
+
+
int main(int argc, char **argv)
{
int err;
printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
}
- /* Test with 8-bit key */
- test_ja = cds_ja_new(8);
- if (!test_ja) {
- printf("Error allocating judy array.\n");
- return -1;
- }
+ rcu_register_thread();
- /* Add keys */
- printf("Test #1: add keys (8-bit).\n");
- for (key = 0; key < 200; key++) {
- struct ja_test_node *node =
- calloc(sizeof(*node), 1);
+ printf("Test start.\n");
- ja_test_node_init(node, key);
- rcu_read_lock();
- ret = cds_ja_add(test_ja, key, &node->node);
- rcu_read_unlock();
+ for (i = 0; i < 3; i++) {
+ ret = test_8bit_key();
if (ret) {
- fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
- ret, key);
- assert(0);
+ return ret;
}
+ rcu_quiescent_state();
}
- printf("OK\n");
+ ret = test_16bit_key();
+ if (ret) {
+ return ret;
+ }
+ rcu_quiescent_state();
- printf("Test #2: successful key lookup (8-bit).\n");
- for (key = 0; key < 200; key++) {
- struct cds_hlist_head *head;
+ ret = test_sparse_key(8);
+ if (ret) {
+ return ret;
+ }
+ rcu_quiescent_state();
- rcu_read_lock();
- head = cds_ja_lookup(test_ja, key);
- if (!head) {
- fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
- assert(0);
- }
- rcu_read_unlock();
+ ret = test_sparse_key(16);
+ if (ret) {
+ return ret;
}
- printf("OK\n");
- printf("Test #3: unsuccessful key lookup (8-bit).\n");
- for (key = 200; key < 240; key++) {
- struct cds_hlist_head *head;
+ rcu_quiescent_state();
- rcu_read_lock();
- head = cds_ja_lookup(test_ja, key);
- if (head) {
- fprintf(stderr,
- "Error unexpected lookup node %" PRIu64 "\n",
- key);
- assert(0);
- }
- rcu_read_unlock();
+ ret = test_sparse_key(32);
+ if (ret) {
+ return ret;
}
- printf("OK\n");
+ rcu_quiescent_state();
- ret = cds_ja_destroy(test_ja);
+ ret = test_sparse_key(64);
if (ret) {
- fprintf(stderr, "Error destroying judy array\n");
- return -1;
+ return ret;
}
+ rcu_quiescent_state();
+
printf("Test end.\n");
+ rcu_unregister_thread();
return 0;
#if 0