[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735) stable
authorDavid Douard <david.douard@logilab.fr>
Mon, 13 Feb 2012 12:41:04 +0100
branchstable
changeset 8224 e35d4d4f7eb3
parent 8217 0a467663c3fe
child 8225 baf8c16cae95
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735) This method returns memory informations in a dictionnary (same informations as the content displayed by the GCView).
server/repository.py
--- a/server/repository.py	Fri Feb 10 17:20:38 2012 +0100
+++ b/server/repository.py	Mon Feb 13 12:41:04 2012 +0100
@@ -492,6 +492,8 @@
             results['%s_cache_hit' % title] =  hits
             results['%s_cache_miss' % title] = misses
             results['%s_cache_hit_percent' % title] = (hits * 100) / (hits + misses)
+        results['type_source_cache_size'] = len(self._type_source_cache)
+        results['extid_cache_size'] = len(self._extid_cache)
         results['sql_no_cache'] = self.system_source.no_cache
         results['nb_open_sessions'] = len(self._sessions)
         results['nb_active_threads'] = threading.activeCount()
@@ -500,6 +502,43 @@
         results['threads'] = ', '.join(sorted(str(t) for t in threading.enumerate()))
         return results
 
+    def gc_stats(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
+
     def get_schema(self):
         """Return the instance schema.