NVIDIA GPU overclocking under Wayland [guide]

You may already know this, but thanks to this Reddit post humanity finally knows the answer to one of the greatest mysteries: “How can one overclock an NVIDIA GPU under Wayland?”.

Based on the previously mentioned post I will give you an example on how to automate the overclocking and apply it on user login.

Setup the overclocking script:

  1. firstly we need to enter the system root user via the su command (will explain why later) and then use cd ~/ to enter the root’s home directory (we want all changes to be made in the root’s home "will explain"™)
  2. now we are going to prepare a new python environment for running our script
  • as the root user create folder named ‘nvgpu-overclock’: mkdir nvgpu-overclock
  • enter the folder: cd nvgpu-overclock
  • create a new python environment: python3 -m venv ./
  • enter the new environment: source ./bin/activate (for fish use activate.fish)
  • install dependencies: pip install nvidia-ml-py pynvml
  1. create the script
  • using your favorite CLI text editor (for example nano) create file named ‘overclock.py’ with the following content while tweaking the values as needed (some offsets may not work on certain GPUs, if this script fails, try commenting an offset instruction(s) and see if it helps to at leas get access to some vaues):
from pynvml import *
nvmlInit()

# This sets the GPU to adjust - if this gives you errors or you have multiple GPUs, set to 1 or try other values.
myGPU = nvmlDeviceGetHandleByIndex(0)

# The GPU frequency offset value should replace the "80" in the line below.
nvmlDeviceSetGpcClkVfOffset(myGPU, 80)

# The Mem frequency Offset should be **multiplied by 2** to replace the "2500" below
# for example, an offset of 500 in GWE means inserting a value of 1000 in the next line
nvmlDeviceSetMemClkVfOffset(myGPU, 2500)

# The power limit should be set below in mW - 216W becomes 216000, etc. May not work for laptops. Remove the below line if you don't want to adjust power limits.
nvmlDeviceSetPowerManagementLimit(myGPU, 216000)
  1. before setting the script to run automatically on startup it’s a good idea to manually test it for correct offsets, to do this you just need to run the script by executing python3 /root/nvgpu-overclock/overclock.py, while still being in the newly created python environment as described in stage 2, enter the environment as described in stage 2 if needed (no need to again create the folder and install dependancies)
  2. now time for the root user part and utomatically applying the overclocking, due to NVML’s setup this method requires root privileges to apply the changes and because of that we are doing all the changes in the root’s home directory (for security and later convenience reasons);
    to run the script automatically we will use cron and sudo following one of the 2 methods:
  • (apply on user login)
    • first we need to create a simple bash script that will run the actual overclocking python script so we can later create a new sudo rule, to create this bash script just create a file named ‘run-nvgpu-overclock(while in the python script directory) with 2 lines of the following content:
      #!/usr/bin/bash
      bash -c 'source /root/nvgpu-overclock/bin/activate && python3 /root/nvgpu-overclock/overclock.py'
    • after creating the bash script make it executable: chmod +x ./run-nvgpu-overclock
    • now we need to create a custom sudo rule, to do this create file /etc/sudoers.d/nv-overclock using your favorite CLI text editor (for example: nano /etc/sudoers.d/nv-overclock) with the following line (make sure to replace the your_user_name with your regular Linux user account name):
      your_user_name ALL=(root) NOPASSWD: /root/nvgpu-overclock/run-nvgpu-overclock
    • exit the root user with the exit command to enter back your regular user console
    • create the following cron entry: @reboot /usr/bin/sudo /root/nvgpu-overclock/run-nvgpu-overclock (use crontab -e to add the cron entry, if it runs in vim see this guide if not familiar with vim)
  • (apply on OS startup)
    • you should think twice before using this method because if you set any wrong offset values that make your GPU crash on system startup you may not be able to enter your system without using a recovery media (a recovery system ISO for example)
    • to do this you just need to add the following cron entry: @reboot /usr/bin/bash -c 'source /root/nvgpu-overclock/bin/activate && python3 /root/nvgpu-overclock/overclock.py' (use crontab -e to add the cron entry, if it runs in vim see this guide if not familiar with vim)

If it works for you then my sleepy mind managed to put it down more or less correct.
If you have any proposals on how to make this mess batter and more understandable please post a comment.

4 Likes

One of the guides of all time.

1 Like

Thank you so MUCH ! I can finally OC my Laptop GPU.

However if you let the nvmlDeviceSetPowerManagementLimit(myGPU, 216000) line it won’t work on laptop as GPU power can’t be changed manually.

And for some reasons cron didn’t made the job for me so I created a gnome startup script like this :

nano ~/.config/autostart/NvOverclock.desktop

[Desktop Entry]
Name=NvOverclock
Exec=/usr/bin/sudo /root/nvgpu-overclock/run-nvgpu-overclock
Terminal=false
Type=Application

And it works like a charm ! Gained 5% of free performances :)

1 Like

Great guide!

Very minor but missing the closing ’

btw how to check that it has been applied correctly?

If the values have been applied too high, and it was set to apply on login, the only way I found to fix it was to boot from a USB. Renamed /var/spool/cron to /var/spool/cron_, then it would boot. Renamed it back after testing the settings properly.

Running /usr/bin/sudo /root/nvgpu-overclock/run-nvgpu-overclock to apply manually.

One thing that could be improved in the guide: don’t be too quick in setting it to auto-apply! First apply it manually and test extensively. Also worth testing with intel-undervolt for under-volting the GPU. But if either intel-undervolt or NVIDIA overclock script is wrong, it’s going to freeze so got to test properly.

1 Like

I’ve developed a CLI tool to accomplish this task. You can download the latest binary from GitHub.

Example usage:
./nvidia_oc set --index 0 --power-limit 200000 --freq-offset 160 --mem-offset 850

Just run this on startup as root as explained in the first post

1 Like

Still haven’t figured out how to do this properly I see.

I’ve added instructions to the README of the github repo: GitHub - Dreaming-Codes/nvidia_oc: A simple command line tool to overclock Nvidia GPUs using the NVML library on Linux. This supports both X11 and Wayland.
If you’re still unable to get this working feel free to reach out on discord (my nickname is dreamingcodes)

What do you mean exactly?

Does the library used have an option to limit the maximum frequency?
So undervolt could also be achieved as is done in UXTU

There is a way to kind of undervolt the GPU too but I don’t remember the exact procedure.
Raising clocks and lower power limit?

OP is a troll and has no idea what he’s doing. Ignore him.

I was looking at the code of @DreamingCodes. In G-Helper you get the undervolt by applying freq-offset, mem-offset and limiting the maximum frequency of the GPU.
In his code, the only thing missing is to be able to limit the maximum frequency. But I don’t know if that possibility exists.

1 Like

nvidia_oc is a nice tool, very simple and it works for overlocking.

I tried what I said above, limiting gpu power to 150W (max is 200W) and raising gpu clock offset… it limited the max clock to a certain limit so it became a slight underclock, some games where not that affected but some lost 200Mhz… so that did not work.
I expected it to at least try to go higher and crash, but it never did.