sheap 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0f61e05e0d4dbe85f032a53a7b61a9d5446d1f7bddd599ed47b46c132c3615ca
4
+ data.tar.gz: f2c2aa947a8c2110df25add16f97b27a4a0df10473dd4e73c98a4634899489b1
5
+ SHA512:
6
+ metadata.gz: d75e2dce655e44a761c0f3a23c29afd59fe2e535f179b286175e1685d3e4552bf3c4a9eefc21495f1be67a31084db6ceb6a400f7fe40ac98bf21b0751665c50d
7
+ data.tar.gz: 5410d3da66d793c4c552878738726e15e34341c017f62bc8c992baebab2933fe3780a9efec522717c8c01ee02ac7bc9cdbbc65c7c9691d69861299b4fda26cb3
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at john@hawthorn.email. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in sheap.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 John Hawthorn
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,39 @@
1
+ # Sheap
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sheap`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jhawthorn/sheap. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/jhawthorn/sheap/blob/main/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Sheap project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jhawthorn/sheap/blob/main/CODE_OF_CONDUCT.md).
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/exe/sheap ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "sheap"
4
+ require "irb"
5
+
6
+ heap = "asdfasdf"
7
+
8
+ usage = <<~USAGE
9
+ Usage: sheap
10
+ sheap HEAPDUMP
11
+ sheap BEFORE AFTER
12
+ USAGE
13
+
14
+ if ARGV.size == 0
15
+ elsif ARGV.size == 1
16
+ $heap = @heap = Sheap::Heap.new(ARGV.shift)
17
+ puts("$heap: #{@heap.inspect}")
18
+ elsif ARGV.size == 2
19
+ $before = @before = Sheap::Heap.new(ARGV.shift)
20
+ $after = @after = Sheap::Heap.new(ARGV.shift)
21
+ $diff = @diff = Sheap::Diff.new(@before, @after)
22
+ puts("$before: #{@before.inspect}, $after: #{@after.inspect}, $diff: #{@diff.inspect}")
23
+ else
24
+ $stderr.puts usage
25
+ exit
26
+ end
27
+
28
+ require 'irb'
29
+ IRB.setup nil
30
+ IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context
31
+ require 'irb/ext/multi-irb'
32
+ IRB.irb nil, @heap
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Sheap
4
+ VERSION = "0.1.0"
5
+ end
data/lib/sheap.rb ADDED
@@ -0,0 +1,279 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "objspace"
4
+ require "fileutils"
5
+ require "json"
6
+
7
+ class Sheap
8
+ def self.reload!
9
+ load __FILE__
10
+ end
11
+
12
+ def initialize
13
+ @dir = File.expand_path("sheap")
14
+ @idx = 0
15
+ FileUtils.mkdir_p(@dir)
16
+ end
17
+
18
+ class Diff
19
+ attr_reader :before, :after
20
+ def initialize(before, after)
21
+ @before = before
22
+ @after = after
23
+ end
24
+
25
+ def retained
26
+ @retained ||= after.objects - before.objects
27
+ end
28
+ end
29
+
30
+ def self.load
31
+ Dir["sheap/*"].map do |file|
32
+ Heap.new(file)
33
+ end
34
+ end
35
+
36
+ def self.load_diff
37
+ before = Heap.new("sheap/snapshot-0.dump")
38
+ after = Heap.new("sheap/snapshot-1.dump")
39
+ Diff.new(before, after)
40
+ end
41
+
42
+ def snapshot(gc: true)
43
+ 3.times { GC.start } if gc
44
+
45
+ output = File.join(@dir, "snapshot-#{@idx}.dump")
46
+ File.open(output, "w") do |file|
47
+ ObjectSpace.dump_all(output: file)
48
+ end
49
+ @idx += 1
50
+ end
51
+
52
+ class << self
53
+ def instance
54
+ @instance ||= new
55
+ end
56
+
57
+ def snapshot
58
+ instance.snapshot
59
+ end
60
+ end
61
+
62
+ EMPTY_ARRAY = [].freeze
63
+
64
+ class HeapObject
65
+ attr_reader :heap, :json
66
+
67
+ def initialize(heap, json)
68
+ @heap = heap
69
+ @json = json
70
+ end
71
+
72
+ def type_str
73
+ @json[/"type":"([A-Z]+)"/, 1]
74
+ end
75
+
76
+ def address
77
+ @json[/"address":"(0x[0-9a-f]+)"/, 1] || @json[/"root":"([a-z_]+)"/, 1]
78
+ end
79
+
80
+ def referenced_addrs
81
+ str = @json[/"references":\[([",0-9a-fx ]+)\]/, 1]
82
+ if str
83
+ str.tr('"', '').split(", ")
84
+ else
85
+ EMPTY_ARRAY
86
+ end
87
+ end
88
+
89
+ def references
90
+ referenced_addrs.map do |addr|
91
+ @heap.at(addr)
92
+ end
93
+ end
94
+
95
+ def inverse_references
96
+ @heap.inverse_references[address] || EMPTY_ARRAY
97
+ end
98
+
99
+ def data
100
+ JSON.parse(@json)
101
+ end
102
+
103
+ def memsize
104
+ @json[/"memsize":(\d+),/, 1].to_i
105
+ end
106
+
107
+ def class_addr
108
+ @json[/"class":"(0x[0-9a-f]+)"/, 1]
109
+ end
110
+
111
+ def imemo_type
112
+ @json[/"imemo_type":"([a-z]+)"/, 1]
113
+ end
114
+
115
+ def struct
116
+ @json[/"struct":"([a-zA-Z]+)"/, 1]
117
+ end
118
+
119
+ def wb_protected?
120
+ @json.include?('"wb_protected":true')
121
+ end
122
+
123
+ def name
124
+ data["name"]
125
+ end
126
+
127
+ def klass
128
+ @heap.at(class_addr)
129
+ end
130
+
131
+ def instances
132
+ raise unless type_str == "CLASS"
133
+ heap.instances_of(self)
134
+ end
135
+
136
+ def inspect
137
+ type_str = self.type_str
138
+ s = +"<#{type_str} #{address}"
139
+
140
+ case type_str
141
+ when "CLASS"
142
+ s << " " << (name || "(anonymous)")
143
+ when "MODULE"
144
+ s << " " << (name || "(anonymous)")
145
+ when "STRING"
146
+ s << " " << data["value"].inspect
147
+ when "IMEMO"
148
+ s << " " << (imemo_type || "unknown")
149
+ when "OBJECT"
150
+ s << " " << (klass.name || "(#{klass.address})")
151
+ when "DATA"
152
+ s << " " << struct.to_s
153
+ end
154
+
155
+ refs = referenced_addrs
156
+ unless refs.empty?
157
+ s << " (#{referenced_addrs.size} refs)"
158
+ end
159
+
160
+ s << ">"
161
+ end
162
+
163
+ def value
164
+ data["value"]
165
+ end
166
+
167
+ def file
168
+ data["file"]
169
+ end
170
+
171
+ def line
172
+ data["line"]
173
+ end
174
+
175
+ def location
176
+ f = file
177
+ "#{f}:#{line}" if f
178
+ end
179
+
180
+ def frozen?
181
+ @json.include?('"frozen":true')
182
+ end
183
+
184
+ def eql?(other)
185
+ if @heap.equal?(other.heap)
186
+ @json == other.json
187
+ else
188
+ address == other.address &&
189
+ type_str == other.type_str
190
+ end
191
+ end
192
+ alias_method :==, :eql?
193
+
194
+ def references_address?(addr)
195
+ @json.include?(addr) && referenced_addrs.include?(addr)
196
+ end
197
+
198
+ def hash
199
+ address.hash
200
+ end
201
+ end
202
+
203
+ class Heap
204
+ attr_reader :filename
205
+
206
+ def initialize(filename)
207
+ @filename = filename
208
+ end
209
+
210
+ def each_object
211
+ return enum_for(__method__) unless block_given?
212
+
213
+ File.open(filename) do |file|
214
+ file.each_line do |json|
215
+ yield HeapObject.new(self, json)
216
+ end
217
+ end
218
+ end
219
+
220
+ def objects
221
+ @objects ||= each_object.to_a
222
+ end
223
+
224
+ def objects_by_addr
225
+ @objects_by_addr ||=
226
+ begin
227
+ hash = {}
228
+ objects.each do |obj|
229
+ hash[obj.address] = obj
230
+ end
231
+ hash.freeze
232
+ end
233
+ end
234
+
235
+ def inverse_references
236
+ @inverse_references ||=
237
+ begin
238
+ hash = {}
239
+ objects.each do |obj|
240
+ obj.referenced_addrs.uniq.each do |addr|
241
+ next if addr == obj.address
242
+ hash[addr] ||= []
243
+ hash[addr] << obj
244
+ end
245
+ end
246
+ hash.freeze
247
+ end
248
+ end
249
+
250
+ def at(addr)
251
+ objects_by_addr[addr]
252
+ end
253
+
254
+ def class_named(name)
255
+ objects.select do |obj|
256
+ obj.json.include?(name) &&
257
+ obj.type_str == "CLASS" &&
258
+ obj.name == name
259
+ end
260
+ end
261
+
262
+ def instances_of(klass)
263
+ addr = klass.address
264
+ objects.select do |obj|
265
+ obj.json.include?(addr) &&
266
+ obj.class_addr == addr
267
+ end
268
+ end
269
+
270
+ def of_type(type)
271
+ type = type.to_s.upcase
272
+ objects.select { |o| o.type_str == type }
273
+ end
274
+
275
+ def inspect
276
+ "#<#{self.class} (#{objects.size} objects)>"
277
+ end
278
+ end
279
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sheap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Hawthorn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A set of helpers for analyzing the output of ObjectSpace.dump_all
14
+ email:
15
+ - john@hawthorn.email
16
+ executables:
17
+ - sheap
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CODE_OF_CONDUCT.md
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - exe/sheap
27
+ - lib/sheap.rb
28
+ - lib/sheap/version.rb
29
+ homepage: https://github.com/jhawthorn/sheap
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/jhawthorn/sheap
34
+ source_code_uri: https://github.com/jhawthorn/sheap
35
+ changelog_uri: https://github.com/jhawthorn/sheap
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.6.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.4.10
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: A helpers for heap dumps
55
+ test_files: []