Skip to content

Commit

Permalink
free memory allocated for results
Browse files Browse the repository at this point in the history
  • Loading branch information
vilterp committed Dec 18, 2021
1 parent 92a769d commit 1474097
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/gc-alloc-profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct AllocProfile {
// == global variables manipulated by callbacks ==

AllocProfile g_alloc_profile;
RawAllocResults *g_alloc_profile_results = nullptr;
int g_alloc_profile_enabled = false;

// === stack stuff ===
Expand All @@ -63,25 +64,27 @@ 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;

auto results = RawAllocResults{
auto results = new RawAllocResults{
g_alloc_profile.allocs.data(),
g_alloc_profile.allocs.size()
};

// package up frees
results.num_frees = g_alloc_profile.frees_by_type_address.size();
results.frees = (FreeInfo*) malloc(sizeof(FreeInfo) * results.num_frees);
results->num_frees = g_alloc_profile.frees_by_type_address.size();
results->frees = (FreeInfo*) malloc(sizeof(FreeInfo) * results->num_frees);
int j = 0;
for (auto type_addr_free_count : g_alloc_profile.frees_by_type_address) {
results.frees[j++] = FreeInfo{
results->frees[j++] = FreeInfo{
type_addr_free_count.first,
type_addr_free_count.second
};
}

g_alloc_profile_results = results;

return results;
}

Expand All @@ -93,6 +96,12 @@ JL_DLLEXPORT void jl_free_alloc_profile() {
free(alloc.backtrace.data);
}
g_alloc_profile.allocs.clear();

if (g_alloc_profile_results != nullptr) {
free(g_alloc_profile_results->frees);
// free the results?
g_alloc_profile_results = nullptr;
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/gc-alloc-profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void _report_gc_finished(
uint64_t pause, uint64_t freed, uint64_t allocd, int full, int recollect
) JL_NOTSAFEPOINT;
JL_DLLEXPORT void jl_start_alloc_profile(int skip_every);
JL_DLLEXPORT struct RawAllocResults jl_stop_alloc_profile(void);
JL_DLLEXPORT struct RawAllocResults *jl_stop_alloc_profile(void);
JL_DLLEXPORT void jl_free_alloc_profile(void);

void _record_allocated_value(jl_value_t *val, size_t size) JL_NOTSAFEPOINT;
Expand Down
3 changes: 2 additions & 1 deletion stdlib/AllocProfile/src/AllocProfile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function start(skip_every::Int=0)
end

function stop()
raw_results = ccall(:jl_stop_alloc_profile, RawAllocResults, ())
raw_results_ptr = ccall(:jl_stop_alloc_profile, Ptr{RawAllocResults}, ())
raw_results = unsafe_load(raw_results_ptr)
decoded_results = decode(raw_results)
return decoded_results
end
Expand Down
1 change: 1 addition & 0 deletions stdlib/AllocProfile/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using AllocProfile
using Base64

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

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

0 comments on commit 1474097

Please sign in to comment.