mercurial/lsprofcalltree.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48946 642e31cb55f0
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     1
"""
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     2
lsprofcalltree.py - lsprof output which is readable by kcachegrind
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     3
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     4
Authors:
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     5
    * David Allouche <david <at> allouche.net>
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     6
    * Jp Calderone & Itamar Shtull-Trauring
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     7
    * Johan Dahlin
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     8
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     9
This software may be used and distributed according to the terms
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    10
of the GNU General Public License, incorporated herein by reference.
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    11
"""
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    12
27505
071af8d385a9 lsprofcalltree: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 8390
diff changeset
    13
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40195
diff changeset
    14
from . import pycompat
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40195
diff changeset
    15
40195
720355c7b7c9 py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40194
diff changeset
    16
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    17
def label(code):
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    18
    if isinstance(code, str):
40195
720355c7b7c9 py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40194
diff changeset
    19
        # built-in functions ('~' sorts at the end)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    20
        return b'~' + pycompat.sysbytes(code)
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    21
    else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    22
        return b'%s %s:%d' % (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40195
diff changeset
    23
            pycompat.sysbytes(code.co_name),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40195
diff changeset
    24
            pycompat.sysbytes(code.co_filename),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40195
diff changeset
    25
            code.co_firstlineno,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40195
diff changeset
    26
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40195
diff changeset
    27
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    28
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
    29
class KCacheGrind:
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    30
    def __init__(self, profiler):
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    31
        self.data = profiler.getstats()
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    32
        self.out_file = None
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    33
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    34
    def output(self, out_file):
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    35
        self.out_file = out_file
40194
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    36
        out_file.write(b'events: Ticks\n')
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    37
        self._print_summary()
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    38
        for entry in self.data:
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    39
            self._entry(entry)
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    40
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    41
    def _print_summary(self):
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    42
        max_cost = 0
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    43
        for entry in self.data:
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    44
            totaltime = int(entry.totaltime * 1000)
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    45
            max_cost = max(max_cost, totaltime)
40194
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    46
        self.out_file.write(b'summary: %d\n' % max_cost)
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    47
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    48
    def _entry(self, entry):
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    49
        out_file = self.out_file
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    50
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    51
        code = entry.code
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    52
        if isinstance(code, str):
40194
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    53
            out_file.write(b'fi=~\n')
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    54
        else:
40195
720355c7b7c9 py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40194
diff changeset
    55
            out_file.write(b'fi=%s\n' % pycompat.sysbytes(code.co_filename))
40194
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    56
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    57
        out_file.write(b'fn=%s\n' % label(code))
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    58
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    59
        inlinetime = int(entry.inlinetime * 1000)
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    60
        if isinstance(code, str):
40194
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    61
            out_file.write(b'0 %d\n' % inlinetime)
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    62
        else:
40194
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    63
            out_file.write(b'%d %d\n' % (code.co_firstlineno, inlinetime))
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    64
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    65
        # recursive calls are counted in entry.calls
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    66
        if entry.calls:
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    67
            calls = entry.calls
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    68
        else:
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    69
            calls = []
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    70
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    71
        if isinstance(code, str):
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    72
            lineno = 0
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    73
        else:
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    74
            lineno = code.co_firstlineno
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    75
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    76
        for subentry in calls:
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    77
            self._subentry(lineno, subentry)
40194
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    78
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    79
        out_file.write(b'\n')
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    80
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    81
    def _subentry(self, lineno, subentry):
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    82
        out_file = self.out_file
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    83
        code = subentry.code
40194
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    84
        out_file.write(b'cfn=%s\n' % label(code))
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    85
        if isinstance(code, str):
40194
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    86
            out_file.write(b'cfi=~\n')
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    87
            out_file.write(b'calls=%d 0\n' % subentry.callcount)
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    88
        else:
40195
720355c7b7c9 py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40194
diff changeset
    89
            out_file.write(b'cfi=%s\n' % pycompat.sysbytes(code.co_filename))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40195
diff changeset
    90
            out_file.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40195
diff changeset
    91
                b'calls=%d %d\n' % (subentry.callcount, code.co_firstlineno)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40195
diff changeset
    92
            )
8024
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    93
9a1b86cfd29e profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    94
        totaltime = int(subentry.totaltime * 1000)
40194
1ae0faa14797 py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27618
diff changeset
    95
        out_file.write(b'%d %d\n' % (lineno, totaltime))