From 8af5c0b2d457c6424ef9571a8c2bcf0b053cbb60 Mon Sep 17 00:00:00 2001 From: alfrix Date: Sun, 12 Feb 2023 01:54:52 -0600 Subject: [PATCH] Trace memory --- panels/job_status.py | 2 ++ panels/print.py | 2 ++ screen.py | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/panels/job_status.py b/panels/job_status.py index a1b53251..b470417b 100644 --- a/panels/job_status.py +++ b/panels/job_status.py @@ -357,6 +357,8 @@ class JobStatusPanel(ScreenPanel): if self.flow_timeout is None: self.flow_timeout = GLib.timeout_add_seconds(2, self.update_flow) self._screen.base_panel_show_all() + logging.debug("Job_status panel") + self._screen.memory_trace() def deactivate(self): if self.flow_timeout is not None: diff --git a/panels/print.py b/panels/print.py index b8c81a1e..0a0fb21b 100644 --- a/panels/print.py +++ b/panels/print.py @@ -82,6 +82,7 @@ class PrintPanel(ScreenPanel): if self.cur_directory != "gcodes": self.change_dir(None, "gcodes") self._refresh_files() + logging.debug("Print panel") def add_directory(self, directory, show=True): parent_dir = os.path.dirname(directory) @@ -414,6 +415,7 @@ class PrintPanel(ScreenPanel): def _refresh_files(self, widget=None): self._files.refresh_files() + self._screen.memory_trace() def show_rename(self, widget, fullpath): self.source = fullpath diff --git a/screen.py b/screen.py index 4fd75cd5..f1807626 100755 --- a/screen.py +++ b/screen.py @@ -10,6 +10,7 @@ import traceback # noqa import locale import sys import gi +import tracemalloc gi.require_version("Gtk", "3.0") from gi.repository import Gtk, Gdk, GLib, Pango @@ -90,8 +91,11 @@ class KlipperScreen(Gtk.Window): reinit_count = 0 max_retries = 4 initialized = False + initial_snapshot = None + switch_count = 0 def __init__(self, args, version): + tracemalloc.start() try: super().__init__(title="KlipperScreen") except Exception as e: @@ -216,6 +220,23 @@ class KlipperScreen(Gtk.Window): self.files = KlippyFiles(self) self._ws.initial_connect() + def memory_trace(self): + snapshot = tracemalloc.take_snapshot() + snapshot = snapshot.filter_traces(( + tracemalloc.Filter(False, ""), + tracemalloc.Filter(False, ""), + tracemalloc.Filter(False, tracemalloc.__file__), + )) + if self.initial_snapshot is None: + self.initial_snapshot = snapshot + logging.debug("[ Initial memory snapshot ]") + else: + self.switch_count += 1 + logging.debug("[ Top 10 memory diffs (Shown: %d times)]", self.switch_count) + top_stats = snapshot.compare_to(self.initial_snapshot, 'lineno') + for stat in top_stats[:10]: + logging.debug(stat) + def ws_subscribe(self): requested_updates = { "objects": {