Skip to content

Commit 6e0bac9

Browse files
Add check for buffer size of Profile.init on 32-bit systems, and fix bug (#41906)
* fix bug multiplying by nthreads twice * limit buffer to 512MB on 32-bit. Add info to error
1 parent 5a39be8 commit 6e0bac9

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

stdlib/Profile/src/Profile.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,26 @@ function init(; n::Union{Nothing,Integer} = nothing, delay::Union{Nothing,Real}
5555
if n === nothing && delay === nothing
5656
return Int(n_cur), delay_cur
5757
end
58-
nthreads = Sys.iswindows() ? 1 : Threads.nthreads() # windows only profiles the main thread
59-
nnew = (n === nothing) ? n_cur : n * nthreads
58+
nnew = (n === nothing) ? n_cur : n
6059
delaynew = (delay === nothing) ? delay_cur : delay
6160
init(nnew, delaynew)
6261
end
6362

6463
function init(n::Integer, delay::Real)
6564
nthreads = Sys.iswindows() ? 1 : Threads.nthreads() # windows only profiles the main thread
66-
status = ccall(:jl_profile_init, Cint, (Csize_t, UInt64), n * nthreads, round(UInt64,10^9*delay))
65+
sample_size_bytes = sizeof(Ptr) # == Sys.WORD_SIZE / 8
66+
buffer_samples = n * nthreads
67+
buffer_size_bytes = buffer_samples * sample_size_bytes
68+
if buffer_size_bytes > 2^29 && Sys.WORD_SIZE == 32
69+
buffer_size_bytes_per_thread = floor(Int, 2^29 / nthreads)
70+
buffer_samples_per_thread = floor(Int, buffer_size_bytes_per_thread / sample_size_bytes)
71+
buffer_samples = buffer_samples_per_thread * nthreads
72+
buffer_size_bytes = buffer_samples * sample_size_bytes
73+
@warn "Requested profile buffer limited to 512MB (n = $buffer_samples_per_thread per thread) given that this system is 32-bit"
74+
end
75+
status = ccall(:jl_profile_init, Cint, (Csize_t, UInt64), buffer_samples, round(UInt64,10^9*delay))
6776
if status == -1
68-
error("could not allocate space for ", n, " instruction pointers per thread being profiled ($nthreads threads)")
77+
error("could not allocate space for ", n, " instruction pointers per thread being profiled ($nthreads threads, $(Base.format_bytes(buffer_size_bytes)) total)")
6978
end
7079
end
7180

0 commit comments

Comments
 (0)