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:
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 "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. 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.

3 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