#!/usr/bin/env zsh

sudo apt-get install -y cpufrequtils linux-cpupower

# Set ignore bios throttling on the kernel commandline
#sed -i "s|^GRUB_CMDLINE_LINUX_DEFAULT=\"\(.*\)\"|GRUB_CMDLINE_LINUX_DEFAULT=\"\1 processor.ignore_ppc=1\"|" /etc/default/grub

# NEWARG="processor.ignore_ppc=1"
# sudo sed -i -E "s|^(GRUB_CMDLINE_LINUX_DEFAULT=\"[^\"]*)\"|\1 ${NEWARG}\"|" /etc/default/grub
# sudo update_grub

# get max available cpu frequency
frequencies=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies)
freqs=(${(@s| |)frequencies})
max_freq=${freqs[1]}
# max_freq=2401000

# set max scaling frequency
number_of_cores=2
(( cores = ${number_of_cores} - 1 ))
for x in /sys/devices/system/cpu/cpu[0-${cores}]/cpufreq/;
do 
	echo "Setting scaling_max_freq of ${max_freq}"
	echo -n ${max_freq} > $x/scaling_max_freq
done

# set bios ignore for running kernel
echo 1 > /sys/module/processor/parameters/ignore_ppc

## cpufreq commands
cpufreq-info --freq      # 606464
cpufreq-info --hwfreq
cpufreq-info --hwlimits  # 800000 2401000
cpufreq-info --driver    # acpi-cpufreq
cpufreq-info --policy    # 800000 800000 userspace
cpufreq-info --governors # conservative userspace powersave ondemand performance schedutil
cpufreq-info --related-cpus    # 0
cpufreq-info --affected-cpus   # 0
cpufreq-info --stats           # 2401000:0, 2400000:0, 2000000:0, 1600000:0, 1200000:0, 800000:18825306%

cpufreq-info --latency         # 10000
# cpufreq-info --proc
#           minimum CPU frequency  -  maximum CPU frequency  -  governor
# CPU  0       800000 kHz ( 33 %)  -     800000 kHz ( 33 %)  -  userspace
# CPU  1       800000 kHz ( 33 %)  -     800000 kHz ( 33 %)  -  userspace

cpufreq-info --cpu 0
cpufreq-info --cpu 1
# cpufrequtils 008: cpufreq-info (C) Dominik Brodowski 2004-2009
# Report errors and bugs to cpufreq@vger.kernel.org, please.
# analyzing CPU 1:
#   driver: acpi-cpufreq
#   CPUs which run at the same hardware frequency: 1
#   CPUs which need to have their frequency coordinated by software: 1
#   maximum transition latency: 10.0 us.
#   hardware limits: 800 MHz - 2.40 GHz
#   available frequency steps: 2.40 GHz, 2.40 GHz, 2.00 GHz, 1.60 GHz, 1.20 GHz, 800 MHz
#   available cpufreq governors: conservative, userspace, powersave, ondemand, performance, schedutil
#   current policy: frequency should be within 800 MHz and 800 MHz.
#                   The governor "userspace" may decide which speed to use
#                   within this range.
#   current CPU frequency is 606 MHz.
#   cpufreq stats: 2.40 GHz:0.00%, 2.40 GHz:0.00%, 2.00 GHz:0.00%, 1.60 GHz:0.00%, 1.20 GHz:0.00%, 800 MHz:100.00%  (1)

echo ""
echo "If the intel_pstate driver is being used there are only two available governors, powersave and performance."
echo ""

# GOVERNOR="userspace"
GOVERNOR="ondemand"
# GOVERNOR="powersave"
# GOVERNOR="performance"

echo ""
echo "Setting governor to ${GOVERNOR}"
echo ""

echo " To use the ACPI driver you need to pass the 'intel_pstate=disable' on the kernel line and ensure the cpufreq_userspace kernel module is loaded."

echo "set the governor: cpupower frequency-set --governor userspace"
echo "set the frequency: cpupower --cpu all frequency-set --freq 800MHz"
echo ""

modprobe acpi-cpufreq

cpufreq-set \
	--cpu 0 \
	--governor ${GOVERNOR} \
	--min 800 \
	--max 2600 

cpufreq-set \
	--cpu 1 \
	--governor ${GOVERNOR} \
	--min 800 \
	--max 2600 
