FeralInteractive / gamemode

Optimise Linux system performance on demand
BSD 3-Clause "New" or "Revised" License
4.64k stars 183 forks source link

frequency boost is not activated by gamemode #223

Open Saroumane opened 4 years ago

Saroumane commented 4 years ago

Describe the bug gamemode tries to maximize performance of CPU (and GPU, I/O etc) for games. For the CPU it can be done with 2 major parameters : the governor and the frequency boost (Turbo Boost for Intel or Precision Boost Overdrive for AMD). Activating frequency boost does not work for gamemode.

To Reproduce Steps used to reproduce the behavior (with AMD Ryzen)

  1. Check CPU state :

    $ cat sys/devices/system/cpu/cpufreq/boost; cat /sys/devices/system/cpu/cpufreq/policy*/scaling_governor
    0
    ondemand
    ...

    (0 means : boost not activated, 1 boost means activated)

  2. Launch glxgears with gamemode

    $ vblank_mode=0  gamemoderun glxgears
    ...
  3. Meanwhile, watch effects of gamemode on CPU :

    $ cat /sys/devices/system/cpu/cpufreq/boost ; cat /sys/devices/system/cpu/cpufreq/policy*/scaling_governor"
    0
    performance
    ...
  4. Frequency boost is not activated.

Expected behavior Frequency boost should be activated to maximize performance.

System Info :

Additional context As a workaround I tried to edit /etc/gamemode.ini :

[custom]
; Custom scripts (executed using the shell) when gamemode starts and ends
start=notify-send "GameMode started"
echo -n 1 > /sys/devices/system/cpu/cpufreq/boost
end=notify-send "GameMode ended"
echo -n 0 > /sys/devices/system/cpu/cpufreq/boost

It does not work, maybe because gamemode is not run as root (and you need root permission to modify /sys/devices/system/cpu/cpufreq/boost ) So now what ? I could add SUID flag on /usr/bin/gamemoderun : it may work but it's probably a bad idea for security :)

Any other idea for the workaround ?

mdiluz commented 4 years ago

FWIW this is a feature request, but I don't really see a reason this should be done by GameMode - most users should have boost on by default anyway, and if it's turned off then usually it's for good reason (it behaves badly with their thermals, their CPU is old and can't handle it, etc.).

Plus, it is by its nature on demand, and if you want it to be used then it should probably just be left enabled at all times.

This isn't the same as the performance governor where there are known issues with the default, but it's beneficial to not be permanently set to the performance setting.

I believe for your personal use you could set up a script with setuuid and proper permissions and launch that in the config, if you desired, but my understanding is you might as well just turn on boost in the bios and/or elsewhere and leave it.

Happy to be convinced otherwise, but it might also help to understand why you have boost turned off in the first place?

Saroumane commented 4 years ago

Thanks for your quick answer. From my user perspective, governor and frequency boost are just CPU tweaks to reach functional needs. When I want performance for gaming, I need performance governor and turbo activated. When I want perfect silence (for office tasks or web browsing), I need ondemand governor and turbo off.

Mixed scenario : if I leave turbo activated and "ondemand" governor, I have noise peaks when the CPU fan kicks off (for instance when I launch a new program or open a tab in Firefox) I'd rather open a tab 0.05 sec slower in silence than having these recurring noise peaks.

Full disclosure : my CPU / Fan are from the very common Ryzen 3xxx family (3600X + stock fan)

