Skip to content

Commit

Permalink
Move type to param of _maybe_record_alloc_to_profile
Browse files Browse the repository at this point in the history
Make an UnknownType for the GC Profiler to handle these unknown types.
  • Loading branch information
NHDaly committed Jan 18, 2022
1 parent 5c6ae25 commit 6bed933
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ JL_DLLEXPORT jl_value_t *jl_alloc_string(size_t len)
s = jl_gc_big_alloc(ptls, allocsz);
}
jl_set_typeof(s, jl_string_type);
maybe_record_alloc_to_profile(s, len);
maybe_record_alloc_to_profile(s, len, jl_string_type);
*(size_t*)s = len;
jl_string_data(s)[len] = 0;
return s;
Expand Down
3 changes: 1 addition & 2 deletions src/gc-alloc-profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ JL_DLLEXPORT void jl_free_alloc_profile() {

// == callback called into by the outside ==

void _maybe_record_alloc_to_profile(jl_value_t *val, size_t size) JL_NOTSAFEPOINT {
void _maybe_record_alloc_to_profile(jl_value_t *val, size_t size, jl_datatype_t *type) JL_NOTSAFEPOINT {
auto& global_profile = g_alloc_profile;
auto& profile = global_profile.per_thread_profiles[jl_threadid()];

Expand All @@ -128,7 +128,6 @@ void _maybe_record_alloc_to_profile(jl_value_t *val, size_t size) JL_NOTSAFEPOIN
return;
}

auto type = (jl_datatype_t*)jl_typeof(val);
profile.allocs.emplace_back(jl_raw_alloc_t{
type,
get_raw_backtrace(),
Expand Down
8 changes: 5 additions & 3 deletions src/gc-alloc-profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ JL_DLLEXPORT void jl_free_alloc_profile(void);
// Functions to call from GC when alloc profiling is enabled
// ---------------------------------------------------------------------

void _maybe_record_alloc_to_profile(jl_value_t *val, size_t size) JL_NOTSAFEPOINT;
void _maybe_record_alloc_to_profile(jl_value_t *val, size_t size, jl_datatype_t *typ) JL_NOTSAFEPOINT;

extern int g_alloc_profile_enabled;

static inline void maybe_record_alloc_to_profile(jl_value_t *val, size_t size) JL_NOTSAFEPOINT {
#define jl_gc_unknown_type_tag ((uintptr_t)0xdeadaa03)

static inline void maybe_record_alloc_to_profile(jl_value_t *val, size_t size, jl_datatype_t *typ) JL_NOTSAFEPOINT {
if (__unlikely(g_alloc_profile_enabled)) {
_maybe_record_alloc_to_profile(val, size);
_maybe_record_alloc_to_profile(val, size, typ);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1201,10 +1201,8 @@ JL_DLLEXPORT jl_value_t *jl_gc_pool_alloc_outer(jl_ptls_t ptls, int pool_offset,
int osize)
{
jl_value_t *val = jl_gc_pool_alloc(ptls, pool_offset, osize);
// zero out the type tag so it's not garbage
jl_set_typeof(val, 0);

maybe_record_alloc_to_profile(val, osize);
maybe_record_alloc_to_profile(val, osize, jl_gc_unknown_type_tag);
return val;
}

Expand Down
2 changes: 1 addition & 1 deletion src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ STATIC_INLINE jl_value_t *jl_gc_alloc_(jl_ptls_t ptls, size_t sz, void *ty)
}
jl_set_typeof(v, ty);

maybe_record_alloc_to_profile(v, sz);
maybe_record_alloc_to_profile(v, sz, (jl_datatype_t*)ty);

return v;
}
Expand Down
4 changes: 4 additions & 0 deletions stdlib/Profile/src/Allocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,19 @@ const BacktraceCache = Dict{BTElement,Vector{StackFrame}}

# copied from julia_internal.h
const JL_BUFF_TAG = UInt(0x4eadc000)
const JL_GC_UNKNOWN_TYPE_TAG = UInt(0xdeadaa03)

struct CorruptType end
struct BufferType end
struct UnknownType end

function load_type(ptr::Ptr{Type})
if UInt(ptr) < UInt(4096)
return CorruptType
elseif UInt(ptr) == JL_BUFF_TAG
return BufferType
elseif UInt(ptr) == JL_GC_UNKNOWN_TYPE_TAG
return UnknownType
end
return unsafe_pointer_to_objref(ptr)
end
Expand Down

0 comments on commit 6bed933

Please sign in to comment.