From 348963b7790e2a660a3572e302429e2d513c1f3c Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Tue, 3 Dec 2024 16:49:37 +0100 Subject: [PATCH 01/14] build!: optimize build process for Haswell CPUs --- .cargo/config-release.toml | 2 +- Dockerfile | 31 ++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/.cargo/config-release.toml b/.cargo/config-release.toml index faab5e6c57..997aae0e9e 100644 --- a/.cargo/config-release.toml +++ b/.cargo/config-release.toml @@ -5,7 +5,7 @@ rustflags = ["-C", "target-feature=-crt-static"] [target.x86_64-unknown-linux-musl] -rustflags = ["-C", "target-feature=-crt-static"] +rustflags = ["-C", "target-feature=-crt-static", "-C", "target-cpu=haswell"] [target.aarch64-unknown-linux-gnu] linker = "aarch64-linux-gnu-gcc" diff --git a/Dockerfile b/Dockerfile index 5c8ac67655..3d1d7b4ae6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -114,9 +114,19 @@ RUN TOOLCHAIN_VERSION="$(grep channel rust-toolchain.toml | awk '{print $3}' | t ONBUILD ENV HOME=/root ONBUILD ENV CARGO_HOME=$HOME/.cargo -# Configure Rust toolchain +ONBUILD ARG CARGO_BUILD_PROFILE=dev + +# Configure Rust toolchain and C / C++ compiler +RUN <> /root/env +echo 'source $HOME/.cargo/env' >> /root/env + +# Enable gcc / g++ optimizations +if [[ "${CARGO_BUILD_PROFILE}" == "release" ]] ; then + echo "export CFLAGS=-march=haswell" >> /root/env + echo "export CXXFLAGS=-march=haswell" >> /root/env +fi +EOS # Install protoc - protobuf compiler # The one shipped with Alpine does not work @@ -258,13 +268,23 @@ WORKDIR /tmp/rocksdb # sccache -s # EOS +# Select whether we want dev or release +# This variable will be also visibe in next stages +ONBUILD ARG CARGO_BUILD_PROFILE=dev + RUN --mount=type=secret,id=AWS < Date: Tue, 3 Dec 2024 17:21:52 +0100 Subject: [PATCH 02/14] build: use elfx86exts to check CPU flags --- .cargo/config-release.toml | 3 +++ Dockerfile | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/.cargo/config-release.toml b/.cargo/config-release.toml index 997aae0e9e..cc5f8c9948 100644 --- a/.cargo/config-release.toml +++ b/.cargo/config-release.toml @@ -7,5 +7,8 @@ rustflags = ["-C", "target-feature=-crt-static"] [target.x86_64-unknown-linux-musl] rustflags = ["-C", "target-feature=-crt-static", "-C", "target-cpu=haswell"] +[target.x86_64-unknown-linux-gnu] +rustflags = ["-C", "target-feature=-crt-static", "-C", "target-cpu=haswell"] + [target.aarch64-unknown-linux-gnu] linker = "aarch64-linux-gnu-gcc" diff --git a/Dockerfile b/Dockerfile index 3d1d7b4ae6..58b6dc2efc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -340,6 +340,14 @@ RUN --mount=type=secret,id=AWS \ --no-track \ --no-confirm +RUN --mount=type=cache,sharing=shared,id=cargo_registry_index,target=${CARGO_HOME}/registry/index \ + --mount=type=cache,sharing=shared,id=cargo_registry_cache,target=${CARGO_HOME}/registry/cache \ + --mount=type=cache,sharing=shared,id=cargo_git,target=${CARGO_HOME}/git/db \ + --mount=type=secret,id=AWS \ + set -ex; \ + source /root/env && \ + cargo install https://github.com/pkgw/elfx86exts@0.6.2 --locked && \ + cargo clean # # Rust build planner to speed up builds @@ -584,6 +592,8 @@ VOLUME /var/log/dash # Double-check that we don't have missing deps RUN ldd /usr/bin/drive-abci +# See what CPU capabilities are required +RUN elfx86exts /usr/bin/drive-abci # # Create new non-root user From 670341e89f3aca4aa2d32dd4e00571a10d627850 Mon Sep 17 00:00:00 2001 From: lklimek <842586+lklimek@users.noreply.github.com> Date: Tue, 3 Dec 2024 17:28:49 +0100 Subject: [PATCH 03/14] Update Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 58b6dc2efc..595fdbc2a3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -346,7 +346,7 @@ RUN --mount=type=cache,sharing=shared,id=cargo_registry_index,target=${CARGO_HOM --mount=type=secret,id=AWS \ set -ex; \ source /root/env && \ - cargo install https://github.com/pkgw/elfx86exts@0.6.2 --locked && \ + cargo install --git https://github.com/pkgw/elfx86exts@0.6.2 --locked && \ cargo clean # From c03f6f41d40ad21980360c57cf2ea6991d02b050 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Tue, 3 Dec 2024 18:19:18 +0100 Subject: [PATCH 04/14] chore: fix elfx86exts --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 595fdbc2a3..7fdf9ef616 100644 --- a/Dockerfile +++ b/Dockerfile @@ -346,7 +346,7 @@ RUN --mount=type=cache,sharing=shared,id=cargo_registry_index,target=${CARGO_HOM --mount=type=secret,id=AWS \ set -ex; \ source /root/env && \ - cargo install --git https://github.com/pkgw/elfx86exts@0.6.2 --locked && \ + cargo install --git https://github.com/pkgw/elfx86exts --locked && \ cargo clean # From d15ffc272ba1999983473883be9289f7a3f7a4b2 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 4 Dec 2024 08:56:39 +0100 Subject: [PATCH 05/14] chore: fix cargo clean --- Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7fdf9ef616..06cd93d10c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -346,8 +346,7 @@ RUN --mount=type=cache,sharing=shared,id=cargo_registry_index,target=${CARGO_HOM --mount=type=secret,id=AWS \ set -ex; \ source /root/env && \ - cargo install --git https://github.com/pkgw/elfx86exts --locked && \ - cargo clean + cargo install --git https://github.com/pkgw/elfx86exts --locked # # Rust build planner to speed up builds From b1e21020e5641b87453347d175c2568167356543 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 4 Dec 2024 09:01:39 +0100 Subject: [PATCH 06/14] cargo binstall elfx86exts --- Dockerfile | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 06cd93d10c..9f570d534c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -333,21 +333,13 @@ RUN --mount=type=secret,id=AWS \ RUN --mount=type=secret,id=AWS \ source /root/env; \ - cargo binstall wasm-bindgen-cli@0.2.86 cargo-chef@0.1.67 \ + cargo binstall wasm-bindgen-cli@0.2.86 cargo-chef@0.1.67 elfx86exts@0.6.2 \ --locked \ --no-discover-github-token \ --disable-telemetry \ --no-track \ --no-confirm -RUN --mount=type=cache,sharing=shared,id=cargo_registry_index,target=${CARGO_HOME}/registry/index \ - --mount=type=cache,sharing=shared,id=cargo_registry_cache,target=${CARGO_HOME}/registry/cache \ - --mount=type=cache,sharing=shared,id=cargo_git,target=${CARGO_HOME}/git/db \ - --mount=type=secret,id=AWS \ - set -ex; \ - source /root/env && \ - cargo install --git https://github.com/pkgw/elfx86exts --locked - # # Rust build planner to speed up builds # From c528ad1adb163cfd6b69ab3fa63d17a2d860f2e1 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 4 Dec 2024 09:09:10 +0100 Subject: [PATCH 07/14] fix elfx86exts invocation --- Dockerfile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9f570d534c..149eb1401b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -481,6 +481,11 @@ RUN --mount=type=cache,sharing=shared,id=cargo_registry_index,target=${CARGO_HOM # Remove /platform to reduce layer size rm -rf /platform +# See what CPU capabilities are required +RUN set -ex; \ + source /root/env && \ + elfx86exts /artifacts/drive-abci + # # STAGE: BUILD JAVASCRIPT INTERMEDIATE IMAGE # @@ -583,8 +588,6 @@ VOLUME /var/log/dash # Double-check that we don't have missing deps RUN ldd /usr/bin/drive-abci -# See what CPU capabilities are required -RUN elfx86exts /usr/bin/drive-abci # # Create new non-root user From 253c4b3bdc8b0602bb372dc32ee80dd7448c2e97 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 4 Dec 2024 09:16:48 +0100 Subject: [PATCH 08/14] fix: aws mount required --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 149eb1401b..861696a3c2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -482,7 +482,8 @@ RUN --mount=type=cache,sharing=shared,id=cargo_registry_index,target=${CARGO_HOM rm -rf /platform # See what CPU capabilities are required -RUN set -ex; \ +RUN --mount=type=secret,id=AWS \ + set -ex; \ source /root/env && \ elfx86exts /artifacts/drive-abci From 8f7431b15ec5270e392adcea2e5d8c37bd8be7ae Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 4 Dec 2024 09:24:16 +0100 Subject: [PATCH 09/14] chore: only optimize for amd64 --- Dockerfile | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 861696a3c2..833529edf7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -122,9 +122,14 @@ RUN <> /root/env # Enable gcc / g++ optimizations -if [[ "${CARGO_BUILD_PROFILE}" == "release" ]] ; then - echo "export CFLAGS=-march=haswell" >> /root/env - echo "export CXXFLAGS=-march=haswell" >> /root/env +if [[ "$TARGETARCH" == "amd64" ]] ; then + if [[ "${CARGO_BUILD_PROFILE}" == "release" ]] ; then + echo "export CFLAGS=-march=haswell" >> /root/env + echo "export CXXFLAGS=-march=haswell" >> /root/env + else + echo "export CFLAGS=-march=x86-64" >> /root/env + echo "export CXXFLAGS=-march=x86-64" >> /root/env + fi fi EOS From 3a04aa079e5df3b6ae5c9932f630f321733cc1f2 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:57:42 +0100 Subject: [PATCH 10/14] chore: release uses target-cpu=x86-64-v3 --- .cargo/config-release.toml | 4 ++-- .cargo/config.toml | 19 ++++++++++++++++++- Dockerfile | 17 ++++++----------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/.cargo/config-release.toml b/.cargo/config-release.toml index cc5f8c9948..fa2932fecb 100644 --- a/.cargo/config-release.toml +++ b/.cargo/config-release.toml @@ -5,10 +5,10 @@ rustflags = ["-C", "target-feature=-crt-static"] [target.x86_64-unknown-linux-musl] -rustflags = ["-C", "target-feature=-crt-static", "-C", "target-cpu=haswell"] +rustflags = ["-C", "target-feature=-crt-static", "-C", "target-cpu=x86-64-v3"] [target.x86_64-unknown-linux-gnu] -rustflags = ["-C", "target-feature=-crt-static", "-C", "target-cpu=haswell"] +rustflags = ["-C", "target-feature=-crt-static", "-C", "target-cpu=x86-64-v3"] [target.aarch64-unknown-linux-gnu] linker = "aarch64-linux-gnu-gcc" diff --git a/.cargo/config.toml b/.cargo/config.toml index 18bc8e4064..307582b0ad 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -4,7 +4,24 @@ rustflags = ["-C", "target-feature=-crt-static", "--cfg", "tokio_unstable"] [target.x86_64-unknown-linux-musl] -rustflags = ["-C", "target-feature=-crt-static", "--cfg", "tokio_unstable"] +rustflags = [ + "-C", + "target-feature=-crt-static", + "--cfg", + "tokio_unstable", + "-C", + "target-cpu=x86-64", +] + +[target.x86_64-unknown-linux-gnu] +rustflags = [ + "-C", + "target-feature=-crt-static", + "--cfg", + "tokio_unstable", + "-C", + "target-cpu=x86-64", +] [target.aarch64-unknown-linux-gnu] linker = "aarch64-linux-gnu-gcc" diff --git a/Dockerfile b/Dockerfile index 833529edf7..d551c7553b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -124,12 +124,16 @@ echo 'source $HOME/.cargo/env' >> /root/env # Enable gcc / g++ optimizations if [[ "$TARGETARCH" == "amd64" ]] ; then if [[ "${CARGO_BUILD_PROFILE}" == "release" ]] ; then - echo "export CFLAGS=-march=haswell" >> /root/env - echo "export CXXFLAGS=-march=haswell" >> /root/env + echo "export CFLAGS=-march=x86-64-v3" >> /root/env + echo "export CXXFLAGS=-march=x86-64-v3" >> /root/env + echo "PORTABLE=x86-64-v3" >> /root/env else echo "export CFLAGS=-march=x86-64" >> /root/env echo "export CXXFLAGS=-march=x86-64" >> /root/env + echo "PORTABLE=1" >> /root/env fi +else + echo "PORTABLE=1" >> /root/env fi EOS @@ -282,15 +286,6 @@ set -ex -o pipefail git clone https://github.com/facebook/rocksdb.git -b v8.10.2 --depth 1 . source /root/env -# For x86-64 release, tune the build to use haswell optimizations. -# It requires 64-bit extensions, MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, POPCNT, CX16, SAHF, FXSR, AVX, XSAVE, PCLMUL, -# FSGSBASE, RDRND, F16C, AVX2, BMI, BMI2, LZCNT, FMA, MOVBE and HLE instruction set support. -if [[ "$TARGETARCH" == "amd64" ]] && [[ "$CARGO_BUILD_PROFILE" == "release" ]] ; then - export PORTABLE=haswell -else - export PORTABLE=1 -fi - make -j$(nproc) static_lib mkdir -p /opt/rocksdb/usr/local/lib cp librocksdb.a /opt/rocksdb/usr/local/lib/ From 17e3324feea48cbd11e2fa50298f943e50d0cfb7 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:58:19 +0100 Subject: [PATCH 11/14] chore: tune linters --- packages/rs-drive-abci/Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/rs-drive-abci/Cargo.toml b/packages/rs-drive-abci/Cargo.toml index f4b3660e2f..250cfcef80 100644 --- a/packages/rs-drive-abci/Cargo.toml +++ b/packages/rs-drive-abci/Cargo.toml @@ -112,3 +112,7 @@ grovedbg = ["drive/grovedbg"] [[bin]] name = "drive-abci" path = "src/main.rs" + + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] } From 3943a2cf2ad70c8ec246a1220dc0381be35e7960 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Fri, 13 Dec 2024 09:25:49 +0100 Subject: [PATCH 12/14] build: -dev portable=x86-64 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d551c7553b..c0cd70154b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -130,7 +130,7 @@ if [[ "$TARGETARCH" == "amd64" ]] ; then else echo "export CFLAGS=-march=x86-64" >> /root/env echo "export CXXFLAGS=-march=x86-64" >> /root/env - echo "PORTABLE=1" >> /root/env + echo "PORTABLE=x86-64" >> /root/env fi else echo "PORTABLE=1" >> /root/env From f235ed11802a326997c7f1f694e77dfc93e0e028 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Fri, 13 Dec 2024 09:32:04 +0100 Subject: [PATCH 13/14] fix: missing export statement --- Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index c0cd70154b..1da6ad1be5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -126,14 +126,14 @@ if [[ "$TARGETARCH" == "amd64" ]] ; then if [[ "${CARGO_BUILD_PROFILE}" == "release" ]] ; then echo "export CFLAGS=-march=x86-64-v3" >> /root/env echo "export CXXFLAGS=-march=x86-64-v3" >> /root/env - echo "PORTABLE=x86-64-v3" >> /root/env + echo "export PORTABLE=x86-64-v3" >> /root/env else echo "export CFLAGS=-march=x86-64" >> /root/env echo "export CXXFLAGS=-march=x86-64" >> /root/env - echo "PORTABLE=x86-64" >> /root/env + echo "export PORTABLE=x86-64" >> /root/env fi else - echo "PORTABLE=1" >> /root/env + echo "export PORTABLE=1" >> /root/env fi EOS From 3c3a30c9e58e2f67d6c4c871dd6211d06fb7ff29 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:17:38 +0100 Subject: [PATCH 14/14] chore: removing elfx86exts which does not work --- Dockerfile | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1da6ad1be5..5b2ff5882a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -333,7 +333,7 @@ RUN --mount=type=secret,id=AWS \ RUN --mount=type=secret,id=AWS \ source /root/env; \ - cargo binstall wasm-bindgen-cli@0.2.86 cargo-chef@0.1.67 elfx86exts@0.6.2 \ + cargo binstall wasm-bindgen-cli@0.2.86 cargo-chef@0.1.67 \ --locked \ --no-discover-github-token \ --disable-telemetry \ @@ -481,11 +481,6 @@ RUN --mount=type=cache,sharing=shared,id=cargo_registry_index,target=${CARGO_HOM # Remove /platform to reduce layer size rm -rf /platform -# See what CPU capabilities are required -RUN --mount=type=secret,id=AWS \ - set -ex; \ - source /root/env && \ - elfx86exts /artifacts/drive-abci # # STAGE: BUILD JAVASCRIPT INTERMEDIATE IMAGE