diff --git a/kitty/boss.py b/kitty/boss.py index 26839a1f4..d5cbae3e4 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -28,6 +28,7 @@ class Boss(Thread): def __init__(self, window, window_width, window_height, opts, args): Thread.__init__(self, name='ChildMonitor') + self.profile = args.profile self.child_fd = create_pty()[0] self.loop = asyncio.get_event_loop() self.loop.add_signal_handler(signal.SIGINT, lambda: self.loop.call_soon_threadsafe(self.shutdown)) @@ -88,7 +89,17 @@ class Boss(Thread): self.char_grid.render() def run(self): + if self.profile: + import cProfile + import pstats + pr = cProfile.Profile() + pr.enable() self.loop.run_forever() + if self.profile: + pr.disable() + pr.create_stats() + s = pstats.Stats(pr) + s.dump_stats(self.profile) def close(self): if not self.shutting_down: diff --git a/kitty/main.py b/kitty/main.py index e7a99a5b2..ca5d9e52b 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -3,6 +3,7 @@ # License: GPL v3 Copyright: 2016, Kovid Goyal import argparse +import tempfile import os import sys import pwd @@ -93,6 +94,8 @@ def main(): child = args.args or [pwd.getpwuid(os.geteuid()).pw_shell or '/bin/sh'] fork_child(child, args.directory, opts) if args.profile: + tf = tempfile.NamedTemporaryFile(prefix='kitty-profiling-stats-') + args.profile = tf.name import cProfile import pstats pr = cProfile.Profile() @@ -101,6 +104,8 @@ def main(): pr.disable() pr.create_stats() s = pstats.Stats(pr) + s.add(args.profile) + tf.close() s.strip_dirs() s.sort_stats('time', 'name') s.print_stats(30)