Skip to content

Commit

Permalink
Add CPU/RAM monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
L3337 committed Nov 22, 2022
1 parent 1e90129 commit 473aa4b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/sglib/lib/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
]

ENGINE_SUBPROCESS = None
ENGINE_PSUTIL = None

def close_engine():
""" Ask the engine to gracefully stop itself, then kill the process if it
doesn't exit on it's own
"""
constants.IPC.stop_server()
global ENGINE_SUBPROCESS
global ENGINE_SUBPROCESS, ENGINE_PSUTIL
if ENGINE_SUBPROCESS is not None:
f_exited = False
for i in range(50):
Expand All @@ -53,6 +54,7 @@ def close_engine():
)
LOG.exception(ex)
ENGINE_SUBPROCESS = None
ENGINE_PSUTIL = None
if os.path.exists(ENGINE_PIDFILE):
os.remove(ENGINE_PIDFILE)
constants.READY = False
Expand Down Expand Up @@ -99,7 +101,6 @@ def open_engine(a_project_path, fps):

f_pid = os.getpid()
LOG.info(f"Starting audio engine with {a_project_path}")
global ENGINE_SUBPROCESS

threads = int(util.DEVICE_SETTINGS['threads'])
if threads == 0:
Expand All @@ -125,6 +126,10 @@ def reopen_engine():
constants.IPC_ENABLED = True

def run_engine(cmd):
global ENGINE_SUBPROCESS
global ENGINE_SUBPROCESS, ENGINE_PSUTIL
ENGINE_SUBPROCESS = run_process(cmd, ENGINE_PIDFILE)
try:
ENGINE_PSUTIL = psutil.Process(ENGINE_SUBPROCESS.pid)
except:
LOG.exception('Could not create ENGINE_PSUTIL')

5 changes: 5 additions & 0 deletions src/sglib/lib/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,8 @@
will pass-through any audio from items or sends.
"""

ENGINE_MON = """\
Shows CPU and memory percentage. Note that the CPU usage is a max of the
cores that are in use, not based on the average of CPU load and available
logical cores, which is less useful.
"""
4 changes: 4 additions & 0 deletions src/sgui/daw/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def __init__(self):
self.last_midi_dir = None
shared.ROUTING_GRAPH_WIDGET.setToolTip(sg_strings.routing_graph)

self.engine_mon_label = QLabel()
self.engine_mon_label.setToolTip(sg_strings.ENGINE_MON)
self.setCornerWidget(self.engine_mon_label)

self.setObjectName("plugin_ui")

# Transport shortcuts (added here so they will work
Expand Down
16 changes: 16 additions & 0 deletions src/sgui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class SgMainWindow(QWidget):
}
daw_callback = Signal(str)
wave_edit_callback = Signal(str)
engine_mon_callback = Signal(str)

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -204,6 +205,13 @@ def setup(self, scaler):
self.current_module = daw
self.current_window = daw.MAIN_WINDOW

self.engine_mon_callback.connect(
daw.MAIN_WINDOW.engine_mon_label.setText
)
self.engine_mon_callback.connect(
wave_edit.MAIN_WINDOW.engine_mon_label.setText
)

for f_module in shared.HOST_MODULES:
self.transport_stack.addWidget(f_module.TRANSPORT.group_box)

Expand Down Expand Up @@ -751,6 +759,14 @@ def timeout_handler():
f_window.exec()

def subprocess_monitor(self):
try:
if engine.ENGINE_PSUTIL:
cpu = round(engine.ENGINE_PSUTIL.cpu_percent(), 1)
mem = round(engine.ENGINE_PSUTIL.memory_percent(), 1)
text = f'CPU: {cpu}% RAM: {mem}%'
self.engine_mon_callback.emit(text)
except:
pass
try:
if (
engine.ENGINE_SUBPROCESS
Expand Down
4 changes: 4 additions & 0 deletions src/sgui/wave_edit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ def __init__(self):
self.main_tabwidget = QTabWidget()
self.main_layout.addWidget(self.main_tabwidget)

self.engine_mon_label = QLabel()
self.engine_mon_label.setToolTip(sg_strings.ENGINE_MON)
self.main_tabwidget.setCornerWidget(self.engine_mon_label)

self.main_tabwidget.addTab(WAVE_EDITOR.widget, _("Wave Editor"))

self.notes_tab = ProjectNotes(
Expand Down

0 comments on commit 473aa4b

Please sign in to comment.