error handling cleanup
This commit is contained in:
@@ -76,14 +76,18 @@ ERR = bcolors.RED + bcolors.BOLD + 'ERR' + bcolors.RESET
|
|||||||
LIM = bcolors.YELLOW + bcolors.BOLD + 'LIM' + bcolors.RESET
|
LIM = bcolors.YELLOW + bcolors.BOLD + 'LIM' + bcolors.RESET
|
||||||
|
|
||||||
|
|
||||||
|
def fatal(msg, code=1):
|
||||||
|
print(msg)
|
||||||
|
sys.exit(code)
|
||||||
|
|
||||||
|
|
||||||
def writemsr(msr, val):
|
def writemsr(msr, val):
|
||||||
msr_list = ['/dev/cpu/{:d}/msr'.format(x) for x in range(cpu_count())]
|
msr_list = ['/dev/cpu/{:d}/msr'.format(x) for x in range(cpu_count())]
|
||||||
if not os.path.exists(msr_list[0]):
|
if not os.path.exists(msr_list[0]):
|
||||||
try:
|
try:
|
||||||
subprocess.check_call(('modprobe', 'msr'))
|
subprocess.check_call(('modprobe', 'msr'))
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
print('[E] Unable to load the msr module.')
|
fatal('[E] Unable to load the msr module.')
|
||||||
sys.exit(1)
|
|
||||||
try:
|
try:
|
||||||
for addr in msr_list:
|
for addr in msr_list:
|
||||||
f = os.open(addr, os.O_WRONLY)
|
f = os.open(addr, os.O_WRONLY)
|
||||||
@@ -92,11 +96,10 @@ def writemsr(msr, val):
|
|||||||
os.close(f)
|
os.close(f)
|
||||||
except (IOError, OSError) as e:
|
except (IOError, OSError) as e:
|
||||||
if e.errno == EPERM or e.errno == EACCES:
|
if e.errno == EPERM or e.errno == EACCES:
|
||||||
print(
|
fatal(
|
||||||
'[E] Unable to write to MSR. Try to disable Secure Boot '
|
'[E] Unable to write to MSR. Try to disable Secure Boot '
|
||||||
'and check if your kernel does not restrict access to MSR.'
|
'and check if your kernel does not restrict access to MSR.'
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
|
||||||
else:
|
else:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
@@ -105,15 +108,13 @@ def writemsr(msr, val):
|
|||||||
def readmsr(msr, from_bit=0, to_bit=63, cpu=None, flatten=False):
|
def readmsr(msr, from_bit=0, to_bit=63, cpu=None, flatten=False):
|
||||||
assert cpu is None or cpu in range(cpu_count())
|
assert cpu is None or cpu in range(cpu_count())
|
||||||
if from_bit > to_bit:
|
if from_bit > to_bit:
|
||||||
print('[E] Wrong readmsr bit params')
|
fatal('[E] Wrong readmsr bit params')
|
||||||
sys.exit(1)
|
|
||||||
msr_list = ['/dev/cpu/{:d}/msr'.format(x) for x in range(cpu_count())]
|
msr_list = ['/dev/cpu/{:d}/msr'.format(x) for x in range(cpu_count())]
|
||||||
if not os.path.exists(msr_list[0]):
|
if not os.path.exists(msr_list[0]):
|
||||||
try:
|
try:
|
||||||
subprocess.check_call(('modprobe', 'msr'))
|
subprocess.check_call(('modprobe', 'msr'))
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
print('[E] Unable to load the msr module.')
|
fatal('[E] Unable to load the msr module.')
|
||||||
sys.exit(1)
|
|
||||||
try:
|
try:
|
||||||
output = []
|
output = []
|
||||||
for addr in msr_list:
|
for addr in msr_list:
|
||||||
@@ -127,8 +128,7 @@ def readmsr(msr, from_bit=0, to_bit=63, cpu=None, flatten=False):
|
|||||||
return output[cpu] if cpu is not None else output
|
return output[cpu] if cpu is not None else output
|
||||||
except (IOError, OSError) as e:
|
except (IOError, OSError) as e:
|
||||||
if e.errno == EPERM or e.errno == EACCES:
|
if e.errno == EPERM or e.errno == EACCES:
|
||||||
print('[E] Unable to read from MSR. Try to disable Secure Boot.')
|
fatal('[E] Unable to read from MSR. Try to disable Secure Boot.')
|
||||||
sys.exit(1)
|
|
||||||
else:
|
else:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
@@ -160,8 +160,7 @@ def is_on_battery(config):
|
|||||||
return not bool(int(f.read()))
|
return not bool(int(f.read()))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
print('[E] No valid Sysfs_Power_Path found!')
|
fatal('[E] No valid Sysfs_Power_Path found!')
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def get_cpu_platform_info():
|
def get_cpu_platform_info():
|
||||||
@@ -272,8 +271,7 @@ def load_config():
|
|||||||
if value is not None:
|
if value is not None:
|
||||||
value = config.set(power_source, option, str(max(0.1, value)))
|
value = config.set(power_source, option, str(max(0.1, value)))
|
||||||
elif option == 'Update_Rate_s':
|
elif option == 'Update_Rate_s':
|
||||||
print('[E] The mandatory "Update_Rate_s" parameter is missing.')
|
fatal('[E] The mandatory "Update_Rate_s" parameter is missing.')
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
trip_temp = config.getfloat(power_source, 'Trip_Temp_C', fallback=None)
|
trip_temp = config.getfloat(power_source, 'Trip_Temp_C', fallback=None)
|
||||||
if trip_temp is not None:
|
if trip_temp is not None:
|
||||||
@@ -406,8 +404,7 @@ def power_thread(config, regs, exit_event):
|
|||||||
try:
|
try:
|
||||||
mchbar_mmio = MMIO(0xFED159A0, 8)
|
mchbar_mmio = MMIO(0xFED159A0, 8)
|
||||||
except MMIOError:
|
except MMIOError:
|
||||||
print('[E] Unable to open /dev/mem. Try to disable Secure Boot.')
|
fatal('[E] Unable to open /dev/mem. Try to disable Secure Boot.')
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
while not exit_event.is_set():
|
while not exit_event.is_set():
|
||||||
# print thermal status
|
# print thermal status
|
||||||
@@ -485,8 +482,7 @@ def power_thread(config, regs, exit_event):
|
|||||||
|
|
||||||
def check_kernel():
|
def check_kernel():
|
||||||
if os.geteuid() != 0:
|
if os.geteuid() != 0:
|
||||||
print('[E] No root no party. Try again with sudo.')
|
fatal('[E] No root no party. Try again with sudo.')
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
kernel_config = None
|
kernel_config = None
|
||||||
try:
|
try:
|
||||||
@@ -504,11 +500,10 @@ def check_kernel():
|
|||||||
if kernel_config is None:
|
if kernel_config is None:
|
||||||
print('[W] Unable to obtain and validate kernel config.')
|
print('[W] Unable to obtain and validate kernel config.')
|
||||||
elif not re.search('CONFIG_DEVMEM=y', kernel_config):
|
elif not re.search('CONFIG_DEVMEM=y', kernel_config):
|
||||||
print('[E] Bad kernel config: you need CONFIG_DEVMEM=y.')
|
fatal('[E] Bad kernel config: you need CONFIG_DEVMEM=y.')
|
||||||
sys.exit(1)
|
|
||||||
elif not re.search('CONFIG_X86_MSR=(y|m)', kernel_config):
|
elif not re.search('CONFIG_X86_MSR=(y|m)', kernel_config):
|
||||||
print('[E] Bad kernel config: you need CONFIG_X86_MSR builtin or as module.')
|
fatal('[E] Bad kernel config: you need CONFIG_X86_MSR builtin or as module.')
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def monitor(exit_event, wait):
|
def monitor(exit_event, wait):
|
||||||
|
|||||||
Reference in New Issue
Block a user