diff --git a/.github/workflows/test-install.yml b/.github/workflows/test-install.yml index 738724d..141fa3b 100644 --- a/.github/workflows/test-install.yml +++ b/.github/workflows/test-install.yml @@ -11,6 +11,10 @@ on: description: Mainline (-rc) version to build and install with --mainline required: false default: 7.3-rc7 + full_build: + description: Run the full CachyOS/BMQ/hardened kernel builds (hours) + type: boolean + default: false schedule: # Weekly, off the hour (#43): the kernel.org stable release moves on its # own, so a new kernel that breaks the build must show up without anyone @@ -393,9 +397,43 @@ jobs: path: /var/log/kernel-installer/kernel_installer.log if-no-files-found: ignore + config-only: + name: Config ${{ matrix.name }} + if: github.event_name != 'schedule' + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + include: + - name: BORE + args: "--source cachyos --sched bore --profile workstation" + - name: BORE+LTO + args: "--source cachyos --sched bore --profile workstation --lto thin" + - name: BMQ + args: "--source cachyos --sched bmq --profile workstation" + - name: RT + args: "--source cachyos --sched bore --profile workstation --rt" + - name: hardened + args: "--hardened --profile workstation --base-config none" + - name: AutoFDO + args: "--source cachyos --autofdo-collect --profile workstation" + container: + image: debian:13 + env: + TERM: xterm + steps: + - name: Install container prerequisites + run: | + apt-get update -qq + apt-get install -y -qq git ca-certificates lsb-release procps + - uses: actions/checkout@v4 + - name: Validate configuration only + run: ./kernel_installer.sh --config-only ${{ matrix.args }} + cachyos-bore-smoke: name: CachyOS BORE + LTO ${{ matrix.name }} - if: github.event_name != 'schedule' + if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.full_build == 'true') runs-on: ubuntu-latest timeout-minutes: 240 strategy: @@ -470,7 +508,7 @@ jobs: hardened-smoke: name: Hardened (linux-hardened 7.3.1) - if: github.event_name != 'schedule' + if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.full_build == 'true') runs-on: ubuntu-latest timeout-minutes: 180 container: @@ -512,6 +550,52 @@ jobs: path: /var/log/kernel-installer/kernel_installer.log if-no-files-found: ignore + bmq-smoke: + name: BMQ (CachyOS 7.3.4) + if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.full_build == 'true') + runs-on: ubuntu-latest + timeout-minutes: 240 + container: + image: debian:13 + env: + TERM: xterm + steps: + - name: Install container prerequisites + run: | + apt-get update -qq + apt-get install -y -qq git ca-certificates lsb-release procps initramfs-tools qemu-system-x86 + - uses: actions/checkout@v4 + - name: Build and install the BMQ kernel + run: ./kernel_installer.sh --source cachyos --sched bmq --profile workstation + - name: Show installer log on failure + if: failure() + run: | + echo "=== /var/log/kernel-installer/kernel_installer.log (last 200 lines) ===" + tail -n 200 /var/log/kernel-installer/kernel_installer.log || true + - name: Verify the BMQ kernel + run: | + set -eux + set -- /boot/vmlinuz-* + test "$#" -eq 1 + release="${1##*/vmlinuz-}" + test "$release" = "7.3.4-cachyos-workstation-bmq" + cfg="/boot/config-${release}" + test -f "$cfg" + grep -q '^CONFIG_CACHY=y$' "$cfg" + grep -q '^CONFIG_SCHED_ALT=y$' "$cfg" + grep -q '^CONFIG_SCHED_BMQ=y$' "$cfg" + grep -q '^CONFIG_HZ=1000$' "$cfg" + echo "BMQ verified in $cfg" + - name: Boot smoke test + run: ./scripts/boot-smoke.sh + - name: Upload installer logs + if: always() + uses: actions/upload-artifact@v4 + with: + name: kernel-installer-logs-bmq + path: /var/log/kernel-installer/kernel_installer.log + if-no-files-found: ignore + # Issue #43: every other job here passes -k, --mainline or --custom-config, # so none of them installs the way a user does -- and a Fedora install of a # *current* kernel broke with no job noticing. These jobs run diff --git a/README.md b/README.md index 7819a52..33f6c47 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,7 @@ Usage: kernel_installer.sh [options] --list-profiles |-lp list optimization profiles and exit --base-config |-bc profile base config: running, a path, cachyos, or none --source | kernel source: vanilla (kernel.org) or cachyos (pinned) - --sched | scheduler for the cachyos source: eevdf or bore + --sched | scheduler for the cachyos source: eevdf, bore, or bmq --cpu | CPU tuning: generic, native, zen4, x86-64-v3, x86-64-v4 --lto | LTO: none, thin, or full (requires clang/lld) --hz | timer frequency: 100, 250, 300, 500, 600, 750, 1000 @@ -157,6 +157,9 @@ Usage: kernel_installer.sh [options] --sched-ext | enable sched_ext and BTF for userspace BPF schedulers --rt | build a PREEMPT_RT real-time kernel (6.12+) --hardened | apply the pinned linux-hardened patch (vanilla 7.3.1) + --autofdo | build with an AutoFDO sample profile (clang 17+) + --autofdo-collect | build a profiling kernel for AutoFDO (clang 17+) + --config-only | validate the configuration and exit before compiling --no-sign | disable automatic Secure Boot kernel signing --kernel-tag | custom localversion suffix (default: auto from source/profile) --verbose |-v increase verbosity @@ -314,6 +317,10 @@ sudo ./kernel_installer.sh -d /path/to/parent \ `--source cachyos`: the BORE patch does not apply cleanly to the vanilla kernel.org 7.3.4 tree. `--sched eevdf` is the default and does not apply the BORE patch. +- `--sched bmq` applies the pinned PRJC/BMQ patch and enables + `CONFIG_SCHED_ALT=y` + `CONFIG_SCHED_BMQ=y`. It requires `--source cachyos` + and is mutually exclusive with BORE. BMQ targets desktop interactivity; the + patch is pinned by SHA256 like the BORE patch. - CI builds and boots the BORE kernel with Clang ThinLTO on Debian 13 and Ubuntu 24.04, with the `workstation` profile. Native CPU tuning is available but is not part of the default CI matrix because it depends on the build @@ -332,10 +339,10 @@ sudo ./kernel_installer.sh -d /path/to/parent \ `clang`, `lld`, and `llvm` on demand, configure `LTO_CLANG_THIN`/`FULL`, and build with `LLVM=1 LLVM_IAS=1`; `none` is the default. The kernel is boot-smoked after the build. -- This is the CachyOS base patchset plus BORE, not the full CachyOS variant - matrix. BMQ and the remaining wiki features are separate follow-ups; - sched_ext, PREEMPT_RT, and linux-hardened are available through - `--sched-ext`, `--rt`, and `--hardened`. +- This is the CachyOS base patchset plus BORE or BMQ, not the full CachyOS + variant matrix. The remaining wiki features are separate follow-ups; + sched_ext, PREEMPT_RT, linux-hardened, and AutoFDO are available through + `--sched-ext`, `--rt`, `--hardened`, and `--autofdo`. ### Kernel naming @@ -344,7 +351,7 @@ Optimized builds get a `CONFIG_LOCALVERSION` so they are distinguishable in `.deb` names: ```text ---[-][-][-][-hz][-bbr3][-binder][-sched-ext][-rt] +--[-][-][-][-hz][-bbr3][-binder][-sched-ext][-rt][-autofdo] ``` | Build | `uname -r` | @@ -355,6 +362,7 @@ Optimized builds get a `CONFIG_LOCALVERSION` so they are distinguishable in | same + 600 Hz + BBR3 + Binder + sched_ext | `7.3.4-cachyos-workstation-bore-hz600-bbr3-binder-sched-ext` | | same + PREEMPT_RT | `7.3.4-cachyos-workstation-bore-rt` | | vanilla workstation + linux-hardened | `7.3.1-hardened1-vanilla-workstation` | +| vanilla workstation + AutoFDO | `7.3.4-vanilla-workstation-autofdo` | | CachyOS EEVDF server | `7.3.4-cachyos-server` | | vanilla desktop | `7.3.4-vanilla-desktop` | @@ -422,6 +430,42 @@ options do not collide in `/lib/modules` or DKMS: 7.3.4-cachyos-workstation-bore-lto-hz600-bbr3-binder-sched-ext ``` +### AutoFDO + +AutoFDO is profile-guided optimization from hardware sampling. You build a +profiling kernel, run a representative workload while `perf` records branch +samples, convert the samples into an AutoFDO profile, and rebuild the kernel +with that profile. Clang then places the hot code and branches where the CPU +can fetch them faster. It is not a scheduler or instrumentation; the gain +depends on how well the workload matches the profile. + +Requirements: clang/LLVM 17 or newer, a kernel source with +`CONFIG_AUTOFDO_CLANG` (7.x; not 6.12), and an x86 CPU with LBR or BRS +(Zen 3 BRS, Zen 4+ `amd_lbr_v2`). + +```bash +# 1. profiling kernel (no profile yet) +sudo ./kernel_installer.sh --autofdo-collect --profile workstation +# reboot into it, then sample the workload +perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k \ + -a -N -b -c 500009 -o perf.data -- +# 2. convert (create_llvm_prof from the AutoFDO project) +create_llvm_prof --binary=/path/to/vmlinux- \ + --profile=perf.data --out=autofdo.profdata +# 3. rebuild with the profile +sudo ./kernel_installer.sh --autofdo autofdo.profdata --profile workstation +``` + +The `--autofdo-collect` build saves `vmlinux-` in the `--dir` parent +directory (default `/opt`), because the profile conversion needs the +unstripped binary. `--autofdo ` passes the profile to clang and tags +the kernel `-autofdo`; `--autofdo-collect` tags it `-autofdo-collect`. + +Limits: the profile is workload-specific; regenerate it when the kernel or +the workload changes. If the source enables Rust and your `rustc` lacks +sample-profile support, disable Rust for the AutoFDO build with +`--custom-config 'scripts/config --disable RUST'`. + ### Secure Boot auto-signing When a MOK key pair is present, the installer signs the kernel it installs and @@ -547,6 +591,13 @@ To uninstall previously installed kernel: Tested and working on: Debian 12/13, Ubuntu 22.04/24.04 and Fedora 44 (see [test-install.yml](.github/workflows/test-install.yml)) - Each CI run uploads the installer log of every distro as a workflow artifact (`kernel-installer-logs-`) on the [Actions](https://github.com/tmiland/kernel-installer/actions/workflows/test-install.yml) page +- For a fast configuration check without compiling: + + ```bash + sudo ./kernel_installer.sh --config-only --source cachyos --sched bmq --profile workstation + ``` + + The config-only matrix runs on every PR; the full kernel build/boot matrix runs on `main` and on demand (`workflow_dispatch` with `full_build=true`). ## Maintenance diff --git a/kernel_installer.sh b/kernel_installer.sh index 3342c97..b5544d1 100755 --- a/kernel_installer.sh +++ b/kernel_installer.sh @@ -76,6 +76,8 @@ CACHYOS_CONFIG_SHA256=81fb56fe326612a0dc4705fc5ec309d04bcfffb487db0cb5843918fe92 CACHYOS_PATCHES_REV=21d721415790e6717b2ece16e82e08eb98d16c00 CACHYOS_BORE_PATCH_URL="https://raw.githubusercontent.com/cachyos/kernel-patches/${CACHYOS_PATCHES_REV}/7.3/sched/0001-bore-cachy.patch" CACHYOS_BORE_PATCH_SHA256=d2eaa8e8dbf1f56bfb304f8b437a7dc30a65408f90a93701367baf8adc2173b4 +CACHYOS_BMQ_PATCH_URL="https://raw.githubusercontent.com/cachyos/kernel-patches/${CACHYOS_PATCHES_REV}/7.3/sched/0001-prjc-cachy.patch" +CACHYOS_BMQ_PATCH_SHA256=0d3c61ecfc84c4b7064e2336ca2b025fa2fa109bfacb37cf370de885db34ce8c CACHYOS_KEYRING_FILE=src/keys/cachyos.asc CACHYOS_KEY_FINGERPRINTS=( E18447AC260021D31F3FF6C4C8A2A4774B8B63C4 @@ -184,6 +186,11 @@ SCHED_EXT=${SCHED_EXT:-0} RT=${RT:-0} # linux-hardened variant (vanilla, pinned to HARDENED_VERSION) HARDENED=${HARDENED:-0} +# AutoFDO profile path (clang sample-use profile) or a profiling build +AUTOFDO_PROFILE=${AUTOFDO_PROFILE:-} +AUTOFDO_COLLECT=${AUTOFDO_COLLECT:-0} +# Validate the configuration and exit before compiling +CONFIG_ONLY=${CONFIG_ONLY:-0} # Automatic Secure Boot signing: sign the installed kernel and DKMS modules # with the local MOK key when it exists. --no-sign disables it. NO_SIGN=${NO_SIGN:-0} @@ -915,6 +922,8 @@ build_kernel_tag() { [ "$BINDER" = "1" ] && tag+="-binder" [ "$SCHED_EXT" = "1" ] && tag+="-sched-ext" [ "$RT" = "1" ] && tag+="-rt" + [ -n "$AUTOFDO_PROFILE" ] && tag+="-autofdo" + [ "$AUTOFDO_COLLECT" = "1" ] && tag+="-autofdo-collect" KERNEL_TAG="$tag" } @@ -946,11 +955,12 @@ profile_assert() { assert_profile() { local -a common=( - PSI=y ZRAM=y TCP_CONG_BBR=y NET_SCH_FQ=y ) + # BMQ replaces CFS and makes PSI unavailable (Kconfig depends on !SCHED_ALT). + [ "$KERNEL_SCHED" != "bmq" ] && common+=(PSI=y) # TRANSPARENT_HUGEPAGE depends on !PREEMPT_RT, so it cannot be asserted on # an RT kernel; olddefconfig drops the profile's THP settings. if [ "$RT" != "1" ]; then @@ -1370,7 +1380,7 @@ usage() { printf "%s\\n" " ${YELLOW}--list-profiles |-lp${NORMAL} list optimization profiles and exit" printf "%s\\n" " ${YELLOW}--base-config |-bc${NORMAL} profile base config: running, a path, cachyos, or none" printf "%s\\n" " ${YELLOW}--source |${NORMAL} kernel source: vanilla (kernel.org) or cachyos (pinned)" - printf "%s\\n" " ${YELLOW}--sched |${NORMAL} scheduler for the cachyos source: eevdf or bore" + printf "%s\\n" " ${YELLOW}--sched |${NORMAL} scheduler for the cachyos source: eevdf, bore, or bmq" printf "%s\\n" " ${YELLOW}--cpu |${NORMAL} CPU tuning: generic, native, zen4, x86-64-v3, x86-64-v4" printf "%s\\n" " ${YELLOW}--lto |${NORMAL} LTO: none, thin, or full (requires clang/lld)" printf "%s\\n" " ${YELLOW}--hz |${NORMAL} timer frequency: 100, 250, 300, 500, 600, 750, 1000" @@ -1379,6 +1389,9 @@ usage() { printf "%s\\n" " ${YELLOW}--sched-ext |${NORMAL} enable sched_ext and BTF for userspace BPF schedulers" printf "%s\\n" " ${YELLOW}--rt |${NORMAL} build a PREEMPT_RT real-time kernel (6.12+)" printf "%s\\n" " ${YELLOW}--hardened |${NORMAL} apply the pinned linux-hardened patch (vanilla 7.3.1)" + printf "%s\\n" " ${YELLOW}--autofdo |${NORMAL} build with an AutoFDO sample profile (clang 17+)" + printf "%s\\n" " ${YELLOW}--autofdo-collect |${NORMAL} build a profiling kernel for AutoFDO (clang 17+)" + printf "%s\\n" " ${YELLOW}--config-only |${NORMAL} validate the configuration and exit before compiling" printf "%s\\n" " ${YELLOW}--no-sign |${NORMAL} disable automatic Secure Boot kernel signing" printf "%s\\n" " ${YELLOW}--kernel-tag |${NORMAL} custom localversion suffix (default: auto from source/profile)" printf "%s\\n" " ${YELLOW}--verbose |-v${NORMAL} increase verbosity" @@ -1495,8 +1508,8 @@ while [[ $# -gt 0 ]]; do --sched) require_arg "$1" "${2:-}" case "$2" in - eevdf | bore) KERNEL_SCHED="$2" ;; - *) fatal "${RED}${BALLOT_X}Fatal:${NORMAL} Unknown scheduler: $2 (eevdf or bore)" ;; + eevdf | bore | bmq) KERNEL_SCHED="$2" ;; + *) fatal "${RED}${BALLOT_X}Fatal:${NORMAL} Unknown scheduler: $2 (eevdf, bore, or bmq)" ;; esac shift 2 ;; @@ -1544,6 +1557,19 @@ while [[ $# -gt 0 ]]; do HARDENED=1 shift ;; + --autofdo) + require_arg "$1" "${2:-}" + AUTOFDO_PROFILE="$2" + shift 2 + ;; + --autofdo-collect) + AUTOFDO_COLLECT=1 + shift + ;; + --config-only) + CONFIG_ONLY=1 + shift + ;; --no-sign) NO_SIGN=1 shift @@ -1611,8 +1637,8 @@ if [ ${#POSITIONAL_ARGS[@]} -gt 0 ]; then set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters fi -if [ "$KERNEL_SCHED" = "bore" ] && [ "$SOURCE" != "cachyos" ]; then - fatal "${RED}${BALLOT_X}Fatal:${NORMAL} --sched bore requires --source cachyos" +if [ "$KERNEL_SCHED" != "eevdf" ] && [ "$SOURCE" != "cachyos" ]; then + fatal "${RED}${BALLOT_X}Fatal:${NORMAL} --sched $KERNEL_SCHED requires --source cachyos" fi if [ "$SOURCE" = "cachyos" ]; then @@ -1636,6 +1662,14 @@ if [ "$HARDENED" = "1" ]; then VER_SELECTED=1 fi +if [ -n "$AUTOFDO_PROFILE" ] && [ "$AUTOFDO_COLLECT" = "1" ]; then + fatal "${RED}${BALLOT_X}Fatal:${NORMAL} --autofdo and --autofdo-collect cannot be combined" +fi +if [ -n "$AUTOFDO_PROFILE" ]; then + [ -f "$AUTOFDO_PROFILE" ] || fatal "${RED}${BALLOT_X}Fatal:${NORMAL} --autofdo profile not found: ${AUTOFDO_PROFILE}" + AUTOFDO_PROFILE=$(realpath -- "$AUTOFDO_PROFILE") +fi + if [ "$BASE_CONFIG" = "cachyos" ] && [ "$SOURCE" != "cachyos" ]; then fatal "${RED}${BALLOT_X}Fatal:${NORMAL} --base-config cachyos requires --source cachyos" fi @@ -1676,7 +1710,15 @@ if [ "$BBR3" = "1" ] && [ "$SOURCE" != "cachyos" ]; then fatal "${RED}${BALLOT_X}Fatal:${NORMAL} --bbr3 requires --source cachyos" fi -if [ "$LTO" != "none" ]; then +if [ "$KERNEL_SCHED" = "bmq" ] && [ "$SCHED_EXT" = "1" ]; then + fatal "${RED}${BALLOT_X}Fatal:${NORMAL} --sched-ext is not available with BMQ (SCHED_ALT disables SCHED_CLASS_EXT)" +fi + +if [ "$CONFIG_ONLY" = "1" ] && [ "$mode" = "uninstall" ]; then + fatal "${RED}${BALLOT_X}Fatal:${NORMAL} --config-only cannot be combined with --uninstall" +fi + +if [ "$LTO" != "none" ] || [ -n "$AUTOFDO_PROFILE" ] || [ "$AUTOFDO_COLLECT" = "1" ]; then MAKE_LLVM="LLVM=1 LLVM_IAS=1" else MAKE_LLVM="" @@ -1908,8 +1950,8 @@ else exit 1; fi -if [ "$LTO" != "none" ]; then - # LTO needs clang/LLD; install them only when requested. +if [ "$LTO" != "none" ] || [ -n "$AUTOFDO_PROFILE" ] || [ "$AUTOFDO_COLLECT" = "1" ]; then + # LTO and AutoFDO need clang/LLD; install them only when requested. INSTALL_PKGS+=" clang lld llvm" fi @@ -2387,6 +2429,22 @@ apply_cachyos_bore_patch() { return 0 } +apply_cachyos_bmq_patch() { + [ "$SOURCE" = "cachyos" ] && [ "$KERNEL_SCHED" = "bmq" ] || return 0 + local patch="cachyos-bmq.patch" + # shellcheck disable=SC2086 + ${CURLBIN:-curl} -fsSL --retry 3 -o "$patch" "$CACHYOS_BMQ_PATCH_URL" 2>/dev/null || + fatal "Could not download the BMQ scheduler patch" + if ! printf '%s %s\n' "$CACHYOS_BMQ_PATCH_SHA256" "$patch" | sha256sum -c - >/dev/null 2>&1; then + fatal "BMQ scheduler patch checksum mismatch (expected ${CACHYOS_BMQ_PATCH_SHA256})" + fi + if ! patch -p1 < "$patch" >/dev/null; then + fatal "BMQ scheduler patch failed to apply to the CachyOS source" + fi + log_success "BMQ scheduler patch applied" + return 0 +} + apply_hardened_patch() { [ "$HARDENED" = "1" ] || return 0 local patch="linux-hardened.patch" @@ -2408,11 +2466,22 @@ apply_hardened_patch() { configure_cachyos_source() { [ "$SOURCE" = "cachyos" ] || return 0 scripts/config --enable CACHY - if [ "$KERNEL_SCHED" = "bore" ]; then - scripts/config --enable SCHED_BORE - else - scripts/config --disable SCHED_BORE - fi + case "$KERNEL_SCHED" in + bore) + scripts/config --enable SCHED_BORE + scripts/config --disable SCHED_ALT + ;; + bmq) + scripts/config --disable SCHED_BORE + scripts/config --enable SCHED_ALT + scripts/config --enable SCHED_BMQ + scripts/config --disable SCHED_PDS + ;; + *) + scripts/config --disable SCHED_BORE + scripts/config --disable SCHED_ALT + ;; + esac } assert_cachyos_source() { @@ -2420,10 +2489,23 @@ assert_cachyos_source() { local got got=$(scripts/config --state CACHY) [ "$got" = "y" ] || fatal "CachyOS source: CONFIG_CACHY is '${got}', expected 'y'" - if [ "$KERNEL_SCHED" = "bore" ]; then - got=$(scripts/config --state SCHED_BORE) - [ "$got" = "y" ] || fatal "CachyOS source: CONFIG_SCHED_BORE is '${got}', expected 'y'" - fi + case "$KERNEL_SCHED" in + bore) + got=$(scripts/config --state SCHED_BORE) + [ "$got" = "y" ] || fatal "CachyOS source: CONFIG_SCHED_BORE is '${got}', expected 'y'" + ;; + bmq) + got=$(scripts/config --state SCHED_ALT) + [ "$got" = "y" ] || fatal "BMQ: CONFIG_SCHED_ALT is '${got}', expected 'y'" + got=$(scripts/config --state SCHED_BMQ) + [ "$got" = "y" ] || fatal "BMQ: CONFIG_SCHED_BMQ is '${got}', expected 'y'" + got=$(scripts/config --state SCHED_PDS) + case "$got" in + n | undef) ;; + *) fatal "BMQ: CONFIG_SCHED_PDS is '${got}', expected 'n'" ;; + esac + ;; + esac } configure_cpu() { @@ -2739,6 +2821,14 @@ configure_lto() { esac } +configure_autofdo() { + [ -n "$AUTOFDO_PROFILE" ] || [ "$AUTOFDO_COLLECT" = "1" ] || return 0 + local got + got=$(scripts/config --state AUTOFDO_CLANG) + [ "$got" != "undef" ] || fatal "AutoFDO: CONFIG_AUTOFDO_CLANG is not available in this kernel source (use a 7.x source)" + scripts/config --enable AUTOFDO_CLANG +} + assert_lto() { [ "$LTO" = "none" ] && return 0 local got @@ -2872,6 +2962,13 @@ assert_hardened() { [ "$got" = "y" ] || fatal "Hardened: CONFIG_SECURITY_PERF_EVENTS_RESTRICT is '${got}', expected 'y'" } +assert_autofdo() { + [ -n "$AUTOFDO_PROFILE" ] || [ "$AUTOFDO_COLLECT" = "1" ] || return 0 + local got + got=$(scripts/config --state AUTOFDO_CLANG) + [ "$got" = "y" ] || fatal "AutoFDO: CONFIG_AUTOFDO_CLANG is '${got}', expected 'y'" +} + install_kernel() { if [ "$INTERACTIVE_MODE" = "on" ]; then clear @@ -3003,6 +3100,7 @@ install_kernel() { if [ "$SOURCE" = "cachyos" ]; then apply_cachyos_bore_patch fi + apply_cachyos_bmq_patch apply_hardened_patch if [ "$CONFIG_INTERACTIVE" = "1" ]; then log_info "Running make $CONFIG_OPTION; save the configuration and exit to continue." @@ -3045,6 +3143,7 @@ install_kernel() { fi configure_cpu configure_lto + configure_autofdo configure_kernel_name if [ -n "$PROFILE" ]; then log_info "Applying optimization profile: ${PROFILE}" @@ -3078,6 +3177,10 @@ install_kernel() { assert_sched_ext assert_rt assert_hardened + assert_autofdo + if [ "$CONFIG_ONLY" = "1" ]; then + exit 0 + fi echo # Compilation log_debug "Phase 5 of 6: Compilation" @@ -3093,6 +3196,9 @@ install_kernel() { # Remember where the build output starts in the log, and handle a # failure here (instead of run_ok's tail) to point at the real error build_log_start=$(wc -l < "$RUN_LOG" 2>/dev/null || echo 0) + if [ -n "$AUTOFDO_PROFILE" ]; then + export CLANG_AUTOFDO_PROFILE="$AUTOFDO_PROFILE" + fi if ! RUN_ERRORS_FATAL='' run_ok "echo $DEF_COM_NUM | ${MAKE_LLVM:+$MAKE_LLVM }make $build_target -j${NPROC}" "Compiling The Linux Kernel source code..."; then report_build_failure "$build_log_start" exit 1 @@ -3104,6 +3210,10 @@ install_kernel() { printf "%s \\n" "${GREEN}▣▣▣▣▣${YELLOW}▣${NORMAL} Phase ${YELLOW}6${NORMAL} of ${GREEN}6${NORMAL}: Kernel Installation" cd - 1>/dev/null 2>&1 || exit 1 ) || exit 1 # a failed configure/build step ends the run here, not at phase 6 + if [ "$CONFIG_ONLY" = "1" ]; then + log_success "Configuration-only run finished (no build)" + exit 0 + fi fi # if [ $LINUX_VER_NAME = "Mainline" ]; then @@ -3137,6 +3247,19 @@ install_kernel() { log_warning "Could not read the kernel release from the built package or the configured tree; using ${LINUX_FULL_VER}. The built packages may be named differently." fi log_info "Kernel release: ${LINUX_FULL_VER}" + if [ "$AUTOFDO_COLLECT" = "1" ]; then + vmlinux_path="$INSTALL_DIR/linux-${LINUX_VER}/vmlinux" + vmlinux_dest="$(dirname "$INSTALL_DIR")/vmlinux-${LINUX_FULL_VER}" + if [ -f "$vmlinux_path" ]; then + if cp -- "$vmlinux_path" "$vmlinux_dest"; then + log_info "AutoFDO vmlinux saved to ${vmlinux_dest}" + else + log_warning "AutoFDO: could not save ${vmlinux_path} to ${vmlinux_dest}" + fi + else + log_warning "AutoFDO: ${vmlinux_path} not found; the profile cannot be generated without it" + fi + fi # Install # Install through the package manager so that runtime dependencies of the # kernel packages (kmod, linux-base, initramfs-tools, ...) are pulled in on diff --git a/tests/test_autofdo.sh b/tests/test_autofdo.sh new file mode 100644 index 0000000..b5bbf50 --- /dev/null +++ b/tests/test_autofdo.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# AutoFDO: CLI validation, config wiring, profile/vmlinux plumbing. +# shellcheck disable=SC2317 # helpers are invoked through check +# shellcheck disable=SC2016 # script text is matched with single quotes +set -u +REPO=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd) +SCRIPT="$REPO/kernel_installer.sh" +fail=0 +check() { + local desc=$1; shift + if "$@"; then echo "ok: $desc"; else echo "FAIL: $desc"; fail=1; fi +} + +out=$(setsid bash "$SCRIPT" --autofdo &1) +check "--autofdo requires an argument" grep -q 'requires an argument' <<<"$out" + +out=$(setsid bash "$SCRIPT" --autofdo /no/such/profile &1) +check "--autofdo rejects a missing profile" grep -q 'profile not found' <<<"$out" + +out=$(setsid bash "$SCRIPT" --autofdo /etc/hostname --autofdo-collect &1) +check "--autofdo and --autofdo-collect are exclusive" \ + grep -q 'cannot be combined' <<<"$out" + +out=$(setsid bash "$SCRIPT" --help &1) +check "help documents --autofdo" grep -q -- '--autofdo' <<<"$out" +check "help documents --autofdo-collect" grep -q -- '--autofdo-collect' <<<"$out" + +# --- configure_autofdo ---------------------------------------------------- +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +mkdir -p "$work/scripts" +cat > "$work/scripts/config" <<'SH' +#!/usr/bin/env bash +printf '%s\n' "$*" >> "${AFDO_LOG:?}" +if [ "$1" = "--state" ] && [ "$2" = "AUTOFDO_CLANG" ]; then + echo "${FAKE_AUTOFDO_STATE:-n}" +fi +exit 0 +SH +chmod +x "$work/scripts/config" +export AFDO_LOG="$work/calls" +cd "$work" || exit 1 +fatal() { echo "FATAL: $*" >&2; return 1; } +func=$(sed -n '/^configure_autofdo() {/,/^}/p' "$SCRIPT") +[ -n "$func" ] || { echo "FAIL: configure_autofdo() not found"; exit 1; } +# shellcheck disable=SC1090 +eval "$func" +export AUTOFDO_COLLECT=1 AUTOFDO_PROFILE='' +: > "$AFDO_LOG" +configure_autofdo +check "autofdo enables AUTOFDO_CLANG" grep -qx -- '--enable AUTOFDO_CLANG' "$AFDO_LOG" + +# --- build plumbing ------------------------------------------------------- +check "the build exports CLANG_AUTOFDO_PROFILE" \ + grep -q 'export CLANG_AUTOFDO_PROFILE' "$SCRIPT" +check "the profile path is resolved with realpath" \ + grep -q 'realpath -- "$AUTOFDO_PROFILE"' "$SCRIPT" +check "the collect build preserves vmlinux" \ + grep -q 'AutoFDO vmlinux saved to' "$SCRIPT" +check "the collect build names the saved vmlinux after the release" \ + grep -q 'vmlinux-${LINUX_FULL_VER}' "$SCRIPT" + +exit "$fail" diff --git a/tests/test_bmq.sh b/tests/test_bmq.sh new file mode 100644 index 0000000..d06d7be --- /dev/null +++ b/tests/test_bmq.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +# BMQ scheduler: pinned patch, configure wiring. +# shellcheck disable=SC2317 # helpers are invoked through check +set -u +REPO=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd) +SCRIPT="$REPO/kernel_installer.sh" +fail=0 +check() { + local desc=$1; shift + if "$@"; then echo "ok: $desc"; else echo "FAIL: $desc"; fail=1; fi +} + +out=$(setsid bash "$SCRIPT" --sched bmq &1) +check "bmq without a CachyOS source is rejected" \ + grep -q 'requires --source cachyos' <<<"$out" + +out=$(setsid bash "$SCRIPT" --sched bmq --source cachyos --sched-ext &1) +check "bmq rejects --sched-ext" grep -q 'not available with BMQ' <<<"$out" + +out=$(setsid bash "$SCRIPT" --help &1) +check "help documents bmq" grep -q -- 'bmq' <<<"$out" + +# --- pinned patch --------------------------------------------------------- +# shellcheck disable=SC2016 # script text, not shell expansion +check "BMQ patch is the pinned 7.3 PRJC patch" \ + grep -qF '7.3/sched/0001-prjc-cachy.patch' "$SCRIPT" +check "BMQ patch SHA256 is pinned" \ + grep -q '^CACHYOS_BMQ_PATCH_SHA256=0d3c61ec' "$SCRIPT" +apply=$(sed -n '/^apply_cachyos_bmq_patch() {/,/^}/p' "$SCRIPT") +check "apply_cachyos_bmq_patch verifies the checksum" \ + grep -q 'CACHYOS_BMQ_PATCH_SHA256' <<<"$apply" +check "apply_cachyos_bmq_patch applies with patch -p1" \ + grep -q 'patch -p1' <<<"$apply" +check "apply_cachyos_bmq_patch is defined before its call site" \ + test "$(grep -n '^apply_cachyos_bmq_patch() {' "$SCRIPT" | cut -d: -f1)" \ + -lt "$(grep -n '^ apply_cachyos_bmq_patch$' "$SCRIPT" | cut -d: -f1)" + +# --- configure_cachyos_source -------------------------------------------- +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +mkdir -p "$work/scripts" +cat > "$work/scripts/config" <<'SH' +#!/usr/bin/env bash +printf '%s\n' "$*" >> "${BMQ_LOG:?}" +exit 0 +SH +chmod +x "$work/scripts/config" +export BMQ_LOG="$work/calls" +cd "$work" || exit 1 +func=$(sed -n '/^configure_cachyos_source() {/,/^}/p' "$SCRIPT") +[ -n "$func" ] || { echo "FAIL: configure_cachyos_source() not found"; exit 1; } +# shellcheck disable=SC1090 +eval "$func" +has() { grep -qx -- "$1" "$BMQ_LOG"; } + +: > "$BMQ_LOG" +export SOURCE=cachyos KERNEL_SCHED=bmq +configure_cachyos_source +check "bmq enables CACHY" has '--enable CACHY' +check "bmq enables SCHED_ALT" has '--enable SCHED_ALT' +check "bmq enables SCHED_BMQ" has '--enable SCHED_BMQ' +check "bmq disables SCHED_PDS" has '--disable SCHED_PDS' +check "bmq disables SCHED_BORE" has '--disable SCHED_BORE' + +: > "$BMQ_LOG" +export KERNEL_SCHED=bore +configure_cachyos_source +check "bore enables SCHED_BORE" has '--enable SCHED_BORE' +check "bore disables SCHED_ALT" has '--disable SCHED_ALT' + +exit "$fail" diff --git a/tests/test_cachyos_cli.sh b/tests/test_cachyos_cli.sh index 2d2eebc..b757e78 100755 --- a/tests/test_cachyos_cli.sh +++ b/tests/test_cachyos_cli.sh @@ -13,6 +13,9 @@ check() { out=$(setsid bash "$SCRIPT" --sched bore &1) check "bore without a CachyOS source is rejected" grep -q 'requires --source cachyos' <<<"$out" +out=$(setsid bash "$SCRIPT" --sched bmq &1) +check "bmq without a CachyOS source is rejected" grep -q 'requires --source cachyos' <<<"$out" + out=$(setsid bash "$SCRIPT" --base-config cachyos &1) check "cachyos base config without the source is rejected" grep -q 'requires --source cachyos' <<<"$out" @@ -40,6 +43,7 @@ check "an invalid kernel tag is rejected" grep -q 'must use lowercase' <<<"$out" out=$(setsid bash "$SCRIPT" --help &1) check "help documents --source" grep -q -- '--source' <<<"$out" check "help documents --sched" grep -q -- '--sched' <<<"$out" +check "help documents bmq" grep -q -- 'bmq' <<<"$out" check "help documents --cpu" grep -q -- '--cpu' <<<"$out" check "help documents --lto" grep -q -- '--lto' <<<"$out" check "help documents --kernel-tag" grep -q -- '--kernel-tag' <<<"$out" diff --git a/tests/test_config_flags.sh b/tests/test_config_flags.sh index ac62bd4..5739cec 100644 --- a/tests/test_config_flags.sh +++ b/tests/test_config_flags.sh @@ -81,7 +81,7 @@ for fn in versionToInt configure_hz configure_bbr3 configure_binder configure_sc done # shellcheck disable=SC1090 eval "$funcs" -export LINUX_VER=7.3.4 +export LINUX_VER=7.3.4 KERNEL_SCHED=eevdf has() { grep -qx -- "$1" "$CFG_LOG"; } : > "$CFG_LOG" @@ -165,6 +165,10 @@ assert_profile workstation check "rt drops the THP assertions" \ not grep -qx 'TRANSPARENT_HUGEPAGE_MADVISE=y' "$work/profile-expect" check "rt drops the fixed PREEMPT model" not grep -qx 'PREEMPT=y' "$work/profile-expect" +export KERNEL_SCHED=bmq +assert_profile workstation +check "bmq drops the PSI assertion" not grep -qx 'PSI=y' "$work/profile-expect" +check "bmq keeps the CFS bandwidth assertion" grep -qx 'CFS_BANDWIDTH=y' "$work/profile-expect" # --- call order ---------------------------------------------------------- line_of() { grep -n "$1" "$SCRIPT" | head -1 | cut -d: -f1; } diff --git a/tests/test_config_only.sh b/tests/test_config_only.sh new file mode 100755 index 0000000..43c9b6b --- /dev/null +++ b/tests/test_config_only.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# --config-only: fast configuration validation. +set -u +REPO=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd) +SCRIPT="$REPO/kernel_installer.sh" +fail=0 +check() { + local desc=$1; shift + if "$@"; then echo "ok: $desc"; else echo "FAIL: $desc"; fail=1; fi +} + +out=$(setsid bash "$SCRIPT" --config-only --uninstall &1) +check "config-only rejects uninstall" grep -q 'cannot be combined' <<<"$out" + +out=$(setsid bash "$SCRIPT" --help &1) +check "help documents --config-only" grep -q -- '--config-only' <<<"$out" + +first=$(grep -n 'CONFIG_ONLY" = "1"' "$SCRIPT" | head -1 | cut -d: -f1) +compile=$(grep -n 'Compiling The Linux Kernel source code' "$SCRIPT" | head -1 | cut -d: -f1) +check "config-only exits before compilation" test "$first" -lt "$compile" + +exit "$fail" diff --git a/tests/test_kernel_tag.sh b/tests/test_kernel_tag.sh index b6f62c2..40ee478 100755 --- a/tests/test_kernel_tag.sh +++ b/tests/test_kernel_tag.sh @@ -13,7 +13,7 @@ func=$(sed -n '/^build_kernel_tag() {/,/^}/p' "$SCRIPT") eval "$func" tag_for() { export SOURCE=$1 PROFILE=$2 KERNEL_SCHED=$3 CPU=$4 LTO=$5 KERNEL_TAG=${6:-} - export HZ_OVERRIDE=${7:-} BBR3=${8:-0} BINDER=${9:-0} SCHED_EXT=${10:-0} RT=${11:-0} HARDENED=${12:-0} + export HZ_OVERRIDE=${7:-} BBR3=${8:-0} BINDER=${9:-0} SCHED_EXT=${10:-0} RT=${11:-0} HARDENED=${12:-0} AUTOFDO_PROFILE=${13:-} AUTOFDO_COLLECT=${14:-0} build_kernel_tag printf '%s' "$KERNEL_TAG" } @@ -22,6 +22,7 @@ check "cachyos BORE workstation native thin" test "$(tag_for cachyos workstation check "vanilla desktop" test "$(tag_for vanilla desktop eevdf generic none)" = "-vanilla-desktop" check "vanilla default stays untagged" test -z "$(tag_for vanilla '' eevdf generic none)" check "cachyos no profile" test "$(tag_for cachyos '' eevdf generic none)" = "-cachyos" +check "cachyos BMQ" test "$(tag_for cachyos workstation bmq generic none)" = "-cachyos-workstation-bmq" check "config flags extend the tag" \ test "$(tag_for cachyos workstation bore generic thin '' 600 1 1 1)" = "-cachyos-workstation-bore-lto-hz600-bbr3-binder-sched-ext" check "rt extends the tag" \ @@ -30,6 +31,10 @@ check "all flags extend the tag in order" \ test "$(tag_for cachyos workstation bore generic thin '' 600 1 1 1 1)" = "-cachyos-workstation-bore-lto-hz600-bbr3-binder-sched-ext-rt" check "hardened keeps the version marker from the patch" \ test "$(tag_for vanilla workstation eevdf generic none '' '' 0 0 0 0 1)" = "-vanilla-workstation" +check "autofdo extends the tag" \ + test "$(tag_for cachyos workstation bore generic none '' '' 0 0 0 0 0 /tmp/profile)" = "-cachyos-workstation-bore-autofdo" +check "autofdo collect extends the tag" \ + test "$(tag_for vanilla workstation eevdf generic none '' '' 0 0 0 0 0 '' 1)" = "-vanilla-workstation-autofdo-collect" check "hz alone names the kernel" test "$(tag_for vanilla '' eevdf generic none '' 1000)" = "-hz1000" check "binder and sched-ext extend the tag" \ test "$(tag_for vanilla workstation eevdf generic none '' '' 0 1 1)" = "-vanilla-workstation-binder-sched-ext"