Skip to content

lib: lock-free hash table - #23247

Open
eqvinox wants to merge 5 commits into
FRRouting:masterfrom
opensourcerouting:atomhash
Open

lib: lock-free hash table#23247
eqvinox wants to merge 5 commits into
FRRouting:masterfrom
opensourcerouting:atomhash

Conversation

@eqvinox

@eqvinox eqvinox commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Maybe I should write 52 more lines of comments for atomhash.c, just so I can claim it's more comments than code 😆 … it does have a HIC SUNT DRACONES 🐉.

Language                     files          blank        comment           code
-------------------------------------------------------------------------------
C                                1            226            648            699

In seriousness though, the documentation is in the last commit.

In a galaxy far, far in the future, at the final frontier, this will be the backing for deduplicating ("interning") the various crap we have in BGP (communities, aspaths, attrs, etc.), and in the same vein allows implementing lock-free structure refcounts.

Anyway, it completed 50000 test cycles (20M ops ea) on ARM and 60000 test cycles (10M ops ea) on ppc64. (And another few times that during a bughunt for a rather nasty bug — but on the plus side the test did find that.)

The lock-free hash table test code cycles through memory rapidly and
tries to hit race conditions while doing so.  To make that work, it
eschews regular RCU memory management and does its own accounting on
what may or may not still be in use.  To do that correctly, it needs the
RCU tail position, i.e. what's guaranteed to be no longer in use.

(`rcu_stats->seq_head - seq_delta` is close, but that whole struct and
function are only intended for display purposes, with no consistency
guarantee.  The test needs the guarantee, otherwise you get random
failures and can't be sure if it was just a glitch.)

Signed-off-by: David 'equinox' Lamparter <equinox@opensourcerouting.org>
@frrbot frrbot Bot added libfrr tests Topotests, make check, etc labels Sep 3, 2026
@greptile-apps

This comment was marked as low quality.

Comment thread lib/atomhash.c
Comment thread tests/lib/test_atomhash.c
Comment on lines +1072 to +1078
#if 0
/* pop() cannot easily be tested :( */
static bool test_pop(unsigned int r)
{
return false;
}
#endif

This comment was marked as low quality.

This provides, well, a lock-free hash table.  (To be used with RCU.)

Unlike a lot of other implementations, this one can shrink its hash
"buckets" without a lock.  To my knowledge, it is at this point the only
lock-free single-CAS hash table to have that capability.

Tests & documentation are in followup commits.

Signed-off-by: David 'equinox' Lamparter <equinox@opensourcerouting.org>
Does what the function name says, permutes the items in an array into a
random order.

Signed-off-by: David 'equinox' Lamparter <equinox@opensourcerouting.org>
@eqvinox
eqvinox force-pushed the atomhash branch 2 times, most recently from 488ef17 to dcc3346 Compare September 3, 2026 17:03
Since this test is probabilistic in nature AND must preferentially be
run on machines with weak memory models, it is not part of the regular
test suite.  It's intended to be run manually, in loops, on systems
specifically picked out for the purpose.  The longer it runs, the more
reliable the result becomes.

It also has a rather large number of tunables, which in all honesty must
be understood by reading the code and tuned in a few ways to test
specific things.

Signed-off-by: David 'equinox' Lamparter <equinox@opensourcerouting.org>
Docs for `DECLARE_ATOMHASH`, 'nuff said.

Signed-off-by: David 'equinox' Lamparter <equinox@opensourcerouting.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libfrr master size/XXL tests Topotests, make check, etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant