VRUtil/vrutil/gpu.py

86 lines
2.9 KiB
Python
Raw Normal View History

import re
import dearpygui.dearpygui as dpg
2024-07-07 16:53:24 +02:00
from vrutil.utils import loaded, store, write_as_sudo
_GPU_POWER_PROFILE_MODE_FILE = '/sys/class/drm/card0/device/pp_power_profile_mode'
_GPU_PERFORMANCE_LEVEL_FILE = '/sys/class/drm/card0/device/power_dpm_force_performance_level'
class GpuSettings:
"""GPU related settings"""
def __init__(self):
self._power_profile_item = None
self._performance_level_item = None
def create_views(self):
with dpg.collapsing_header(label='GPU', default_open=True):
self._power_profile_item = dpg.add_combo(
label='Power Profile',
items=self._get_available_profiles(),
default_value=self._get_selected_profile(),
width=200,
callback=self._select_profile,
)
self._performance_level_item = dpg.add_combo(
label='Performance Level',
items=self._get_available_levels(),
default_value=self._get_selected_level(),
width=200,
callback=self._select_level,
)
dpg.add_spacer(height=10)
2024-07-07 16:53:24 +02:00
def reload(self):
dpg.set_value(self._power_profile_item, self._get_selected_profile())
dpg.set_value(self._performance_level_item, self._get_selected_level())
# region Power Profile
def _get_available_profiles(self):
return tuple(map(lambda profile: profile[1], self._raw_gpu_profiles()))
def _get_selected_profile(self):
active_profile = next(filter(lambda profile: profile[2] == '*', self._raw_gpu_profiles()), None)
loaded('GPU Profile', active_profile)
return active_profile[1] if active_profile is not None else None
def _select_profile(self, _sender, app_data, _user_data):
selected_profile = next(filter(lambda profile: profile[1] == app_data, self._raw_gpu_profiles()), None)
store('GPU Profile', selected_profile)
if selected_profile is not None:
2024-07-07 16:53:24 +02:00
write_as_sudo(_GPU_POWER_PROFILE_MODE_FILE, selected_profile[0])
self.reload()
@staticmethod
def _raw_gpu_profiles():
rex = re.compile('^ *(\\d) +(\\w+)([ *]):$', flags=re.MULTILINE)
with open(_GPU_POWER_PROFILE_MODE_FILE, 'r') as f:
matches = re.findall(rex, f.read())
return matches
# endregion
# region Performance Level
@staticmethod
def _get_available_levels():
return 'auto', 'high'
@staticmethod
def _get_selected_level():
with open(_GPU_PERFORMANCE_LEVEL_FILE, 'r') as f:
level = f.readline().strip()
loaded('GPU Level', level)
return level
def _select_level(self, _sender, level, _user_data):
store('GPU Level', level)
if level is not None:
2024-07-07 16:53:24 +02:00
write_as_sudo(_GPU_PERFORMANCE_LEVEL_FILE, level)
self.reload()
# endregion