Repository navigation
feat: EXC-2012: Use THP for host memory allocation - #4671
berestovskyy wants to merge 3 commits into
Conversation
3364c75 to
f3a470d
Compare
The transparent huge pages improve host memory performance, resulting in a ~30% improvement across benchmarks without regressions. Some benchmarks (like `query_write_bwd_1gb_step_8_page_delta`) reduce their runtime by more than 700 ms (more than 60% of the runtime). Some benchmarks (like `query_read_bwd_1gb_step_4kb_new_allocation`) are improved by more than 90% from 226 ms down to 7 ms. ``` = 5d40b0f..4b15a2867f: Embedders Heap total time: 96426 ms (improved by -43022.6 ms / -30.9%) Top 10 by time: - query/wasm32_query_write_bwd_1gb_step_8_page_delta time improved by -729.9 ms (-63.1%) - query/wasm32_query_read_bwd_1gb_step_8_page_delta time improved by -729.0 ms (-63.8%) - query/wasm64_query_read_fwd_1gb_step_8_page_delta time improved by -728.8 ms (-63.0%) - query/wasm64_query_read_bwd_1gb_step_4kb_page_delta time improved by -725.2 ms (-70.9%) - query/wasm32_query_write_fwd_1gb_step_4kb_page_delta time improved by -724.0 ms (-70.8%) - query/wasm32_query_write_bwd_1gb_step_4kb_page_delta time improved by -721.7 ms (-70.7%) - query/wasm32_query_read_bwd_1gb_step_4kb_page_delta time improved by -718.4 ms (-70.5%) - query/wasm64_query_read_bwd_1gb_step_8_page_delta time improved by -717.2 ms (-62.3%) - query/wasm32_query_read_fwd_1gb_step_8_page_delta time improved by -716.7 ms (-63.4%) - query/wasm64_query_write_fwd_1gb_step_4kb_page_delta time improved by -709.9 ms (-70.4%) Top 10 by percentage: - update/wasm64_update_read_bwd_1gb_step_4kb_new_allocation time improved by -96.9% (-219.4 ms) - query/wasm64_query_read_bwd_1gb_step_4kb_new_allocation time improved by -96.9% (-220.5 ms) - query/wasm64_query_read_fwd_1gb_step_4kb_new_allocation time improved by -96.9% (-221.0 ms) - update/wasm64_update_read_fwd_1gb_step_4kb_new_allocation time improved by -96.8% (-219.1 ms) - update/wasm32_update_read_fwd_1gb_step_4kb_new_allocation time improved by -96.7% (-219.0 ms) - update/wasm32_update_read_bwd_1gb_step_4kb_new_allocation time improved by -96.7% (-219.5 ms) - query/wasm32_query_read_bwd_1gb_step_4kb_new_allocation time improved by -96.7% (-219.9 ms) - query/wasm32_query_read_fwd_1gb_step_4kb_new_allocation time improved by -96.7% (-222.5 ms) - update/wasm64_update_read_bwd_1gb_step_16kb_new_allocation time improved by -91.7% (-66.2 ms) - update/wasm32_update_read_bwd_1gb_step_16kb_new_allocation time improved by -91.6% (-66.6 ms) ```
f3a470d to
87af899
Compare
|
My main concern is I am not sure if this is harmful if we do random accesses to the canister's memory. All benchmarks are are a variation of reading lots of consecutive or almost consecutive data, and I am not surprised that they benefit from large pages. Could we add some benchmarks where we do random accesses? Maybe one where we do only one or two accesses, and another one where we do many accesses but without any order. A |
| #[cfg(all(target_os = "linux", target_arch = "x86_64"))] | ||
| unsafe { | ||
| use nix::sys::mman::{madvise, MmapAdvise}; | ||
| madvise(start, size_in_bytes, MmapAdvise::MADV_HUGEPAGE).unwrap_or_else(|err| { |
There was a problem hiding this comment.
I don't think that I understand fully how this interacts with what we do in apply_memory_instructions.
If there is a base file, we first mmap that as a whole. Afterwards, we prefetch relatively small chunks of up to 4Mb (512 pages forward and 512 pages backwards), which means we check if there are pages in either the page delta or an overlay, and apply them on top.
But this raises two questions:
- What happens exactly with the huge pages? Does the fact that we mmap first a large file mean that we end up with huge pages either way? Or do we end up with small pages if we have a few deltas in the middle?
- Do we even have base files in the benchmarks? On a new canister, we only write the first base file after the first merge, which only happens a couple of checkpoints in. Could we have a relevant discrepancy here between the benchmarks and actual usage?
There was a problem hiding this comment.
That's why I offered a chat 😉 High-level:
- The page size remains 4 KiB. All memory mappings and access protections are unchanged (except 128 -> 512 max prefetch increase and huge page alignment decrease). If we
mprotecta memory area that is huge page aligned and at least huge page long, the kernel will attempt to use a single 2 MiB page instead of 512 individual 4 KiB pages. - Yes, all
checkpointbenchmarks use a single base file. As we discussed with Adam, this differs slightly from the mainnet (which has overlays). But there should be no regressions or read/write amplifications, so it's good enough.
There was a problem hiding this comment.
Yes, all checkpoint benchmarks use a single base file. As we discussed with Adam, this differs slightly from the mainnet (which has overlays). But there should be no regressions or read/write amplifications, so it's good enough.
I am not 100% convinced that there can't be any regressions there, for example the case where we mmap the base file first, and then mmap smaller chunks of an overlay file is something you can only trigger with overlay files.
However, I think this doesn't affect the next steps. I think we should roll this out and observe what happens. In the end, there are workloads that benefit, there are workloads that suffer, and there are workloads that aren't covered by the benchmarks, but no amount of artificial workloads can answer the question of how much they each matter for production workloads.
There was a problem hiding this comment.
the case where we mmap the base file first, and then mmap smaller chunks of an overlay file is something you can only trigger with overlay files.
This PR should not introduce any regressions with mmaps or read/write amplification. I believe synthetic benchmarks are sufficient to confirm this. The page size should remain 4 KiB for the described scenario.
I've tested this PR with subnet-load-tester on the dll server using production-like storage (admittedly, still synthetic benchmarks). I will double-check the final version of this PR before merging.
there are workloads that suffer
There are eight regressions, all in new_allocation. Hopefully, you agree that's not a main production workload case. IMO the regression is acceptable for the finalization rate, and will be addressed in the near future, as it's a blocker for increasing the 6 GiB limit.
there are workloads that aren't covered by the benchmarks
I'm happy to add more benchmarks or run production-like workloads.
how much they each matter for production workloads
Production workloads also vary over time and across different dapps... Synthetic benchmarks are an easy way to trigger all known corner cases and assess risks based on the results. To be clear, I'm not claiming that this PR increases the finalization rate by 30% across all subnets.
|
Hey @schneiderstefan,
Tracked random reads/writes are restricted by
I temporary added 144 pseudo-random benchmarks with 2, 5 and 500 MiB steps. Out of those 144 benchmarks there are 9 regressions: Most regressions are in the (already very fast) new allocations, i.e. As |
|
Closing this PR — it looks inactive (last update on 2025-04-14, no recent discussion). No judgment on the work itself, just repo hygiene. If this was closed by mistake or you're still planning to land it, feel free to reopen — no need to ask anyone first. Generated by Claude Code |
Upgrades `rules_go` from 0.62.0 to [0.65.0](https://github.com/bazel-contrib/rules_go/releases/tag/v0.65.0), its latest release. The repository has no Go sources and no `go_*` targets of its own. rules_go builds `//:gazelle` (which `//pre-commit:gazelle-check` and the Linter job's autofix run) and `//:gazelle-update-repos`, and provides the `go` tool behind `//:gobin`. `bazel query 'rdeps(//..., @rules_go//:go_context_data)'` lists only those targets in the main repository. ## Changes - **`MODULE.bazel`**: version bump, and a rewritten comment above it. The old comment said that 0.62.0 matched the resolved graph because the llvm module pulled it in. The llvm module (0.8.16, and also the latest 0.8.27) declares `bazel_dep(name = "rules_go", version = "0.62.0", repo_name = None)`, so it only sets a minimum version. gazelle 0.51.3 requires 0.59.0 and rules_buf 0.5.2 requires 0.42.0. The root module's 0.65.0 is now the highest requirement, so it's the version that resolution selects. BCR's `rules_go/0.65.0` has only the usual `module_dot_bazel_version.patch`, and we carry no patch or override for rules_go. In the module graph, only rules_go's version changes. It also gains an edge to `package_metadata`: 0.65.0 requires 0.0.5, which already resolves to 0.0.7. On a fresh Bazel server the warnings are the same as on master (only the existing rules_qemu `use_repo` one). That includes gazelle's `go_deps` extension, which prints no warnings. ## What changes for us - **Default Go SDK 1.25.0 → 1.26.7.** We don't declare a `go_sdk`, and no other module in our graph does outside its dev dependencies. `bazel mod show_extension @rules_go//go:extensions.bzl%go_sdk` shows only rules_go's own `go_sdk.from_file(name = "go_default_sdk", go_mod = "//:go.mod")`; gazelle only imports `go_host_compatible_sdk_label`. That SDK's version comes from the `toolchain` line of rules_go's own `go.mod`, which [bazel-contrib/rules_go#4697](bazel-contrib/rules_go#4697) bumped from go1.25.0 to go1.26.7 (released in 0.64.0). So Gazelle is now compiled with Go 1.26.7, and `bazel run //:gobin -- version` prints `go1.26.7` instead of `go1.25.0`. - **Go modules in the Gazelle build.** gazelle's `go_deps` extension resolves the Go modules that all Bazel modules' `go.mod` files require, and the highest version wins. rules_go's `go.mod` now raises the following (from `bazel mod show_repo`, master → this PR): - compiled into Gazelle: `golang.org/x/mod` v0.25.0 → v0.41.0, `golang.org/x/sync` v0.15.0 → v0.23.0 and `golang.org/x/sys` v0.33.0 → v0.48.0. - `golang.org/x/tools` v0.34.0 → v0.51.0 ([dfinity#4701](bazel-contrib/rules_go#4701), [dfinity#4740](bazel-contrib/rules_go#4740)). Its `goyacc` generates the BUILD-file parser of buildtools during the Gazelle build. Gazelle's other dependencies (buildtools, doublestar, go-difflib, `x/tools/go/vcs`) don't change. rules_go's `go.mod` requires none of the Go modules that our `go.mod` requires directly. ## Upstream changes that touch our setup - [dfinity#4762](bazel-contrib/rules_go#4762) raises the minimum Bazel version to 7.7.0 (`bazel_compatibility`). We use 9.3.2. - [dfinity#4761](bazel-contrib/rules_go#4761) removes the `//go/private:request_nogo` setting. We don't configure nogo, and neither gazelle 0.51.3 nor 0.54.0 references that setting. Gazelle 0.51.3's `gazelle_binary` uses `go_context`, `new_go_info`, `GoArchive` and `CGO_ATTRS`/`CGO_FRAGMENTS`/`CGO_TOOLCHAINS`, which all still exist. Gazelle 0.54.0 (dfinity#12149) uses `go_binary`, `GoArchive`, `go_context` and the same `CGO_*` symbols. - cgo, which the hermetic LLVM toolchain compiles: - [dfinity#4671](bazel-contrib/rules_go#4671) replaces cgo's default `-fdebug-prefix-map` flags with `-ffile-prefix-map`, which clang supports, so `__FILE__` no longer embeds absolute paths. This also covers the cgo part of the standard library build. - [dfinity#4739](bazel-contrib/rules_go#4739) also makes `-internal-externc-isystem`/`-stdlib++-isystem` header paths absolute. llvm 0.8.16 (and 0.8.27) passes neither and uses `-Xclang -internal-isystem`, which rules_go already handled. - [dfinity#4667](bazel-contrib/rules_go#4667) turns off path mapping for cgo standard library builds. We don't enable path mapping. - [dfinity#4689](bazel-contrib/rules_go#4689) and [dfinity#4704](bazel-contrib/rules_go#4704) change how the `static`, `pure` and `linkmode` settings reach Go tool binaries. [dfinity#4706](bazel-contrib/rules_go#4706) applies the `static` attribute at link time instead of through a transition. We set none of the `//go/config` flags or those attributes. - [dfinity#4728](bazel-contrib/rules_go#4728) changes the build IDs of the Go binaries that rules_go links. 0.62.0 linked them with `-buildid=redacted`. 0.65.0 links with a placeholder, then overwrites it with a SHA-256 of the linked binary: - ELF: the Go build ID, and the GNU build ID if the binary has one. - Mach-O: the Go build ID and `LC_UUID`. If the binary is code-signed (Go's linker signs darwin/arm64 binaries), rules_go's builder then re-signs it ad hoc in Go, without Apple's `codesign`. For us this affects Gazelle, the `go` tool runner behind `//:gobin`, and `goyacc`, which the Gazelle build runs. I checked the first two on Linux and cross-compiled for macOS (see Verification). ## Verification (dev container) - `bazel mod graph`, `bazel mod explain rules_go`/`gazelle`/`package_metadata` and `bazel mod show_extension`/`show_repo` give the resolution described above, compared with the same commands on master. - `bazel build //... --nobuild`: all 3387 targets analyze. - Linter-job parity: `shfmt-check`, `ruff-check`, `buf-check`, `DFINITY-capitalization-check`, `do-not-merge-check`, `buildifier-check`, `gazelle-check` and `buf-breaking-check` pass. After them, `buildifier-fix`, `shfmt-fix`, `ruff-fix` and `bazel run //:gazelle` leave the tree unchanged. - `//:gazelle`, `//:gazelle-update-repos` and `//:gobin` build, with Gazelle and the Go standard library rebuilt with Go 1.26.7. - With no Go code to test, I exercised rules_go's rules and cgo through the "Hello World" workflow in `bazel/README.md`. I used a temporary package that isn't committed: a `main.go` with a C function and `os/user`, plus a `main_test.go`. - `bazel run //:gazelle` generated a `go_library` (`cgo = True`), a `go_binary` and a `go_test`. - The binary printed `hello world from go1.26.7: 1+2=3`, and the test passed. - Its build compiled the standard library with `CGO_ENABLED=1` and the hermetic clang 22.1.8 as `CC`. - After dfinity#12149 (gazelle 0.54.0) landed, I merged master into this branch. Its gazelle line sits next to this PR's rules_go line; the resolution keeps both. Against master, `bazel mod graph` changes only `rules_go` (0.62.0 → 0.65.0) with no new warnings, `bazel build //... --nobuild` analyzes all 3389 targets, and `bazel run //:buildifier` changes nothing. - Before that, I repeated this with gazelle temporarily bumped to 0.54.0, as in dfinity#12149. It resolves to gazelle 0.54.0 and rules_go 0.65.0 without new warnings, Gazelle builds, `gazelle-check` leaves the tree clean, and the temporary package builds, runs and passes its test. - Release artifacts: I didn't run the byte-identity rebuild. I ran `bazel cquery 'deps(...)'` over the seven release targets: the IC-OS bundles that `ci/container/build-ic.sh --icos` builds, `//publish/binaries:bundle` and `//publish/canisters:bundle`. None of the 110,935 configured targets it lists references rules_go, gazelle or a Go SDK or toolchain repo. No other module's resolved version changes either, so this upgrade can't change what we ship. - Build IDs ([dfinity#4728](bazel-contrib/rules_go#4728)), which I read from the binaries with a small Python script: - Linux: the Go build IDs of Gazelle and the `go` tool runner are now content hashes in `go build`'s format (four 20-character chunks), not `redacted`. The runner's GNU build ID matches its Go build ID. Gazelle's binary has no GNU build ID note, so only its Go build ID is rewritten. - macOS, cross-compiled on Linux: I built `@gazelle//cmd/gazelle` and `@rules_go//go/tools/go_bin_runner` with `--platforms=@rules_go//go/toolchain:darwin_arm64`, and again with `darwin_amd64`. These platforms turn cgo off, so Go's internal linker links the binaries. In all four binaries, the Go build ID and `LC_UUID` come from the same content hash. The two arm64 binaries have a valid ad hoc signature: every CodeDirectory page hash (2,861 and 945 of them) matches the file. The amd64 binaries have no signature, because Go's linker signs only darwin/arm64 binaries. - Not verified on a Mac, either locally or in CI. CI's `Bazel Test arm64-darwin` job builds three `//publish/binaries` targets (`pocket-ic.gz` also for x86_64), `//rs/...` and `//packages/...`. Of those, it tests the targets tagged `test_all_platforms` or `test_all_platforms_slow`. - `bazel query 'deps(...)'` over these targets follows every `select()` branch. It lists 79,392 labels, and none comes from rules_go, gazelle, a Go SDK or a Go module repository. - Across the whole workspace, a `bazel query` for main-repository targets that depend on such a label finds only the targets named at the top and their two `-runner` targets. - On a Mac, those targets now build with Go 1.26.7. On Apple silicon, the Go binaries involved (Gazelle, the `go` tool runner and `goyacc`) are re-signed after linking, as described above. The cross-compiled binaries don't show that they build and run on a Mac. - Before merging, could someone on a Mac run `bazel run //:gazelle` and `bazel run //:gobin -- version` on this branch? The second should print `go1.26.7`. - On Linux, CI covers the rest. Because this PR changes `MODULE.bazel`, CI builds and tests all of `//...`, not just the affected targets (PR runs still skip `long_test`s). The Linter job also runs `gazelle-check`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
The transparent huge pages improve host memory performance, resulting in a ~30% improvement across benchmarks without regressions. Some benchmarks (like
query_write_bwd_1gb_step_8_page_delta) reduce their runtime by more than 700 ms (more than 60% of the runtime). Some benchmarks (likequery_read_bwd_1gb_step_4kb_new_allocation) are improved by more than 90% from 226 ms down to 7 ms (or ~32x).