Skip to content

Commit

Permalink
Merge pull request #6 from vilterp/nhd-alloc-profile-performance
Browse files Browse the repository at this point in the history
Alloc profile performance improvements
  • Loading branch information
NHDaly authored Dec 21, 2021
2 parents 3718e2e + cae4480 commit aa71d23
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/gc-alloc-profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ CombinedResults g_combined_results; // will live forever
// === stack stuff ===

RawBacktrace get_raw_backtrace() {
jl_bt_element_t *bt_data = (jl_bt_element_t*) malloc(JL_MAX_BT_SIZE);
static jl_bt_element_t static_bt_data[JL_MAX_BT_SIZE];

// TODO: tune the number of frames that are skipped
size_t bt_size = rec_backtrace(bt_data, JL_MAX_BT_SIZE, 1);
size_t bt_size = rec_backtrace(static_bt_data, JL_MAX_BT_SIZE, 1);

size_t bt_bytes = bt_size * sizeof(jl_bt_element_t);
jl_bt_element_t *bt_data = (jl_bt_element_t*) malloc(bt_bytes);
memcpy(bt_data, static_bt_data, bt_bytes);

return RawBacktrace{
bt_data,
Expand Down
27 changes: 17 additions & 10 deletions stdlib/AllocProfile/src/AllocProfile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,36 +100,29 @@ end

function decode(raw_results::RawAllocResults)::AllocResults
cache = BacktraceCache()
@info "ALLOCS"
allocs = [
decode_alloc(cache, unsafe_load(raw_results.allocs, i))
for i in 1:raw_results.num_allocs
]

@info "FREES"
frees = Dict{Type,UInt}()
for i in 1:raw_results.num_frees
free = unsafe_load(raw_results.frees, i)
type = load_type(free.type)
frees[type] = free.count
end

return AllocResults(
allocs,
frees
)
end

const f = Ref{IOStream}()

function __init__()
f[] = open("debug.log", "w")
end

function load_backtrace(trace::RawBacktrace)::Vector{Ptr{Cvoid}}
println(f[], "load_backtrace: trace.data: $(trace.data)")
println(f[], "load_backtrace: trace.size: $(trace.size)")
out = Vector{Ptr{Cvoid}}()
for i in 1:trace.size
println(f[], " $i")
push!(out, unsafe_load(trace.data, i))
end

Expand Down Expand Up @@ -158,4 +151,18 @@ function stacktrace_memoized(
return stack
end

# Precompile once for the package cache,
precompile(start, ())
precompile(stop, ())

function __init__()
# And once when loading the package, to get the full machine code precompiled.
# TOOD: Although actually, we probably don't need this since this package will be
# precompiled into the sysimg, so the top-level statements will be enough to get the
# machine code codegen precompiled as well. :)
# We can delete this function once we make this package a stdlib.
precompile(start, ())
precompile(stop, ())
end

end

0 comments on commit aa71d23

Please sign in to comment.