#!/usr/bin/ruby
# 2013-01-14 10:47
# perf-sum-selftime.rb  DWF
# Usage:  perf-sum-selftime.rb [file ...]
# Input:  one or more perf scripts, or a concatenation of perf scripts,
#         in the format -f ip,sym,dso [NO CALLCHAINS]
# Output:  self time by function.
# Tested only on simple cases of profiling a single application.
samples = 0
counts = Hash.new(0)
ARGF.lines do |line|
  if line[0] != "#"
    raise "Input format error" if line[0] != " "
    counts[line.split(' ',2)[1]] += 1
    samples += 1
  end
end
decimals = Math.log10(samples).ceil + 1
outbuf = []
counts.each do |fn, count|
  # Ruby has rationals, but right now we just want the float.
  proportion = Float(count) / samples
  outbuf.push ("%.#{decimals}f %s" % [proportion, fn])
end
puts "#{samples} samples"
puts outbuf.sort!.reverse!
