Mercurial > public > mercurial-scm > hg
comparison mercurial/profiling.py @ 43077:687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Done with
python3.7 contrib/byteify-strings.py -i $(hg files 'set:mercurial/**.py - mercurial/thirdparty/** + hgext/**.py - hgext/fsmonitor/pywatchman/** - mercurial/__init__.py')
black -l 80 -t py33 -S $(hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**" - hgext/fsmonitor/pywatchman/**')
# skip-blame mass-reformatting only
Differential Revision: https://phab.mercurial-scm.org/D6972
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:48:39 -0400 |
parents | 2372284d9457 |
children | eef9a2d67051 |
comparison
equal
deleted
inserted
replaced
43076:2372284d9457 | 43077:687b865b95ad |
---|---|
31 return getattr(mod, 'profile', None) | 31 return getattr(mod, 'profile', None) |
32 | 32 |
33 | 33 |
34 @contextlib.contextmanager | 34 @contextlib.contextmanager |
35 def lsprofile(ui, fp): | 35 def lsprofile(ui, fp): |
36 format = ui.config('profiling', 'format') | 36 format = ui.config(b'profiling', b'format') |
37 field = ui.config('profiling', 'sort') | 37 field = ui.config(b'profiling', b'sort') |
38 limit = ui.configint('profiling', 'limit') | 38 limit = ui.configint(b'profiling', b'limit') |
39 climit = ui.configint('profiling', 'nested') | 39 climit = ui.configint(b'profiling', b'nested') |
40 | 40 |
41 if format not in ['text', 'kcachegrind']: | 41 if format not in [b'text', b'kcachegrind']: |
42 ui.warn(_("unrecognized profiling format '%s'" " - Ignored\n") % format) | 42 ui.warn( |
43 format = 'text' | 43 _(b"unrecognized profiling format '%s'" b" - Ignored\n") % format |
44 ) | |
45 format = b'text' | |
44 | 46 |
45 try: | 47 try: |
46 from . import lsprof | 48 from . import lsprof |
47 except ImportError: | 49 except ImportError: |
48 raise error.Abort( | 50 raise error.Abort( |
49 _( | 51 _( |
50 'lsprof not available - install from ' | 52 b'lsprof not available - install from ' |
51 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/' | 53 b'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/' |
52 ) | 54 ) |
53 ) | 55 ) |
54 p = lsprof.Profiler() | 56 p = lsprof.Profiler() |
55 p.enable(subcalls=True) | 57 p.enable(subcalls=True) |
56 try: | 58 try: |
57 yield | 59 yield |
58 finally: | 60 finally: |
59 p.disable() | 61 p.disable() |
60 | 62 |
61 if format == 'kcachegrind': | 63 if format == b'kcachegrind': |
62 from . import lsprofcalltree | 64 from . import lsprofcalltree |
63 | 65 |
64 calltree = lsprofcalltree.KCacheGrind(p) | 66 calltree = lsprofcalltree.KCacheGrind(p) |
65 calltree.output(fp) | 67 calltree.output(fp) |
66 else: | 68 else: |
75 try: | 77 try: |
76 from flamegraph import flamegraph | 78 from flamegraph import flamegraph |
77 except ImportError: | 79 except ImportError: |
78 raise error.Abort( | 80 raise error.Abort( |
79 _( | 81 _( |
80 'flamegraph not available - install from ' | 82 b'flamegraph not available - install from ' |
81 'https://github.com/evanhempel/python-flamegraph' | 83 b'https://github.com/evanhempel/python-flamegraph' |
82 ) | 84 ) |
83 ) | 85 ) |
84 # developer config: profiling.freq | 86 # developer config: profiling.freq |
85 freq = ui.configint('profiling', 'freq') | 87 freq = ui.configint(b'profiling', b'freq') |
86 filter_ = None | 88 filter_ = None |
87 collapse_recursion = True | 89 collapse_recursion = True |
88 thread = flamegraph.ProfileThread( | 90 thread = flamegraph.ProfileThread( |
89 fp, 1.0 / freq, filter_, collapse_recursion | 91 fp, 1.0 / freq, filter_, collapse_recursion |
90 ) | 92 ) |
94 yield | 96 yield |
95 finally: | 97 finally: |
96 thread.stop() | 98 thread.stop() |
97 thread.join() | 99 thread.join() |
98 print( | 100 print( |
99 'Collected %d stack frames (%d unique) in %2.2f seconds.' | 101 b'Collected %d stack frames (%d unique) in %2.2f seconds.' |
100 % ( | 102 % ( |
101 util.timer() - start_time, | 103 util.timer() - start_time, |
102 thread.num_frames(), | 104 thread.num_frames(), |
103 thread.num_frames(unique=True), | 105 thread.num_frames(unique=True), |
104 ) | 106 ) |
107 | 109 |
108 @contextlib.contextmanager | 110 @contextlib.contextmanager |
109 def statprofile(ui, fp): | 111 def statprofile(ui, fp): |
110 from . import statprof | 112 from . import statprof |
111 | 113 |
112 freq = ui.configint('profiling', 'freq') | 114 freq = ui.configint(b'profiling', b'freq') |
113 if freq > 0: | 115 if freq > 0: |
114 # Cannot reset when profiler is already active. So silently no-op. | 116 # Cannot reset when profiler is already active. So silently no-op. |
115 if statprof.state.profile_level == 0: | 117 if statprof.state.profile_level == 0: |
116 statprof.reset(freq) | 118 statprof.reset(freq) |
117 else: | 119 else: |
118 ui.warn(_("invalid sampling frequency '%s' - ignoring\n") % freq) | 120 ui.warn(_(b"invalid sampling frequency '%s' - ignoring\n") % freq) |
119 | 121 |
120 track = ui.config( | 122 track = ui.config( |
121 'profiling', 'time-track', pycompat.iswindows and 'cpu' or 'real' | 123 b'profiling', b'time-track', pycompat.iswindows and b'cpu' or b'real' |
122 ) | 124 ) |
123 statprof.start(mechanism='thread', track=track) | 125 statprof.start(mechanism=b'thread', track=track) |
124 | 126 |
125 try: | 127 try: |
126 yield | 128 yield |
127 finally: | 129 finally: |
128 data = statprof.stop() | 130 data = statprof.stop() |
129 | 131 |
130 profformat = ui.config('profiling', 'statformat') | 132 profformat = ui.config(b'profiling', b'statformat') |
131 | 133 |
132 formats = { | 134 formats = { |
133 'byline': statprof.DisplayFormats.ByLine, | 135 b'byline': statprof.DisplayFormats.ByLine, |
134 'bymethod': statprof.DisplayFormats.ByMethod, | 136 b'bymethod': statprof.DisplayFormats.ByMethod, |
135 'hotpath': statprof.DisplayFormats.Hotpath, | 137 b'hotpath': statprof.DisplayFormats.Hotpath, |
136 'json': statprof.DisplayFormats.Json, | 138 b'json': statprof.DisplayFormats.Json, |
137 'chrome': statprof.DisplayFormats.Chrome, | 139 b'chrome': statprof.DisplayFormats.Chrome, |
138 } | 140 } |
139 | 141 |
140 if profformat in formats: | 142 if profformat in formats: |
141 displayformat = formats[profformat] | 143 displayformat = formats[profformat] |
142 else: | 144 else: |
143 ui.warn(_('unknown profiler output format: %s\n') % profformat) | 145 ui.warn(_(b'unknown profiler output format: %s\n') % profformat) |
144 displayformat = statprof.DisplayFormats.Hotpath | 146 displayformat = statprof.DisplayFormats.Hotpath |
145 | 147 |
146 kwargs = {} | 148 kwargs = {} |
147 | 149 |
148 def fraction(s): | 150 def fraction(s): |
149 if isinstance(s, (float, int)): | 151 if isinstance(s, (float, int)): |
150 return float(s) | 152 return float(s) |
151 if s.endswith('%'): | 153 if s.endswith(b'%'): |
152 v = float(s[:-1]) / 100 | 154 v = float(s[:-1]) / 100 |
153 else: | 155 else: |
154 v = float(s) | 156 v = float(s) |
155 if 0 <= v <= 1: | 157 if 0 <= v <= 1: |
156 return v | 158 return v |
157 raise ValueError(s) | 159 raise ValueError(s) |
158 | 160 |
159 if profformat == 'chrome': | 161 if profformat == b'chrome': |
160 showmin = ui.configwith(fraction, 'profiling', 'showmin', 0.005) | 162 showmin = ui.configwith(fraction, b'profiling', b'showmin', 0.005) |
161 showmax = ui.configwith(fraction, 'profiling', 'showmax') | 163 showmax = ui.configwith(fraction, b'profiling', b'showmax') |
162 kwargs.update(minthreshold=showmin, maxthreshold=showmax) | 164 kwargs.update(minthreshold=showmin, maxthreshold=showmax) |
163 elif profformat == 'hotpath': | 165 elif profformat == b'hotpath': |
164 # inconsistent config: profiling.showmin | 166 # inconsistent config: profiling.showmin |
165 limit = ui.configwith(fraction, 'profiling', 'showmin', 0.05) | 167 limit = ui.configwith(fraction, b'profiling', b'showmin', 0.05) |
166 kwargs[r'limit'] = limit | 168 kwargs[r'limit'] = limit |
167 showtime = ui.configbool('profiling', 'showtime') | 169 showtime = ui.configbool(b'profiling', b'showtime') |
168 kwargs[r'showtime'] = showtime | 170 kwargs[r'showtime'] = showtime |
169 | 171 |
170 statprof.display(fp, data=data, format=displayformat, **kwargs) | 172 statprof.display(fp, data=data, format=displayformat, **kwargs) |
171 | 173 |
172 | 174 |
202 if not self._entered: | 204 if not self._entered: |
203 raise error.ProgrammingError() | 205 raise error.ProgrammingError() |
204 if self._started: | 206 if self._started: |
205 return | 207 return |
206 self._started = True | 208 self._started = True |
207 profiler = encoding.environ.get('HGPROF') | 209 profiler = encoding.environ.get(b'HGPROF') |
208 proffn = None | 210 proffn = None |
209 if profiler is None: | 211 if profiler is None: |
210 profiler = self._ui.config('profiling', 'type') | 212 profiler = self._ui.config(b'profiling', b'type') |
211 if profiler not in ('ls', 'stat', 'flame'): | 213 if profiler not in (b'ls', b'stat', b'flame'): |
212 # try load profiler from extension with the same name | 214 # try load profiler from extension with the same name |
213 proffn = _loadprofiler(self._ui, profiler) | 215 proffn = _loadprofiler(self._ui, profiler) |
214 if proffn is None: | 216 if proffn is None: |
215 self._ui.warn( | 217 self._ui.warn( |
216 _("unrecognized profiler '%s' - ignored\n") % profiler | 218 _(b"unrecognized profiler '%s' - ignored\n") % profiler |
217 ) | 219 ) |
218 profiler = 'stat' | 220 profiler = b'stat' |
219 | 221 |
220 self._output = self._ui.config('profiling', 'output') | 222 self._output = self._ui.config(b'profiling', b'output') |
221 | 223 |
222 try: | 224 try: |
223 if self._output == 'blackbox': | 225 if self._output == b'blackbox': |
224 self._fp = util.stringio() | 226 self._fp = util.stringio() |
225 elif self._output: | 227 elif self._output: |
226 path = self._ui.expandpath(self._output) | 228 path = self._ui.expandpath(self._output) |
227 self._fp = open(path, 'wb') | 229 self._fp = open(path, b'wb') |
228 elif pycompat.iswindows: | 230 elif pycompat.iswindows: |
229 # parse escape sequence by win32print() | 231 # parse escape sequence by win32print() |
230 class uifp(object): | 232 class uifp(object): |
231 def __init__(self, ui): | 233 def __init__(self, ui): |
232 self._ui = ui | 234 self._ui = ui |
243 self._fpdoclose = False | 245 self._fpdoclose = False |
244 self._fp = self._ui.ferr | 246 self._fp = self._ui.ferr |
245 | 247 |
246 if proffn is not None: | 248 if proffn is not None: |
247 pass | 249 pass |
248 elif profiler == 'ls': | 250 elif profiler == b'ls': |
249 proffn = lsprofile | 251 proffn = lsprofile |
250 elif profiler == 'flame': | 252 elif profiler == b'flame': |
251 proffn = flameprofile | 253 proffn = flameprofile |
252 else: | 254 else: |
253 proffn = statprofile | 255 proffn = statprofile |
254 | 256 |
255 self._profiler = proffn(self._ui, self._fp) | 257 self._profiler = proffn(self._ui, self._fp) |
262 propagate = None | 264 propagate = None |
263 if self._profiler is not None: | 265 if self._profiler is not None: |
264 propagate = self._profiler.__exit__( | 266 propagate = self._profiler.__exit__( |
265 exception_type, exception_value, traceback | 267 exception_type, exception_value, traceback |
266 ) | 268 ) |
267 if self._output == 'blackbox': | 269 if self._output == b'blackbox': |
268 val = 'Profile:\n%s' % self._fp.getvalue() | 270 val = b'Profile:\n%s' % self._fp.getvalue() |
269 # ui.log treats the input as a format string, | 271 # ui.log treats the input as a format string, |
270 # so we need to escape any % signs. | 272 # so we need to escape any % signs. |
271 val = val.replace('%', '%%') | 273 val = val.replace(b'%', b'%%') |
272 self._ui.log('profile', val) | 274 self._ui.log(b'profile', val) |
273 self._closefp() | 275 self._closefp() |
274 return propagate | 276 return propagate |
275 | 277 |
276 def _closefp(self): | 278 def _closefp(self): |
277 if self._fpdoclose and self._fp is not None: | 279 if self._fpdoclose and self._fp is not None: |