author | Gregory Szorc <gregory.szorc@gmail.com> |
Sun, 06 Oct 2019 13:28:56 -0400 | |
changeset 43085 | eef9a2d67051 |
parent 43077 | 687b865b95ad |
child 43089 | c59eb1560c44 |
permissions | -rw-r--r-- |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
1 |
# profiling.py - profiling functions |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 |
# |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2016 Gregory Szorc <gregory.szorc@gmail.com> |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 |
# |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
6 |
# GNU General Public License version 2 or any later version. |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 |
|
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 |
from __future__ import absolute_import, print_function |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 |
|
29783
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
10 |
import contextlib |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 |
|
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
12 |
from .i18n import _ |
43085
eef9a2d67051
py3: manually import pycompat.open into files that need it
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43077
diff
changeset
|
13 |
from .pycompat import open |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
14 |
from . import ( |
30820
6a70cf94d1b5
py3: replace pycompat.getenv with encoding.environ.get
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30664
diff
changeset
|
15 |
encoding, |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 |
error, |
32417
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
17 |
extensions, |
36683
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
18 |
pycompat, |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
19 |
util, |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
20 |
) |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
21 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
22 |
|
32417
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
23 |
def _loadprofiler(ui, profiler): |
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
24 |
"""load profiler extension. return profile method, or None on failure""" |
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
25 |
extname = profiler |
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
26 |
extensions.loadall(ui, whitelist=[extname]) |
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
27 |
try: |
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
28 |
mod = extensions.find(extname) |
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
29 |
except KeyError: |
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
30 |
return None |
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
31 |
else: |
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
32 |
return getattr(mod, 'profile', None) |
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
30975
diff
changeset
|
33 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
34 |
|
29783
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
35 |
@contextlib.contextmanager |
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
36 |
def lsprofile(ui, fp): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
37 |
format = ui.config(b'profiling', b'format') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
38 |
field = ui.config(b'profiling', b'sort') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
39 |
limit = ui.configint(b'profiling', b'limit') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
40 |
climit = ui.configint(b'profiling', b'nested') |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
41 |
|
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
42 |
if format not in [b'text', b'kcachegrind']: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
43 |
ui.warn( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
44 |
_(b"unrecognized profiling format '%s'" b" - Ignored\n") % format |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
45 |
) |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
46 |
format = b'text' |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
47 |
|
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
48 |
try: |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
49 |
from . import lsprof |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
50 |
except ImportError: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
51 |
raise error.Abort( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
52 |
_( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
53 |
b'lsprof not available - install from ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
54 |
b'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
55 |
) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
56 |
) |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
57 |
p = lsprof.Profiler() |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
58 |
p.enable(subcalls=True) |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
59 |
try: |
29783
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
60 |
yield |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
61 |
finally: |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
62 |
p.disable() |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
63 |
|
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
64 |
if format == b'kcachegrind': |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
65 |
from . import lsprofcalltree |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
66 |
|
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
67 |
calltree = lsprofcalltree.KCacheGrind(p) |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
68 |
calltree.output(fp) |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
69 |
else: |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
70 |
# format == 'text' |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
71 |
stats = lsprof.Stats(p.getstats()) |
40192
b8f6a99ad89b
py3: convert sorting field to sysstr
Gregory Szorc <gregory.szorc@gmail.com>
parents:
38260
diff
changeset
|
72 |
stats.sort(pycompat.sysstr(field)) |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
73 |
stats.pprint(limit=limit, file=fp, climit=climit) |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
74 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
75 |
|
29783
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
76 |
@contextlib.contextmanager |
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
77 |
def flameprofile(ui, fp): |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
78 |
try: |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
79 |
from flamegraph import flamegraph |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
80 |
except ImportError: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
81 |
raise error.Abort( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
82 |
_( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
83 |
b'flamegraph not available - install from ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
84 |
b'https://github.com/evanhempel/python-flamegraph' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
85 |
) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
86 |
) |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
87 |
# developer config: profiling.freq |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
88 |
freq = ui.configint(b'profiling', b'freq') |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
89 |
filter_ = None |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
90 |
collapse_recursion = True |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
91 |
thread = flamegraph.ProfileThread( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
92 |
fp, 1.0 / freq, filter_, collapse_recursion |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
93 |
) |
30975
22fbca1d11ed
mercurial: switch to util.timer for all interval timings
Simon Farnsworth <simonfar@fb.com>
parents:
30930
diff
changeset
|
94 |
start_time = util.timer() |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
95 |
try: |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
96 |
thread.start() |
29783
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
97 |
yield |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
98 |
finally: |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
99 |
thread.stop() |
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
100 |
thread.join() |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
101 |
print( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
102 |
b'Collected %d stack frames (%d unique) in %2.2f seconds.' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
103 |
% ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
104 |
util.timer() - start_time, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
105 |
thread.num_frames(), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
106 |
thread.num_frames(unique=True), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
107 |
) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
108 |
) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
109 |
|
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
110 |
|
29783
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
111 |
@contextlib.contextmanager |
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
112 |
def statprofile(ui, fp): |
30316
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
113 |
from . import statprof |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
114 |
|
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
115 |
freq = ui.configint(b'profiling', b'freq') |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
116 |
if freq > 0: |
29785
88d3c1ab03a7
profiling: don't error with statprof when profiling has already started
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29784
diff
changeset
|
117 |
# Cannot reset when profiler is already active. So silently no-op. |
88d3c1ab03a7
profiling: don't error with statprof when profiling has already started
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29784
diff
changeset
|
118 |
if statprof.state.profile_level == 0: |
88d3c1ab03a7
profiling: don't error with statprof when profiling has already started
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29784
diff
changeset
|
119 |
statprof.reset(freq) |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
120 |
else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
121 |
ui.warn(_(b"invalid sampling frequency '%s' - ignoring\n") % freq) |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
122 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
123 |
track = ui.config( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
124 |
b'profiling', b'time-track', pycompat.iswindows and b'cpu' or b'real' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
125 |
) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
126 |
statprof.start(mechanism=b'thread', track=track) |
30316
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
127 |
|
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
128 |
try: |
29783
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
129 |
yield |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
130 |
finally: |
30316
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
131 |
data = statprof.stop() |
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
132 |
|
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
133 |
profformat = ui.config(b'profiling', b'statformat') |
30316
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
134 |
|
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
135 |
formats = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
136 |
b'byline': statprof.DisplayFormats.ByLine, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
137 |
b'bymethod': statprof.DisplayFormats.ByMethod, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
138 |
b'hotpath': statprof.DisplayFormats.Hotpath, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
139 |
b'json': statprof.DisplayFormats.Json, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
140 |
b'chrome': statprof.DisplayFormats.Chrome, |
30316
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
141 |
} |
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
142 |
|
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
143 |
if profformat in formats: |
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
144 |
displayformat = formats[profformat] |
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
145 |
else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
146 |
ui.warn(_(b'unknown profiler output format: %s\n') % profformat) |
30316
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
147 |
displayformat = statprof.DisplayFormats.Hotpath |
faf1b8923da2
profiling: use vendored statprof and upstream enhancements (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29785
diff
changeset
|
148 |
|
30930
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
149 |
kwargs = {} |
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
150 |
|
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
151 |
def fraction(s): |
32978
41b081ac2145
profiling: cope with configwith default value handling changes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32851
diff
changeset
|
152 |
if isinstance(s, (float, int)): |
41b081ac2145
profiling: cope with configwith default value handling changes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32851
diff
changeset
|
153 |
return float(s) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
154 |
if s.endswith(b'%'): |
30930
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
155 |
v = float(s[:-1]) / 100 |
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
156 |
else: |
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
157 |
v = float(s) |
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
158 |
if 0 <= v <= 1: |
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
159 |
return v |
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
160 |
raise ValueError(s) |
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
161 |
|
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
162 |
if profformat == b'chrome': |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
163 |
showmin = ui.configwith(fraction, b'profiling', b'showmin', 0.005) |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
164 |
showmax = ui.configwith(fraction, b'profiling', b'showmax') |
30930
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
165 |
kwargs.update(minthreshold=showmin, maxthreshold=showmax) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
166 |
elif profformat == b'hotpath': |
33195
5d8942dbe49e
check-config: syntax to allow inconsistent config values
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32978
diff
changeset
|
167 |
# inconsistent config: profiling.showmin |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
168 |
limit = ui.configwith(fraction, b'profiling', b'showmin', 0.05) |
36400
7b86aa31b004
py3: fix handling of keyword arguments at more places
Pulkit Goyal <7895pulkit@gmail.com>
parents:
34412
diff
changeset
|
169 |
kwargs[r'limit'] = limit |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
170 |
showtime = ui.configbool(b'profiling', b'showtime') |
42423
0ae593e791fb
profiling: show actual time spent in hotpath display
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
40418
diff
changeset
|
171 |
kwargs[r'showtime'] = showtime |
30930
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
172 |
|
517bc1cd7033
profiling: add statprof support for Chrome trace viewer rendering
Bryan O'Sullivan <bryano@fb.com>
parents:
30820
diff
changeset
|
173 |
statprof.display(fp, data=data, format=displayformat, **kwargs) |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
174 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
175 |
|
32783
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
176 |
class profile(object): |
29783
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
177 |
"""Start profiling. |
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
178 |
|
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
179 |
Profiling is active when the context manager is active. When the context |
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
180 |
manager exits, profiling results will be written to the configured output. |
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
181 |
""" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
182 |
|
32785
37ec8f24d912
profile: introduce a knob to control if the context is actually profiling
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32784
diff
changeset
|
183 |
def __init__(self, ui, enabled=True): |
32783
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
184 |
self._ui = ui |
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
185 |
self._output = None |
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
186 |
self._fp = None |
32805
2b0fc56840d0
profile: use explicit logic to control file closing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32804
diff
changeset
|
187 |
self._fpdoclose = True |
32783
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
188 |
self._profiler = None |
32785
37ec8f24d912
profile: introduce a knob to control if the context is actually profiling
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32784
diff
changeset
|
189 |
self._enabled = enabled |
32784
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
190 |
self._entered = False |
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
191 |
self._started = False |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
192 |
|
32783
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
193 |
def __enter__(self): |
32784
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
194 |
self._entered = True |
32785
37ec8f24d912
profile: introduce a knob to control if the context is actually profiling
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32784
diff
changeset
|
195 |
if self._enabled: |
37ec8f24d912
profile: introduce a knob to control if the context is actually profiling
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32784
diff
changeset
|
196 |
self.start() |
32786
0ead06d54ffe
profile: make the contextmanager object available to the callers
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32785
diff
changeset
|
197 |
return self |
32784
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
198 |
|
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
199 |
def start(self): |
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
200 |
"""Start profiling. |
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
201 |
|
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
202 |
The profiling will stop at the context exit. |
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
203 |
|
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
204 |
If the profiler was already started, this has no effect.""" |
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
205 |
if not self._entered: |
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
206 |
raise error.ProgrammingError() |
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
207 |
if self._started: |
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
208 |
return |
086c1ef0f666
profile: introduce a "start" method to the profile context
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32783
diff
changeset
|
209 |
self._started = True |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
210 |
profiler = encoding.environ.get(b'HGPROF') |
32783
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
211 |
proffn = None |
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
212 |
if profiler is None: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
213 |
profiler = self._ui.config(b'profiling', b'type') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
214 |
if profiler not in (b'ls', b'stat', b'flame'): |
32783
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
215 |
# try load profiler from extension with the same name |
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
216 |
proffn = _loadprofiler(self._ui, profiler) |
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
217 |
if proffn is None: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
218 |
self._ui.warn( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
219 |
_(b"unrecognized profiler '%s' - ignored\n") % profiler |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
220 |
) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
221 |
profiler = b'stat' |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
222 |
|
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
223 |
self._output = self._ui.config(b'profiling', b'output') |
29781
2654a0aac80d
profiling: move profiling code from dispatch.py (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
224 |
|
32808
336700745a5c
profile: close 'fp' on error within '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32807
diff
changeset
|
225 |
try: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
226 |
if self._output == b'blackbox': |
32807
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
227 |
self._fp = util.stringio() |
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
228 |
elif self._output: |
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
229 |
path = self._ui.expandpath(self._output) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
230 |
self._fp = open(path, b'wb') |
36683
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
231 |
elif pycompat.iswindows: |
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
232 |
# parse escape sequence by win32print() |
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
233 |
class uifp(object): |
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
234 |
def __init__(self, ui): |
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
235 |
self._ui = ui |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
236 |
|
36683
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
237 |
def write(self, data): |
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
238 |
self._ui.write_err(data) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
239 |
|
36683
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
240 |
def flush(self): |
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
241 |
self._ui.flush() |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
242 |
|
36683
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
243 |
self._fpdoclose = False |
e39953fdd924
profile: colorize output on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
36400
diff
changeset
|
244 |
self._fp = uifp(self._ui) |
32807
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
245 |
else: |
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
246 |
self._fpdoclose = False |
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
247 |
self._fp = self._ui.ferr |
32783
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
248 |
|
32807
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
249 |
if proffn is not None: |
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
250 |
pass |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
251 |
elif profiler == b'ls': |
32807
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
252 |
proffn = lsprofile |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
253 |
elif profiler == b'flame': |
32807
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
254 |
proffn = flameprofile |
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
255 |
else: |
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
256 |
proffn = statprofile |
29783
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
257 |
|
32807
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
258 |
self._profiler = proffn(self._ui, self._fp) |
54b356d65079
profile: indent part of '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32806
diff
changeset
|
259 |
self._profiler.__enter__() |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
260 |
except: # re-raises |
32808
336700745a5c
profile: close 'fp' on error within '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32807
diff
changeset
|
261 |
self._closefp() |
336700745a5c
profile: close 'fp' on error within '__enter__'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32807
diff
changeset
|
262 |
raise |
29783
5d44197c208b
profiling: make profiling functions context managers (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29781
diff
changeset
|
263 |
|
32783
4483696dacee
profile: upgrade the "profile" context manager to a full class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32417
diff
changeset
|
264 |
def __exit__(self, exception_type, exception_value, traceback): |
32810
6675d23da748
profile: properly propagate exception from the sub-context manager
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32809
diff
changeset
|
265 |
propagate = None |
32809
062eb859d3ee
profile: close 'fp' in all cases
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32808
diff
changeset
|
266 |
if self._profiler is not None: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
267 |
propagate = self._profiler.__exit__( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
268 |
exception_type, exception_value, traceback |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42423
diff
changeset
|
269 |
) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
270 |
if self._output == b'blackbox': |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
271 |
val = b'Profile:\n%s' % self._fp.getvalue() |
32809
062eb859d3ee
profile: close 'fp' in all cases
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32808
diff
changeset
|
272 |
# ui.log treats the input as a format string, |
062eb859d3ee
profile: close 'fp' in all cases
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32808
diff
changeset
|
273 |
# so we need to escape any % signs. |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
274 |
val = val.replace(b'%', b'%%') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
275 |
self._ui.log(b'profile', val) |
32806
3a4c677cbd6e
profile: remove now useless indent
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32805
diff
changeset
|
276 |
self._closefp() |
32810
6675d23da748
profile: properly propagate exception from the sub-context manager
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32809
diff
changeset
|
277 |
return propagate |
32804
c0b2c8f25ad9
profiling: move 'fp' closing logic into its own function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32788
diff
changeset
|
278 |
|
c0b2c8f25ad9
profiling: move 'fp' closing logic into its own function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32788
diff
changeset
|
279 |
def _closefp(self): |
32805
2b0fc56840d0
profile: use explicit logic to control file closing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32804
diff
changeset
|
280 |
if self._fpdoclose and self._fp is not None: |
2b0fc56840d0
profile: use explicit logic to control file closing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32804
diff
changeset
|
281 |
self._fp.close() |