From aa64d6cfd590b2e58efd8c5cf5d9594459f567ff Mon Sep 17 00:00:00 2001 From: ekobres Date: Sun, 27 Oct 2024 14:41:57 -0400 Subject: [PATCH 1/2] Added sleeps and process titles to prevent CPU thrashing and ease debugging --- controller_process.py | 6 +++++- games/game.py | 7 ++++++- kill_processes.sh | 2 +- pacemanager.py | 2 ++ piaudio.py | 7 +++++++ piparty.py | 16 +++++++++++++--- setup.sh | 2 +- webui.py | 2 ++ 8 files changed, 37 insertions(+), 7 deletions(-) diff --git a/controller_process.py b/controller_process.py index a7c81eac..50dd3a8b 100644 --- a/controller_process.py +++ b/controller_process.py @@ -1,6 +1,7 @@ from games import game, joust_non_stop, werewolf, zombie, commander, tournament, speed_bomb, fight_club import common, piparty import logging +import setproctitle logger = logging.getLogger(__name__) @@ -10,6 +11,9 @@ def main_track_move(menu, restart, move_serial, move_num, menu_opts, game_opts, team, team_color_enum, sensitivity, dead_move, invincible_move, music_speed, show_team_colors, red_on_kill,\ revive, kill_proc): + # Set the process title + setproctitle.setproctitle(f"JoustMania-main_track_move({move_serial})") + move = common.get_move(move_serial, move_num) while not kill_proc.value: @@ -69,4 +73,4 @@ def main_track_move(menu, restart, move_serial, move_num, menu_opts, game_opts, move=move, team=team, team_color_enum=team_color_enum, dead_move=dead_move, invincible_move=invincible_move, \ force_color=force_color, music_speed=music_speed, show_team_colors=show_team_colors, red_on_kill=red_on_kill, \ restart=restart, menu=menu, sensitivity=sensitivity, revive=revive, opts=game_opts - ) \ No newline at end of file + ) diff --git a/games/game.py b/games/game.py index c1428afd..c0a58683 100644 --- a/games/game.py +++ b/games/game.py @@ -425,11 +425,13 @@ def game_loop(self): self.handle_status() self.check_end_game() - if self.game_end: self.end_game() + #Give up 10ms to prevent CPU thrashing + time.sleep(0.01) + self.stop_tracking_moves() def switch_teams(self, serial, team): @@ -587,3 +589,6 @@ def track_move(cls, move, team, team_color_enum, dead_move, invincible_move, for move.set_leds(*Colors.Black.value) move.update_leds() move.set_rumble(0) + + #Give up 10ms to prevent CPU thrashing + time.sleep(0.01) diff --git a/kill_processes.sh b/kill_processes.sh index 10104457..e64bca3d 100755 --- a/kill_processes.sh +++ b/kill_processes.sh @@ -3,4 +3,4 @@ #this is for development purposes only, to stop the automattically #running piparty scripts sudo supervisorctl stop joustmania -sudo kill -9 $(ps aux | grep '[p]iparty' | awk '{print $2}') +sudo kill -9 $(ps aux | grep 'JoustMania-' | awk '{print $2}') diff --git a/pacemanager.py b/pacemanager.py index e092df94..8f64ad13 100644 --- a/pacemanager.py +++ b/pacemanager.py @@ -63,6 +63,8 @@ def choose_new_pace_(self, old_pace) -> typing.Tuple[object, float]: @common.async_print_exceptions async def run_(self): + import setproctitle + setproctitle.setproctitle(f"JoustMania-PaceManager") await asyncio.sleep(self.initial_pace_time_) pace = self.initial_pace_ diff --git a/piaudio.py b/piaudio.py index ee3d39ba..3ccc46ad 100644 --- a/piaudio.py +++ b/piaudio.py @@ -9,6 +9,8 @@ import pygame import random import glob +import setproctitle + from sys import platform if platform == "linux" or platform == "linux2": import alsaaudio @@ -189,10 +191,13 @@ def Resample(samples): wf.close() device.close() song_loaded = False + #Give up 500ms to prevent CPU thrashing + time.sleep(0.5) @functools.lru_cache(maxsize=128) class Audio: def __init__(self, fname): + setproctitle.setproctitle(f"JoustMania-Audio({fname})") #these are probably not necessary #segment = AudioSegment.from_file(fname) #buf = io.BytesIO() @@ -224,6 +229,8 @@ def start_effect_and_wait(self): @functools.lru_cache(maxsize=16) class Music: def __init__(self, name): + setproctitle.setproctitle(f"JoustMania-Music({name})") + self.name = name self.transition_future_ = asyncio.Future() diff --git a/piparty.py b/piparty.py index e5a4c944..c1ce6560 100644 --- a/piparty.py +++ b/piparty.py @@ -12,6 +12,7 @@ from sys import platform from dotenv import find_dotenv, load_dotenv import logging.config +import setproctitle if platform == "linux" or platform == "linux2": import jm_dbus @@ -86,7 +87,7 @@ def track_move(serial, move_num, move, menu_opts, force_color, battery, dead_cou while True: if(restart.value == 1 or menu.value == 0 or kill_proc.value): return # Stop tracking move if restarting, exiting menu, or kill_procedures - time.sleep(0.01) + # If there is a new event from the move if move.poll(): @@ -297,10 +298,14 @@ def track_move(serial, move_num, move, menu_opts, force_color, battery, dead_cou move.set_leds(*move_color) #Update colors move.update_leds() - + + #Give up 30ms to prevent CPU thrashing + time.sleep(0.03) + class Menu(): def __init__(self): - + setproctitle.setproctitle(f"JoustMania-Menu()") + # Set up shared namespace between webserver and joustmania self.command_queue = Queue() self.joust_manager = Manager() @@ -654,6 +659,8 @@ def check_charging_controller(self): move_opt[Opts.STATUS.value] = Status.ALIVE.value #If move is not charging, set it to alive def game_loop(self): + import setproctitle + setproctitle.setproctitle(f"JoustMania-game_loop()") self.play_menu_music = True while True: # Only start the music the first loop @@ -704,6 +711,9 @@ def game_loop(self): self.check_command_queue() self.update_status('menu') + #Give up 30ms to prevent CPU thrashing + time.sleep(0.03) + def check_admin_controls(self): show_bat = False diff --git a/setup.sh b/setup.sh index 68162ff9..62701dfe 100755 --- a/setup.sh +++ b/setup.sh @@ -55,7 +55,7 @@ setup() { PYTHON=$VENV/bin/python3 echo "installing virtual environment dependencies" - $PYTHON -m pip install --ignore-installed flask Flask-WTF pyalsaaudio pydub pyyaml dbus-python python-dotenv || exit -1 + $PYTHON -m pip install --ignore-installed flask Flask-WTF pyalsaaudio pydub pyyaml dbus-python python-dotenv setproctitle || exit -1 #Sometimes pygame tries to install without a whl, and fails (like 2.4.0) this #checks that only correct versions will install diff --git a/webui.py b/webui.py index 9f234448..ed732b20 100644 --- a/webui.py +++ b/webui.py @@ -207,6 +207,8 @@ def randomize_teams(self,num_teams): return str(team_colors).replace("'",'"')#JSON is dumb and demands double quotes def start_web(command_queue, ns): + import setproctitle + setproctitle.setproctitle(f"JoustMania-WebUI") webui = WebUI(command_queue,ns) webui.web_loop() From 3427426ce472f51bcd71ca3b764b5965816082df Mon Sep 17 00:00:00 2001 From: ekobres Date: Mon, 14 Jul 2025 12:39:06 -0400 Subject: [PATCH 2/2] Moved setproctitle to apt-get install in setup.sh --- setup.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/setup.sh b/setup.sh index 62701dfe..bace0de8 100755 --- a/setup.sh +++ b/setup.sh @@ -29,7 +29,7 @@ setup() { libudev-dev swig libbluetooth-dev \ alsa-utils alsa-tools libasound2-dev libsdl2-mixer-2.0-0 \ python-dbus-dev python3-dbus libdbus-glib-1-dev usbutils libatlas-base-dev \ - python3-pyaudio python3-psutil || exit -1 + python3-pyaudio python3-psutil python3-setproctitle || exit -1 echo "Installing PS move A.P.I. software updates" #install components for psmoveapi @@ -55,7 +55,8 @@ setup() { PYTHON=$VENV/bin/python3 echo "installing virtual environment dependencies" - $PYTHON -m pip install --ignore-installed flask Flask-WTF pyalsaaudio pydub pyyaml dbus-python python-dotenv setproctitle || exit -1 + + $PYTHON -m pip install --ignore-installed flask Flask-WTF pyalsaaudio pydub pyyaml dbus-python python-dotenv #Sometimes pygame tries to install without a whl, and fails (like 2.4.0) this #checks that only correct versions will install