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

View File

@ -247,10 +247,7 @@ this should be an IP address", metavar="IFACE",
open_logfile() open_logfile()
if options.profile: def run_daemon(options, args):
import hotshot
hsp = hotshot.Profile(deluge.configmanager.get_config_dir("deluged.profile"))
hsp.start()
try: try:
from deluge.core.daemon import Daemon from deluge.core.daemon import Daemon
Daemon(options, args) Daemon(options, args)
@ -262,12 +259,20 @@ this should be an IP address", metavar="IFACE",
except Exception, e: except Exception, e:
log.exception(e) log.exception(e)
sys.exit(1) sys.exit(1)
finally:
if options.profile: if options.profile:
hsp.stop() import cProfile
hsp.close() profiler = cProfile.Profile()
import hotshot.stats profile_output = deluge.configmanager.get_config_dir("deluged.profile")
stats = hotshot.stats.load(deluge.configmanager.get_config_dir("deluged.profile"))
stats.strip_dirs() # Twisted catches signals to terminate
stats.sort_stats("time", "calls") def save_profile_stats():
stats.print_stats(400) 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)