Skip to content

Instantly share code, notes, and snippets.

@p4block
Created March 3, 2025 10:47
Show Gist options
  • Save p4block/7c5687810cc7c4581e09b79514f01e20 to your computer and use it in GitHub Desktop.
Save p4block/7c5687810cc7c4581e09b79514f01e20 to your computer and use it in GitHub Desktop.
bench.sh
#!/bin/bash
LOG_FILE="benchmark_results_$(hostname)_$(date +%Y%m%d_%H%M%S).log"
echo "===== Linux Kernel Compile Benchmark =====" | tee -a "$LOG_FILE"
echo "Running on: $(hostname)" | tee -a "$LOG_FILE"
echo "CPU: $(lscpu | grep 'Model name')" | tee -a "$LOG_FILE"
echo "Cores/Threads: $(nproc)" | tee -a "$LOG_FILE"
echo "-----------------------------------------" | tee -a "$LOG_FILE"
# Install required packages
echo "[1/6] Installing dependencies..." | tee -a "$LOG_FILE"
sudo apt update && sudo apt install -y build-essential flex bison libssl-dev bc git lm-sensors libelf-dev &>> "$LOG_FILE"
# Check if Linux source folder exists
if [ -d "linux" ]; then
echo "[2/6] Linux folder found. Resetting repository..." | tee -a "$LOG_FILE"
cd linux || exit
git reset --hard HEAD &>> "$LOG_FILE"
git clean -fdx &>> "$LOG_FILE"
git pull &>> "$LOG_FILE"
else
echo "[2/6] Cloning Linux kernel source..." | tee -a "$LOG_FILE"
git clone --depth=1 https://github.com/torvalds/linux.git &>> "$LOG_FILE"
cd linux || exit
fi
# Ensure a clean build BEFORE configuring
echo "[3/6] Cleaning previous builds..." | tee -a "$LOG_FILE"
make clean | tee -a "$LOG_FILE"
# Configure minimal kernel
echo "[4/6] Configuring kernel..." | tee -a "$LOG_FILE"
make defconfig | tee -a "$LOG_FILE"
# Start CPU monitoring in the background
echo "[5/6] Monitoring CPU usage, temperature, and power..." | tee -a "$LOG_FILE"
(
echo "Time(s), Temp(C), Power(W)" > ../cpu_log.csv
while true; do
TIMESTAMP=$(date +%s)
TEMP=$(sensors | grep 'Package id 0' | awk '{print $4}' | tr -d '+\UffffffffC')
# Try reading CPU power for Intel (RAPL) and AMD (if available)
POWER="N/A"
# Check for Intel RAPL
if [ -f /sys/class/powercap/intel-rapl:0/energy_uj ]; then
ENERGY=$(sudo cat /sys/class/powercap/intel-rapl:0/energy_uj 2>/dev/null)
if [[ $ENERGY =~ ^[0-9]+$ ]]; then
POWER=$(echo "scale=3; $ENERGY / 1000000" | bc) # Convert \UffffffffJ to W
fi
fi
# Check for AMD energy counters (if Intel RAPL is not available)
if [ "$POWER" == "N/A" ] && [ -f /sys/class/powercap/amd-rapl:0/energy_uj ]; then
ENERGY=$(sudo cat /sys/class/powercap/amd-rapl:0/energy_uj 2>/dev/null)
if [[ $ENERGY =~ ^[0-9]+$ ]]; then
POWER=$(echo "scale=3; $ENERGY / 1000000000" | bc) # Convert \UffffffffJ to W
fi
fi
# Write the current time, temperature, and power to the log file
echo "$TIMESTAMP, $TEMP, $POWER" >> ../cpu_log.csv
sleep 1
done
) &
MONITOR_PID=$!
# Run compilation benchmark with a spinner
echo "[6/6] Starting kernel compilation benchmark..." | tee -a "$LOG_FILE"
START_TIME=$(date +%s)
# Background compilation
(make -j$(nproc) &>> "$LOG_FILE") &
MAKE_PID=$!
# Progress spinner
SPINNER="/|\\-"
while kill -0 $MAKE_PID 2>/dev/null; do
for i in $(seq 0 3); do
echo -ne "\rCompiling... ${SPINNER:$i:1}"
sleep 0.2
done
done
echo -ne "\rCompilation complete! \n"
END_TIME=$(date +%s)
# Stop monitoring
kill $MONITOR_PID
# Calculate total compile time
COMPILE_TIME=$((END_TIME - START_TIME))
echo "-----------------------------------------" | tee -a "$LOG_FILE"
echo "Kernel compilation time: $COMPILE_TIME seconds" | tee -a "$LOG_FILE"
echo "CPU monitoring log saved to cpu_log.csv" | tee -a "$LOG_FILE"
echo "Full benchmark log saved to $LOG_FILE" | tee -a "$LOG_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment