forked from Witko/nvidia-xrun
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnvidia-xrun
119 lines (97 loc) · 2.55 KB
/
nvidia-xrun
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
DRY_RUN=0
function printHelp {
echo "Utility to run games and applications in separate X on discrete Nvidia graphic card"
echo "Usage: "
echo "nvidia-xrun [<options>] [<app>]"
echo "Options: "
echo " -d Dry run - prints the final command but does not execute it"
}
function execute {
if [ ${DRY_RUN} -eq 1 ]
then
echo ">>Dry run. Command: $*"
else
eval $*
fi
}
if [[ $EUID -eq 0 ]]; then
echo "This script must not be run as root" >&2
exit 1
fi
if [ "$1" == "-d" ]
then
DRY_RUN=1
shift 1
fi
# calculate current VT
LVT=`sudo fgconsole`
# calculate first usable display
XNUM="-1"
SOCK="something"
while [ ! -z "$SOCK" ]
do
XNUM=$(( $XNUM + 1 ))
SOCK=$(ls -A -1 /tmp/.X11-unix | grep "X$XNUM" )
done
NEWDISP=":$XNUM"
if [ ! -z "$*" ] # generate exec line if arguments are given
then
# test if executable exists in path
if [ -x "$(which $1 2> /dev/null)" ]
then
# generate exec line
EXECL="$(which $1)"
# test if executable exists on disk
elif [ -e "$(realpath "$1")" ]
then
# generate exec line
EXECL="$(realpath "$1")"
else
echo "$1: No such executable!"
exit 1
fi
shift 1
EXECL="$EXECL $*"
else # prepare to start new X sessions if no arguments passed
EXECL=""
fi
EXECL="/etc/X11/xinit/nvidia-xinitrc $EXECL"
COMMAND="startx $EXECL -- $NEWDISP vt$LVT -background none -noreset -keeptty -verbose 7 -config nvidia-xorg.conf"
# --------- TURNING ON GPU -----------
echo 'Waking up nvidia GPU'
if ! [ -f /proc/acpi/bbswitch ]
then
execute "sudo modprobe bbswitch"
fi
execute "sudo tee /proc/acpi/bbswitch <<<ON"
# ---------- LOADING MODULES ----------
echo 'Loading nvidia module'
execute "sudo modprobe nvidia"
echo 'Loading nvidia_uvm module'
execute "sudo modprobe nvidia_uvm"
echo 'Loading nvidia_modeset module'
execute "sudo modprobe nvidia_modeset"
echo 'Loading nvidia_drm module'
execute "sudo modprobe nvidia_drm"
# ---------- EXECUTING COMMAND --------
execute ${COMMAND}
# ---------- UNLOADING MODULES --------
echo 'Unloading nvidia_drm module'
execute "sudo rmmod nvidia_drm"
echo 'Unloading nvidia_modeset module'
execute "sudo rmmod nvidia_modeset"
echo 'Unloading nvidia_uvm module'
execute "sudo rmmod nvidia_uvm"
echo 'Unloading nvidia module'
execute "sudo rmmod nvidia"
# --------- TURNING OFF GPU ----------
if [ -f /proc/acpi/bbswitch ]
then
echo 'Turning off nvidia GPU'
execute "sudo tee /proc/acpi/bbswitch <<<OFF"
echo -n 'Current state of nvidia GPU: '
execute "cat /proc/acpi/bbswitch"
else
echo "Bbswitch kernel module not loaded."
fi