Skip to content
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

make the alloc profiler multithreaded #5

Merged
merged 4 commits into from
Dec 20, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
hold onto vectors
d'oh
vilterp committed Dec 20, 2021
commit b949295f21ca490459c40a31efd85d321ccfeaed
36 changes: 17 additions & 19 deletions src/gc-alloc-profiler.cpp
Original file line number Diff line number Diff line change
@@ -39,11 +39,16 @@ struct AllocProfile {
vector<PerThreadAllocProfile> per_thread_profiles;
};

struct CombinedResults {
vector<RawAlloc> combined_allocs;
vector<FreeInfo> combined_frees;
};

// == global variables manipulated by callbacks ==

AllocProfile g_alloc_profile;
RawAllocResults *g_alloc_profile_results = nullptr;
int g_alloc_profile_enabled = false;
CombinedResults g_combined_results; // will live forever

// === stack stuff ===

@@ -73,38 +78,33 @@ JL_DLLEXPORT void jl_start_alloc_profile(int skip_every) {

extern "C" { // Needed since the function doesn't take any arguments.

JL_DLLEXPORT struct RawAllocResults* jl_stop_alloc_profile() {
JL_DLLEXPORT struct RawAllocResults jl_stop_alloc_profile() {
g_alloc_profile_enabled = false;

// combine allocs
vector<RawAlloc> combined_allocs;
for (auto profile : g_alloc_profile.per_thread_profiles) {
for (auto alloc : profile.allocs) {
combined_allocs.push_back(alloc);
// TODO: interleave to preserve ordering
for (const auto& profile : g_alloc_profile.per_thread_profiles) {
for (const auto& alloc : profile.allocs) {
g_combined_results.combined_allocs.push_back(alloc);
}
}

// package up frees
vector<FreeInfo> combined_frees;
for (auto profile : g_alloc_profile.per_thread_profiles) {
for (auto free_info : profile.frees_by_type_address) {
combined_frees.push_back(FreeInfo{
for (const auto& profile : g_alloc_profile.per_thread_profiles) {
for (const auto& free_info : profile.frees_by_type_address) {
g_combined_results.combined_frees.push_back(FreeInfo{
free_info.first,
free_info.second
});
}
}

auto results = new RawAllocResults{
return RawAllocResults{
combined_allocs.data(),
combined_allocs.size(),
combined_frees.data(),
combined_frees.size()
};

g_alloc_profile_results = results;

return results;
}

JL_DLLEXPORT void jl_free_alloc_profile() {
@@ -115,10 +115,8 @@ JL_DLLEXPORT void jl_free_alloc_profile() {
}
g_alloc_profile.per_thread_profiles.clear();

if (g_alloc_profile_results != nullptr) {
// TODO: does this call its destructors?
g_alloc_profile_results = nullptr;
}
g_combined_results.combined_allocs.clear();
g_combined_results.combined_frees.clear();
}

}
15 changes: 10 additions & 5 deletions stdlib/AllocProfile/src/AllocProfile.jl
Original file line number Diff line number Diff line change
@@ -18,11 +18,6 @@ struct RawAlloc
size::Csize_t
end

struct TypeNamePair
addr::Csize_t
name::Ptr{UInt8}
end

struct FreeInfo
type::Ptr{Type}
count::UInt
@@ -124,11 +119,21 @@ function decode(raw_results::RawAllocResults)::AllocResults
)
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

return out
end

2 changes: 1 addition & 1 deletion stdlib/AllocProfile/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ using AllocProfile
using Base64

results = AllocProfile.stop()
AllocProfile.clear()
# AllocProfile.clear()

@test length(results.allocs) > 0
first_alloc = results.allocs[1]