arch laptop fixes

This commit is contained in:
Krzysztof Rudnicki 2026-02-20 00:13:25 +01:00
parent 7523c708d1
commit aa8501cc49
4 changed files with 715 additions and 5 deletions

View File

@ -0,0 +1,371 @@
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
# shellcheck source=../lib/common.sh
source "$SCRIPT_DIR/../lib/common.sh"
REPORT_DIR="${HOME}/.local/state/system-diagnostics"
REPORT_FILE="$REPORT_DIR/arch-performance-$(date +%Y%m%d_%H%M%S).log"
APPLY_SAFE_FIXES=false
INSTALL_TOOLS=false
declare -a FINDINGS=()
declare -a ACTIONS=()
usage() {
cat << 'EOF'
diagnose_arch_performance.sh - Diagnose common causes of Arch Linux slowness/instability
Usage:
diagnose_arch_performance.sh [OPTIONS]
Options:
--apply-safe-fixes Apply conservative fixes (requires sudo)
--install-tools Install optional diagnostics packages (requires sudo)
-h, --help Show help
Safe fixes applied when --apply-safe-fixes is used:
- Enable/start fstrim.timer if missing
- Resolve TLP vs power-profiles-daemon conflict (keeps power-profiles-daemon)
- Vacuum journal logs if they exceed 1GiB
Notes:
- Script does not reboot automatically.
- Some checks are informational and provide next-step commands.
EOF
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--apply-safe-fixes)
APPLY_SAFE_FIXES=true
shift
;;
--install-tools)
INSTALL_TOOLS=true
shift
;;
-h | --help)
usage
exit 0
;;
*)
log_error "Unknown option: $1"
usage
exit 2
;;
esac
done
}
add_finding() {
FINDINGS+=("$1")
log_warn "$1"
}
add_action() {
ACTIONS+=("$1")
log_info "$1"
}
run_and_log() {
local header="$1"
shift
{
echo
echo "=== $header ==="
"$@" 2>&1 || true
} >> "$REPORT_FILE"
}
check_root_if_needed() {
if [[ $APPLY_SAFE_FIXES == "true" || $INSTALL_TOOLS == "true" ]]; then
require_root "$@"
fi
}
install_optional_tools() {
if [[ $INSTALL_TOOLS != "true" ]]; then
return
fi
local packages=(lm_sensors smartmontools nvtop iotop powertop)
log_info "Installing optional diagnostic packages: ${packages[*]}"
pacman -S --needed --noconfirm "${packages[@]}"
}
collect_basics() {
run_and_log "Kernel" uname -a
run_and_log "Uptime" uptime
run_and_log "Memory" free -h
run_and_log "Swap" swapon --show
run_and_log "CPU (lscpu)" lscpu
run_and_log "Disk Usage" df -h /
run_and_log "Boot Time" systemd-analyze
run_and_log "Failed Units" systemctl --failed --no-pager
run_and_log "Recent Errors (this boot)" journalctl -b -p err --no-pager -n 200
local cpu_count
cpu_count=$(getconf _NPROCESSORS_ONLN 2> /dev/null || echo 1)
local load1
load1=$(awk '{print int($1)}' /proc/loadavg 2> /dev/null || echo 0)
if [[ ${load1:-0} -ge ${cpu_count:-1} ]]; then
add_finding "1-minute load average is at/above CPU thread count (${load1}/${cpu_count}); background tasks may be saturating the system."
fi
local failed_count
failed_count=$(systemctl --failed --no-legend 2> /dev/null | wc -l || true)
failed_count=${failed_count//[[:space:]]/}
if [[ ${failed_count:-0} -gt 0 ]]; then
add_finding "One or more systemd units are failed (${failed_count}); failed services can cause repeated retries and instability."
fi
local acpi_error_count
acpi_error_count=$(journalctl -b -p err --no-pager 2> /dev/null | grep -ic 'acpi' || true)
if [[ ${acpi_error_count:-0} -ge 5 ]]; then
add_finding "Frequent ACPI errors detected in current boot (${acpi_error_count}); BIOS/firmware update may improve stability."
fi
local top_snapshot
top_snapshot=$(ps -eo pid,comm,%cpu,%mem --sort=-%cpu | head -n 12 || true)
{
echo
echo "=== Top CPU Processes ==="
echo "$top_snapshot"
} >> "$REPORT_FILE"
local xorg_cpu
xorg_cpu=$(ps -C Xorg -o %cpu= | awk '{sum+=$1} END {printf "%.0f", sum+0}' || echo 0)
if [[ ${xorg_cpu:-0} -ge 20 ]]; then
add_finding "Xorg is using high CPU (${xorg_cpu}%); desktop/compositor/GPU driver path may be a primary slowdown source."
fi
}
check_cpu_governor() {
local gov_files
gov_files=$(find /sys/devices/system/cpu -maxdepth 3 -name scaling_governor 2> /dev/null || true)
if [[ -z $gov_files ]]; then
add_action "CPU governor files not found (may be unsupported on this platform)."
return
fi
local summary
summary=$(awk '{count[$1]++} END {for (g in count) printf "%s:%d ", g, count[g]}' $gov_files 2> /dev/null || true)
echo "CPU governor summary: ${summary:-unknown}" >> "$REPORT_FILE"
if grep -q '^powersave$' $gov_files 2> /dev/null; then
add_finding "CPU governor includes 'powersave' on one or more cores; this can make high-end hardware feel slow."
fi
}
check_thermal_state() {
if has_cmd sensors; then
run_and_log "Temperatures (sensors)" sensors
else
add_action "Install lm_sensors and run 'sensors' to verify thermal throttling."
fi
if has_cmd dmesg; then
local therm_hits
therm_hits=$(dmesg | grep -Ei 'throttl|thermal|overheat|cpu clock throttled' | tail -n 30 || true)
if [[ -n $therm_hits ]]; then
add_finding "Kernel logs show thermal/throttling related messages."
{
echo
echo "=== Thermal/Throttling dmesg excerpts ==="
echo "$therm_hits"
} >> "$REPORT_FILE"
fi
fi
}
check_power_services() {
local tlp_enabled="false"
local ppd_enabled="false"
if systemctl is-enabled tlp.service > /dev/null 2>&1; then
tlp_enabled="true"
fi
if systemctl is-enabled power-profiles-daemon.service > /dev/null 2>&1; then
ppd_enabled="true"
fi
echo "Power services: tlp=${tlp_enabled}, power-profiles-daemon=${ppd_enabled}" >> "$REPORT_FILE"
if [[ $tlp_enabled == "true" && $ppd_enabled == "true" ]]; then
add_finding "Both TLP and power-profiles-daemon are enabled; they often conflict and cause inconsistent performance."
fi
if [[ $tlp_enabled == "false" && $ppd_enabled == "false" ]]; then
add_action "No power management daemon is enabled; consider installing/enabling power-profiles-daemon for predictable AC/battery behavior."
fi
}
check_storage_health() {
run_and_log "Block Devices" lsblk -o NAME,MODEL,ROTA,SIZE,TYPE,MOUNTPOINT,FSTYPE
if has_cmd fstrim; then
run_and_log "fstrim dry-run" fstrim -av --dry-run
fi
if systemctl is-enabled fstrim.timer > /dev/null 2>&1; then
add_action "fstrim.timer is enabled (good for SSD performance longevity)."
else
add_finding "fstrim.timer is not enabled; SSD maintenance trimming may be missing."
fi
if has_cmd smartctl; then
local root_disk
root_disk=$(findmnt -n -o SOURCE / | sed 's/[0-9]*$//' | sed 's/p$//' || true)
if [[ -n ${root_disk:-} && -b $root_disk ]]; then
run_and_log "SMART Summary ($root_disk)" smartctl -H "$root_disk"
fi
else
add_action "Install smartmontools and run SMART health checks for your SSD/NVMe."
fi
}
check_memory_pressure() {
local mem_total mem_available swap_total swap_free
mem_total=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
mem_available=$(awk '/MemAvailable/ {print $2}' /proc/meminfo)
swap_total=$(awk '/SwapTotal/ {print $2}' /proc/meminfo)
swap_free=$(awk '/SwapFree/ {print $2}' /proc/meminfo)
if [[ ${swap_total:-0} -gt 0 ]]; then
local swap_used
swap_used=$((swap_total - swap_free))
local swap_pct
swap_pct=$((swap_used * 100 / swap_total))
echo "Swap usage: ${swap_pct}%" >> "$REPORT_FILE"
if [[ $swap_pct -ge 35 && ${mem_available:-0} -gt $((mem_total / 3)) ]]; then
add_finding "High swap usage while RAM is still available; this can cause stutter."
add_action "Consider lowering swappiness (temporary: sudo sysctl vm.swappiness=10)."
fi
fi
if [[ -f /proc/pressure/memory ]]; then
run_and_log "Memory PSI" cat /proc/pressure/memory
fi
}
check_gpu_state() {
if has_cmd nvidia-smi; then
run_and_log "NVIDIA State" nvidia-smi
local pstate util power
pstate=$(nvidia-smi --query-gpu=pstate --format=csv,noheader 2> /dev/null | head -n 1 | xargs || true)
util=$(nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits 2> /dev/null | head -n 1 | xargs || true)
power=$(nvidia-smi --query-gpu=power.draw --format=csv,noheader,nounits 2> /dev/null | head -n 1 | xargs || true)
echo "NVIDIA pstate: ${pstate:-unknown}" >> "$REPORT_FILE"
echo "NVIDIA util: ${util:-unknown}%" >> "$REPORT_FILE"
echo "NVIDIA power: ${power:-unknown}W" >> "$REPORT_FILE"
if [[ ${pstate:-} == "P0" && ${util:-100} -le 5 ]]; then
add_finding "NVIDIA GPU is in P0 high-performance state while mostly idle; this can increase heat and trigger thermal limits."
add_action "If laptop has hybrid graphics, prefer iGPU mode for desktop workloads and use dGPU on demand."
fi
else
run_and_log "PCI VGA Devices" lspci -nnk | grep -A3 -Ei 'vga|3d|display'
fi
}
check_journal_size() {
local journal_line
journal_line=$(journalctl --disk-usage 2> /dev/null || true)
echo "Journal usage: ${journal_line:-unknown}" >> "$REPORT_FILE"
if [[ $journal_line =~ ([0-9]+\.?[0-9]*)\ (G|M) ]]; then
local value unit
value="${BASH_REMATCH[1]}"
unit="${BASH_REMATCH[2]}"
if [[ $unit == "G" ]]; then
add_finding "Systemd journal is large (${value}G); excessive logs can waste I/O and disk space."
fi
fi
}
apply_safe_fixes() {
if [[ $APPLY_SAFE_FIXES != "true" ]]; then
return
fi
log_info "Applying safe fixes..."
if ! systemctl is-enabled fstrim.timer > /dev/null 2>&1; then
systemctl enable --now fstrim.timer
add_action "Enabled and started fstrim.timer."
fi
if systemctl is-enabled tlp.service > /dev/null 2>&1 && systemctl is-enabled power-profiles-daemon.service > /dev/null 2>&1; then
systemctl disable --now tlp.service
add_action "Disabled tlp.service to avoid conflict with power-profiles-daemon."
fi
local journal_line
journal_line=$(journalctl --disk-usage 2> /dev/null || true)
if [[ $journal_line =~ ([0-9]+\.?[0-9]*)\ G ]]; then
journalctl --vacuum-size=300M
add_action "Vacuumed systemd journal to 300M."
fi
}
print_summary() {
echo
echo "=============================="
echo " Arch Performance Diagnostics"
echo "=============================="
echo "Report: $REPORT_FILE"
echo
if [[ ${#FINDINGS[@]} -eq 0 ]]; then
log_ok "No high-confidence bottlenecks detected by automated checks."
else
log_warn "Likely issues found (${#FINDINGS[@]}):"
local item
for item in "${FINDINGS[@]}"; do
echo " - $item"
done
fi
if [[ ${#ACTIONS[@]} -gt 0 ]]; then
echo
log_info "Actions/recommendations:"
local action
for action in "${ACTIONS[@]}"; do
echo " - $action"
done
fi
echo
echo "Recommended next command for deep per-process analysis:"
echo " sudo iotop -oPa"
echo " top"
echo " systemd-analyze blame"
}
main() {
parse_args "$@"
check_root_if_needed "$@"
mkdir -p "$REPORT_DIR"
log_info "Writing diagnostic report to: $REPORT_FILE"
collect_basics
install_optional_tools
check_cpu_governor
check_thermal_state
check_power_services
check_storage_health
check_memory_pressure
check_gpu_state
check_journal_size
apply_safe_fixes
print_summary
}
main "$@"

View File

@ -0,0 +1,326 @@
#!/usr/bin/env bash
# Fix common Arch Linux performance issues on laptops with NVIDIA hybrid graphics
#
# Issues addressed:
# 1. NVIDIA xorg RenderAccel disabled → forces software rendering, Xorg eats 30%+ CPU
# 2. CPU governor stuck on powersave → AMD Ryzen throttled despite AC power
# 3. No power management daemon → inconsistent CPU/GPU power state management
# 4. Systemd journal bloated (>1GiB) → wastes disk I/O
# 5. NetworkManager-wait-online.service → adds ~6s to every boot for no benefit
# 6. media-organizer.service broken → wrong script path & user, fails every boot
#
# Usage:
# ./fix_arch_performance.sh # Apply all fixes
# ./fix_arch_performance.sh --dry-run # Show what would be done
# ./fix_arch_performance.sh --interactive # Prompt before each fix
# ./fix_arch_performance.sh -h # Show help
#
# Safe to re-run: all fixes are idempotent.
# Requires reboot/re-login for xorg changes to take effect.
set -euo pipefail
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
# shellcheck source=../lib/common.sh
source "$SCRIPT_DIR/../lib/common.sh"
parse_interactive_args "$@"
shift "$COMMON_ARGS_SHIFT"
DRY_RUN=false
for arg in "$@"; do
case "$arg" in
--dry-run)
DRY_RUN=true
;;
-h | --help)
cat << 'EOF'
fix_arch_performance.sh - Fix common Arch Linux laptop performance issues
Usage: fix_arch_performance.sh [OPTIONS]
Options:
--dry-run Show what would be done without making changes
-i, --interactive Prompt before each fix
-h, --help Show this help message
Fixes applied:
1. Enable NVIDIA hardware acceleration (RenderAccel true)
2. Install/enable power-profiles-daemon, set performance profile
3. Vacuum systemd journal to 300M, cap future size
4. Disable NetworkManager-wait-online.service (saves ~6s boot)
5. Fix media-organizer.service (correct path and user)
All fixes are idempotent and safe to re-run.
Xorg fixes require reboot/re-login to take effect.
EOF
exit 0
;;
esac
done
require_root "$@"
print_setup_header "Arch Linux Performance Fix"
FIXES_APPLIED=0
FIXES_SKIPPED=0
# ---------------------------------------------------------------------------
# Helper: run or print a fix depending on --dry-run / --interactive
# ---------------------------------------------------------------------------
apply_fix() {
local description="$1"
shift
echo ""
log_info "$description"
if [[ $DRY_RUN == "true" ]]; then
echo " [dry-run] Would run: $*"
return 0
fi
if [[ $INTERACTIVE_MODE == "true" ]]; then
if ! ask_yes_no " Apply this fix?"; then
log_warn "Skipped."
((FIXES_SKIPPED++)) || true
return 0
fi
fi
if "$@"; then
log_ok "Done."
((FIXES_APPLIED++)) || true
else
log_error "Failed (non-fatal, continuing)."
fi
}
# ===================================================================
# Fix 1: NVIDIA RenderAccel
# ===================================================================
fix_nvidia_render_accel() {
local conf="/etc/X11/xorg.conf.d/20-nvidia.conf"
# Check if RenderAccel is already correct
if [[ -f $conf ]] && grep -qi 'RenderAccel.*true' "$conf"; then
log_ok "NVIDIA RenderAccel is already enabled — skipping."
return 0
fi
mkdir -p /etc/X11/xorg.conf.d
# Back up the current config if it exists and has the bad setting
if [[ -f $conf ]]; then
cp "$conf" "${conf}.bak.$(date +%Y%m%d_%H%M%S)"
fi
cat > "$conf" << 'XORGEOF'
# NVIDIA configuration - hardware acceleration enabled
# Disabling RenderAccel forces Xorg into software rendering,
# causing 30%+ CPU usage on desktop. Keep this set to "true".
Section "Device"
Identifier "NVIDIA Card"
Driver "nvidia"
Option "RenderAccel" "true"
EndSection
XORGEOF
# Clean up old backups left by nvidia_troubleshoot.sh
rm -f /etc/X11/xorg.conf.d/20-nvidia.conf.backup.* 2> /dev/null || true
return 0
}
# ===================================================================
# Fix 2: Power management daemon + performance profile
# ===================================================================
fix_power_management() {
# Install power-profiles-daemon if missing
if ! pacman -Qi power-profiles-daemon > /dev/null 2>&1; then
log_info "Installing power-profiles-daemon..."
pacman -S --needed --noconfirm power-profiles-daemon
fi
# Enable and start the service
if ! systemctl is-enabled power-profiles-daemon.service > /dev/null 2>&1; then
systemctl enable --now power-profiles-daemon.service
elif ! systemctl is-active power-profiles-daemon.service > /dev/null 2>&1; then
systemctl start power-profiles-daemon.service
fi
# Resolve TLP conflict if both are enabled
if systemctl is-enabled tlp.service > /dev/null 2>&1; then
log_warn "TLP conflicts with power-profiles-daemon — disabling TLP."
systemctl disable --now tlp.service
fi
# Set performance profile (appropriate when plugged in with strong hardware)
sleep 1
if has_cmd powerprofilesctl; then
powerprofilesctl set performance
log_info "Power profile set to: $(powerprofilesctl get)"
fi
return 0
}
# ===================================================================
# Fix 3: Journal vacuum + permanent size cap
# ===================================================================
fix_journal() {
local usage_line
usage_line=$(journalctl --disk-usage 2> /dev/null || true)
local needs_vacuum=false
if [[ $usage_line =~ ([0-9]+\.?[0-9]*)\ G ]]; then
needs_vacuum=true
fi
if [[ $needs_vacuum == "true" ]]; then
journalctl --vacuum-size=300M
else
log_ok "Journal is already under 1GiB."
fi
# Create permanent size cap via drop-in
local dropin_dir="/etc/systemd/journald.conf.d"
local dropin_file="$dropin_dir/size-limit.conf"
if [[ -f $dropin_file ]] && grep -q 'SystemMaxUse=300M' "$dropin_file"; then
log_ok "Journal size cap already configured."
else
mkdir -p "$dropin_dir"
cat > "$dropin_file" << 'JOURNALEOF'
[Journal]
SystemMaxUse=300M
JOURNALEOF
systemctl restart systemd-journald
fi
return 0
}
# ===================================================================
# Fix 4: Disable NetworkManager-wait-online
# ===================================================================
fix_nm_wait_online() {
if ! systemctl is-enabled NetworkManager-wait-online.service > /dev/null 2>&1; then
log_ok "NetworkManager-wait-online is already disabled — skipping."
return 0
fi
systemctl disable NetworkManager-wait-online.service
return 0
}
# ===================================================================
# Fix 5: media-organizer.service
# ===================================================================
fix_media_organizer() {
local service_file="/etc/systemd/system/media-organizer.service"
# Find the organize_downloads.sh script
local script_path=""
local candidates=(
"/home/kuhy/testsAndMisc/linux_configuration/scripts/utils/organize_downloads.sh"
"/home/kuhy/linux-configuration/scripts/utils/organize_downloads.sh"
)
for candidate in "${candidates[@]}"; do
if [[ -f $candidate ]]; then
script_path="$candidate"
break
fi
done
if [[ -z $script_path ]]; then
log_warn "organize_downloads.sh not found — skipping media-organizer fix."
return 0
fi
local target_user="${SUDO_USER:-kuhy}"
# Check if already correct
if [[ -f $service_file ]]; then
if grep -q "User=$target_user" "$service_file" \
&& grep -q "ExecStart=$script_path" "$service_file"; then
log_ok "media-organizer.service is already correctly configured — skipping."
return 0
fi
fi
systemctl stop media-organizer.service 2> /dev/null || true
cat > "$service_file" << EOF
[Unit]
Description=Media File Organizer
After=graphical-session.target
Wants=graphical-session.target
[Service]
Type=oneshot
User=$target_user
Group=$target_user
ExecStart=$script_path
StandardOutput=journal
StandardError=journal
RemainAfterExit=no
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl reset-failed media-organizer.service 2> /dev/null || true
systemctl enable media-organizer.service
return 0
}
# ===================================================================
# Apply all fixes
# ===================================================================
main() {
apply_fix \
"Fix 1/5: Enable NVIDIA hardware acceleration (RenderAccel → true)" \
fix_nvidia_render_accel
apply_fix \
"Fix 2/5: Install/enable power-profiles-daemon + set performance profile" \
fix_power_management
apply_fix \
"Fix 3/5: Vacuum journal logs + set permanent 300M size cap" \
fix_journal
apply_fix \
"Fix 4/5: Disable NetworkManager-wait-online.service (~6s boot saving)" \
fix_nm_wait_online
apply_fix \
"Fix 5/5: Fix media-organizer.service (correct path and user)" \
fix_media_organizer
# ---------------------------------------------------------------
# Summary
# ---------------------------------------------------------------
echo ""
echo "=============================="
echo " Performance Fix Summary"
echo "=============================="
if [[ $DRY_RUN == "true" ]]; then
log_info "Dry-run mode — no changes were made."
else
log_ok "Fixes applied: $FIXES_APPLIED"
if [[ $FIXES_SKIPPED -gt 0 ]]; then
log_warn "Fixes skipped: $FIXES_SKIPPED"
fi
fi
echo ""
log_info "Reboot or re-login for xorg changes (Fix 1) to take effect."
log_info "After reboot, verify with: diagnose_arch_performance.sh"
}
main

View File

@ -10,8 +10,18 @@ set -euo pipefail
SERVICE_NAME="media-organizer"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
ORGANIZE_SCRIPT="/home/kuhy/linux-configuration/scripts/utils/organize_downloads.sh"
TARGET_USER="kuhy"
DEFAULT_ORGANIZE_SCRIPT="/home/kuhy/testsAndMisc/linux_configuration/scripts/utils/organize_downloads.sh"
LEGACY_ORGANIZE_SCRIPT="/home/kuhy/linux-configuration/scripts/utils/organize_downloads.sh"
if [[ -f $DEFAULT_ORGANIZE_SCRIPT ]]; then
ORGANIZE_SCRIPT="$DEFAULT_ORGANIZE_SCRIPT"
elif [[ -f $LEGACY_ORGANIZE_SCRIPT ]]; then
ORGANIZE_SCRIPT="$LEGACY_ORGANIZE_SCRIPT"
else
ORGANIZE_SCRIPT="$DEFAULT_ORGANIZE_SCRIPT"
fi
TARGET_USER="${SUDO_USER:-kuhy}"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"

View File

@ -69,17 +69,20 @@ configure_xorg() {
backup_file "$NVIDIA_CONF"
# Create NVIDIA-specific configuration
# NOTE: RenderAccel must be "true" (or omitted, since it defaults to true).
# Setting it to "false" forces software rendering, causing Xorg to consume
# 30%+ CPU on the desktop and making the system feel extremely sluggish.
cat > "$NVIDIA_CONF" << EOF
# NVIDIA configuration with RenderAccel disabled
# NVIDIA configuration - hardware acceleration enabled
# Created by nvidia_troubleshoot.sh on $(date)
Section "Device"
Identifier "NVIDIA Card"
Driver "nvidia"
Option "RenderAccel" "false"
Option "RenderAccel" "true"
EndSection
EOF
echo "✓ Created $NVIDIA_CONF with RenderAccel disabled"
echo "✓ Created $NVIDIA_CONF with RenderAccel enabled"
}
# Function to add GCC mismatch workaround