Source code for torx.performance.system_information_m
"""Contain functions that return information about the system and the user."""
import subprocess
import re
import getpass
import psutil
from torx.autodoc_decorators_m import autodoc_function
[docs]
@autodoc_function
def get_num_cores():
"""Return number of available cores (including hyperthreads)."""
return psutil.cpu_count()
[docs]
@autodoc_function
def get_num_cores_phys():
"""Return number of available physical cores (without hyperthreads)."""
return psutil.cpu_count(logical=False)
[docs]
@autodoc_function
def get_mem_summary():
"""
Return an object containing a summary of all memory channels.
This includes total, available, etc..
"""
return psutil.virtual_memory()
[docs]
@autodoc_function
def get_mem_avail():
"""Return available memory in bytes."""
return get_mem_summary().available
[docs]
@autodoc_function
def get_mem_user():
"""Return the memory currently used in processes by the user."""
user = get_user()
mem_count = 0
for proc in psutil.process_iter():
# Process might be killed while checking, thus we try to get the
# memory and if it fails we skip it
try:
if proc.username() == user:
mem_count += proc.memory_info()[0]
except:
pass
return mem_count
[docs]
@autodoc_function
def get_user():
"""Return the name of the user."""
return getpass.getuser()
[docs]
@autodoc_function
def sys_call(command: list):
"""
Execute a system call given by the command list.
The list needs to contain the command and command line arguments.
"""
process = subprocess.run(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
stdout = process.stdout.strip()
stderr = process.stderr
if(stderr != ""):
raise EnvironmentError(r"System call failed, returning stderr:\n" \
+ stderr)
return stdout