GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [improve] Preload mimalloc in the Alpine-based Pulsar image by lhotari · Pull Request #26791 · apache/pulsar

Conversation

@lhotari

@lhotari lhotari commented Sep 30, 2026

Copy link
Copy Markdown
Member

Main Issue: #26786

Motivation

Pulsar's default Docker image is based on Alpine Linux, whose C library is musl. musl's malloc (mallocng) serves every allocation of MMAP_THRESHOLD (131,052 bytes) or more with a new mmap, and frees it with munmap. The chunks that Netty's buffer allocators allocate and free at a steady load are of that size, so every chunk costs system calls. When it is first written, each of its 4 KB pages also costs a page fault. When it is freed, the munmap flushes the TLB of every core running a thread of the JVM. See #26786 for the details.

mimalloc keeps and reuses freed memory, instead of returning each large block to the operating system with munmap.

Modifications

  • A build stage for mimalloc. A separate stage of docker/pulsar/Dockerfile builds mimalloc from its release sources with CMake and Ninja:
    • a release build (-DCMAKE_BUILD_TYPE=Release);
    • no secure mode (-DMI_SECURE=OFF);
    • no CPU-specific optimizations, so the image runs on any CPU of its architecture (-DMI_NO_OPT_ARCH=ON);
    • the musl settings (-DMI_LIBC_MUSL=ON);
    • -D__USE_ISOC11.
  • Only the shared library goes into the image. The final image copies just the library, as /usr/lib/libmimalloc.so.3, and preloads it before gcompat: LD_PRELOAD=/usr/lib/libmimalloc.so.3:/lib/libgcompat.so.0. Setting LD_PRELOAD=/lib/libgcompat.so.0 restores musl's malloc.
  • The version. The mimalloc version is mimalloc in gradle/libs.versions.toml, 3.5.3, which the image build passes as the MIMALLOC_VERSION build argument; the Dockerfile's default for it is also 3.5.3.
  • The Wolfi image (Dockerfile.wolfi) is unchanged: it uses glibc.

Verifying this change

  • Make sure that the change passes the CI checks.

What was checked.

  • The image builds.
  • Every JVM of the cluster and the clients maps mimalloc in the performance tests: the brokers, the bookies, ZooKeeper, the gateways and the applications.
  • In the IoT telemetry performance scenarios of tests/performance, from unbatched 128-byte entries to 128 KB entries, mimalloc lowered the page faults of the Pulsar containers by 90 to 99 %.

Pending. More benchmarks are in progress: throughput, latency and CPU, with longer measurements at larger entry sizes. Checks are also running to see whether the page faults are a regression since Pulsar 4.2.x, by comparing Netty's adaptive allocator (#26720) with the pooled allocator, and batch reads enabled and disabled. This description will be updated with the results.

Does this pull request potentially affect one of the following parts:

If the box was checked, please highlight the changes

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment: the Alpine-based Docker image preloads mimalloc as the processes' malloc, in place of musl's.

