From 1c5b6342e521b987fdef6b60320e85419a1489b6 Mon Sep 17 00:00:00 2001 From: Andy Werner Date: Wed, 14 Apr 2021 19:10:55 +0200 Subject: [PATCH 1/3] Prevents ready_to_laser if cpu is high (longer waiting time) --- octoprint_mrbeam/iobeam/onebutton_handler.py | 43 +++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/octoprint_mrbeam/iobeam/onebutton_handler.py b/octoprint_mrbeam/iobeam/onebutton_handler.py index 41a767b83..928449679 100644 --- a/octoprint_mrbeam/iobeam/onebutton_handler.py +++ b/octoprint_mrbeam/iobeam/onebutton_handler.py @@ -1,3 +1,4 @@ +import re import threading import time from subprocess import check_output @@ -7,6 +8,7 @@ from octoprint_mrbeam.mrbeam_events import MrBeamEvents from octoprint_mrbeam.iobeam.iobeam_handler import IoBeamEvents from octoprint_mrbeam.mrb_logger import mrb_logger +from octoprint_mrbeam.util.cmd_exec import exec_cmd_output from flask.ext.babel import gettext from octoprint_mrbeam.printing.comm_acc2 import PrintingGcodeFromMemoryInformation @@ -389,15 +391,54 @@ def set_rtl_file(self, gcode_file): self.ready_to_laser_file = gcode_file def set_ready_to_laser(self, gcode_file=None): + cpu_load = self.wait_for_cpu_below(90.0) if gcode_file is not None: self._test_conditions(gcode_file) self.ready_to_laser_file = gcode_file self.ready_to_laser_flag = True self.ready_to_laser_ts = time.time() self.print_started = -1 - self._fireEvent(MrBeamEvents.READY_TO_LASER_START) + self._fireEvent(MrBeamEvents.READY_TO_LASER_START, dict(cpu_load=cpu_load)) self._check_if_still_ready_to_laser() + def wait_for_cpu_below(self, cpu_max=70.0, check_interval=2.5): + """ + > ps aux | sort -nrk 3,3 | head -n 5 + pi 3466 86.0 12.0 387884 105960 ? Sl 15:49 1:22 /home/pi/oprint/bin/python /home/pi/oprint/bin/octoprint --host=127.0.0.1 --port=5000 + root 925 5.3 1.5 29168 13524 ? Sl Apr12 83:33 /usr/bin/python /usr/local/bin/mrbeam_ledstrips + root 571 2.9 1.8 29328 16408 ? Sl Apr12 45:56 /usr/bin/python /usr/local/bin/netconnectd --config /etc/netconnectd.yaml --address /var/run/netconnectd.sock --logfile /var/log/netconnectd.log --foreground + root 436 1.2 2.8 97856 24668 ? Ssl Apr12 19:06 /usr/bin/python /usr/local/bin/iobeam + pi 23641 0.6 0.2 5108 2448 pts/1 S+ 15:11 0:14 top + sort: write failed: standard output: Broken pipe + sort: write error + """ + cpu_load = -1.0 + find_cpu_load = re.compile( + r"^\s*.+\s+\d+\s+([0-9.]+)\s+[0-9.]+\s+\d+\s+\d+\s+", re.MULTILINE + ) + while cpu_load < 0.0 or cpu_load > cpu_max: + try: + ps_out, code = exec_cmd_output( + "ps aux | sort -nrk 3,3 | head -n 5", shell=True + ) + self._logger.debug( + "wait_for_cpu_below() test command output:\n%s", ps_out + ) + + cpu_load = sum(map(lambda x: float(x), find_cpu_load.findall(ps_out))) + except Exception: + self._logger.exception("Exception while reading cpu load: ") + time.sleep(check_interval) + self._logger.info( + "wait_for_cpu_below() cpu_load: %s, code: %s", cpu_load, code + ) + if cpu_load > cpu_max: + time.sleep(check_interval) + self._logger.info( + "wait_for_cpu_below() final cpu_load: %s, code: %s", cpu_load, code + ) + return cpu_load + def unset_ready_to_laser(self, lasering=False): self._logger.debug("unset_ready_to_laser()") self._cancel_ready_to_laser_timer() From e953f1cadcc15e1dbcde62cc2d45043e0efab1ae Mon Sep 17 00:00:00 2001 From: Andy Werner Date: Fri, 16 Apr 2021 10:51:54 +0200 Subject: [PATCH 2/3] Switched to command top --- octoprint_mrbeam/iobeam/onebutton_handler.py | 38 ++++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/octoprint_mrbeam/iobeam/onebutton_handler.py b/octoprint_mrbeam/iobeam/onebutton_handler.py index 928449679..02d36467a 100644 --- a/octoprint_mrbeam/iobeam/onebutton_handler.py +++ b/octoprint_mrbeam/iobeam/onebutton_handler.py @@ -391,7 +391,7 @@ def set_rtl_file(self, gcode_file): self.ready_to_laser_file = gcode_file def set_ready_to_laser(self, gcode_file=None): - cpu_load = self.wait_for_cpu_below(90.0) + cpu_load = self.wait_for_cpu_below() if gcode_file is not None: self._test_conditions(gcode_file) self.ready_to_laser_file = gcode_file @@ -401,36 +401,36 @@ def set_ready_to_laser(self, gcode_file=None): self._fireEvent(MrBeamEvents.READY_TO_LASER_START, dict(cpu_load=cpu_load)) self._check_if_still_ready_to_laser() - def wait_for_cpu_below(self, cpu_max=70.0, check_interval=2.5): + def wait_for_cpu_below(self, cpu_max=60.0, check_interval=2.5): """ - > ps aux | sort -nrk 3,3 | head -n 5 - pi 3466 86.0 12.0 387884 105960 ? Sl 15:49 1:22 /home/pi/oprint/bin/python /home/pi/oprint/bin/octoprint --host=127.0.0.1 --port=5000 - root 925 5.3 1.5 29168 13524 ? Sl Apr12 83:33 /usr/bin/python /usr/local/bin/mrbeam_ledstrips - root 571 2.9 1.8 29328 16408 ? Sl Apr12 45:56 /usr/bin/python /usr/local/bin/netconnectd --config /etc/netconnectd.yaml --address /var/run/netconnectd.sock --logfile /var/log/netconnectd.log --foreground - root 436 1.2 2.8 97856 24668 ? Ssl Apr12 19:06 /usr/bin/python /usr/local/bin/iobeam - pi 23641 0.6 0.2 5108 2448 pts/1 S+ 15:11 0:14 top - sort: write failed: standard output: Broken pipe - sort: write error + now it's actually not the cpu load, it's the cpu usage of the octoprint process. + + this would be an alternative but it's downside is that it updates slower than top + # r"^\s*.+\s+\d+\s+([0-9.]+)\s+[0-9.]+\s+\d+\s+\d+\s+", re.MULTILINE + # command = "ps aux | sort -nrk 3,3 | head -n 5" + (seemingly it's a avg over that pas minute, documentation says it's the avg for the process since ever.) """ cpu_load = -1.0 find_cpu_load = re.compile( - r"^\s*.+\s+\d+\s+([0-9.]+)\s+[0-9.]+\s+\d+\s+\d+\s+", re.MULTILINE + r"^\s*\d+\s\w+\s+.*\s+[DRSTI]\s+([0-9.]+)\s+.*\s+octoprint$", re.MULTILINE ) + command = "top -b -n 1 | grep octoprint" + while cpu_load < 0.0 or cpu_load > cpu_max: try: - ps_out, code = exec_cmd_output( - "ps aux | sort -nrk 3,3 | head -n 5", shell=True - ) - self._logger.debug( - "wait_for_cpu_below() test command output:\n%s", ps_out - ) - + ps_out, code = exec_cmd_output(command, shell=True) + # self._logger.debug( + # "wait_for_cpu_below() test command output:\n%s", ps_out + # ) cpu_load = sum(map(lambda x: float(x), find_cpu_load.findall(ps_out))) except Exception: self._logger.exception("Exception while reading cpu load: ") time.sleep(check_interval) self._logger.info( - "wait_for_cpu_below() cpu_load: %s, code: %s", cpu_load, code + "wait_for_cpu_below() cpu_load: %s, cpu_max: %s, cpu_load > cpu_max: %s", + cpu_load, + cpu_max, + cpu_load > cpu_max, ) if cpu_load > cpu_max: time.sleep(check_interval) From 8331bbbfa5802e526b46b1970d5a85dbfe5cfb2f Mon Sep 17 00:00:00 2001 From: Andy Werner Date: Fri, 16 Apr 2021 14:50:22 +0200 Subject: [PATCH 3/3] changed log level --- octoprint_mrbeam/iobeam/onebutton_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/octoprint_mrbeam/iobeam/onebutton_handler.py b/octoprint_mrbeam/iobeam/onebutton_handler.py index 02d36467a..4523851cf 100644 --- a/octoprint_mrbeam/iobeam/onebutton_handler.py +++ b/octoprint_mrbeam/iobeam/onebutton_handler.py @@ -426,7 +426,7 @@ def wait_for_cpu_below(self, cpu_max=60.0, check_interval=2.5): except Exception: self._logger.exception("Exception while reading cpu load: ") time.sleep(check_interval) - self._logger.info( + self._logger.debug( "wait_for_cpu_below() cpu_load: %s, cpu_max: %s, cpu_load > cpu_max: %s", cpu_load, cpu_max,