Skip to content

Commit

Permalink
[Profile] Add Heap Snapshot tool (#46862)
Browse files Browse the repository at this point in the history
We expose a function `Profile.take_heap_snapshot(file)`, which writes a
heap snapshot in Chrome's .heapsnapshot JSON format to the given IO
stream.

This can be loaded into Chrome Devtools' snapshot viewer to explore the
heap and find memory leaks.

Co-Authored-By: Dean De Leo <dean.deleo@relational.ai>
Co-Authored-By: Nathan Daly <NHDaly@gmail.com>
Co-Authored-By: Pete Vilter <7341+vilterp@users.noreply.github.com>
Co-Authored-By: Valentin Churavy <v.churavy@gmail.com>
Co-Authored-By: Jameson Nash <vtjnash@gmail.com>
  • Loading branch information
6 people committed Oct 5, 2022
1 parent fbd5a72 commit 956e0a3
Show file tree
Hide file tree
Showing 10 changed files with 751 additions and 35 deletions.
6 changes: 4 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ SRCS := \
dlload sys init task array dump staticdata toplevel jl_uv datatype \
simplevector runtime_intrinsics precompile jloptions \
threading partr stackwalk gc gc-debug gc-pages gc-stacks gc-alloc-profiler method \
jlapi signal-handling safepoint timing subtype rtutils \
jlapi signal-handling safepoint timing subtype rtutils gc-heap-snapshot \
crc32c APInt-C processor ircode opaque_closure codegen-stubs coverage runtime_ccall

RT_LLVMLINK :=
Expand Down Expand Up @@ -293,7 +293,9 @@ $(BUILDDIR)/disasm.o $(BUILDDIR)/disasm.dbg.obj: $(SRCDIR)/debuginfo.h $(SRCDIR)
$(BUILDDIR)/dump.o $(BUILDDIR)/dump.dbg.obj: $(addprefix $(SRCDIR)/,common_symbols1.inc common_symbols2.inc builtin_proto.h serialize.h)
$(BUILDDIR)/gc-debug.o $(BUILDDIR)/gc-debug.dbg.obj: $(SRCDIR)/gc.h
$(BUILDDIR)/gc-pages.o $(BUILDDIR)/gc-pages.dbg.obj: $(SRCDIR)/gc.h
$(BUILDDIR)/gc.o $(BUILDDIR)/gc.dbg.obj: $(SRCDIR)/gc.h $(SRCDIR)/gc-alloc-profiler.h
$(BUILDDIR)/gc.o $(BUILDDIR)/gc.dbg.obj: $(SRCDIR)/gc.h $(SRCDIR)/gc-heap-snapshot.h $(SRCDIR)/gc-alloc-profiler.h
$(BUILDDIR)/gc-heap-snapshot.o $(BUILDDIR)/gc-heap-snapshot.dbg.obj: $(SRCDIR)/gc.h $(SRCDIR)/gc-heap-snapshot.h
$(BUILDDIR)/gc-alloc-profiler.o $(BUILDDIR)/gc-alloc-profiler.dbg.obj: $(SRCDIR)/gc.h $(SRCDIR)/gc-alloc-profiler.h
$(BUILDDIR)/init.o $(BUILDDIR)/init.dbg.obj: $(SRCDIR)/builtin_proto.h
$(BUILDDIR)/interpreter.o $(BUILDDIR)/interpreter.dbg.obj: $(SRCDIR)/builtin_proto.h
$(BUILDDIR)/jitlayers.o $(BUILDDIR)/jitlayers.dbg.obj: $(SRCDIR)/jitlayers.h $(SRCDIR)/codegen_shared.h $(SRCDIR)/debug-registry.h
Expand Down
17 changes: 6 additions & 11 deletions src/gc-debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -1227,20 +1227,17 @@ void gc_count_pool(void)
jl_safe_printf("************************\n");
}

int gc_slot_to_fieldidx(void *obj, void *slot)
int gc_slot_to_fieldidx(void *obj, void *slot, jl_datatype_t *vt) JL_NOTSAFEPOINT
{
jl_datatype_t *vt = (jl_datatype_t*)jl_typeof(obj);
int nf = (int)jl_datatype_nfields(vt);
for (int i = 0; i < nf; i++) {
void *fieldaddr = (char*)obj + jl_field_offset(vt, i);
if (fieldaddr >= slot) {
return i;
}
for (int i = 1; i < nf; i++) {
if (slot < (void*)((char*)obj + jl_field_offset(vt, i)))
return i - 1;
}
return -1;
return nf - 1;
}

int gc_slot_to_arrayidx(void *obj, void *_slot)
int gc_slot_to_arrayidx(void *obj, void *_slot) JL_NOTSAFEPOINT
{
char *slot = (char*)_slot;
jl_datatype_t *vt = (jl_datatype_t*)jl_typeof(obj);
Expand All @@ -1258,8 +1255,6 @@ int gc_slot_to_arrayidx(void *obj, void *_slot)
}
else if (vt->name == jl_array_typename) {
jl_array_t *a = (jl_array_t*)obj;
if (!a->flags.ptrarray)
return -1;
start = (char*)a->data;
len = jl_array_len(a);
elsize = a->elsize;
Expand Down
Loading

0 comments on commit 956e0a3

Please sign in to comment.