#! /usr/bin/env python

import sys
import operator

import re

tags = {}

pattern_cycles = 0
total_cycles = 0

if len(sys.argv) > 1:
	pattern = re.compile(sys.argv[1])
else:
	pattern = None

if len(sys.argv) > 2:
	custom_total_cycles = int(sys.argv[2])
else:
	custom_total_cycles = -1

for line in sys.stdin:

	if line.startswith("#!"):
		# Parse header and cache indices we need.
		t = {}

		m = line.split()[1:]

		for i in range(len(m)):
			t[m[i]] = i

		idx_tag = t["tag"]
		idx_type = t["type"]
		idx_cycles = t["cycles"]

	if line.startswith("#"):
		continue

	m = line.split()

	tag = m[idx_tag]
	type = m[idx_type]
	cycles = int(m[idx_cycles])

#	if type == "B":
#		tags[tag] = tags.get(tag, 0) - cycles
#		total_cycles = total_cycles - cycles
#	elif type == "E":
#		tags[tag] = tags.get(tag, 0) + cycles
#		total_cycles = total_cycles + cycles

	if type == "E":
		tags[tag] = tags.get(tag, 0) + cycles
		total_cycles = total_cycles + cycles

		if pattern and pattern.match(tag):
			pattern_cycles += cycles

if custom_total_cycles >= 0:
	total_cycles = custom_total_cycles

for (tag, val) in sorted(tags.iteritems(), key=operator.itemgetter(1), reverse=True):

	match = "+" if (pattern and pattern.match(tag)) else " "

	if total_cycles:
		print "%s %10.2f%%   %s" % (match, 100.0 * val / total_cycles, tag)
	else:
		print "%s %13s   %s" % (match, "N/A", tag)

if pattern:
	print "Cycles matching pattern: %10.2f%% (%lu)" % (100.0 * pattern_cycles / total_cycles, pattern_cycles)
