From 61a9f308a110c187ab0318d8b090559c258a5b76 Mon Sep 17 00:00:00 2001 From: Mochamad Taufan Rezzafri Date: Tue, 10 Jul 2018 21:40:48 +0700 Subject: [PATCH 1/8] Support to execute command with arguments. --- nvidia-xinitrc | 2 +- nvidia-xrun | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nvidia-xinitrc b/nvidia-xinitrc index bf3e2ac..10001f0 100644 --- a/nvidia-xinitrc +++ b/nvidia-xinitrc @@ -39,6 +39,6 @@ if [ -f "$userxinitrc" ]; then sh ${userxinitrc} $# else if [ $# -gt 0 ]; then - "$*" + sh -c "exec $*" fi fi diff --git a/nvidia-xrun b/nvidia-xrun index 23189f0..1def0b0 100644 --- a/nvidia-xrun +++ b/nvidia-xrun @@ -65,7 +65,7 @@ else # prepare to start new X sessions if no arguments passed EXECL="" fi -EXECL="/etc/X11/xinit/nvidia-xinitrc $EXECL" +EXECL="/etc/X11/xinit/nvidia-xinitrc \"$EXECL\"" COMMAND="xinit $EXECL -- $NEWDISP vt$LVT -nolisten tcp -br -config nvidia-xorg.conf -configdir nvidia-xorg.conf.d" @@ -85,7 +85,7 @@ echo 'Loading nvidia_modeset module' execute "sudo modprobe nvidia_modeset" echo 'Loading nvidia_drm module' -execute "sudo modprobe nvidia_drm" +execute "sudo modprobe nvidia_drm modeset=1" # ---------- EXECUTING COMMAND -------- execute ${COMMAND} From 43735003aa38b884251243ec907519ebd68ca471 Mon Sep 17 00:00:00 2001 From: Witko Date: Mon, 24 Sep 2018 20:03:28 +0200 Subject: [PATCH 2/8] Removed bbswitch --- .gitignore | 2 ++ README.md | 2 ++ config/nvidia-xrun | 5 +++ nvidia-xinitrc | 19 +++++------ nvidia-xrun | 78 +++++++++++++++++++++------------------------- 5 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 config/nvidia-xrun mode change 100644 => 100755 nvidia-xrun diff --git a/.gitignore b/.gitignore index 2bc7515..505e08f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ # Created by .ignore support plugin (hsz.mobi) nvidia-xrun.iml .idea +test-install.sh +test-cleanup.sh \ No newline at end of file diff --git a/README.md b/README.md index fe4330b..1085ae8 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Currently sudo is required as the script needs to wake up GPU, modprobe the nvid * **/etc/X11/xinit/nvidia-xinitrc** - xinitrc config file. Contains the setting of provider output source * **/etc/X11/xinit/nvidia-xinitrc.d** - custom xinitrc scripts directory * **/etc/X11/nvidia-xorg.conf.d** - custom X config directory +* **/etc/default/nvidia-xorg** - nvidia-xrun config file * **/usr/share/xsessions/nvidia-xrun-openbox.desktop** - xsession file for openbox * **/usr/share/xsessions/nvidia-xrun-plasma.desktop** - xsession file for plasma * **[OPTIONAL] ~/.nvidia-xinitrc** - user-level custom xinit script file. You can put here your favourite window manager for example @@ -44,6 +45,7 @@ Also this way you can adjust some nvidia settings if you encounter issues: # Option "AllowEmptyInitialConfiguration" "Yes" # Option "UseDisplayDevice" "none" EndSection +You also need to set the bus id in the `/etc/default/nvidia-xorg` file - e.g. `BUS_ID=0000:00:01.0` ## Automatically run window manager For convenience you can create `nano ~/.nvidia-xinitrc` and put there your favourite window manager: diff --git a/config/nvidia-xrun b/config/nvidia-xrun new file mode 100644 index 0000000..967917b --- /dev/null +++ b/config/nvidia-xrun @@ -0,0 +1,5 @@ +BUS_ID=0000:00:01.0 +MODULE_NVIDIA_DRM_PARAMS="modeset=1" +BUS_RESCAN_WAIT_SEC=1 +MODULES_LOAD=(nvidia nvidia_uvm nvidia_modeset "nvidia_drm modeset=1") +MODULES_UNLOAD=(nvidia_drm nvidia_modeset nvidia_uvm nvidia) diff --git a/nvidia-xinitrc b/nvidia-xinitrc index defaa4b..ffa4f11 100644 --- a/nvidia-xinitrc +++ b/nvidia-xinitrc @@ -1,4 +1,5 @@ -#!/bin/bash +#!/usr/bin/env bash + userresources=$HOME/.Xresources usermodmap=$HOME/.Xmodmap sysresources=/etc/X11/xinit/.Xresources @@ -6,28 +7,28 @@ sysmodmap=/etc/X11/xinit/.Xmodmap userxinitrc=$HOME/.nvidia-xinitrc # merge in defaults and keymaps -if [ -f ${sysresources} ]; then +if [[ -f ${sysresources} ]]; then xrdb -merge ${sysresources} fi -if [ -f ${sysmodmap} ]; then +if [[ -f ${sysmodmap} ]]; then xmodmap ${sysmodmap} fi -if [ -f "$userresources" ]; then +if [[ -f "$userresources" ]]; then xrdb -merge "$userresources" fi -if [ -f "$usermodmap" ]; then +if [[ -f "$usermodmap" ]]; then xmodmap "$usermodmap" fi export LD_LIBRARY_PATH=/usr/lib64/nvidia/:/usr/lib32/nvidia:/usr/lib:${LD_LIBRARY_PATH} # load additional configs -if [ -d /etc/X11/xinit/nvidia-xinitrc.d ] ; then +if [[ -d /etc/X11/xinit/nvidia-xinitrc.d ]] ; then for f in /etc/X11/xinit/nvidia-xinitrc.d/?*.sh ; do - [ -x "$f" ] && . "$f" + [[ -x "$f" ]] && . "$f" done unset f fi @@ -35,10 +36,10 @@ fi xrandr --setprovideroutputsource modesetting NVIDIA-0 xrandr --auto -if [ -f "$userxinitrc" ]; then +if [[ -f "$userxinitrc" ]]; then sh ${userxinitrc} $* else - if [ $# -gt 0 ]; then + if [[ $# -gt 0 ]]; then sh -c "exec $*" fi fi diff --git a/nvidia-xrun b/nvidia-xrun old mode 100644 new mode 100755 index c72ebed..4e1f939 --- a/nvidia-xrun +++ b/nvidia-xrun @@ -1,4 +1,5 @@ -#!/bin/bash +#!/usr/bin/env bash + DRY_RUN=0 function printHelp { echo "Utility to run games and applications in separate X on discrete Nvidia graphic card" @@ -9,7 +10,7 @@ function printHelp { } function execute { - if [ ${DRY_RUN} -eq 1 ] + if [[ ${DRY_RUN} -eq 1 ]] then echo ">>Dry run. Command: $*" else @@ -23,19 +24,22 @@ if [[ $EUID -eq 0 ]]; then exit 1 fi -if [ "$1" == "-d" ] +if [[ "$1" == "-d" ]] then DRY_RUN=1 shift 1 fi +# load config file +. /etc/default/nvidia-xrun + # calculate current VT LVT=`fgconsole` # calculate first usable display XNUM="-1" SOCK="something" -while [ ! -z "$SOCK" ] +while [[ ! -z "$SOCK" ]] do XNUM=$(( $XNUM + 1 )) SOCK=$(ls -A -1 /tmp/.X11-unix | grep "X$XNUM" ) @@ -43,15 +47,15 @@ done NEWDISP=":$XNUM" -if [ ! -z "$*" ] # generate exec line if arguments are given -then +if [[ ! -z "$*" ]] # generate exec line if arguments are given +then # test if executable exists in path - if [ -x "$(which $1 2> /dev/null)" ] + if [[ -x "$(which $1 2> /dev/null)" ]] then # generate exec line EXECL="$(which $1)" # test if executable exists on disk - elif [ -e "$(realpath "$1")" ] + elif [[ -e "$(realpath "$1")" ]] then # generate exec line EXECL="$(realpath "$1")" @@ -70,50 +74,38 @@ EXECL="/etc/X11/xinit/nvidia-xinitrc \"$EXECL\"" COMMAND="xinit $EXECL -- $NEWDISP vt$LVT -nolisten tcp -br -config nvidia-xorg.conf -configdir nvidia-xorg.conf.d" # --------- TURNING ON GPU ----------- -echo 'Waking up nvidia GPU' -if ! [ -f /proc/acpi/bbswitch ] -then - execute "sudo modprobe bbswitch" +if [[ ! -d /sys/bus/pci/devices/${BUS_ID} ]]; then + echo 'Rescanning PCI devices' + execute "sudo tee /sys/bus/pci/rescan <<<1" + echo "Waiting ${BUS_RESCAN_WAIT_SEC} second for rescan" + execute "sleep ${BUS_RESCAN_WAIT_SEC}" fi -execute "sudo tee /proc/acpi/bbswitch << Date: Thu, 4 Apr 2019 20:25:51 +0100 Subject: [PATCH 3/8] Add -r to unload modprobe command --- nvidia-xrun | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nvidia-xrun b/nvidia-xrun index 6f402ee..7565c90 100755 --- a/nvidia-xrun +++ b/nvidia-xrun @@ -104,7 +104,7 @@ execute ${COMMAND} for module in "${MODULES_UNLOAD[@]}" do echo "Unloading module ${module}" - execute "sudo modprobe ${module}" + execute "sudo modprobe -r ${module}" done # --------- TURNING OFF GPU ---------- From 20a99ec6c954716e241f9efa2f3db410e0a03f03 Mon Sep 17 00:00:00 2001 From: Michele Sorcinelli Date: Thu, 4 Apr 2019 20:26:30 +0100 Subject: [PATCH 4/8] Fix typo in the readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8cff420..c0da4fa 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ When the nvidia-xrun command is used, the device is added again to the tree so t * **/etc/X11/xinit/nvidia-xinitrc.d** - custom xinitrc scripts directory * **/etc/X11/nvidia-xorg.conf.d** - custom X config directory * **/etc/systemd/system/nvidia-xrun-pm.service** systemd service -* **/etc/default/nvidia-xorg** - nvidia-xrun config file +* **/etc/default/nvidia-xrun** - nvidia-xrun config file * **/usr/share/xsessions/nvidia-xrun-openbox.desktop** - xsession file for openbox * **/usr/share/xsessions/nvidia-xrun-plasma.desktop** - xsession file for plasma * **[OPTIONAL] ~/.nvidia-xinitrc** - user-level custom xinit script file. You can put here your favourite window manager for example @@ -60,7 +60,7 @@ Also this way you can adjust some nvidia settings if you encounter issues: # Option "UseDisplayDevice" "none" EndSection -You also need to set the bus id in the `/etc/default/nvidia-xorg` file - e.g. `BUS_ID=0000:00:01.0` +You also need to set the bus id in the `/etc/default/nvidia-xrun` file - e.g. `DEVICE_BUS_ID=0000:00:01.0` ## Automatically run window manager For convenience you can create `nano ~/.nvidia-xinitrc` and put there your favourite window manager: From de8040a6cb50abbd7770486a64173f7766a2dd0a Mon Sep 17 00:00:00 2001 From: Michele Sorcinelli Date: Thu, 4 Apr 2019 20:42:07 +0100 Subject: [PATCH 5/8] Fix wrong logic The command to set the power controller for the PCI express controller needs to run in every case, so the if-else needs to be removed --- nvidia-xrun | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/nvidia-xrun b/nvidia-xrun index 7565c90..0a3a734 100755 --- a/nvidia-xrun +++ b/nvidia-xrun @@ -108,10 +108,8 @@ do done # --------- TURNING OFF GPU ---------- -if [[ -f /sys/bus/pci/devices/${DEVICE_BUS_ID}/remove ]]; then - echo 'Removing Nvidia bus from the kernel' - execute "sudo tee /sys/bus/pci/devices/${DEVICE_BUS_ID}/remove <<<1" -else - echo 'Enabling powersave for the PCIe controller' - execute "sudo tee /sys/bus/pci/devices/${CONTROLLER_BUS_ID}/power/control << Date: Thu, 4 Apr 2019 21:52:35 +0100 Subject: [PATCH 6/8] Integrate the systemd service with nvidia-xrun script --- nvidia-xrun | 26 +++++++++++++++++--------- nvidia-xrun-pm.service | 4 ++-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/nvidia-xrun b/nvidia-xrun index 0a3a734..2719096 100755 --- a/nvidia-xrun +++ b/nvidia-xrun @@ -18,11 +18,13 @@ function execute { fi } +function turn_off_gpu { + echo 'Removing Nvidia bus from the kernel' + execute "sudo tee /sys/bus/pci/devices/${DEVICE_BUS_ID}/remove <<<1" -if [[ $EUID -eq 0 ]]; then - echo "This script must not be run as root" >&2 - exit 1 -fi + echo 'Enabling powersave for the PCIe controller' + execute "sudo tee /sys/bus/pci/devices/${CONTROLLER_BUS_ID}/power/control <<&2 + exit 1 +fi + # calculate current VT LVT=`fgconsole` @@ -108,8 +120,4 @@ do done # --------- TURNING OFF GPU ---------- -echo 'Removing Nvidia bus from the kernel' -execute "sudo tee /sys/bus/pci/devices/${DEVICE_BUS_ID}/remove <<<1" - -echo 'Enabling powersave for the PCIe controller' -execute "sudo tee /sys/bus/pci/devices/${CONTROLLER_BUS_ID}/power/control << /sys/bus/pci/devices/0000:01:00.0/remove' -ExecStart=/bin/bash -c 'echo auto > /sys/bus/pci/devices/0000:00:01.0/power/control' +Environment="TURN_OFF_GPU_ONLY=1" +ExecStart=/usr/bin/nvidia-xrun [Install] WantedBy=multi-user.target From 17331d61860ca9025c2a36e42c4b69754053edba Mon Sep 17 00:00:00 2001 From: Michele Sorcinelli Date: Fri, 5 Apr 2019 13:06:06 +0100 Subject: [PATCH 7/8] Explain how to set bus ids in the readme --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c0da4fa..50051d0 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,12 @@ Also this way you can adjust some nvidia settings if you encounter issues: # Option "UseDisplayDevice" "none" EndSection -You also need to set the bus id in the `/etc/default/nvidia-xrun` file - e.g. `DEVICE_BUS_ID=0000:00:01.0` - +In order to make power management features work properly, you need to make sure +that bus ids in `/etc/default/nvidia-xrun` are correctly set for both the +NVIDIA graphic card and the PCI express controller that hosts it. You should be +able to find both the ids in the output of `lshw`: the PCIe controller is +usually displayed right before the graphic card. + ## Automatically run window manager For convenience you can create `nano ~/.nvidia-xinitrc` and put there your favourite window manager: From dacecb6ebfd1633ce927ce6d8413d98cccf2e561 Mon Sep 17 00:00:00 2001 From: Michele Sorcinelli Date: Fri, 5 Apr 2019 13:19:56 +0100 Subject: [PATCH 8/8] Remove unused parameter in config --- config/nvidia-xrun | 1 - 1 file changed, 1 deletion(-) diff --git a/config/nvidia-xrun b/config/nvidia-xrun index a42cea3..1c87056 100644 --- a/config/nvidia-xrun +++ b/config/nvidia-xrun @@ -1,6 +1,5 @@ CONTROLLER_BUS_ID=0000:00:01.0 DEVICE_BUS_ID=0000:01:00.0 -MODULE_NVIDIA_DRM_PARAMS="modeset=1" BUS_RESCAN_WAIT_SEC=1 MODULES_LOAD=(nvidia nvidia_uvm nvidia_modeset "nvidia_drm modeset=1") MODULES_UNLOAD=(nvidia_drm nvidia_modeset nvidia_uvm nvidia)