Manually run GC on reactor tick.

This also adds a metric for amount of time spent in GC.
This commit is contained in:
Erik Johnston 2016-05-09 10:13:25 +01:00
parent 96d9d5d388
commit 1f1dee94f6

View File

@ -22,6 +22,7 @@ import functools
import os import os
import stat import stat
import time import time
import gc
from twisted.internet import reactor from twisted.internet import reactor
@ -155,6 +156,7 @@ get_metrics_for("process").register_callback("fds", _process_fds, labels=["type"
reactor_metrics = get_metrics_for("reactor") reactor_metrics = get_metrics_for("reactor")
tick_time = reactor_metrics.register_distribution("tick_time") tick_time = reactor_metrics.register_distribution("tick_time")
pending_calls_metric = reactor_metrics.register_distribution("pending_calls") pending_calls_metric = reactor_metrics.register_distribution("pending_calls")
gc_time = reactor_metrics.register_distribution("gc_time")
def runUntilCurrentTimer(func): def runUntilCurrentTimer(func):
@ -182,6 +184,18 @@ def runUntilCurrentTimer(func):
end = time.time() * 1000 end = time.time() * 1000
tick_time.inc_by(end - start) tick_time.inc_by(end - start)
pending_calls_metric.inc_by(num_pending) pending_calls_metric.inc_by(num_pending)
threshold = gc.get_threshold()
counts = gc.get_count()
start = time.time() * 1000
for i in [2, 1, 0]:
if threshold[i] < counts[i]:
logger.info("Collecting gc %d", i)
gc.collect(i)
end = time.time() * 1000
gc_time.inc_by(end - start)
return ret return ret
return f return f
@ -196,5 +210,6 @@ try:
# runUntilCurrent is called when we have pending calls. It is called once # runUntilCurrent is called when we have pending calls. It is called once
# per iteratation after fd polling. # per iteratation after fd polling.
reactor.runUntilCurrent = runUntilCurrentTimer(reactor.runUntilCurrent) reactor.runUntilCurrent = runUntilCurrentTimer(reactor.runUntilCurrent)
gc.disable()
except AttributeError: except AttributeError:
pass pass