summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2021-03-29 18:27:28 +0100
committerErik Johnston <erik@matrix.org>2021-03-29 18:27:28 +0100
commitc232e16d237dc4630396b836b52d44a89c6e20ab (patch)
tree08fe185efd7841ec21aa2979202a35530b233c30
parentMerge remote-tracking branch 'origin/develop' into erikj/cache_memory_usage (diff)
downloadsynapse-c232e16d237dc4630396b836b52d44a89c6e20ab.tar.xz
Export jemalloc stats
-rw-r--r--synapse/metrics/__init__.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/synapse/metrics/__init__.py b/synapse/metrics/__init__.py
index 3b499efc07..3a690dd7bf 100644
--- a/synapse/metrics/__init__.py
+++ b/synapse/metrics/__init__.py
@@ -614,3 +614,50 @@ __all__ = [
     "InFlightGauge",
     "BucketCollector",
 ]
+
+
+try:
+    import ctypes
+    import ctypes.util
+
+    jemalloc = ctypes.CDLL(ctypes.util.find_library("jemalloc"))
+
+    def get_val(name):
+        allocated = ctypes.c_size_t(0)
+        allocated_len = ctypes.c_size_t(ctypes.sizeof(allocated))
+        jemalloc.mallctl(
+            name, ctypes.byref(allocated), ctypes.byref(allocated_len), None, None
+        )
+        return allocated.value
+
+    def refresh_stats():
+        epoch = ctypes.c_uint64(0)
+        jemalloc.mallctl(
+            b"epoch", None, None, ctypes.byref(epoch), ctypes.sizeof(epoch)
+        )
+
+    class JemallocCollector(object):
+        def collect(self):
+            refresh_stats()
+
+            yield GaugeMetricFamily(
+                "jemalloc_stats_allocated", "", value=get_val(b"stats.allocated")
+            )
+            yield GaugeMetricFamily(
+                "jemalloc_stats_active", "", value=get_val(b"stats.active")
+            )
+            yield GaugeMetricFamily(
+                "jemalloc_stats_resident", "", value=get_val(b"stats.resident")
+            )
+            yield GaugeMetricFamily(
+                "jemalloc_stats_mapped", "", value=get_val(b"stats.mapped")
+            )
+            yield GaugeMetricFamily(
+                "jemalloc_stats_retained", "", value=get_val(b"stats.retained")
+            )
+
+    REGISTRY.register(JemallocCollector)
+
+
+except Exception:
+    pass