From f158c8270b7572b3f3384a52cb6f6c51ecd7ec57 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sat, 17 Jan 2015 02:58:07 +0300 Subject: Rich man's profiler - handling quotes --- Debug/poor-mans-profiler.sh | 49 ++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'Debug') diff --git a/Debug/poor-mans-profiler.sh b/Debug/poor-mans-profiler.sh index 7657e4dc7..89ff4f424 100755 --- a/Debug/poor-mans-profiler.sh +++ b/Debug/poor-mans-profiler.sh @@ -122,13 +122,12 @@ fi # [ -f $stacksfile ] || die "Where are the stack samples?" -cat $stacksfile | python -c " +cat << 'EOF' > /tmp/pmpn-folder.py # # This stack folder correctly handles C++ types. # - from __future__ import print_function, division -import fileinput, collections, os +import fileinput, collections, os, sys def enforce(x, msg='Invalid input'): if not x: @@ -137,20 +136,29 @@ def enforce(x, msg='Invalid input'): def split_first_part_with_parens(line): LBRACES = {'(':'()', '<':'<>', '[':'[]', '{':'{}'} RBRACES = {')':'()', '>':'<>', ']':'[]', '}':'{}'} + QUOTES = set(['"', "'"]) + quotes = collections.defaultdict(bool) braces = collections.defaultdict(int) out = '' for ch in line: out += ch + # escape character cancels further processing + if ch == '\\': + continue # special cases if out.endswith('operator>') or out.endswith('operator->'): # gotta love c++ braces['<>'] += 1 if out.endswith('operator<'): braces['<>'] -= 1 - # counting parens - if ch in LBRACES.keys(): - braces[LBRACES[ch]] += 1 - if ch in RBRACES.keys(): - braces[RBRACES[ch]] -= 1 + # switching quotes + if ch in QUOTES: + quotes[ch] = not quotes[ch] + # counting parens only when outside quotes + if sum(quotes.values()) == 0: + if ch in LBRACES.keys(): + braces[LBRACES[ch]] += 1 + if ch in RBRACES.keys(): + braces[RBRACES[ch]] -= 1 # sanity check for v in braces.values(): enforce(v >= 0, 'Unaligned braces: ' + str(dict(braces))) @@ -206,19 +214,24 @@ def parse(line): stacks = collections.defaultdict(int) current = '' -for line in fileinput.input(): - line = line.strip() - if line: - inf = parse(line) - fun = inf['function'] - current = (fun + ';' + current) if current else fun - elif current: - stacks[current] += 1 - current = '' +for idx,line in enumerate(fileinput.input()): + try: + line = line.strip() + if line: + inf = parse(line) + fun = inf['function'] + current = (fun + ';' + current) if current else fun + elif current: + stacks[current] += 1 + current = '' + except Exception, ex: + print('ERROR (line %d):' % (idx + 1), ex, file=sys.stderr) for s, f in sorted(stacks.items(), key=lambda (s, f): s): print(s, f) -" > $foldfile +EOF + +cat $stacksfile | python /tmp/pmpn-folder.py > $foldfile echo "Folded stacks saved to $foldfile" -- cgit v1.2.3