class Converter

Attributes

first_map[R]

Public Class Methods

read(inp) click to toggle source
# File perf2pprof, line 50
def self.read(inp)
  self.new.read(inp)
end

Public Instance Methods

read(inp) click to toggle source
# File perf2pprof, line 15
def read(inp)
  maps = Set.new
  first_map = nil

  all_stacks = []

  inp.each_line do |line|
    while true
      line.chomp!
      if line =~ /\A\S/
        if line =~ MMAPRE
          addr, size, off, rxp, rest = $1.to_i(0), $2.to_i(0), $3.to_i(0), $4, $5
          m = [addr, size, off, rxp, rest]
          first_map = m unless first_map
          maps << m
        end
      elsif line !~ /^$/
        raise "unexpected: #{line}" unless line =~ STACKRE
        stack = [$1.to_i(16)+1]
        while (line = inp.gets)
          break unless line =~ STACKRE
          stack << $1.to_i(16)
        end
        all_stacks << stack
        next if line
      end
      break
    end
  end
  @all_stacks = all_stacks
  @maps = maps
  @first_map = first_map
  self
end
write(outp) click to toggle source
# File perf2pprof, line 54
def write(outp)
  procmaps = ""
  @maps.to_a.sort.each do |(addr, size, off, rxp, rest)|
    procmaps << "#{addr.to_s(16)}-#{(addr+size).to_s(16)} #{rxp} #{off.to_s(16)} 00:00 0  #{rest}\n"
  end

  # this is 64-bit cpu-profile header for pprof
  hdr = %w[0x00000 0x00000 0x00003 0x00000
         0x00000 0x00000 0x02710 0x00000
         0x00000 0x00000].map {|s| s.to_i(0)}.pack("L<*")

  outp.print hdr

  @all_stacks.each do |stack|
    s = stack.size
    newstack = [1, s, *stack]
    outp.print(newstack.pack("Q<*"))
  end

  outp.print([0, 1, 0].pack("Q<*"))

  outp.print procmaps
end