From 7b019d24f0af52a599c026af1c1b21a64e6228bb Mon Sep 17 00:00:00 2001 From: Hemna Date: Fri, 2 Apr 2021 18:54:00 -0400 Subject: [PATCH] Updated stats output for KeepAlive thread Also added the aprsd uptime to the VersionPlugin --- aprsd/plugins/version.py | 9 +++++++-- aprsd/stats.py | 4 ++-- aprsd/threads.py | 19 +++++++++---------- aprsd/utils.py | 7 +++++++ 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/aprsd/plugins/version.py b/aprsd/plugins/version.py index 80cf766..cc0e078 100644 --- a/aprsd/plugins/version.py +++ b/aprsd/plugins/version.py @@ -1,7 +1,7 @@ import logging import aprsd -from aprsd import plugin, trace +from aprsd import plugin, stats, trace LOG = logging.getLogger("APRSD") @@ -20,4 +20,9 @@ class VersionPlugin(plugin.APRSDPluginBase): @trace.trace def command(self, fromcall, message, ack): LOG.info("Version COMMAND") - return "APRSD version '{}'".format(aprsd.__version__) + stats_obj = stats.APRSDStats() + s = stats_obj.stats() + return "APRSD ver:{} uptime:{}".format( + aprsd.__version__, + s["aprsd"]["uptime"], + ) diff --git a/aprsd/stats.py b/aprsd/stats.py index 6729564..a8c3a4e 100644 --- a/aprsd/stats.py +++ b/aprsd/stats.py @@ -49,7 +49,7 @@ class APRSDStats: @property def uptime(self): with self.lock: - return str(datetime.datetime.now() - self.start_time) + return datetime.datetime.now() - self.start_time @property def memory(self): @@ -183,7 +183,7 @@ class APRSDStats: stats = { "aprsd": { "version": aprsd.__version__, - "uptime": self.uptime, + "uptime": utils.strfdelta(self.uptime), "memory_current": self.memory, "memory_current_str": utils.human_size(self.memory), "memory_peak": self.memory_peak, diff --git a/aprsd/threads.py b/aprsd/threads.py index 01439ee..b8805c4 100644 --- a/aprsd/threads.py +++ b/aprsd/threads.py @@ -1,6 +1,5 @@ import abc import datetime -import gc import logging import queue import threading @@ -75,25 +74,24 @@ class KeepAliveThread(APRSDThread): def loop(self): if self.cntr % 6 == 0: - nuked = gc.collect() tracker = messaging.MsgTrack() stats_obj = stats.APRSDStats() now = datetime.datetime.now() last_email = stats_obj.email_thread_time if last_email: - email_thread_time = str(now - last_email) + email_thread_time = utils.strfdelta(now - last_email) else: email_thread_time = "N/A" - last_msg_time = str(now - stats_obj.aprsis_keepalive) + last_msg_time = utils.strfdelta(now - stats_obj.aprsis_keepalive) current, peak = tracemalloc.get_traced_memory() stats_obj.set_memory(current) stats_obj.set_memory_peak(peak) - LOG.debug( - "Uptime ({}) Tracker({}) " - "Msgs: TX:{} RX:{} Last: {} - EmailThread: {} - RAM: Current:{} Peak:{} Nuked: {}".format( - stats_obj.uptime, + keepalive = ( + "Uptime {} Tracker {} " + "Msgs TX:{} RX:{} Last:{} Email:{} RAM Current:{} Peak:{}".format( + utils.strfdelta(stats_obj.uptime), len(tracker), stats_obj.msgs_tx, stats_obj.msgs_rx, @@ -101,9 +99,10 @@ class KeepAliveThread(APRSDThread): email_thread_time, utils.human_size(current), utils.human_size(peak), - nuked, - ), + ) ) + LOG.debug(keepalive) + LOG.debug(len(keepalive)) self.cntr += 1 time.sleep(10) return True diff --git a/aprsd/utils.py b/aprsd/utils.py index 5373fa0..5158ee5 100644 --- a/aprsd/utils.py +++ b/aprsd/utils.py @@ -368,3 +368,10 @@ def human_size(bytes, units=None): if not units: units = [" bytes", "KB", "MB", "GB", "TB", "PB", "EB"] return str(bytes) + units[0] if bytes < 1024 else human_size(bytes >> 10, units[1:]) + + +def strfdelta(tdelta, fmt="{hours}:{minutes}:{seconds}"): + d = {"days": tdelta.days} + d["hours"], rem = divmod(tdelta.seconds, 3600) + d["minutes"], d["seconds"] = divmod(rem, 60) + return fmt.format(**d)