Skip to content

Commit 8a8e3d1

Browse files
KristofferCtopolarityKristoffer Carlssonpchintalapudi
authored
transition @zone in Core.Compiler to directly use jl_timing (#58213)
This builds on the prior work from Kristoffer and Prem to implement a Julia-side `@zone` macro that interacts with the JL_TIMING system to provide profiling events to Tracy, VTune, and/or the TIMING_COUNTS back-end. This implementation includes some extra tricks to make sure that the timing block is stack-allocated, which can be important for performance if we start to use `@zone` for high-frequency events. Co-authored-by: Cody Tapscott <[email protected]> Co-authored-by: Kristoffer Carlsson <[email protected]> Co-authored-by: Prem Chintalapudi <[email protected]>
1 parent a47b58f commit 8a8e3d1

File tree

7 files changed

+43
-135
lines changed

7 files changed

+43
-135
lines changed

Compiler/src/Compiler.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function is_return_type(Core.@nospecialize(f))
120120
return false
121121
end
122122

123-
include("profiling.jl")
123+
include("timing.jl")
124124
include("sort.jl")
125125

126126
# We don't include some.jl, but this definition is still useful.

Compiler/src/profiling.jl

Lines changed: 0 additions & 33 deletions
This file was deleted.

Compiler/src/profiling/ittapi.jl

Lines changed: 0 additions & 18 deletions
This file was deleted.

Compiler/src/profiling/tracy.jl

Lines changed: 0 additions & 63 deletions
This file was deleted.

Compiler/src/timing.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
if ccall(:jl_timing_enabled, Cint, ()) != 0
2+
function getzonedexpr(name::Union{Symbol, String}, ex::Expr, func::Symbol, file::Symbol, line::Integer, color::Integer)
3+
event = RefValue{Ptr{Cvoid}}(C_NULL)
4+
name = QuoteNode(Symbol(name))
5+
func = QuoteNode(func)
6+
file = QuoteNode(file)
7+
8+
# XXX: This buffer must be large enough to store any jl_timing_block_t (runtime-checked)
9+
buffer = (0, 0, 0, 0, 0, 0, 0)
10+
buffer_size = Core.sizeof(buffer)
11+
return quote
12+
if $event[] === C_NULL
13+
$event[] = ccall(:_jl_timing_event_create, Ptr{Cvoid},
14+
(Ptr{UInt8}, Ptr{UInt8}, Ptr{UInt8}, Ptr{UInt8}, Cint, Cint),
15+
:CORE_COMPILER, $name, $func, $file, $line, $color)
16+
end
17+
timing_block = RefValue($buffer)
18+
block_ptr = pointer_from_objref(timing_block)
19+
$(Expr(:gc_preserve, quote
20+
ccall(:_jl_timing_block_init, Cvoid, (Ptr{Cvoid}, Csize_t, Ptr{Cvoid}), block_ptr, $buffer_size, $event[])
21+
ccall(:_jl_timing_block_start, Cvoid, (Ptr{Cvoid},), block_ptr)
22+
$(Expr(:tryfinally,
23+
:($(Expr(:escape, ex))),
24+
quote
25+
ccall(:_jl_timing_block_end, Cvoid, (Ptr{Cvoid},), block_ptr)
26+
end
27+
))
28+
end, :timing_block))
29+
end
30+
end
31+
macro zone(name, ex::Expr)
32+
return getzonedexpr(name, ex, :unknown_julia_function, __source__.file, __source__.line, 0)
33+
end
34+
else
35+
macro zone(name, ex::Expr)
36+
return esc(ex)
37+
end
38+
end

src/timing.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,14 @@ jl_module_t *jl_module_root(jl_module_t *m);
1616
extern "C" {
1717
#endif
1818

19-
JL_DLLEXPORT int jl_tracy_enabled(void) {
20-
#ifdef USE_TRACY
21-
return 1;
22-
#else
23-
return 0;
24-
#endif
25-
}
26-
27-
JL_DLLEXPORT int jl_ittapi_enabled(void) {
28-
#ifdef USE_ITTAPI
19+
JL_DLLEXPORT int jl_timing_enabled(void) {
20+
#ifdef ENABLE_TIMINGS
2921
return 1;
3022
#else
3123
return 0;
3224
#endif
3325
}
3426

35-
JL_DLLEXPORT int jl_nvtx_enabled(void) {
36-
#ifdef USE_NVTX
37-
return 1;
38-
#else
39-
return 0;
40-
#endif
41-
}
4227

4328
#ifdef ENABLE_TIMINGS
4429

src/timing.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ JL_DLLEXPORT jl_timing_event_t *_jl_timing_event_create(const char *subsystem, c
5555
JL_DLLEXPORT void _jl_timing_block_init(char *buf, size_t size, jl_timing_event_t *event);
5656
JL_DLLEXPORT void _jl_timing_block_start(jl_timing_block_t *cur_block);
5757
JL_DLLEXPORT void _jl_timing_block_end(jl_timing_block_t *cur_block);
58-
JL_DLLEXPORT int jl_tracy_enabled(void);
59-
JL_DLLEXPORT int jl_ittapi_enabled(void);
60-
JL_DLLEXPORT int jl_nvtx_enabled(void);
58+
JL_DLLEXPORT int jl_timing_enabled(void);
6159

6260

6361
#ifdef __cplusplus
@@ -191,6 +189,7 @@ JL_DLLEXPORT void jl_timing_puts(jl_timing_block_t *cur_block, const char *str);
191189
X(STACKWALK) \
192190
X(DL_OPEN) \
193191
X(JULIA_INIT) \
192+
X(CORE_COMPILER) \
194193

195194

196195
#define JL_TIMING_COUNTERS \

0 commit comments

Comments
 (0)