Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #771 from matrix-org/erikj/gc_tick
Browse files Browse the repository at this point in the history
Manually run GC on reactor tick.
  • Loading branch information
erikjohnston committed Jun 7, 2016
2 parents efe8126 + 60d53f9 commit 8c966fb
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions synapse/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
import stat
import time
import gc

from twisted.internet import reactor

Expand Down Expand Up @@ -152,6 +153,12 @@ def _process_fds():
tick_time = reactor_metrics.register_distribution("tick_time")
pending_calls_metric = reactor_metrics.register_distribution("pending_calls")

gc_time = (
reactor_metrics.register_distribution("gc_time_gen0"),
reactor_metrics.register_distribution("gc_time_gen2"),
reactor_metrics.register_distribution("gc_time_gen2"),
)


def runUntilCurrentTimer(func):

Expand All @@ -178,6 +185,21 @@ def f(*args, **kwargs):
end = time.time() * 1000
tick_time.inc_by(end - start)
pending_calls_metric.inc_by(num_pending)

# Check if we need to do a manual GC (since its been disabled), and do
# one if necessary.
threshold = gc.get_threshold()
counts = gc.get_count()
for i in [2, 1, 0]:
if threshold[i] < counts[i]:
logger.info("Collecting gc %d", i)

start = time.time() * 1000
gc.collect(i)
end = time.time() * 1000

gc_time[i].inc_by(end - start)

return ret

return f
Expand All @@ -192,5 +214,9 @@ def f(*args, **kwargs):
# runUntilCurrent is called when we have pending calls. It is called once
# per iteratation after fd polling.
reactor.runUntilCurrent = runUntilCurrentTimer(reactor.runUntilCurrent)

# We manually run the GC each reactor tick so that we can get some metrics
# about time spent doing GC,
gc.disable()
except AttributeError:
pass

0 comments on commit 8c966fb

Please sign in to comment.