heapviz 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2187fa754ebc6e334dabd3ba02614c7ba986ca5e118e94008a7afdbb47fa8daf
4
+ data.tar.gz: 4b72b34cbf9a0603ca3df40ce45b915a8c94c8b194bd52e0d655f51f3c2d5140
5
+ SHA512:
6
+ metadata.gz: 37bdd98fad55a7025f7208fea532775c231f7a0caac2bbdf2058444becd03f7939a902bd1693c2b613e138527210c2bf866d260d1e969200ba9eb12f2ac911ab
7
+ data.tar.gz: 984326ab4d1cfd28f1d9cf3689c21b411706d4af2bbe168484bf7473729e1de1495dcf7245142954499557af4329b57f600aa93b65003c1a912917fc8aedda9a
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec
5
+
6
+ gem "rake", "~> 13.0"
7
+
8
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ heapviz (0.1.0)
5
+ chunky_png (~> 1.4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ chunky_png (1.4.0)
11
+ minitest (5.15.0)
12
+ mocha (1.13.0)
13
+ rake (13.0.6)
14
+
15
+ PLATFORMS
16
+ arm64-darwin-21
17
+ x86_64-darwin-20
18
+ x86_64-linux
19
+
20
+ DEPENDENCIES
21
+ heapviz!
22
+ minitest (~> 5.0)
23
+ mocha (~> 1.13.0)
24
+ rake (~> 13.0)
25
+
26
+ BUNDLED WITH
27
+ 2.3.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Matt Valentine-House
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Heapviz
2
+
3
+ **Warning: This probably shouldn't be used for anything important. It
4
+ requires patching and building a custom Ruby**
5
+
6
+ Produces a png of a json dump of the Ruby objspace. Heap dump images
7
+ are organised in rows and columns:
8
+
9
+ * A heap page is a vertical column 4 pixels wide
10
+ * A slot is a vertical chunk that is 4 pixels wide and
11
+ (4*slot_size/sizeof(RVALUE)) tall. so 40 byte slots are 4x4 pixels,
12
+ 80 bytes slots are 4x8 pixels, 160 bytes are 4x16 pixels and so on.
13
+
14
+ ## Running
15
+
16
+ This script requires a commit that recently landed on Ruby master to
17
+ add a `slot_size` field to the output of `ObjectSpace.dump_all`. Use
18
+ your Ruby version manager of choice to install the development release
19
+ of Ruby and then write a script that dumps the heap to a file.
20
+
21
+ ```
22
+ require 'objspace'
23
+ ObjectSpace.dump_all(output: File.new("output.json", "w"))
24
+ ```
25
+
26
+ Run it with Ruby
27
+
28
+ ```
29
+ ./ruby test.rb
30
+ ```
31
+
32
+ Then run this script with your json output, and a filename of a png
33
+ to write to (the png doesn't need to exist)
34
+
35
+ ```
36
+ ../heapviz/exe/heapviz output.json output.png
37
+ open output.png
38
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "heapviz"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/heapviz ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.prepend("#{__dir__}/../lib")
4
+ require 'heapviz'
5
+
6
+ if ARGV.length != 2
7
+ p "Usage: heapviz <path_to_heap_dump.json> <output.png>"
8
+ exit 1
9
+ end
10
+
11
+ heap = Heapviz::Heap.new
12
+
13
+ File.open(ARGV[0]) do |f|
14
+ f.each_line do |line|
15
+ live_object = Heapviz::Slot.new(line)
16
+
17
+ if !live_object.gc_root?
18
+ page = heap.get_or_build_page_for(live_object)
19
+ page.fill_slot(live_object)
20
+ end
21
+ end
22
+ end
23
+
24
+ Heapviz::Renderer.new(ARGV[1], heap).render
@@ -0,0 +1,13 @@
1
+ require 'fiddle'
2
+
3
+ module Heapviz
4
+ module Config
5
+ SIZEOF_PAGE_HEADER = Fiddle::SIZEOF_VOIDP
6
+ SIZEOF_RVALUE = GC::INTERNAL_CONSTANTS[:RVALUE_SIZE]
7
+ HEAP_PAGE_ALIGN_LOG = 16
8
+ HEAP_PAGE_ALIGN = 1 << HEAP_PAGE_ALIGN_LOG # 2 ^ 14 (or 16 on MacOS)
9
+ HEAP_PAGE_ALIGN_MASK = ~(~0 << HEAP_PAGE_ALIGN_LOG) # Mask for getting page address
10
+ HEAP_PAGE_SIZE = HEAP_PAGE_ALIGN # Actual page size
11
+ HEAP_PAGE_OBJ_LIMIT = (HEAP_PAGE_SIZE - SIZEOF_PAGE_HEADER) / SIZEOF_RVALUE
12
+ end
13
+ end
@@ -0,0 +1,53 @@
1
+ require 'heapviz/config'
2
+
3
+ module Heapviz
4
+ class Heap
5
+ include Heapviz::Config
6
+
7
+ def initialize
8
+ @pages = {}
9
+ end
10
+
11
+ def get_or_build_page_for(slot)
12
+ @pages[slot.page_body_address] ||= build_page(slot.page_body_address, slot.size)
13
+ end
14
+
15
+ def pages
16
+ @pages.values
17
+ end
18
+
19
+ def page_count
20
+ @pages.length
21
+ end
22
+
23
+ def max_page_size
24
+ HEAP_PAGE_OBJ_LIMIT
25
+ end
26
+
27
+ private
28
+
29
+ def build_page(page_body_address, slot_size)
30
+ # Pages have a header with information, so we have to take that in to account
31
+ start = page_body_address + SIZEOF_PAGE_HEADER
32
+
33
+ # If the object start address isn't evenly divisible by the size of a
34
+ # Ruby object, we need to calculate the padding required to find the first
35
+ # address that is divisible by SIZEOF_RVALUE
36
+ if start % slot_size != 0
37
+ delta = SIZEOF_RVALUE - (start % SIZEOF_RVALUE)
38
+ start += delta # Move forward to first address
39
+
40
+ if num_in_page(start) == 1
41
+ start += slot_size - SIZEOF_RVALUE
42
+ end
43
+ end
44
+ limit = (HEAP_PAGE_SIZE - (start - page_body_address)) / slot_size
45
+
46
+ Page.new(page_body_address, start, limit, slot_size)
47
+ end
48
+
49
+ def num_in_page(obj)
50
+ (obj & HEAP_PAGE_ALIGN_MASK) / SIZEOF_RVALUE
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,58 @@
1
+ module Heapviz
2
+ class Page
3
+ attr_reader :live_objects, :slot_size, :address
4
+
5
+ def initialize(address, obj_start_address, capacity, slot_size)
6
+ @address = address
7
+ @obj_start_address = obj_start_address
8
+ @capacity = capacity
9
+ @slot_size = slot_size
10
+
11
+ @live_objects = []
12
+ end
13
+
14
+ def fill_slot(slot)
15
+ fail "slot size mismatch" if slot.size != @slot_size
16
+ @live_objects << slot
17
+ end
18
+
19
+ def each_slot
20
+ return enum_for(:each_slot) unless block_given?
21
+
22
+ objs = sorted_objects
23
+
24
+ @capacity.times do |i|
25
+ expected = @obj_start_address + (i * slot_size)
26
+ if objs.any? && objs.first.address == expected
27
+ yield objs.shift
28
+ else
29
+ yield nil
30
+ end
31
+ end
32
+ end
33
+
34
+ def sorted_objects
35
+ @live_objects.sort
36
+ end
37
+
38
+ def full?
39
+ @live_objects.count == @capacity
40
+ end
41
+
42
+ def pinned_count
43
+ @pinned_count ||= live_objects.find_all { |lo| lo.dig("flags", "pinned") }.count
44
+ end
45
+
46
+ def live_object_count
47
+ @live_objects.count
48
+ end
49
+
50
+ def fragmented?
51
+ !full?
52
+ end
53
+
54
+ def to_s
55
+ "{page: #{address.to_s(16)}, slot_size: #{slot_size}, full: #{full?}}"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,68 @@
1
+ require 'logger'
2
+ require 'chunky_png'
3
+
4
+ module Heapviz
5
+ class Renderer
6
+ SLOT_BASE_SIZE = 4
7
+ COLORS = {
8
+ 40 => ChunkyPNG::Color.rgba(104, 195, 163, 255),
9
+ 80 => ChunkyPNG::Color.rgba(27, 163, 156, 255),
10
+ 160 => ChunkyPNG::Color.rgba(200, 247, 197, 255),
11
+ 320 => ChunkyPNG::Color.rgba(22, 160, 133, 255),
12
+ 640 => ChunkyPNG::Color.rgba(22, 160, 255, 255),
13
+ }
14
+ WHITE = ChunkyPNG::Color.rgba(255, 255, 255, 255);
15
+ BLACK = ChunkyPNG::Color.rgba(0, 0, 0, 255);
16
+
17
+ def initialize(op_path, heap, logger = nil)
18
+ @op_path = op_path
19
+ @logger = logger || Logger.new(op_path + ".log")
20
+ @logger.datetime_format = "%s.%L"
21
+ @heap = heap
22
+ @img = ChunkyPNG::Image.new(
23
+ heap.page_count * SLOT_BASE_SIZE,
24
+ heap.max_page_size * SLOT_BASE_SIZE
25
+ )
26
+ end
27
+
28
+ def render
29
+ @heap.pages.each_with_index do |page, i|
30
+ start_i = i * SLOT_BASE_SIZE
31
+ render_page(page, start_i)
32
+ end
33
+
34
+ @img.save(@op_path, interlace: true)
35
+ end
36
+
37
+ def slot_colour(slot)
38
+ return WHITE unless slot
39
+
40
+ if slot.pinned?
41
+ BLACK
42
+ elsif slot.type == "NONE"
43
+ WHITE
44
+ else
45
+ COLORS[slot.size]
46
+ end
47
+ end
48
+
49
+ def render_page(page, x)
50
+ # The height of each slot will be scaled up depending on the
51
+ # slot size of the page. Slot size is a multiple of rvalue size
52
+ # right now
53
+ adjusted_height = SLOT_BASE_SIZE * (page.slot_size / Heap::SIZEOF_RVALUE)
54
+
55
+ @logger.debug "#{page}"
56
+ page.each_slot.with_index do |slot, y|
57
+ @logger.debug "\t=> #{slot}"
58
+ y = y * adjusted_height
59
+
60
+ adjusted_height.times do |y_offset|
61
+ SLOT_BASE_SIZE.times do |x_offset|
62
+ @img[x + x_offset, y + y_offset] = slot_colour(slot)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,43 @@
1
+ require 'json'
2
+ require 'heapviz/heap'
3
+
4
+ module Heapviz
5
+ class Slot
6
+ include Comparable
7
+
8
+ attr_reader :type, :address, :flags, :size, :page_body_address
9
+
10
+ def initialize(json)
11
+ attrs = JSON.parse(json)
12
+
13
+ @type = attrs.fetch("type")
14
+ if !gc_root?
15
+ @address = attrs.fetch("address").to_i(16)
16
+ @size = attrs.fetch("slot_size")
17
+ end
18
+ @flags = attrs.fetch("flags", {})
19
+
20
+ @page_body_address = @address & ~Heap::HEAP_PAGE_ALIGN_MASK
21
+ end
22
+
23
+ def gc_root?
24
+ @type == "ROOT"
25
+ end
26
+
27
+ def pinned?
28
+ @flags.fetch('pinned', false)
29
+ end
30
+
31
+ def has_flags?
32
+ !gc_root?
33
+ end
34
+
35
+ def <=>(o)
36
+ address <=> o.address
37
+ end
38
+
39
+ def to_s
40
+ "{address: 0x#{address.to_s(16)}, type: #{type}, flags: #{flags}}"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Heapviz
4
+ VERSION = "0.1.0"
5
+ end
data/lib/heapviz.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'fiddle'
2
+ require 'heapviz/version.rb'
3
+
4
+ require 'heapviz/heap'
5
+ require 'heapviz/page'
6
+ require 'heapviz/slot'
7
+ require 'heapviz/renderer'
data/test.rb ADDED
@@ -0,0 +1,14 @@
1
+ def popcount(n)
2
+ x = 0
3
+ while n > 1
4
+ x += 1
5
+ n >>= 1
6
+ p n.to_s(2)
7
+ end
8
+ x
9
+ end
10
+
11
+
12
+ p popcount 0b111000
13
+ p popcount 0b111111
14
+
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heapviz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Valentine-House
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chunky_png
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 13.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 13.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.13.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.13.0
69
+ description: Visualise a Ruby heap dump output from ObjectSpace.dump_all
70
+ email:
71
+ - matt@eightbitraptor.com
72
+ executables:
73
+ - heapviz
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".ruby-version"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - exe/heapviz
86
+ - lib/heapviz.rb
87
+ - lib/heapviz/config.rb
88
+ - lib/heapviz/heap.rb
89
+ - lib/heapviz/page.rb
90
+ - lib/heapviz/renderer.rb
91
+ - lib/heapviz/slot.rb
92
+ - lib/heapviz/version.rb
93
+ - test.rb
94
+ homepage: https://github.com/eightbitraptor/heapviz
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ homepage_uri: https://github.com/eightbitraptor/heapviz
99
+ source_code_uri: https://github.com/eightbitraptor/heapviz
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: 3.1.0
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.3.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Visualise a Ruby heap dump output from ObjectSpace.dump_all
119
+ test_files: []