Skip to content

5.3.1 couchbase#8

Merged
jimwwalker merged 5 commits into
couchbasedeps:5.3.1-couchbasefrom
jimwwalker:5.3.1-couchbase
Jul 6, 2026
Merged

5.3.1 couchbase#8
jimwwalker merged 5 commits into
couchbasedeps:5.3.1-couchbasefrom
jimwwalker:5.3.1-couchbase

Conversation

@jimwwalker

Copy link
Copy Markdown

Create a 5.3.1 couchbase branch copying the customisation from 5.3.0-couchbase

veselink1 and others added 5 commits July 3, 2026 11:01
Make the file autoconf-generated. In the follow-up change, the
representation of the mallocx flags will be made configurable at
compile-time.

(cherry picked from commit 82caac4)
In the Couchbase Server Data Service (KV Engine), we use one tcache per
arena, per thread (multipies).

We have one arena per bucket, and we currently support 30 buckets, but
might eventually bump this up to 100 buckets per instance.

However, because we allocate a tcache per thread as well, the number of
tcache which we use ends up being num-buckets x num-threads. Tcaches are
allocated when a thread decides to use a bucket's arena, allocating
tcaches lazily, so not all threads will need a tcache, but most will.

jemalloc can automatically allocate and make use of a tcache, but we've
found that can result in incorrect accounting of memory stats per-arena,
which is a deal-breaker for us, because we rely on some of these stats
to calculate per-arena memory fragmentation.

The old limit on the number of tcaches was largely dependant on the
representation of the tcache ID in the 32-bit flags through which it was
specified to je_mallocx.

---

Flag bits:
a: arena
t: tcache
.: configurable, dependent on --with-lg-tcache-limit:
   The higher this value, the more of the configurable bits will be teated as
   tcache bits (the rest are arena bits).
0: unused
z: zero
n: alignment

Old representation:
  aaaaaaaa aaaatttt tttttttt 0znnnnnn
New representation:
  aaaaaaa. ........ ....tttt 0znnnnnn

Before this change, we had 12 bits for the arena index, 12 bits for the
tcache ID, giving us 4094 (tcache 0 and 1 reserved) tcaches and 4096
arenas.

By using more bits from the arena index representation, we can increase
the representation of the tcache ID to be wider/narrower, allowing up
to 2^17 valid tcache IDs to be specified (but 0 and 1 are reserved by
jemalloc). In this example, however, the number of areans falls to 128.

(cherry picked from commit 8903343)
As seen in our tests when we override the global new allocator
and perform allocations from the context we're cleaning up
thread local data we may end up calling je_mallocx and
explicitly specify arena 0 which cause jemalloc to fail an
assert (before it explicitly change the allocation to use
arena 0).

This asssert is currently blocking our upgrade of folly,
and the effect of allowing arena 0 to be specified is
harmless in this context (as jemalloc would change it
to 0 on the next statement)

See jemalloc#2791
See https://jira.issues.couchbase.com/browse/MB-65040

(cherry picked from commit 13f495b)
On Windows, we have seen that the tls callback is called before C++
thread_local destructors run. This means that malloc_tsd_dalloc is
called before potential further alloc/free is done, leaving us with a
dangling tsd_t returned by tsd_wrapper_get().

In order to workaround this, first we need to reset the tls storage to
NULL. It can now get re-created if needed. Also, provide a new exported
symbol je_tls_cleanup() which allows users which alloc/free at
thread_local destruction to manually cleanup a potential re-created
tsd_t.

The contract for the je_thread_cleanup() method is that it can be called
at any point of the thread lifetime, and it will clear the thread tsd_t.
The tsd_t will be re-created automatically if required again. Calling
je_thread_cleanup() at the end of the thread lifetime ensures that there
is not a memory leak in threads which allocate during thread exit.

(cherry picked from commit fc0eda6)
Otherwise, the symbol name is not correct on macOS.

Before:
  nm -gU lib/libjemalloc.a | grep je_th
  0000000000006c74 T _je_je_thread_cleanup
  0000000000000818 D _je_thp_mode_names

After:
  nm -gU lib/libjemalloc.a | grep je_th
  0000000000006c74 T _je_thread_cleanup
  0000000000000818 D _je_thp_mode_names
(cherry picked from commit 59aa12e)
@jimwwalker
jimwwalker marked this pull request as ready for review July 6, 2026 12:28
@jimwwalker
jimwwalker merged commit 7b51cc2 into couchbasedeps:5.3.1-couchbase Jul 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants