Shell script to adjust Intel Speed Shift Technology on the fly

if you have a modern intel CPU you can use this shell script to adjust the efficiency factor and control performance vs battery life on your laptop

intel 6th gen (skylake) and above,
I hope you all find it useful.


Code:
#!/bin/sh

if [ $# != 1 ]
then
  echo "You need to give me the effeciency scale from 0 to 100."
  echo "0  : Best performance"
  echo "100: Best battery life."
  exit
fi

if ! [ $(echo $1 | grep -E '^[0-9]+$') ]
then
   echo "Error: Not a valid number."
   echo "       effeciency scale must be a number in the range of 0 - 100 "
   exit
fi

if  [ $1 -gt 100 ]   
then
  echo "Error : effeciency scale is out of range."
  echo "        effeciency scale must be a number in the range of 0 - 100 "
  exit
fi

Ncores=$(sysctl kern.smp.cores | cut -f2 -d':')                                                                                                               
NThrds=$(sysctl kern.smp.threads_per_core | cut -f2 -d':')

MAX_CPU_UNITS=$((Ncores*NThrds-1))

for c in $(seq 0 $MAX_CPU_UNITS)
do
  sudo sysctl dev.hwpstate_intel.$c.epp=$1
done



# Copyright 2023 Dr Amr Osman, Consultant of cardiology
# BSD 3 Clause
 
Looks nice. :)
Just a FYI: I've been using an one-liner below on zsh as root.

sysctl -aN | fgrep dev.hwpstate | fgrep epp | while read OID ; do ; sysctl ${OID}=0 ; done
WOW , respect man ! this is genius.
 
Beware, epp=0 could be a little bit much for general use. On my desktop it actually locks the frequency to uber max speed permanently:
Code:
# sysctl dev.cpu.0.freq
dev.cpu.0.freq: 5006
Very bad for your electricity bill and longevity of the CPU. YMMV.
 
Beware, epp=0 could be a little bit much for general use. On my desktop it actually locks the frequency to uber max speed permanently:
Code:
# sysctl dev.cpu.0.freq
dev.cpu.0.freq: 5006
Very bad for your electricity bill and longevity of the CPU. YMMV.
Of course, ${OID}=0 is just an example, and my choice for notebooks on AC line.
Any integer between 0 to 100 can be used instead of 0. It's just the admin's choice.;)
 
Beware, epp=0 could be a little bit much for general use. On my desktop it actually locks the frequency to uber max speed permanently:
Code:
# sysctl dev.cpu.0.freq
dev.cpu.0.freq: 5006
Very bad for your electricity bill and longevity of the CPU. YMMV.

Now all we need is to connect such methods of setting epp, to something like powerd[xx]'s evaluation/s of system load to determine how hard (or soft) to crank cpu speed vs economy at different times, determined by user-settable parameters.

I'm a bit surprised the Usual Suspects haven't stepped up yet, but no doubt people are exploring it.

No reason both Speed Step and Speed Shift technologies couldn't be handled in one daemon.

Sure, it could be done in sh as a means of prototyping before coding it up to C or whatever.

Just a thought ...
 
No reason both Speed Step and Speed Shift technologies couldn't be handled in one daemon.

Sure, it could be done in sh as a means of prototyping before coding it up to C or whatever.
One thing to consider is that, for implementing power management daemon (powerd and so on), all (at least Tier1) archs which FreeBSD supports should be covered, if any power magagement feature(s) is (are) supported.
Without it, admins would be confused(, detailed arch-specific options would be welcomed, though).
 
Very bad for your electricity bill and longevity of the CPU. YMMV.
Other than heat, where is the reduced longevity?
This sounds like a variant of overclocking, but with factory-legal parameters.

If one wants to go fast, then one deals with heat by using any of the excellent cooling solutions available today.
Unless this is a laptop, where a decent cooling solution is impossible.

For example, I run the Noctua NH-D15S coolers on my Xeons.
Doing ffmpeg H265 transcodes in software really generates the CPU heat.
 
Other than heat, where is the reduced longevity?
This sounds like a variant of overclocking, but with factory-legal parameters.

Yes, the 5Ghz is the highest frequency of my CPU when overclocking is allowed by the BIOS.
I had the impression that constant high voltage (by runnig at high clockspeed) is reducing the longevity of a CPU. A quick internet search on this topic showed me, indeed, that isn't really a problem anymore for reasonable recent CPUs. As long as the temperature stays in range.
So you are right, if the heat is under control than there's no problem (still, the electricity bill will be higher at 5Ghz 24/7 ;) ).
 
Yes, the 5Ghz is the highest frequency of my CPU when overclocking is allowed by the BIOS.

Fine, but what's the point of running at 5GHz while writing email, or watching videos, or ...

I had the impression that constant high voltage (by runnig at high clockspeed) is reducing the longevity of a CPU. A quick internet search on this topic showed me, indeed, that isn't really a problem anymore for reasonable recent CPUs.

Depends on who and what you ask. Try $searching:
"effect of heat on computers'

As long as the temperature stays in range.
So you are right, if the heat is under control than there's no problem (still, the electricity bill will be higher at 5Ghz 24/7 ;) ).

It's not just about CPUs, which can indeed withstand huge temperatures these days, but heat is the enemy of almost all electronics, from solder joints to capacitors to cables and ancillaries like disks including SSDs, and of course fans - even on desktops or servers, quite apart from laptops.

Far better to have a responsive system that can rapidly crank up speed on demand, and relax (and quieten down) while twiddling its thumbs, waiting for the next keystroke.

IMNSHO, of course <&^}=
 
It's not just about CPUs, which can indeed withstand huge temperatures these days, but heat is the enemy of almost all electronics

I forgot to mention heatsink thermal paste, very directly affected by CPU temperature, being a long term maintenance issue for hot systems - and the worse it gets, the worse it gets ...
 
Back
Top