statistics: Move stats handling to new "extras" module

Move the generation of statistics to its own module.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-09-02 13:07:43 -04:00
parent 18b04ffe68
commit 8fe8a6deb3
3 changed files with 31 additions and 14 deletions

View File

@ -0,0 +1,29 @@
# Support for logging periodic statistics
#
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging
class PrinterStats:
def __init__(self, config):
self.printer = config.get_printer()
reactor = self.printer.get_reactor()
self.stats_timer = reactor.register_timer(self.generate_stats)
self.stats_cb = []
def printer_state(self, state):
if state == 'ready':
self.stats_cb = [o.stats for n, o in self.printer.lookup_objects()
if hasattr(o, 'stats')]
if self.printer.get_start_args().get('debugoutput') is None:
reactor = self.printer.get_reactor()
reactor.update_timer(self.stats_timer, reactor.NOW)
def generate_stats(self, eventtime):
stats = [cb(eventtime) for cb in self.stats_cb]
if max([s[0] for s in stats]):
logging.info("Stats %.1f: %s", eventtime,
' '.join([s[1] for s in stats]))
return eventtime + 1.
def load_config(config):
return PrinterStats(config)

View File

@ -135,12 +135,10 @@ class Printer:
self.reactor = reactor.Reactor() self.reactor = reactor.Reactor()
gc = gcode.GCodeParser(self, input_fd) gc = gcode.GCodeParser(self, input_fd)
self.objects = collections.OrderedDict({'gcode': gc}) self.objects = collections.OrderedDict({'gcode': gc})
self.stats_timer = self.reactor.register_timer(self._stats)
self.reactor.register_callback(self._connect) self.reactor.register_callback(self._connect)
self.state_message = message_startup self.state_message = message_startup
self.is_shutdown = False self.is_shutdown = False
self.run_result = None self.run_result = None
self.stats_cb = []
self.state_cb = [] self.state_cb = []
def get_start_args(self): def get_start_args(self):
return self.start_args return self.start_args
@ -177,12 +175,6 @@ class Printer:
logging.info(info) logging.info(info)
if self.bglogger is not None: if self.bglogger is not None:
self.bglogger.set_rollover_info(name, info) self.bglogger.set_rollover_info(name, info)
def _stats(self, eventtime, force_output=False):
stats = [cb(eventtime) for cb in self.stats_cb]
if max([s[0] for s in stats] + [force_output]):
logging.info("Stats %.1f: %s", eventtime,
' '.join([s[1] for s in stats]))
return eventtime + 1.
def try_load_module(self, config, section): def try_load_module(self, config, section):
if section in self.objects: if section in self.objects:
return self.objects[section] return self.objects[section]
@ -233,9 +225,7 @@ class Printer:
raise self.config_error( raise self.config_error(
"Option '%s' is not valid in section '%s'" % ( "Option '%s' is not valid in section '%s'" % (
option, section)) option, section))
# Determine which printer objects have stats/state callbacks # Determine which printer objects have state callbacks
self.stats_cb = [o.stats for o in self.objects.values()
if hasattr(o, 'stats')]
self.state_cb = [o.printer_state for o in self.objects.values() self.state_cb = [o.printer_state for o in self.objects.values()
if hasattr(o, 'printer_state')] if hasattr(o, 'printer_state')]
def _connect(self, eventtime): def _connect(self, eventtime):
@ -250,8 +240,6 @@ class Printer:
if self.state_message is not message_ready: if self.state_message is not message_ready:
return self.reactor.NEVER return self.reactor.NEVER
cb('ready') cb('ready')
if self.start_args.get('debugoutput') is None:
self.reactor.update_timer(self.stats_timer, self.reactor.NOW)
except (self.config_error, pins.error) as e: except (self.config_error, pins.error) as e:
logging.exception("Config error") logging.exception("Config error")
self._set_state("%s%s" % (str(e), message_restart)) self._set_state("%s%s" % (str(e), message_restart))
@ -281,7 +269,6 @@ class Printer:
# Check restart flags # Check restart flags
run_result = self.run_result run_result = self.run_result
try: try:
self._stats(self.reactor.monotonic(), force_output=True)
if run_result == 'firmware_restart': if run_result == 'firmware_restart':
for n, m in self.lookup_objects(module='mcu'): for n, m in self.lookup_objects(module='mcu'):
m.microcontroller_restart() m.microcontroller_restart()

View File

@ -233,6 +233,7 @@ class ToolHead:
self.flush_timer = self.reactor.register_timer(self._flush_handler) self.flush_timer = self.reactor.register_timer(self._flush_handler)
self.move_queue.set_flush_time(self.buffer_time_high) self.move_queue.set_flush_time(self.buffer_time_high)
self.printer.try_load_module(config, "idle_timeout") self.printer.try_load_module(config, "idle_timeout")
self.printer.try_load_module(config, "statistics")
# Setup iterative solver # Setup iterative solver
ffi_main, ffi_lib = chelper.get_ffi() ffi_main, ffi_lib = chelper.get_ffi()
self.cmove = ffi_main.gc(ffi_lib.move_alloc(), ffi_lib.free) self.cmove = ffi_main.gc(ffi_lib.move_alloc(), ffi_lib.free)