Skip to content

Commit 956e0a3

Browse files
apaz-cliwhatsthecraicNHDalyvilterpvchuravy
authored
[Profile] Add Heap Snapshot tool (#46862)
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 <[email protected]> Co-Authored-By: Nathan Daly <[email protected]> Co-Authored-By: Pete Vilter <[email protected]> Co-Authored-By: Valentin Churavy <[email protected]> Co-Authored-By: Jameson Nash <[email protected]>
1 parent fbd5a72 commit 956e0a3

File tree

10 files changed

+751
-35
lines changed

10 files changed

+751
-35
lines changed

src/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ SRCS := \
4545
dlload sys init task array dump staticdata toplevel jl_uv datatype \
4646
simplevector runtime_intrinsics precompile jloptions \
4747
threading partr stackwalk gc gc-debug gc-pages gc-stacks gc-alloc-profiler method \
48-
jlapi signal-handling safepoint timing subtype rtutils \
48+
jlapi signal-handling safepoint timing subtype rtutils gc-heap-snapshot \
4949
crc32c APInt-C processor ircode opaque_closure codegen-stubs coverage runtime_ccall
5050

5151
RT_LLVMLINK :=
@@ -293,7 +293,9 @@ $(BUILDDIR)/disasm.o $(BUILDDIR)/disasm.dbg.obj: $(SRCDIR)/debuginfo.h $(SRCDIR)
293293
$(BUILDDIR)/dump.o $(BUILDDIR)/dump.dbg.obj: $(addprefix $(SRCDIR)/,common_symbols1.inc common_symbols2.inc builtin_proto.h serialize.h)
294294
$(BUILDDIR)/gc-debug.o $(BUILDDIR)/gc-debug.dbg.obj: $(SRCDIR)/gc.h
295295
$(BUILDDIR)/gc-pages.o $(BUILDDIR)/gc-pages.dbg.obj: $(SRCDIR)/gc.h
296-
$(BUILDDIR)/gc.o $(BUILDDIR)/gc.dbg.obj: $(SRCDIR)/gc.h $(SRCDIR)/gc-alloc-profiler.h
296+
$(BUILDDIR)/gc.o $(BUILDDIR)/gc.dbg.obj: $(SRCDIR)/gc.h $(SRCDIR)/gc-heap-snapshot.h $(SRCDIR)/gc-alloc-profiler.h
297+
$(BUILDDIR)/gc-heap-snapshot.o $(BUILDDIR)/gc-heap-snapshot.dbg.obj: $(SRCDIR)/gc.h $(SRCDIR)/gc-heap-snapshot.h
298+
$(BUILDDIR)/gc-alloc-profiler.o $(BUILDDIR)/gc-alloc-profiler.dbg.obj: $(SRCDIR)/gc.h $(SRCDIR)/gc-alloc-profiler.h
297299
$(BUILDDIR)/init.o $(BUILDDIR)/init.dbg.obj: $(SRCDIR)/builtin_proto.h
298300
$(BUILDDIR)/interpreter.o $(BUILDDIR)/interpreter.dbg.obj: $(SRCDIR)/builtin_proto.h
299301
$(BUILDDIR)/jitlayers.o $(BUILDDIR)/jitlayers.dbg.obj: $(SRCDIR)/jitlayers.h $(SRCDIR)/codegen_shared.h $(SRCDIR)/debug-registry.h

src/gc-debug.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,20 +1227,17 @@ void gc_count_pool(void)
12271227
jl_safe_printf("************************\n");
12281228
}
12291229

1230-
int gc_slot_to_fieldidx(void *obj, void *slot)
1230+
int gc_slot_to_fieldidx(void *obj, void *slot, jl_datatype_t *vt) JL_NOTSAFEPOINT
12311231
{
1232-
jl_datatype_t *vt = (jl_datatype_t*)jl_typeof(obj);
12331232
int nf = (int)jl_datatype_nfields(vt);
1234-
for (int i = 0; i < nf; i++) {
1235-
void *fieldaddr = (char*)obj + jl_field_offset(vt, i);
1236-
if (fieldaddr >= slot) {
1237-
return i;
1238-
}
1233+
for (int i = 1; i < nf; i++) {
1234+
if (slot < (void*)((char*)obj + jl_field_offset(vt, i)))
1235+
return i - 1;
12391236
}
1240-
return -1;
1237+
return nf - 1;
12411238
}
12421239

1243-
int gc_slot_to_arrayidx(void *obj, void *_slot)
1240+
int gc_slot_to_arrayidx(void *obj, void *_slot) JL_NOTSAFEPOINT
12441241
{
12451242
char *slot = (char*)_slot;
12461243
jl_datatype_t *vt = (jl_datatype_t*)jl_typeof(obj);
@@ -1258,8 +1255,6 @@ int gc_slot_to_arrayidx(void *obj, void *_slot)
12581255
}
12591256
else if (vt->name == jl_array_typename) {
12601257
jl_array_t *a = (jl_array_t*)obj;
1261-
if (!a->flags.ptrarray)
1262-
return -1;
12631258
start = (char*)a->data;
12641259
len = jl_array_len(a);
12651260
elsize = a->elsize;

0 commit comments

Comments
 (0)