sobjects/services.py
changeset 9035 63f3d25bab14
parent 9034 cc3442054e48
child 9687 00c2356faba7
--- a/sobjects/services.py	Mon Jun 24 12:05:35 2013 +0200
+++ b/sobjects/services.py	Mon Jun 24 12:03:37 2013 +0200
@@ -55,3 +55,48 @@
         results['available_cnxsets'] = repo._cnxsets_pool.qsize()
         results['threads'] = ', '.join(sorted(str(t) for t in threading.enumerate()))
         return results
+
+class GcStatsService(Service):
+    """Return a dictionary containing some statistics about the repository
+    resources usage.
+    """
+
+    __regid__  = 'repo_gc_stats'
+    __select__ = match_user_groups('managers')
+
+    def call(self, nmax=20):
+        """Return a dictionary containing some statistics about the repository
+        memory usage.
+
+        This is a public method, not requiring a session id.
+
+        nmax is the max number of (most) referenced object returned as
+        the 'referenced' result
+        """
+
+        from cubicweb._gcdebug import gc_info
+        from cubicweb.appobject import AppObject
+        from cubicweb.rset import ResultSet
+        from cubicweb.dbapi import Connection, Cursor
+        from cubicweb.web.request import CubicWebRequestBase
+        from rql.stmts import Union
+
+        lookupclasses = (AppObject,
+                         Union, ResultSet,
+                         Connection, Cursor,
+                         CubicWebRequestBase)
+        try:
+            from cubicweb.server.session import Session, InternalSession
+            lookupclasses += (InternalSession, Session)
+        except ImportError:
+            pass  # no server part installed
+
+        results = {}
+        counters, ocounters, garbage = gc_info(lookupclasses,
+                                               viewreferrersclasses=())
+        values = sorted(counters.iteritems(), key=lambda x: x[1], reverse=True)
+        results['lookupclasses'] = values
+        values = sorted(ocounters.iteritems(), key=lambda x: x[1], reverse=True)[:nmax]
+        results['referenced'] = values
+        results['unreachable'] = len(garbage)
+        return results