And to be honest, I think it's not consistent to allow GameMode to overclock/boost GPU Frequency, but deny it for CPU :smile: ("Turbo boost" is nothing more than an advanced, monitored overclock of the CPU) If you think it's dangerous (I think it's not : CPU limits frequency boost according to max TDP), maybe you could hide this setting behind a "Here Be Dragons!" warning in gamemode.ini ? (And gamemode should restore it to its initial state when the game is left)

For that part "I believe for your personal use you could set up a script with setuuid and proper permissions and launch that in the config, if you desired" there is a little problem : Linux ignores the setuid bit on all scripts (for security reasons) So I guess I could write a little C program, would it be called by gamemode.ini ? Or it only works for shell script ?

Saroumane commented 4 years ago

So I tried with C, here is my trivial BoostCPU.c in case anyone is interested :

#include <unistd.h>

int main()
{
 setuid(0);
//  execl("/bin/sh","sh", "-c", "whoami", NULL);
 execl("/bin/sh","sh", "-c", "echo -n 1 > /sys/devices/system/cpu/cpufreq/boost", NULL);
  return 0;
}

(Never ever use system() like this : it's forbidden territory)

Then I did this : $ cc BoostCPU.c -o BoostCPU && sudo chown root:root BoostCPU && sudo chmod u+s BoostCPU

And the executable works ! But I can't launch it with gamemode.ini : In [custom] section, line 87, I tried different things:

/mypath/BoostCPU
. /mypath/BoostCPU
sh /mypath/BoostCPU
bash /mypath/BoostCPU

Each time I exit gamemode.ini, I read this in logs :

/usr/bin/gamemoded[5023]: Detected config file changes
/usr/bin/gamemoded[5023]: Reloading config...
/usr/bin/gamemoded[5023]: Loading config file [/etc/gamemode.ini]
/usr/bin/gamemoded[5023]: Failed to parse config file - error on line 87!

Am I missing something obvious or gamemode.ini really works only for shell scripts ?

Edit : Ok, this is VERY tricky : you have to put 5 empty spaces before invoking anything (I know there is an example, but ...). So here we go :

[custom]
; Custom scripts (executed using the shell) when gamemode starts and ends
start=notify-send "GameMode started"
;    /home/me/bin/stop_ethmining.sh
     /mypath/BoostCPU

Of course everything has to be duplicated for an "UnboostCPU" called in the [custom] "end" part of gamemode.ini.

mdiluz commented 4 years ago

It seems like a fair argument to add it as a feature, best done (like you have) in something like cpugovctl which has root privileges but is isolated.

The question becomes what to do by default - I'm a little wary of turning on boost if someone has it intentionally off, by default. Perhaps it could default not change it.

FWIW The issue with the config file is just because of the way .ini file parsing works. I suspect you can just do this:

[custom]
; Custom scripts (executed using the shell) when gamemode starts and ends
start=notify-send "GameMode started"
start=/mypath/BoostCPU
Pipshag commented 4 years ago

Just to add another use case: I don't need my Ryzen to boost and have the fans kick in just because I was browsing. Normally have boost off and then enable it whenever I have a use. Makes the thermals go down quit a bit in my SFF case.

Was surprised not to see boosting supported out of the box.

Having boost enabled by default would be a way to get "more performance out of the box" for the product but I lean towards having it configurable in the gamemode.ini instead.

jurf commented 4 years ago

In my use-case an optional setting in gamemode would be perfect; I get the argument that it shouldn’t touch it by itself, but if configurable by users it would be really helpful, e.g. my laptop handles boost normally, but it gets a bit too hot for my tastes under normal usage. However, during gaming I have it on a flat surface, usually with a cooler fan underneath, so I’d prefer to have it on.

emrebicer commented 1 year ago

Is there any update on this? I am in the same boat as @Saroumane. I am using intel 1240p CPU on my laptop which has a boost that can go up to 4.4 GHz, however, I don't prefer having it on as I simply don't need it for light tasks like browsing, watching stuff, writing code, etc. If I enable CPU boost the laptop gets loud and heats up easier, so I prefer keeping it off for basic tasks and daily usage. However, for gaming, I want cpu boost enabled so that I can get the maximum performance out of my computer.

One solution I use right now is to use TLP to manage CPU boost based on battery or AC power;

CPU_BOOST_ON_AC = 1;
CPU_BOOST_ON_BAT = 0;

However, this is still not what is actually desired, as it will still boost unnecessarily when the laptop is plugged into AC and not running any games.