Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 8 additions & 13 deletions src/gc-alloc-profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ struct PerThreadAllocProfile {
vector<RawAlloc> allocs;
unordered_map<size_t, size_t> type_address_by_value_address;
unordered_map<size_t, size_t> frees_by_type_address;

size_t alloc_counter;
size_t last_recorded_alloc;
};

struct AllocProfile {
int skip_every;
double sample_rate;

vector<PerThreadAllocProfile> per_thread_profiles;
};
Expand Down Expand Up @@ -69,8 +66,8 @@ RawBacktrace get_raw_backtrace() {

// == exported interface ==

JL_DLLEXPORT void jl_start_alloc_profile(int skip_every) {
g_alloc_profile = AllocProfile{skip_every};
JL_DLLEXPORT void jl_start_alloc_profile(double sample_rate) {
g_alloc_profile = AllocProfile{sample_rate};

for (int i = 0; i < jl_n_threads; i++) {
g_alloc_profile.per_thread_profiles.push_back(PerThreadAllocProfile{});
Expand Down Expand Up @@ -131,20 +128,18 @@ JL_DLLEXPORT void jl_free_alloc_profile() {

void _record_allocated_value(jl_value_t *val, size_t size) JL_NOTSAFEPOINT {
auto& global_profile = g_alloc_profile;

auto& profile = global_profile.per_thread_profiles[jl_threadid()];

profile.alloc_counter++;
auto diff = profile.alloc_counter - profile.last_recorded_alloc;
if (diff < g_alloc_profile.skip_every) {
auto sample_val = double(rand()) / double(RAND_MAX);
auto should_record = sample_val <= global_profile.sample_rate;
if (!should_record) {
return;
}
profile.last_recorded_alloc = profile.alloc_counter;

auto type = (jl_datatype_t*)jl_typeof(val);

// Used when counting frees. We can't get type type info then,
// because it gets corrupted during garbage collection.
profile.type_address_by_value_address[(size_t)val] = (size_t)type;

profile.allocs.emplace_back(RawAlloc{
type,
get_raw_backtrace(),
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 @@ -23,7 +23,7 @@ struct RawAllocResults {
size_t num_frees;
};

JL_DLLEXPORT void jl_start_alloc_profile(int skip_every);
JL_DLLEXPORT void jl_start_alloc_profile(double sample_rate);
JL_DLLEXPORT struct RawAllocResults jl_fetch_alloc_profile(void);
JL_DLLEXPORT void jl_stop_alloc_profile(void);
JL_DLLEXPORT void jl_free_alloc_profile(void);
Expand Down
6 changes: 3 additions & 3 deletions stdlib/Profile/src/Allocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ macro profile(opts, ex)
_prof_expr(ex, opts)
end
macro profile(ex)
_prof_expr(ex, :(skip_every=1000))
_prof_expr(ex, :(sample_rate=0.0001))
end

function _prof_expr(expr, opts)
Expand All @@ -54,8 +54,8 @@ function _prof_expr(expr, opts)
end
end

function start(; skip_every::Int)
ccall(:jl_start_alloc_profile, Cvoid, (Cint,), skip_every)
function start(; sample_rate::Float64)
ccall(:jl_start_alloc_profile, Cvoid, (Cdouble,), sample_rate)
end

function stop()
Expand Down