Skip to content

Commit 10e8b9a

Browse files
committed
revise implementation
1 parent 3bf2a16 commit 10e8b9a

File tree

1 file changed

+71
-42
lines changed

1 file changed

+71
-42
lines changed

Compiler/src/timing.jl

Lines changed: 71 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
11
if ccall(:jl_timing_enabled, Cint, ()) != 0
2-
have_scoped = isdefined(@__MODULE__, :Base) && isdefined(Base, :ScopedValues)
3-
if have_scoped
4-
using Base.ScopedValues: ScopedValue
5-
const TRACY_CONTEXT = ScopedValue{Ptr{Cvoid}}(C_NULL)
6-
7-
function _get_current_tracy_block()
8-
ctx = TRACY_CONTEXT[]
9-
if ctx === C_NULL
10-
error("must be called from within a @zone")
11-
end
12-
return ctx
2+
const scoped_values_available = isdefined(@__MODULE__, :Base) && isdefined(Base, :ScopedValues)
3+
const use_tls = true # use tls or scoped values
4+
if !scoped_values_available
5+
const CURRENT_TIMING_BLOCK = RefValue(C_NULL)
6+
@inline _get_current_timing_block_nocheck() = CURRENT_TIMING_BLOCK
7+
@inline function set_current_timing_block!(block::Ptr{Cvoid})
8+
old_block = CURRENT_TIMING_BLOCK[]
9+
CURRENT_TIMING_BLOCK[] = block
10+
return old_block
1311
end
14-
15-
function timing_print(str)
16-
ccall(
17-
:jl_timing_puts,
18-
Cvoid,
19-
(Ptr{Cvoid}, Cstring),
20-
_get_current_tracy_block(),
21-
string(str)
22-
)
12+
@inline function restore_current_timing_block!(old_block::Ptr{Cvoid})
13+
CURRENT_TIMING_BLOCK[] = old_block
14+
end
15+
elseif use_tls
16+
const TIMING_BLOCK_TLS_KEY = :timing_block_tls_key
17+
@inline function _get_current_timing_block_nocheck()
18+
ref_block = get(task_local_storage(), TIMING_BLOCK_TLS_KEY, nothing)::Union{Nothing, RefValue{Ptr{Cvoid}}}
19+
return ref_block === nothing ? C_NULL : ref_block[]
2320
end
21+
@inline function set_current_timing_block!(block::Ptr{Cvoid})
22+
ref_block = get!(()->RefValue(C_NULL), task_local_storage(), TIMING_BLOCK_TLS_KEY)::RefValue{Ptr{Cvoid}}
23+
old = ref_block[]
24+
println("Setting to $block")
25+
ref_block[] = block
26+
return (old, ref_block)
27+
end
28+
@inline function restore_current_timing_block!((old, ref)::Tuple{Ptr{Cvoid}, RefValue{Ptr{Cvoid}}})
29+
println("Restoring to $old")
30+
ref[] = old
31+
end
32+
else
33+
using Base.ScopedValues: ScopedValue
34+
const CURRENT_TIMING_BLOCK = ScopedValue{Ptr{Cvoid}}(C_NULL)
35+
_get_current_timing_block_nocheck() = CURRENT_TIMING_BLOCK[]
36+
end
2437

25-
#=
26-
macro timing_printf(fmt, args...)
27-
esc(:(
28-
ccall(
29-
:jl_timing_printf,
30-
Cvoid,
31-
(Ptr{Cvoid}, Cstring, Cstring...),
32-
_get_current_tracy_block(),
33-
$fmt,
34-
$(args...)
35-
)
36-
))
38+
function _get_current_timing_block()
39+
block = _get_current_timing_block_nocheck()
40+
if block === C_NULL
41+
error("must be called from within a @zone")
3742
end
38-
=#
43+
return block
3944
end
4045

4146
function getzonedexpr(name::Union{Symbol, String}, ex::Expr, func::Symbol, file::Symbol, line::Integer, color::Integer)
@@ -48,9 +53,23 @@ if ccall(:jl_timing_enabled, Cint, ()) != 0
4853
buffer = (0, 0, 0, 0, 0, 0, 0)
4954
buffer_size = Core.sizeof(buffer)
5055

51-
scope_expr = have_scoped ?
52-
:(Base.ScopedValues.Scope(Core.current_scope(), TRACY_CONTEXT => timing_block_ptr)) :
56+
before_expr = if !scoped_values_available || use_tls
57+
:(prev_block_ptr = $set_current_timing_block!(timing_block_ptr))
58+
else
59+
nothing
60+
end
61+
62+
after_expr = if !scoped_values_available || use_tls
63+
:($restore_current_timing_block!(prev_block_ptr))
64+
else
5365
nothing
66+
end
67+
68+
scope_expr = if !scoped_values_available || use_tls
69+
nothing
70+
else
71+
:(Base.ScopedValues.Scope(Core.current_scope(), CURRENT_TIMING_BLOCK => timing_block_ptr))
72+
end
5473

5574
return quote
5675
if $event[] === C_NULL
@@ -64,9 +83,13 @@ if ccall(:jl_timing_enabled, Cint, ()) != 0
6483
ccall(:_jl_timing_block_init, Cvoid, (Ptr{Cvoid}, Csize_t, Ptr{Cvoid}), timing_block_ptr, $buffer_size, $event[])
6584
ccall(:_jl_timing_block_start, Cvoid, (Ptr{Cvoid},), timing_block_ptr)
6685
$(Expr(:tryfinally,
67-
:($(Expr(:escape, ex))),
86+
quote
87+
$before_expr
88+
$(Expr(:escape, ex))
89+
end,
6890
quote
6991
ccall(:_jl_timing_block_end, Cvoid, (Ptr{Cvoid},), timing_block_ptr)
92+
$after_expr
7093
end,
7194
:($scope_expr)
7295
))
@@ -76,14 +99,20 @@ if ccall(:jl_timing_enabled, Cint, ()) != 0
7699
macro zone(name, ex::Expr)
77100
return getzonedexpr(name, ex, :unknown_julia_function, __source__.file, __source__.line, 0)
78101
end
102+
@inline function timing_print(str)
103+
block = _get_current_timing_block()
104+
println("Printing $str with block $block")
105+
ccall(
106+
:jl_timing_puts,
107+
Cvoid,
108+
(Ptr{Cvoid}, Ptr{UInt8}),
109+
block,
110+
string(str)
111+
)
112+
end
79113
else
80114
macro zone(name, ex::Expr)
81115
return esc(ex)
82116
end
83-
timing_print(str) = nothing
84-
#=
85-
macro timing_printf(fmt, args...)
86-
return nothing
87-
end
88-
=#
117+
timing_print(str::String) = nothing
89118
end

0 commit comments

Comments
 (0)