reactor: Record time of recent gc collection sweeps

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-09-17 01:59:18 -04:00
parent cd23c95760
commit 1af4a4ae9f
2 changed files with 16 additions and 10 deletions

View File

@ -165,8 +165,8 @@ class Printer:
return return
except Exception as e: except Exception as e:
logging.exception("Unhandled exception during connect") logging.exception("Unhandled exception during connect")
self._set_state("Internal error during connect: %s\n%s" % ( self._set_state("Internal error during connect: %s\n%s"
str(e), message_restart,)) % (str(e), message_restart,))
return return
try: try:
self._set_state(message_ready) self._set_state(message_ready)
@ -176,8 +176,8 @@ class Printer:
cb() cb()
except Exception as e: except Exception as e:
logging.exception("Unhandled exception during ready callback") logging.exception("Unhandled exception during ready callback")
self.invoke_shutdown("Internal error during ready callback: %s" % ( self.invoke_shutdown("Internal error during ready callback: %s"
str(e),)) % (str(e),))
def run(self): def run(self):
systime = time.time() systime = time.time()
monotime = self.reactor.monotonic() monotime = self.reactor.monotonic()
@ -224,6 +224,8 @@ class Printer:
cb() cb()
except: except:
logging.exception("Exception during shutdown handler") logging.exception("Exception during shutdown handler")
logging.info("Reactor garbage collection: %s",
self.reactor.get_gc_stats())
def invoke_async_shutdown(self, msg): def invoke_async_shutdown(self, msg):
self.reactor.register_async_callback( self.reactor.register_async_callback(
(lambda e: self.invoke_shutdown(msg))) (lambda e: self.invoke_shutdown(msg)))

View File

@ -96,8 +96,10 @@ class SelectReactor:
def __init__(self, gc_checking=False): def __init__(self, gc_checking=False):
# Main code # Main code
self._process = False self._process = False
self._check_gc = gc_checking
self.monotonic = chelper.get_ffi()[1].get_monotonic self.monotonic = chelper.get_ffi()[1].get_monotonic
# Python garbage collection
self._check_gc = gc_checking
self._last_gc_times = [0., 0., 0.]
# Timers # Timers
self._timers = [] self._timers = []
self._next_timer = self.NEVER self._next_timer = self.NEVER
@ -110,6 +112,8 @@ class SelectReactor:
self._g_dispatch = None self._g_dispatch = None
self._greenlets = [] self._greenlets = []
self._all_greenlets = [] self._all_greenlets = []
def get_gc_stats(self):
return tuple(self._last_gc_times)
# Timers # Timers
def update_timer(self, timer_handler, waketime): def update_timer(self, timer_handler, waketime):
timer_handler.waketime = waketime timer_handler.waketime = waketime
@ -134,13 +138,13 @@ class SelectReactor:
gi = gc.get_count() gi = gc.get_count()
if gi[0] >= 700: if gi[0] >= 700:
# Reactor looks idle and gc is due - run it # Reactor looks idle and gc is due - run it
gc_level = 0
if gi[1] >= 10: if gi[1] >= 10:
gc_level = 1
if gi[2] >= 10: if gi[2] >= 10:
gc.collect(2) gc_level = 2
else: self._last_gc_times[gc_level] = eventtime
gc.collect(1) gc.collect(gc_level)
else:
gc.collect(0)
return 0. return 0.
return min(1., max(.001, self._next_timer - eventtime)) return min(1., max(.001, self._next_timer - eventtime))
self._next_timer = self.NEVER self._next_timer = self.NEVER