Module: Profmem

Extended by:
Profmem
Included in:
Profmem
Defined in:
lib/profmem.rb,
lib/profmem/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#finalizeObject



16
17
18
19
20
21
22
23
24
# File 'lib/profmem.rb', line 16

def finalize
  if path = ENV['PROFMEM']
    io = File.open(path, "w")
    ObjectSpace.dump_all(output: io)
    io.close
    puts "est. total memory consumed: #{ObjectSpace.memsize_of_all}"
    puts "memory profiling data written to #{path} (#{File.size(path)})"
  end
end

#run(args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/profmem.rb', line 26

def run(args)

  puts
  puts 'Reading input file...'
  total = `wc -l "#{args[0]}"`.strip.split(' ')[0].to_i
  puts "Data points:\t#{total}"
  width = total.to_s.length

  data = []
  counter = 0
  File.open(args[0]) do |f|
    f.each_line do |line|
      data << (parsed=JSON.parse(line))
      counter += 1
      print "Progress:\t% #{width}s\r" % counter
    end
  end

  puts
  puts
  puts 'Grouping by generation...'

  generations = data.group_by { |row| row["generation"] }

  puts 'Ranking...'
  ranked = {}
  generations.each do |gen, allocs|
    gen ||= 1 # sometimes gen is nil
    ranked[gen] = {
      rank: gen * allocs.size,
      allocs: allocs
    }
  end
  generations = nil # free some memory

  puts 'Sorting...'
  ranked = ranked.sort_by { |k, v| -v[:rank] }

  puts 'Writing output file...'
  puts "Generations:\t#{ranked.size}"
  width = ranked.size.to_s.length

  File.open(args[1], 'w') do |f|
    counter = 0
    ranked.each do |k,v|
      counter += 1
      print "Progress:\t% #{width}s\r" % counter
      f.puts "generation #{k} objects #{v[:allocs].count} rank #{v[:rank]}"
      locs = v[:allocs].group_by { |a| [a['file'], a['line']] * ':' }
      locs = locs.sort_by { |loc, allocs| -allocs.size }
      locs.each do |loc, allocs|
        f.puts "  #{allocs.size} #{loc}"
      end
    end
  end

  puts
  puts

end

#setupObject



9
10
11
12
13
14
# File 'lib/profmem.rb', line 9

def setup
  if ENV['PROFMEM']
    require 'objspace'
    ObjectSpace.trace_object_allocations_start
  end
end