Replace hotspot with cProfile for daemon

Added a twisted signal handler to save profiler stats when
deluged is killed with SIGINT.
This commit is contained in:
bendikro 2012-11-25 01:56:00 +01:00 committed by Calum Lind
parent 4b99a39779
commit 763f5de904
1 changed files with 28 additions and 23 deletions

View File

@ -247,10 +247,7 @@ this should be an IP address", metavar="IFACE",
open_logfile()
if options.profile:
import hotshot
hsp = hotshot.Profile(deluge.configmanager.get_config_dir("deluged.profile"))
hsp.start()
def run_daemon(options, args):
try:
from deluge.core.daemon import Daemon
Daemon(options, args)
@ -262,12 +259,20 @@ this should be an IP address", metavar="IFACE",
except Exception, e:
log.exception(e)
sys.exit(1)
finally:
if options.profile:
hsp.stop()
hsp.close()
import hotshot.stats
stats = hotshot.stats.load(deluge.configmanager.get_config_dir("deluged.profile"))
stats.strip_dirs()
stats.sort_stats("time", "calls")
stats.print_stats(400)
import cProfile
profiler = cProfile.Profile()
profile_output = deluge.configmanager.get_config_dir("deluged.profile")
# Twisted catches signals to terminate
def save_profile_stats():
profiler.dump_stats(profile_output)
print "Profile stats saved to %s" % profile_output
from twisted.internet import reactor
reactor.addSystemEventTrigger("before", "shutdown", save_profile_stats)
print "Running with profiler..."
profiler.runcall(run_daemon, options, args)
else:
run_daemon(options, args)