* tables and machines lacking per-cpu data suppport.
*/
#define COUNT_COMMIT_ORDER 10
+#define DEFAULT_SPLIT_COUNT_MASK 0xFUL
#define CHAIN_LEN_TARGET 1
#define CHAIN_LEN_RESIZE_THRESHOLD 3
static
void cds_lfht_resize_lazy(struct cds_lfht *ht, unsigned long size, int growth);
-/*
- * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
- * available, then we support hash table item accounting.
- * In the unfortunate event the number of CPUs reported would be
- * inaccurate, we use modulo arithmetic on the number of CPUs we got.
- */
-#if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
-
static
void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
unsigned long count);
static long nr_cpus_mask = -1;
static long split_count_mask = -1;
+#if defined(HAVE_SYSCONF)
static void ht_init_nr_cpus_mask(void)
{
long maxcpus;
maxcpus = 1UL << get_count_order_ulong(maxcpus);
nr_cpus_mask = maxcpus - 1;
}
+#else /* #if defined(HAVE_SYSCONF) */
+static void ht_init_nr_cpus_mask(void)
+{
+ nr_cpus_mask = -2;
+}
+#endif /* #else #if defined(HAVE_SYSCONF) */
static
struct ht_items_count *alloc_split_items_count(void)
if (nr_cpus_mask == -1) {
ht_init_nr_cpus_mask();
- split_count_mask = nr_cpus_mask;
+ if (nr_cpus_mask < 0)
+ split_count_mask = DEFAULT_SPLIT_COUNT_MASK;
+ else
+ split_count_mask = nr_cpus_mask;
}
- if (split_count_mask < 0)
- return NULL;
- else
- return calloc(split_count_mask + 1, sizeof(*count));
+ assert(split_count_mask >= 0);
+ return calloc(split_count_mask + 1, sizeof(*count));
}
static
poison_free(count);
}
+#if defined(HAVE_SCHED_GETCPU)
static
-int ht_get_split_count_index(void)
+int ht_get_split_count_index(unsigned long hash)
{
int cpu;
assert(split_count_mask >= 0);
cpu = sched_getcpu();
if (unlikely(cpu < 0))
- return cpu;
+ return hash & split_count_mask;
else
return cpu & split_count_mask;
}
+#else /* #if defined(HAVE_SCHED_GETCPU) */
+static
+int ht_get_split_count_index(unsigned long hash)
+{
+ return hash & split_count_mask;
+}
+#endif /* #else #if defined(HAVE_SCHED_GETCPU) */
static
-void ht_count_add(struct cds_lfht *ht, unsigned long size)
+void ht_count_add(struct cds_lfht *ht, unsigned long size, unsigned long hash)
{
unsigned long split_count;
int index;
if (unlikely(!ht->split_count))
return;
- index = ht_get_split_count_index();
- if (unlikely(index < 0))
- return;
+ index = ht_get_split_count_index(hash);
split_count = uatomic_add_return(&ht->split_count[index].add, 1);
if (unlikely(!(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
long count;
}
static
-void ht_count_del(struct cds_lfht *ht, unsigned long size)
+void ht_count_del(struct cds_lfht *ht, unsigned long size, unsigned long hash)
{
unsigned long split_count;
int index;
if (unlikely(!ht->split_count))
return;
- index = ht_get_split_count_index();
- if (unlikely(index < 0))
- return;
+ index = ht_get_split_count_index(hash);
split_count = uatomic_add_return(&ht->split_count[index].del, 1);
if (unlikely(!(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
long count;
}
}
-#else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
-
-static const long nr_cpus_mask = -2;
-static const long split_count_mask = -2;
-
-static
-struct ht_items_count *alloc_split_items_count(void)
-{
- return NULL;
-}
-
-static
-void free_split_items_count(struct ht_items_count *count)
-{
-}
-
-static
-void ht_count_add(struct cds_lfht *ht, unsigned long size)
-{
-}
-
-static
-void ht_count_del(struct cds_lfht *ht, unsigned long size)
-{
-}
-
-#endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
-
-
static
void check_resize(struct cds_lfht *ht, unsigned long size, uint32_t chain_len)
{
size = rcu_dereference(ht->t.size);
_cds_lfht_add(ht, size, node, NULL, 0);
- ht_count_add(ht, size);
+ ht_count_add(ht, size, hash);
}
struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
size = rcu_dereference(ht->t.size);
_cds_lfht_add(ht, size, node, &iter, 0);
if (iter.node == node)
- ht_count_add(ht, size);
+ ht_count_add(ht, size, hash);
return iter.node;
}
for (;;) {
_cds_lfht_add(ht, size, node, &iter, 0);
if (iter.node == node) {
- ht_count_add(ht, size);
+ ht_count_add(ht, size, hash);
return NULL;
}
int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter)
{
- unsigned long size;
+ unsigned long size, hash;
int ret;
size = rcu_dereference(ht->t.size);
ret = _cds_lfht_del(ht, size, iter->node, 0);
- if (!ret)
- ht_count_del(ht, size);
+ if (!ret) {
+ hash = bit_reverse_ulong(iter->node->p.reverse_hash);
+ ht_count_del(ht, size, hash);
+ }
return ret;
}
}
}
-#if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
-
static
void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
unsigned long count)
CMM_STORE_SHARED(ht->t.resize_initiated, 1);
}
}
-
-#endif