sobjects/services.py
changeset 9035 63f3d25bab14
parent 9034 cc3442054e48
child 9687 00c2356faba7
equal deleted inserted replaced
9034:cc3442054e48 9035:63f3d25bab14
    53         looping_tasks = repo._tasks_manager._looping_tasks
    53         looping_tasks = repo._tasks_manager._looping_tasks
    54         results['looping_tasks'] = ', '.join(str(t) for t in looping_tasks)
    54         results['looping_tasks'] = ', '.join(str(t) for t in looping_tasks)
    55         results['available_cnxsets'] = repo._cnxsets_pool.qsize()
    55         results['available_cnxsets'] = repo._cnxsets_pool.qsize()
    56         results['threads'] = ', '.join(sorted(str(t) for t in threading.enumerate()))
    56         results['threads'] = ', '.join(sorted(str(t) for t in threading.enumerate()))
    57         return results
    57         return results
       
    58 
       
    59 class GcStatsService(Service):
       
    60     """Return a dictionary containing some statistics about the repository
       
    61     resources usage.
       
    62     """
       
    63 
       
    64     __regid__  = 'repo_gc_stats'
       
    65     __select__ = match_user_groups('managers')
       
    66 
       
    67     def call(self, nmax=20):
       
    68         """Return a dictionary containing some statistics about the repository
       
    69         memory usage.
       
    70 
       
    71         This is a public method, not requiring a session id.
       
    72 
       
    73         nmax is the max number of (most) referenced object returned as
       
    74         the 'referenced' result
       
    75         """
       
    76 
       
    77         from cubicweb._gcdebug import gc_info
       
    78         from cubicweb.appobject import AppObject
       
    79         from cubicweb.rset import ResultSet
       
    80         from cubicweb.dbapi import Connection, Cursor
       
    81         from cubicweb.web.request import CubicWebRequestBase
       
    82         from rql.stmts import Union
       
    83 
       
    84         lookupclasses = (AppObject,
       
    85                          Union, ResultSet,
       
    86                          Connection, Cursor,
       
    87                          CubicWebRequestBase)
       
    88         try:
       
    89             from cubicweb.server.session import Session, InternalSession
       
    90             lookupclasses += (InternalSession, Session)
       
    91         except ImportError:
       
    92             pass  # no server part installed
       
    93 
       
    94         results = {}
       
    95         counters, ocounters, garbage = gc_info(lookupclasses,
       
    96                                                viewreferrersclasses=())
       
    97         values = sorted(counters.iteritems(), key=lambda x: x[1], reverse=True)
       
    98         results['lookupclasses'] = values
       
    99         values = sorted(ocounters.iteritems(), key=lambda x: x[1], reverse=True)[:nmax]
       
   100         results['referenced'] = values
       
   101         results['unreachable'] = len(garbage)
       
   102         return results