-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add PerfTools::DumpHeap
#30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HertzDevil
wants to merge
5
commits into
main
Choose a base branch
from
feature/dump-heap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−7
Open
Changes from 4 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| {% if flag?(:gc_none) || flag?(:wasm32) %} | ||
| require "./gc/none" | ||
| {% else %} | ||
| require "./gc/boehm" | ||
| {% end %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| lib LibGC | ||
| GC_I_PTRFREE = 0 | ||
| GC_I_NORMAL = 1 | ||
|
|
||
| fun get_kind_and_size = GC_get_kind_and_size(p : Void*, psize : SizeT*) : Int | ||
|
|
||
| alias ReachableObjectFunc = Void*, SizeT, Void* -> Void | ||
|
|
||
| fun enumerate_reachable_objects_inner = GC_enumerate_reachable_objects_inner(proc : ReachableObjectFunc, client_data : Void*) | ||
|
|
||
| fun register_disclaim_proc = GC_register_disclaim_proc(kind : Int, proc : Void* -> Int, mark_from_all : Int) | ||
| end | ||
|
|
||
| module GC | ||
| # Returns whether *ptr* is a pointer to the base of an atomic allocation. | ||
| def self.atomic?(ptr : Pointer) : Bool | ||
| LibGC.get_kind_and_size(ptr, nil) == LibGC::GC_I_PTRFREE | ||
| end | ||
|
|
||
| # Walks the entire GC heap, yielding each allocation's base address and size | ||
| # to the given *block*. | ||
| # | ||
| # The *block* must not allocate memory using the GC. | ||
| def self.each_reachable_object(&block : Void*, UInt64 ->) : Nil | ||
| Thread.stop_world | ||
| GC.lock_write | ||
| begin | ||
| LibGC.enumerate_reachable_objects_inner(LibGC::ReachableObjectFunc.new do |obj, bytes, client_data| | ||
| fn = client_data.as(typeof(pointerof(block))).value | ||
| fn.call(obj, bytes.to_u64!) | ||
| end, pointerof(block)) | ||
| ensure | ||
| GC.unlock_write | ||
| Thread.start_world | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module GC | ||
| # Returns whether *ptr* is a pointer to the base of an atomic allocation. | ||
| def self.atomic?(ptr : Pointer) : Bool | ||
| false | ||
| end | ||
|
|
||
| # Walks the entire GC heap, yielding each allocation's base address and size | ||
| # to the given *block*. | ||
| # | ||
| # The *block* must not allocate memory using the GC. | ||
| def self.each_reachable_object(&block : Void*, UInt64 ->) : Nil | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| require "./common" | ||
| require "../core_ext/gc" | ||
|
|
||
| # Functions to produce binary dumps of the running process's GC heap, useful for | ||
| # out-of-memory analysis. | ||
| # | ||
| # Methods in this module are independent from other tools like `MemProf`. | ||
| module PerfTools::DumpHeap | ||
| # Dumps a compact representation of the GC heap to the given *io*, sufficient | ||
| # to reconstruct all pointers between allocations. | ||
| # | ||
| # All writes to *io* must not allocate memory using the GC. For `IO::Buffered` | ||
| # this can be achieved by disabling write buffering (`io.sync = true`). | ||
| # | ||
| # The binary dump consists of a sequential list of allocation records. Each | ||
| # record contains the following fields, all 64-bit little-endian integers, | ||
| # unless otherwise noted: | ||
| # | ||
| # * The base address of the allocation. | ||
| # * The byte size of the allocation. This may be larger than the size | ||
| # originally passed to `GC.malloc` or a similar method, as the GC may | ||
| # reserve trailing padding bytes for alignment. Additionally, for an atomic | ||
| # allocation, the most significant bit of this field is set as well. | ||
| # * The pointer-sized word at the start of the allocation. If this allocation | ||
| # corresponds to an instance of a Crystal reference type, the lower bytes | ||
| # will contain that type's ID. | ||
| # * If the allocation is non-atomic, then for each inner pointer, the field | ||
| # offset relative to the allocation base and the pointer value are written; | ||
| # this list is terminated by a single `UInt64::MAX` field. Atomic records do | ||
| # not have this list. | ||
| # | ||
| # All the records are then terminated by a single `UInt64::MAX` field. | ||
| def self.graph(io : IO) : Nil | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: maybe call it |
||
| GC.collect | ||
|
|
||
| GC.each_reachable_object do |obj, bytes| | ||
| is_atomic = GC.atomic?(obj) | ||
| io.write_bytes(obj.address.to_u64!) | ||
| io.write_bytes(bytes.to_u64! | (is_atomic ? Int64::MIN : 0_i64)) | ||
|
|
||
| ptr = obj.as(Void**) | ||
| io.write_bytes(ptr.value.address.to_u64!) | ||
|
|
||
| unless is_atomic | ||
| b = ptr | ||
| e = (obj + bytes).as(Void**) | ||
| while ptr < e | ||
| inner = ptr.value | ||
| if GC.is_heap_ptr(inner) | ||
| io.write_bytes((ptr.address &- b.address).to_u64!) | ||
| io.write_bytes(inner.address.to_u64!) | ||
| end | ||
| ptr += 1 | ||
| end | ||
| io.write_bytes(UInt64::MAX) | ||
| end | ||
| end | ||
|
|
||
| io.write_bytes(UInt64::MAX) | ||
| end | ||
|
|
||
| # Dumps the contents of the GC heap to the given *io*. | ||
| # | ||
| # All writes to *io* must not allocate memory using the GC. For `IO::Buffered` | ||
| # this can be achieved by disabling write buffering (`io.sync = true`). | ||
| # | ||
| # The binary dump consists of a sequential list of allocation records. Each | ||
| # record contains the following fields, all 64-bit little-endian integers, | ||
| # unless otherwise noted: | ||
| # | ||
| # * The base address of the allocation. | ||
| # * The byte size of the allocation. This may be larger than the size | ||
| # originally passed to `GC.malloc` or a similar method, as the GC may | ||
| # reserve trailing padding bytes for alignment. Additionally, for an atomic | ||
| # allocation, the most significant bit of this field is set as well. | ||
| # * The full contents of the allocation. | ||
| # | ||
| # All the records are then terminated by a single `UInt64::MAX` field. | ||
| def self.full(io : IO) : Nil | ||
| GC.collect | ||
|
|
||
| GC.each_reachable_object do |obj, bytes| | ||
| io.write_bytes(obj.address.to_u64!) | ||
| io.write_bytes(bytes.to_u64! | (GC.atomic?(obj) ? Int64::MIN : 0_i64)) | ||
| io.write(obj.as(UInt8*).to_slice(bytes)) # TODO: 32-bit overflow? | ||
| end | ||
|
|
||
| io.write_bytes(UInt64::MAX) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.