-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu-mode-switcher.sh
More file actions
executable file
·180 lines (158 loc) · 3.63 KB
/
gpu-mode-switcher.sh
File metadata and controls
executable file
·180 lines (158 loc) · 3.63 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env bash
# Usage:
# chmod +x gpu-mode-switcher.sh
# ./gpu-mode-switcher.sh
set -u
RUNTIME_STATUS_PATH="/sys/bus/pci/devices/0000:01:00.0/power/runtime_status"
NVIDIA_POWER_PATH="/proc/driver/nvidia/gpus/0000:01:00.0/power"
has_cmd() {
command -v "$1" >/dev/null 2>&1
}
run_with_sudo() {
if [[ "${EUID}" -eq 0 ]]; then
"$@"
else
sudo "$@"
fi
}
confirm() {
local prompt="$1"
local answer
read -r -p "${prompt} [y/N]: " answer
[[ "${answer}" =~ ^[Yy]$ ]]
}
require_cmd() {
local cmd="$1"
if ! has_cmd "${cmd}"; then
echo "Missing command: ${cmd}"
return 1
fi
}
post_switch_notice() {
echo
echo "Mode change applied. A reboot is required."
echo
}
switch_to_hybrid() {
require_cmd envycontrol || return
if run_with_sudo envycontrol -s hybrid --rtd3 3; then
post_switch_notice
fi
}
switch_to_integrated() {
require_cmd envycontrol || return
if run_with_sudo envycontrol -s integrated; then
post_switch_notice
fi
}
switch_to_nvidia() {
require_cmd envycontrol || return
if run_with_sudo envycontrol -s nvidia; then
post_switch_notice
fi
}
run_initramfs_update() {
require_cmd update-initramfs || return
run_with_sudo update-initramfs -u
}
reboot_now() {
if confirm "Reboot now?"; then
run_with_sudo reboot
fi
}
show_verification_checks() {
echo
echo "Checking dGPU runtime status:"
if [[ -r "${RUNTIME_STATUS_PATH}" ]]; then
cat "${RUNTIME_STATUS_PATH}"
else
echo "Cannot read ${RUNTIME_STATUS_PATH}"
fi
echo
echo "Checking NVIDIA RTD3 info:"
if [[ -r "${NVIDIA_POWER_PATH}" ]]; then
cat "${NVIDIA_POWER_PATH}"
else
echo "Cannot read ${NVIDIA_POWER_PATH}"
fi
echo
echo "Checking TLP battery thresholds:"
if has_cmd tlp-stat; then
run_with_sudo tlp-stat -b
else
echo "Missing command: tlp-stat"
fi
echo
}
show_renderer_checks() {
echo
echo "prime-run renderer check:"
if has_cmd prime-run && has_cmd glxinfo; then
prime-run glxinfo | grep "OpenGL renderer" || echo "Renderer string not found"
else
echo "Missing command(s): prime-run and/or glxinfo"
fi
echo
echo "Default renderer check:"
if has_cmd glxinfo; then
glxinfo | grep "OpenGL renderer" || echo "Renderer string not found"
else
echo "Missing command: glxinfo"
fi
echo
}
optional_actions_menu() {
local choice
while true; do
cat <<'EOF'
Optional actions:
1) update-initramfs -u
2) Reboot now
3) Run post-reboot verification checks
4) Run renderer checks (prime-run / default)
5) Back
EOF
read -r -p "Select option: " choice
case "${choice}" in
1) run_initramfs_update ;;
2) reboot_now ;;
3) show_verification_checks ;;
4) show_renderer_checks ;;
5) return ;;
*) echo "Invalid option." ;;
esac
done
}
startup_checks() {
echo "Quick dependency check:"
has_cmd envycontrol || echo "- Missing: envycontrol"
has_cmd update-initramfs || echo "- Missing: update-initramfs"
has_cmd tlp-stat || echo "- Missing (optional): tlp-stat"
has_cmd glxinfo || echo "- Missing (optional): glxinfo"
has_cmd prime-run || echo "- Missing (optional): prime-run"
echo
}
main_menu() {
local choice
while true; do
cat <<'EOF'
GPU mode switcher (envycontrol)
1) Hybrid (envycontrol -s hybrid --rtd3 3)
2) Integrated (envycontrol -s integrated)
3) NVIDIA (envycontrol -s nvidia)
4) Optional actions
5) Exit
EOF
read -r -p "Select option: " choice
case "${choice}" in
1) switch_to_hybrid ;;
2) switch_to_integrated ;;
3) switch_to_nvidia ;;
4) optional_actions_menu ;;
5) exit 0 ;;
*) echo "Invalid option." ;;
esac
done
}
startup_checks
main_menu