Exemplo n.º 1
0
class Memdump(PeriodicExecutor):
    # memdump timeout
    Timeout = 30

    def __init__(self):
        super(Memdump, self).__init__(self.Timeout, self.OnMemdump, [])
        self.sizer = MemSizer()
        self.sizer.sizeall()

    def OnMemdump(self):
        self.sizer.sizeall()
#         print "\n... Memstat ...\n"
#         print "\n".join(self.sizer.get_sizes()) + "\n"
        print "... Memdiff ..."
        print "\n".join(self.sizer.get_deltas()) + "\n"
        import gc
        print "... Summary ..."
        print "%d objects managed by the GC\n" % len(gc.get_objects())
Exemplo n.º 2
0
 def __init__(self, reactor, params):
     """
     Initialize the bootstrap object and all necessary objects.
     Also does the local IP address discovery.
     """
     self.reactor = reactor
     self.params = params
     self.pool = []
     self.caller = DelayedCaller(self.reactor)
     if self.params.memdebug:
         self.memsizer = MemSizer()
         #~ gc.set_debug(gc.DEBUG_LEAK)
     else:
         self.memsizer = None
Exemplo n.º 3
0
class Bootstrap(object):
    """
    This class handles the launching of the node or pool of nodes
    according to parameters given by the user.
    """

    memdebug_period = 10.0

    def __init__(self, reactor, params):
        """
        Initialize the bootstrap object and all necessary objects.
        Also does the local IP address discovery.
        """
        self.reactor = reactor
        self.params = params
        self.pool = []
        self.caller = DelayedCaller(self.reactor)
        if self.params.memdebug:
            self.memsizer = MemSizer()
            #~ gc.set_debug(gc.DEBUG_LEAK)
        else:
            self.memsizer = None

    def Run(self):
        """
        Initiate the network connections and launch the main loop.
        """
        if self.params.memdebug:
            dc_memdebug = self.caller.CallPeriodically(self.memdebug_period, self._MemDebug)
        else:
            dc_memdebug = None
        # Enter event loop
        self.reactor.callLater(0, self.LaunchPool)
        try:
            self.reactor.run()
        finally:
            if dc_memdebug:
                dc_memdebug.Cancel()

    def LaunchPool(self):
        """
        Launch the pool of local nodes.
        """
        deferreds = []
        if self.params.pool:
            # Prepare a pool of nodes
            for i in range(self.params.pool):
                p = NodeLauncher(self.reactor, self.params)
                self.pool.append(p)
                deferreds.append(p.Prepare(i))
        else:
            # Prepare a single node
            p = NodeLauncher(self.reactor, self.params)
            self.pool.append(p)
            deferreds.append(p.Prepare())

        # Load default startup addresses from a separate config file
        if not self.params.as_seed:
            path = os.path.join(self.params.config_dir, self.params.default_entities_file)
            bootup_addresses = self._ParseDefaultEntities(path)
        else:
            bootup_addresses = []

        def _succeed(results):
            # Build the list of local addresses once they are known
            addresses = []
            for ok, address in results:
                if ok:
                    addresses.append(address)
            for p in self.pool:
                self.reactor.callLater(0, p.Launch, bootup_addresses or addresses)
            # Send statistics
            if self.params.send_stats:
                getPage('http://solipsis.netofpeers.net/stat/index.php?z=1')

        def _fail(failure):
            raise Exception(failure)

        # We must wait for all addresses to be known
        # before we can really launch the nodes
        d = defer.DeferredList(deferreds, fireOnOneErrback=True, consumeErrors=True)
        d.addErrback(_fail)
        d.addCallback(_succeed)


    #
    # Private methods
    #
    def _ParseDefaultEntities(self, filename):
        f = file(filename, "r")
        try:
            addresses = []
            for line in f:
                p = line.find('#')
                if p >= 0:
                    line = line[:p]
                t = line.strip().split()
                if len(t) >= 2:
                    host, port = t[0], int(t[1])
                    addresses.append(Address(host, port))
        finally:
            f.close()
        return addresses

    def _MemDebug(self):
        gc.collect()
        self.memsizer.sizeall()
        print "\n... memdump ...\n"
        items = self.memsizer.get_deltas()
        print "\n".join(items)
Exemplo n.º 4
0
 def __init__(self):
     super(Memdump, self).__init__(self.Timeout, self.OnMemdump, [])
     self.sizer = MemSizer()
     self.sizer.sizeall()