Skip to content

Commit 4328436

Browse files
committed
PR feedback
Co-Authored-By: [email protected]
1 parent 686d021 commit 4328436

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

stdlib/Profile/src/Allocs.jl

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ A sample rate of 1.0 will record everything; 0.0 will record nothing.
3939
julia> Profile.Allocs.@profile sample_rate=0.01 peakflops()
4040
1.03733270279065e11
4141
42-
julia> results = Profile.Allocs.fetch();
42+
julia> results = Profile.Allocs.fetch()
4343
4444
julia> last(sort(results.allocs, by=x->x.size))
4545
Profile.Allocs.Alloc(Vector{Any}, Base.StackTraces.StackFrame[_new_array_ at array.c:127, ...], 5576)
@@ -54,10 +54,9 @@ end
5454

5555
# globals used for tracking how many allocs we're missing
5656
# vs the alloc counters used by @time
57-
const g_gc_num = Ref{Base.GC_Num}()
58-
const g_total_allocs = Ref{Int}(0)
59-
const g_sample_rate = Ref{Real}(0)
60-
const g_estimated_total_allocs = Ref{Int}(0)
57+
const g_gc_num_before = Ref{Base.GC_Num}()
58+
const g_sample_rate = Ref{Real}()
59+
const g_expected_sampled_allocs = Ref{Float64}(0)
6160

6261
function _prof_expr(expr, opts)
6362
quote
@@ -78,7 +77,7 @@ function start(; sample_rate::Real)
7877
ccall(:jl_start_alloc_profile, Cvoid, (Cdouble,), Float64(sample_rate))
7978

8079
g_sample_rate[] = sample_rate
81-
g_gc_num[] = Base.gc_num()
80+
g_gc_num_before[] = Base.gc_num()
8281
end
8382

8483
"""
@@ -89,17 +88,14 @@ Stop recording allocations.
8988
function stop()
9089
ccall(:jl_stop_alloc_profile, Cvoid, ())
9190

91+
# increment a counter of how many allocs we would expect
92+
# the memory profiler to see, based on how many allocs
93+
# actually happened.
9294
gc_num_after = Base.gc_num()
93-
gc_diff = Base.GC_Diff(gc_num_after, g_gc_num[])
95+
gc_diff = Base.GC_Diff(gc_num_after, g_gc_num_before[])
9496
alloc_count = Base.gc_alloc_count(gc_diff)
95-
g_total_allocs[] = g_total_allocs[] + alloc_count
96-
97-
# TODO: ugh, fetch has side effects
98-
raw_results = ccall(:jl_fetch_alloc_profile, RawAllocResults, ())
99-
num_sampled = raw_results.num_allocs
100-
estimated_total = round(Int, Float64(num_sampled) / g_sample_rate[])
101-
102-
g_estimated_total_allocs[] = g_estimated_total_allocs[] + estimated_total
97+
expected_samples = alloc_count * g_sample_rate[]
98+
g_expected_sampled_allocs[] += expected_samples
10399
end
104100

105101
"""
@@ -110,8 +106,7 @@ Clear all previously profiled allocation information from memory.
110106
function clear()
111107
ccall(:jl_free_alloc_profile, Cvoid, ())
112108

113-
g_estimated_total_allocs[] = 0
114-
g_total_allocs[] = 0
109+
g_expected_sampled_allocs[] = 0
115110
end
116111

117112
"""
@@ -124,14 +119,15 @@ function fetch()
124119
raw_results = ccall(:jl_fetch_alloc_profile, RawAllocResults, ())
125120
decoded_results = decode(raw_results)
126121

127-
println("total: $(g_total_allocs[])")
128-
println("estimated total: $(g_estimated_total_allocs[])")
129-
130-
missed_allocs = g_total_allocs[] - g_estimated_total_allocs[]
131-
missed_percentage = round(Int, Float64(missed_allocs) / Float64(g_total_allocs[]) * 100)
122+
@show(length(decoded_results.allocs))
123+
@show(g_expected_sampled_allocs[])
132124

133-
@warn("This allocation profiler missed $(missed_percentage)% of allocs in the last run. " *
134-
"For more info see https://github.com/JuliaLang/julia/issues/43688")
125+
missed_allocs = g_expected_sampled_allocs[] - length(decoded_results.allocs)
126+
missed_percentage = round(Int, missed_allocs / g_expected_sampled_allocs[] * 100)
127+
@warn("The allocation profiler is not fully implemented, and missed $(missed_percentage)% " *
128+
"($(round(Int, missed_allocs)) / $(round(Int, g_expected_sampled_allocs[]))) " *
129+
"of allocs in the last run. " *
130+
"For more info see https://github.com/JuliaLang/julia/issues/43688")
135131
return decoded_results
136132
end
137133

0 commit comments

Comments
 (0)