sapling/hgext/traceprof.pyx
Jun Wu 04f520998c traceprof: enforce native stderr on Windows to avoid crash
Summary:
On Windows `FILE*` seems to be different in traceprofimpl.cpp and Python:

```
# fp passed from traceprof.pyx:profile(fp=sys.stderr) to traceprofimpl.cpp
0x0000000000d23cc0 {_Placeholder=0x0000000000d23360 }
    _Placeholder: 0x0000000000d23360

# stderr as seen from traceprofimpl.cpp natively
ucrtbase.dll!0x00007ffb29465510 (load symbols for additional information) {_Placeholder=0x0000000000000000 }
    _Placeholder: 0x0000000000000000
```

Workaround that by enforcing output Windows to use stderr for now.

Test Plan:
```
C:\hg> python2 setup.py build_ext -i
...

C:\hg> type a.py
import sys
from mercurial import ui
from hgext import traceprof
def f(): g(1)
def g(x): print(x)
with traceprof.profile(ui.ui(), sys.stderr): f()

C:\hg> python2 a.py
1
    | <module>                                  a.py:1
    | __exit__                                  C:\...\contextlib.py:21
Total time: 0 ms
```

Reviewers: #mercurial

Differential Revision: https://phabricator.intern.facebook.com/D6686958
2018-01-09 13:26:14 -08:00

76 lines
2.0 KiB
Cython

# distutils: language = c++
# traceprof.pyx - C++ to Python bridge for the traceprof Mercurial extension
#
# Copyright 2017 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
"""accurate callgraph profiling
lsprof's high precision, plus statprof's intuitive output format.
Config::
[traceprof]
# whether to disable Python GC before profiling
disablegc = no
# minimal microseconds to show a function
timethreshold = 2000
# minimal call count to show "(N times)"
countthreshold = 2
# frame de-duplication (slower to print outputs)
framededup = yes
"""
from libc.stdio cimport FILE
from cpython.object cimport PyObject
import contextlib
import gc
cdef extern from "extlib/traceprofimpl.cpp":
void enable()
void disable()
IF UNAME_SYSNAME == "Windows":
void report()
ELSE:
void report(FILE *)
void settimethreshold(double)
void setcountthreshold(size_t)
void setdedup(int)
void clear()
cdef extern from "Python.h":
FILE* PyFile_AsFile(PyObject *p)
@contextlib.contextmanager
def profile(ui, fp):
if ui is not None:
if ui.configbool('traceprof', 'disablegc'):
gc.disable() # slightly more predictable
microseconds = ui.configint('traceprof', 'timethreshold')
if microseconds is not None:
settimethreshold((<double>microseconds) / 1000.0)
count = ui.configint('traceprof', 'countthreshold')
if count is not None:
setcountthreshold(count)
dedup = ui.configbool('traceprof', 'framededup', True)
setdedup(<int>dedup)
enable()
try:
yield
finally:
disable()
IF UNAME_SYSNAME == "Windows":
# On Windows, "FILE*" could be incompatible between Python.exe and
# traceimpl.exp. Use stderr from traceimpl.exp conservatively.
report()
ELSE:
report(PyFile_AsFile(<PyObject *>fp))
clear()