|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# ----------------------------------------------------------------------------- |
| 4 | +# Description: |
| 5 | +# Apply Arch/CachyOS performance tuning when NVIDIA hardware is present. |
| 6 | +# |
| 7 | + |
| 8 | +enable_unit_if_available() { |
| 9 | + local unit=$1 |
| 10 | + if systemctl list-unit-files --no-legend 2>/dev/null | awk '{print $1}' | grep -Fxq "$unit"; then |
| 11 | + if sudo systemctl enable --now "$unit" >/dev/null 2>&1; then |
| 12 | + log_info "Enabled systemd unit: $unit" |
| 13 | + else |
| 14 | + log_warn "Unable to enable $unit" |
| 15 | + fi |
| 16 | + else |
| 17 | + log_warn "Unit $unit not found; skipping" |
| 18 | + fi |
| 19 | +} |
| 20 | + |
| 21 | +skip_with_reason() { |
| 22 | + log_warn "$1" |
| 23 | + return 0 |
| 24 | +} |
| 25 | + |
| 26 | +main() { |
| 27 | + if [[ "${OSTYPE:-}" != linux-gnu* ]]; then |
| 28 | + skip_with_reason "Skipping Arch performance tuning: requires Linux" |
| 29 | + return |
| 30 | + fi |
| 31 | + |
| 32 | + if [[ ! -r /etc/os-release ]]; then |
| 33 | + skip_with_reason "Skipping Arch performance tuning: /etc/os-release not found" |
| 34 | + return |
| 35 | + fi |
| 36 | + |
| 37 | + # shellcheck disable=SC1091 |
| 38 | + . /etc/os-release |
| 39 | + |
| 40 | + local id_lower id_like_lower |
| 41 | + id_lower=$(echo "${ID:-}" | tr '[:upper:]' '[:lower:]') |
| 42 | + id_like_lower=$(echo "${ID_LIKE:-}" | tr '[:upper:]' '[:lower:]') |
| 43 | + |
| 44 | + if [[ "$id_lower" != "arch" && "$id_lower" != "cachyos" && "$id_like_lower" != *"arch"* ]]; then |
| 45 | + skip_with_reason "Skipping Arch performance tuning: ${NAME:-unknown} is not Arch based" |
| 46 | + return |
| 47 | + fi |
| 48 | + |
| 49 | + if ! command -v pacman >/dev/null 2>&1; then |
| 50 | + skip_with_reason "Skipping Arch performance tuning: pacman not available" |
| 51 | + return |
| 52 | + fi |
| 53 | + |
| 54 | + if ! command -v nvidia-smi >/dev/null 2>&1; then |
| 55 | + skip_with_reason "Skipping Arch performance tuning: NVIDIA utilities not detected" |
| 56 | + return |
| 57 | + fi |
| 58 | + |
| 59 | + log_info "Applying performance profile for ${NAME:-Arch Linux}" |
| 60 | + |
| 61 | + local -a packages=( |
| 62 | + cpupower |
| 63 | + fastfetch |
| 64 | + gamemode |
| 65 | + lib32-gamemode |
| 66 | + lib32-nvidia-utils |
| 67 | + nvidia-settings |
| 68 | + nvidia-utils |
| 69 | + nvtop |
| 70 | + vulkan-tools |
| 71 | + ) |
| 72 | + |
| 73 | + log_info "Ensuring required packages are installed" |
| 74 | + sudo pacman -S --noconfirm --needed "${packages[@]}" |
| 75 | + |
| 76 | + log_info "Configuring cpupower defaults" |
| 77 | + sudo tee /etc/default/cpupower >/dev/null <<'EOF' |
| 78 | +# Managed by dotfiles performance install |
| 79 | +governor='schedutil' |
| 80 | +min_freq='0' |
| 81 | +max_freq='0' |
| 82 | +EOF |
| 83 | + enable_unit_if_available "cpupower.service" |
| 84 | + |
| 85 | + log_info "Writing sysctl overrides" |
| 86 | + sudo tee /etc/sysctl.d/99-dots-performance.conf >/dev/null <<'EOF' |
| 87 | +# Managed by dotfiles performance install |
| 88 | +vm.swappiness = 10 |
| 89 | +vm.vfs_cache_pressure = 50 |
| 90 | +kernel.unprivileged_userns_clone = 1 |
| 91 | +EOF |
| 92 | + sudo sysctl -p /etc/sysctl.d/99-dots-performance.conf >/dev/null |
| 93 | + |
| 94 | + log_info "Setting transparent huge page policy" |
| 95 | + sudo tee /etc/tmpfiles.d/dots-performance-thp.conf >/dev/null <<'EOF' |
| 96 | +# Managed by dotfiles performance install |
| 97 | +w /sys/kernel/mm/transparent_hugepage/enabled - - - - madvise |
| 98 | +w /sys/kernel/mm/transparent_hugepage/defrag - - - - madvise |
| 99 | +EOF |
| 100 | + sudo systemd-tmpfiles --create /etc/tmpfiles.d/dots-performance-thp.conf |
| 101 | + |
| 102 | + log_info "Configuring NVMe scheduler" |
| 103 | + sudo tee /etc/udev/rules.d/60-dots-nvme-scheduler.rules >/dev/null <<'EOF' |
| 104 | +# Managed by dotfiles performance install |
| 105 | +ACTION=="add|change", KERNEL=="nvme[0-9]n[0-9]", ATTR{queue/scheduler}="none" |
| 106 | +EOF |
| 107 | + sudo udevadm control --reload-rules |
| 108 | + sudo udevadm trigger --subsystem-match=block --action=change |
| 109 | + |
| 110 | + log_info "Applying NVIDIA module defaults" |
| 111 | + sudo tee /etc/modprobe.d/dots-nvidia-performance.conf >/dev/null <<'EOF' |
| 112 | +# Managed by dotfiles performance install |
| 113 | +options nvidia NVreg_UsePageAttributeTable=1 |
| 114 | +options nvidia NVreg_InitializeSystemMemoryAllocations=0 |
| 115 | +options nvidia NVreg_EnableGpuFirmware=1 |
| 116 | +options nvidia_drm modeset=1 fbdev=1 |
| 117 | +EOF |
| 118 | + |
| 119 | + if command -v mkinitcpio >/dev/null 2>&1; then |
| 120 | + log_info "Rebuilding initramfs for NVIDIA modules" |
| 121 | + sudo mkinitcpio -P >/dev/null |
| 122 | + fi |
| 123 | + |
| 124 | + enable_unit_if_available "nvidia-persistenced.service" |
| 125 | + enable_unit_if_available "fstrim.timer" |
| 126 | + |
| 127 | + local config_home="${XDG_CONFIG_HOME:-$HOME/.config}" |
| 128 | + mkdir -p "$config_home" |
| 129 | + |
| 130 | + log_info "Writing GameMode configuration" |
| 131 | + cat <<'EOF' > "$config_home/gamemode.ini" |
| 132 | +# Managed by dotfiles performance install |
| 133 | +[general] |
| 134 | +renice=10 |
| 135 | +ioprio=0 |
| 136 | +inhibit_screensaver=1 |
| 137 | +
|
| 138 | +[cpu] |
| 139 | +desiredgov=schedutil |
| 140 | +
|
| 141 | +[gpu] |
| 142 | +apply_gpu_optimisations=accept |
| 143 | +gpu_device=0 |
| 144 | +nv_powermizer_mode=1 |
| 145 | +
|
| 146 | +[custom] |
| 147 | +start=logger -t dots-perf "GameMode activated" |
| 148 | +end=logger -t dots-perf "GameMode deactivated" |
| 149 | +EOF |
| 150 | + |
| 151 | + log_info "Configuring Proton/DXVK defaults" |
| 152 | + local env_dir="$config_home/environment.d" |
| 153 | + mkdir -p "$env_dir" |
| 154 | + cat <<'EOF' > "$env_dir/99-dots-performance.conf" |
| 155 | +# Managed by dotfiles performance install |
| 156 | +PROTON_ENABLE_NVAPI=1 |
| 157 | +PROTON_HIDE_NVIDIA_GPU=0 |
| 158 | +DXVK_ASYNC=1 |
| 159 | +__GL_THREADED_OPTIMIZATION=1 |
| 160 | +EOF |
| 161 | + |
| 162 | + log_pass "Arch performance tuning complete" |
| 163 | +} |
| 164 | + |
| 165 | +main "$@" |
| 166 | +unset -f main |
| 167 | +unset -f enable_unit_if_available |
| 168 | +unset -f skip_with_reason |
0 commit comments