This change was prepared with the assistance of Claude Code (claude-opus-5-5); I have reviewed and verified it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@lhotari
[improve][docker] Preload mimalloc in the Alpine-based Pulsar image
4f71ed5
musl's malloc serves every allocation of 128 KB or more with a new mmap and frees it with munmap, so the chunks that
Netty's buffer allocators allocate and free at a steady load cost system calls, a page fault for each page of every
new chunk, and TLB shootdowns (apache#26786). Build mimalloc (https://github.com/microsoft/mimalloc) in a separate stage and
preload it before gcompat in the Alpine-based image, in place of musl's malloc. The version is mimalloc in
gradle/libs.versions.toml, passed as the MIMALLOC_VERSION build argument, which defaults to 3.5.3.

Assisted-by: Claude Code (claude-opus-5-5)
@lhotari
lhotari requested review from Technoboy-, dao-jun, merlimat and nodece September 30, 2026 23:37
@merlimat merlimat changed the title [improve][docker] Preload mimalloc in the Alpine-based Pulsar image [improve] Preload mimalloc in the Alpine-based Pulsar image Sep 30, 2026
@lhotari

lhotari commented Oct 1, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Page-fault regression checks since 4.2.x

I ran the IoT telemetry max-rate scenario of tests/performance on the stock Alpine image without mimalloc. The setup was 1 broker and 3 bookies on one host, unbatched messages, and 2 interleaved runs per setting. The aim was to find out which changes since 4.2.x raised the broker's page faults. Every run delivered every message correctly.

Netty's adaptive allocator (#26720), compared with -Dpulsar.allocator.default.type=pooled in every Pulsar JVM:

Broker page faults/s, per run, and the change from pooled to adaptive in the runs' averages:

Message size Pooled Adaptive (default) Change
128 B 3,210 / 2,735 24,494 / 26,318 +755 %
8 KB 39,257 / 41,501 84,695 / 84,270 +109 %

With the adaptive allocator, the broker takes about 9 times as many page faults at 128 B and about 2 times as many at 8 KB. The bookies' page faults don't change. The extra page faults didn't show up as a CPU or throughput cost in these runs. At 128 B, the adaptive allocator used less broker CPU per message than the pooled one.

Batch reads with storage reads (managedLedgerBatchReadEnabled, which copies the entries read from storage into the cache): 8 KB messages, 3 topics, E=1/Qw=1/Qa=1, and managedLedgerCacheSizeMB=2, so that the broker read from the bookies. The bookies served about 7,400–8,100 read entries/s.

Batch reads Broker page faults/s
enabled (default) 89,658 / 93,123
disabled 54,377 / 50,960

With batch reads, the broker takes about 70 % more page faults. Throughput and CPU per message were the same within the runs' noise.

-Dio.netty.allocator.useCachedMagazinesForNonEventLoopThreads=true doesn't reduce the broker's page faults: they were the same at 128 B, and about 25 % higher at 8 KB.

Both changes add to the churn of chunks that musl's malloc maps and unmaps each time (#26786), which is what this PR's mimalloc avoids. More benchmarks of this PR are still pending.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

merlimat
merlimat reviewed Oct 1, 2026
View reviewed changes
Comment thread docker/pulsar/Dockerfile Resolved

Uh oh!

There was an error while loading. Please reload this page.

merlimat
merlimat approved these changes Oct 1, 2026
View reviewed changes
@lhotari
lhotari merged commit f18cab1 into apache:master Oct 1, 2026
43 of 44 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@lhotari
lhotari deleted the lh-improve-docker-mimalloc branch October 1, 2026 02:51
@lhotari lhotari mentioned this pull request Oct 1, 2026
[improve][broker] Netty's direct memory chunk churn causes 14x more broker page faults with musl's malloc in the Alpine-based image #26786
Closed
2 tasks done
@lhotari

lhotari commented Oct 1, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Measurements: before and after this change

The performance tests' iot-telemetry-max-rate scenario (500 producers publishing unbatched messages to one topic, one 20-consumer Key_Shared subscription, single-copy ledgers on 3 bookies), at 128 B, 8 KB, 64 KB and 128 KB. Before: the parent commit of this PR (99afb59057fc), after: this PR's merge commit (f18cab13a6f5); the two images differ only by the mimalloc preload. 3 interleaved runs per side and size, about 60 s measured (37 s at 128 B). Host: Intel i9-9980HK, 8 cores / 16 threads at a fixed 2.4 GHz. To keep the disk from limiting the large messages, both sides ran with the bookies' journals on a tmpfs and the write barriers of the host's file system disabled. Every run delivered every message without duplicates or ordering violations.

Means of 3 (min–max) Before (musl's malloc) After (mimalloc) Change
128 B throughput, msg/s 107.6k (106.3k–108.5k) 109.2k (108.3k–110.0k) +1.4 %
broker CPU s per M messages 52.2 (51.7–52.8) 53.0 (52.1–54.0) +1.4 %
all containers CPU s per M messages 120.6 (119.8–121.8) 120.9 (119.9–122.4) +0.3 %
broker page faults/s 26,242 1,156 −95.6 %
8 KB throughput, msg/s 49.6k (48.6k–50.7k) 51.3k (48.7k–53.4k) +3.5 %
broker CPU s per M messages 97.8 (96.3–99.7) 90.0 (88.2–91.7) −8.0 %
all containers CPU s per M messages 246.6 (244.2–249.4) 236.7 (234.1–239.0) −4.0 %
broker page faults/s 82,961 240 −99.7 %
64 KB throughput, msg/s 7,688 (7,622–7,760) 7,691 (7,675–7,724) 0.0 %
broker CPU s per M messages 348.5 (343.4–352.3) 348.3 (342.8–352.9) −0.1 %
all containers CPU s per M messages 1,145.5 (1,141.8–1,150.1) 1,100.4 (1,091.1–1,111.4) −3.9 %
broker page faults/s 18,683 57 −99.7 %
128 KB throughput, msg/s 3,868 (3,849–3,904) 3,823 (3,773–3,899) −1.1 %
broker CPU s per M messages 608.1 (595.5–615.4) 625.2 (617.2–632.2) +2.8 %
all containers CPU s per M messages 2,172.2 (2,147.0–2,188.5) 2,054.1 (2,036.0–2,065.1) −5.4 %
broker page faults/s 14,077 63 −99.5 %
  • Page faults: they fall by 95.6–99.7 % in the broker, and similarly in the bookies and the clients (for example the gateways' 71k/s at 64 KB and 129k/s at 128 KB to under 50/s).
  • 128 B: throughput +1.4 %, with the ranges nearly touching; CPU per message is unchanged.
  • 8 KB: broker CPU per message −8.0 %, with no overlap between the sides; throughput +3.5 %, within the spread.
  • 64 KB and 128 KB: throughput is the same; the CPU of all containers per message falls 4–5 %, almost all of it in the gateways, the producers' Pulsar clients (−13 % and −18 %), while the consuming applications' rises about 4 %. The broker's CPU per message is unchanged at 64 KB and 2.8 % higher at 128 KB, without overlap. At these sizes both sides are capped at about 500 MB/s of payload with the host 57–65 % busy, a limit that isn't the disk and that I'm looking into separately.
  • Memory: mimalloc keeps freed memory resident longer: the broker's resident set was 0.6 GB (128 B) to 1.8 GB (8 KB) larger, 5.2–6.4 GB → 5.8–8.3 GB, and each bookie's about 0.3 GB larger; the JVMs' direct memory use was the same. mimalloc 3.5.3 purges unused memory after purge_delay (1,000 ms), times arena_purge_mult (4) for its arenas. Checking MIMALLOC_PURGE_DELAY against this is a follow-up.
  • An earlier set with only about 10 s measured at 8 KB had one mimalloc run that slowed down for about 25 s in its measurement, while the broker's heap was still growing; with 60 s measured, the 8 KB runs were steady on both sides.

Broker page faults, and the change in throughput and CPU per message with mimalloc, by message size

Latency by percentile of each side's median run, per message size

[!NOTE]
Each chart has its own scale.

128 B, before (musl's malloc)

Latency by percentile, 128 B, before

128 B, after (mimalloc)

Latency by percentile, 128 B, after

8 KB, before (musl's malloc)

Latency by percentile, 8 KB, before

8 KB, after (mimalloc)

Latency by percentile, 8 KB, after

64 KB, before (musl's malloc)

Latency by percentile, 64 KB, before

64 KB, after (mimalloc)

Latency by percentile, 64 KB, after

128 KB, before (musl's malloc)

Latency by percentile, 128 KB, before

128 KB, after (mimalloc)

Latency by percentile, 128 KB, after

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lhotari lhotari mentioned this pull request Oct 1, 2026
[improve][broker] Default numIOThreads to half the available processors, at least 8 lhotari/pulsar#335
Closed
1 of 11 tasks
@lhotari lhotari added this to the 5.0.0 milestone Oct 1, 2026
Radiancebobo pushed a commit to Radiancebobo/pulsar that referenced this pull request Oct 8, 2026
@lhotari hechengbo
[improve] Preload mimalloc in the Alpine-based Pulsar image (apache#2…
1823e79
…6791)
@lhotari lhotari mentioned this pull request Oct 27, 2026
[improve][broker] Remove the inbound FlowControlHandler queue #27126
Open
2 of 10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@merlimat merlimat merlimat approved these changes
@nodece nodece Awaiting requested review from nodece
@dao-jun dao-jun Awaiting requested review from dao-jun
@Technoboy- Technoboy- Awaiting requested review from Technoboy-

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

5.0.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@lhotari @merlimat
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.