252 points · by chetangoti · 5y ago · 127 comments
The CPU itself can change frequencies much faster on its own instead of the software based speedstep which programs like this controlled.
This eliminates the need for an on-battery or plugged-in performance mode because it changes frequencies so fast, you're no longer losing performance by waiting for the software to speed up the cpu. So you can keep it on all the time.
For these CPUs, all this is in the kernel and enabled by default.
Here's my /etc/rc.d/rc.local script:
#!/bin/sh
for policy in /sys/devices/system/cpu/cpufreq/policy*
do
echo "power" > "$policy"/energy_performance_preference
done
Here's available preferences: $ cat /sys/devices/system/cpu/cpufreq/policy0/energy_performance_available_preferences
default performance balance_performance balance_power power
Basically I can choose between "performance" and "power" (low performance) modes. I'm choosing power, so my fans are silent. And they're silent indeed, no matter the load. echo power | tee /sys/devices/system/cpu/cpufreq/policy*/energy_performance_preferenceFound that one way later than I'd care to admit. I use tee $FILE > /dev/null <EOF quite often in some situations where redirects get ugly, and sponge is a nice util to tidy up the invocation.
"AMD’s Ryzen 9 3900X also has the new Collaborative Power Performance Control 2 (CPPC2) feature, which manipulates Ryzen 3000's power states from within the operating system. This is similar to Intel's Speed Shift technology and reduces power state transition latency. Ultimately, that results in a more efficient processor during all facets of operation."
Though that seems to be about boosting clocks higher clocks when it can.
https://www.amd.com/en/technologies/smartshift
Again does seem to focus upon boosting performance
So no clear-cut stand-out answer, but does seem some level of OS interaction is required, I couldn't say with 100% certainty.
I recently figured out TLP was the source of a couple of irritating issues for me on an XPS 9370:
- Bluetooth audio continually cutting in and out, only when wireless module on AC "non power saving mode"!?!
- DC bias on the audio out jack, on both modes, worse on power saving. (forcing a power amp into self protect mode).
Both resolved by disabling TLP entirely. I knew TLP was not a set and forget tool, but it's easy to forget when on some machines it's fine (and necessary, especially older laptops). But then changing to a new machine where it causes lots of issues. It was a good reminder that these tools need to be used with care because they often exploit unusual hardware modes and can have strange side effects.
Neither do X11 programs (well the ones that care about power effienciency anyways, so not games).
It also suggests using "powersave" on battery, based on the old idea that lower CPU frequencies save energy. This is not a given; lower frequencies use less power but also take longer to get the same amount of work done. While the CPU is active, it is consuming static power regardless of frequency. This has gotten even more significant with newer CPUs.
What the optimal power saving config is varies from system to system. Ultimately, what you need is to actually benchmark the performance and energy consumption of your system. It's complicated and you also have to deal with things like switching latencies so the cpufreq governor and scheduler can make optimized decisions. Most PCs don't really do a good job of this at all, which is one reason why battery life can be all over the place.
Sadly, switching to the "powersave" governor may not, in fact, end up saving you power, depending on what you're doing :)
Hmmmm... I've always heard from chip engineers that power consumption / heat dissipation was not linear compared to clock speed. I'd be very surprised to find a case where a linear amount of work to be done by the CPU would cause more energy consumption overall at lower clock speed then the same task ran on the same CPU in a shorter timeframe, but at a higher clock rate.
I do agree that if the task takes longer to run, there's a longer timeframe where more scheduling/switching and whatnots needs to be done, so it's not a totally linear increase in time, but can that really be sufficient to offset the energy consumption when running the CPU at an higher clock speed?
There has to be some numbers out there: it's not hard that hard to test.
EDIT: as per the other comments, googling "race to idle" and "race to sleep" which may enlighten my curious self
When you gate on/off portions of a chip, you are changing the capacitance.
Both cases ("slower is better" and "faster is better") are true at different points on the frequency curve, because the frequency (hereafter "F") and power (hereafter "P") relationship has both linear and superlinear terms.
To a first order approximation, dynamic power (roughly the power actually used to do work) scales with F^3, but chips also have a significant static power draw as well: power they draw for just being on. Actually, the relationship is more complicated than that since static power may also depend on V, hence indirectly on frequency, but the bottom line is that as you reduce frequency to zero power use doesn't go to zero but asymptotically approaches some non-zero plateau.
So imagine a simplified CPU where we care only about the order-0 and order-3 terms, and it happens that power goes like (P in W, F in MHz):
P(F) = 2 + 1e-10*F^3
That is, this chip draws at least 2W at any frequency, plus a cubic term in F.This gives a power curve like this [1] where I've also plotted "work vs energy" which is energy efficiency: how much computation you can do for a given energy input (this is simply F/P).
As you can see, power use increases in a cubic way to the right, but has an asymptote at 2W on the left. Work/energy has a maximum in the middle: at too low frequencies, you are doing very little work/time but paying the full 2W cost, while at the right the cubic term kills power: P increases with F^3 but work only with F. The most efficient spot is somewhere in the middle. Let's call this point Feff.
So it never really makes sense to run your CPU at less than Feff: you are less efficient and it takes longer. It can definite makes sense, however, to run your CPU at more than Feff: you use somewhat more energy but get your work done faster. People don't buy 5 GHz CPUs because they just want their work done efficiently, after all: they want it done fast too.
That's what a lot of the discussion misses: it's not a one dimensional problem that can be solved in terms of joules and MHz: it depends on your time preference too. That's why there are so many tunables, such as Intel's EPP (energy performance preference).
Different processes may have different ideal F values as well: if you have a periodic job running in the background that takes 1s of (nominal) CPU every minute, you won't care if it is on your CPU for 0.5s or 2s, as it runs on a fixed schedule anyway which is much less than a full CPU: this should run at Feff. OTOH when you are compiling a source file and twiddling your thumbs there might be a big difference between 5 and 20 seconds.
This is really only brushing the surface: there are a lot of additional considerations too: e.g., the whole chip may have a power limit, so it may not be possible to go all the way to right on the F graph, especially if multiple cores are running: even if your "performance preference" is way to the right (prefer a higher work/rate regardless of the power cost) you might get the fastest work rate by running more cores at lower F, or even one core at lower F to avoid throttling (because running at F is generally more efficient than running at F-d and F+d in a 50/50 ratio, due to the cubic term, so throttling is inherently inefficient).
---
[1] https://gist.githubusercontent.com/travisdowns/1e685007c9719...
Independently of whether that's true, isn't most consumer hardware (laptops) running "idle" most of the time?
Not setting "powersave" might be better if you want to compile something, transcode a video or do some machine learning and turn the machine off as soon as it's done. But that's hardly how a majority uses their devices.
Yes, which is why you want to become idle as fast as possible - and that means running at the fastest clock speed when you need to get some work done. Otherwise you are extending the amount of the time system is not idle, which has a fixed power cost beyond the CPU cores themselves.
Here's a Lenovo power optimization guide. It's for servers, but the same principles apply to pretty much all modern systems. Spoiler alert: the "ondemand" governor has the highest energy efficiency, significantly higher than "powersave" (page 14). The newer schedutil governor (page 23) is even better, over 34% more power-efficient than powersave.
https://lenovopress.com/lp0870.pdf
"Powersave" means saving power, it doesn't mean saving energy. Your battery holds a fixed amount of energy, not a fixed amount of power. Energy is what matters.
“ … Using tools like TLP will help in this situation with extending battery life (which is something I did for numerous years now), but it also might come with its own set of problems, like losing turbo boost.
With that said, I needed a simple tool which would automatically make "cpufreq" related changes, save battery like TLP, but let Linux kernel do most of the heavy lifting. That's how auto-cpufreq was born. … “
I feel that laptop performance is so subjective and very hard to track. Particularly thermal throttling on mobile Ryzen CPU makes my new Lenovo sometimes close to unusable. Particularly trying to do work while pandemic video conferencing seems to be more difficult than necessary...
All mainstream CPUs and operating systems are dynamically adjusting frequency and other internal behavior to optimize power versus other factors. Some of these adjustment are done in hardware (i.e., intel_pstate aka "speed shift" aka HWP) which is both faster than software and can use internal metrics not available to software.
So you can't just say you're adjusting frequency dynamically to be optimal: something like that (or an attempt, at least) already happens. So what does this tool really do?
Please take a look at the auto-cpufreq demo video I made which should clear some things out: https://www.youtube.com/watch?v=QkYRpVEEIlg
It's old and many things have been improved and added since then, but it describes the essence.
You can also refer to blog post on what prompted me to make one such tool: https://foolcontrol.org/?p=3124
I am talking about hardware power control in general. I wouldn't expect hardware power control itself to make different decisions based on plugged-in or not: that's a decision delegated to the user via the OS. HWP only implements low-level control based on the parameters set by something higher up.
I don't know if I'll watch the video, but you should make clear in your README what this tool actually does. Saying that it adjusts frequency dynamically isn't very useful. Telling people to disable `intel_pstate` without further comment is extremely suspect since then you're giving up HWP at an unknown cost.
If this simply adjusts the power governor when the laptop is plugged/unplugged, just say that!
If you like what it suggest you can run it in "live" mode, which will make the suggested changes. But after you stop "live" mode after reboot all settings will go back to defaults.
If you're happy with results then and want to run it in background all the time proceed with "install".
Reference: https://github.com/AdnanHodzic/auto-cpufreq/#how-to-run-auto...
Also I suggest you watch auto-cpufreq demo Youtube video which explains it all: https://www.youtube.com/watch?v=QkYRpVEEIlg
ffmpeg -i input.mp4 output.mp4
will use x264 which is well optimized to use your whole CPU (you can check htop).
Don't most distros default to ondemand or schedutil governor these days?
Powersave is noticeably slower on heavy workloads, and performance drains the battery on idle. So you have to keep toggling manually depending on your workload. Gonna try out this tool and hope it works.
Sad it can’t be built into the default governor. It’s not exactly easy to find these settings. There must be a lot of people out there with fancy hardware which they are unaware they are not utilizing to their full potential.
I sometimes want to run benchmarks and I'd like my laptop to stick to one speed
I understand it's a bit problematic with the latest CPUs bc the gap between turbo and non turbo is so huge - and the CPU is in turbo a good chunk of the time (and your cache performance will be very different running at 1Ghz vs 4Ghz)
Anyone have a good solution?
> Anyone have a good solution?
A good cooler
----
Downvotes, really? There is really no trick around thermals being the main performance limiting factor for the last 10 years, since CPUs started coming with hardware "turbo"
You cannot optimise around that.
And with modern CPUs the power density is sufficently high that the die and heatspreader thermal resistance is enough to cause substantial temperature changes even with completely overspecced cooling.
No, you can up/down the base/turbo limits, but the effect will be miniscule.
As said above, the performance, and power consumption of modern CPUs is an "all, or nothing" choice.
Modern laptop CPUs are nothing, but well disguised desktop/server cores. Nobody designs designated mobile use cores, unless you are in ARM world.
In principle, they are unable to work in the "middle gear." CPU makers tried to make them to, but never managed to get a reasonable trade off. So you cannot run them reasonably at "half-performance"
I wonder if its benefits outweigh the disadvantages. Would be good to have numbers for a bunch of different hardware configs...
When I'm reading a web page, ideally my CPU is halted, using barely any power. With the screen dim, I might be hoping for 20 hours battery life in that state.
To wake up, just to poll something, and go back to sleep again uses a lot of energy compared to doing nothing. Something polling a few times a second might reduce my 20 hours down to just 10.
Custom profiles that can control boot command line options, sysctl parameters, sysfs, and so on.
Or am I wrong?
Most of my experience has been with desktops, so take this with a pile of salt
I've found the effect can be difficult to notice... performance wise especially. Power wise it might be a dozen watts here and there.
Even between powersave/performance it's hard to tell - mostly in latency. As a result, I just stay down on powersave.
The firmware does a decent job at 'parking' cores and such as needed, and I still get decent boost performance!
All that said, in a laptop I could definitely see squeezing out every last bit like this being a big help
https://github.com/FeralInteractive/gamemode
You run the game with gamemoderun and it will do few adjustments for you, including adjusting the CPU governor.
It can be done in TLP config.
In the recent years you really shouldn't have to worry about this anymore. You can check in powertop if your laptop CPU package idles for the most part in C8 or higher, and if it does great don't worry about it, and if it doesn't then look for the rogue software (like maybe some random scripts).