Skip to content

Commit

Permalink
Add hook for inference (#44)
Browse files Browse the repository at this point in the history
* add hooks into compiler inference
* add docs
* update tests
* bump tag
  • Loading branch information
simonbyrne authored Jan 11, 2024
1 parent 410f291 commit a1637c4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NVTX"
uuid = "5da4648a-3479-48b8-97b9-01cb529c0a1f"
authors = ["Simon Byrne <simonbyrne@gmail.com>"]
version = "0.3.3"
version = "0.3.4"

[deps]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Expand Down
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The following macros provide the most convenient usage of this package

```@docs
enable_gc_hooks()
enable_inference_hook
name_threads_julia()
```

Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end

## Instrumenting Julia garbage collector

Additionally, it is possible to annotate the Julia garbage collector by calling [`NVTX.enable_gc_hooks()`](@ref), or setting the `JULIA_NVTX_CALLBACKS` environment variable.
Additionally, it is possible to annotate the Julia garbage collector by calling [`NVTX.enable_gc_hooks()`](@ref) and [`NVTX.enable_inference_hook()`](@ref), or setting the `JULIA_NVTX_CALLBACKS` environment variable.

## Running the profiler

Expand Down
1 change: 1 addition & 0 deletions src/NVTX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function activate()
alloc="alloc" in callbacks,
free="free" in callbacks
)
enable_inference_hook("inference" in callbacks)
end

function __init__()
Expand Down
34 changes: 31 additions & 3 deletions src/julia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const GC_FREE_MESSAGE = StringHandle(JULIA_DOMAIN, "free")
const GC_COLOR = Ref{UInt32}(Colors.ARGB32(Colors.colorant"brown").color)
const GC_ALLOC_COLOR = Ref{UInt32}(Colors.ARGB32(Colors.colorant"goldenrod1").color)
const GC_FREE_COLOR = Ref{UInt32}(Colors.ARGB32(Colors.colorant"dodgerblue").color)
const INFERENCE_COLOR = Ref{UInt32}(Colors.ARGB32(Colors.colorant"turquoise3").color)

"""
NVTX.enable_gc_hooks(;gc=true, alloc=false, free=false)
Expand Down Expand Up @@ -71,9 +72,9 @@ function enable_gc_hooks(;gc::Bool=true, alloc::Bool=false, free::Bool=false)
unsafe_store!(cglobal((:gc_message,libjulia_nvtx_callbacks),Ptr{Cvoid}), GC_MESSAGE.ptr)
unsafe_store!(cglobal((:gc_color,libjulia_nvtx_callbacks),UInt32), GC_COLOR[])
# https://github.com/JuliaLang/julia/blob/v1.8.3/src/julia.h#L879-L883
name_category(JULIA_DOMAIN, 1+0, "auto")
name_category(JULIA_DOMAIN, 1+1, "full")
name_category(JULIA_DOMAIN, 1+2, "incremental")
name_category(JULIA_DOMAIN, 1+0, "GC auto")
name_category(JULIA_DOMAIN, 1+1, "GC full")
name_category(JULIA_DOMAIN, 1+2, "GC incremental")
end
if alloc
init!(GC_ALLOC_MESSAGE)
Expand All @@ -97,3 +98,30 @@ function enable_gc_hooks(;gc::Bool=true, alloc::Bool=false, free::Bool=false)
return nothing
end

typeinf_ext_nvtx(mi::Base.Core.MethodInstance, world::UInt) = typeinf_ext_nvtx(Base.Core.Compiler.NativeInterpreter(world), mi)
function typeinf_ext_nvtx(interp::Base.Core.Compiler.AbstractInterpreter, linfo::Base.Core.MethodInstance)
method = linfo.def
types = linfo.specTypes.parameters[2:end]
message = "$(method.name)($(join([string("::", t) for t in types], ", "))) @ $(method.module) $(method.file):$(method.line)"
id = range_start(JULIA_DOMAIN; message, color = INFERENCE_COLOR[], category = 11)
ret = Core.Compiler.typeinf_ext_toplevel(interp, linfo)
range_end(id)
return ret
end
precompile(typeinf_ext_nvtx, (Base.Core.Compiler.NativeInterpreter, Base.Core.MethodInstance))
precompile(typeinf_ext_nvtx, (Base.Core.MethodInstance, UInt))

"""
NVTX.enable_inference_hook(active::Bool=true)
Add hooks for method inference. Can also be activated by adding `inference` to
the `JULIA_NVTX_CALLBACKS` environment variable.
"""
function enable_inference_hook(enable::Bool=true)
if enable
name_category(JULIA_DOMAIN, 11, "compiler inference")
ccall(:jl_set_typeinf_func, Cvoid, (Any,), typeinf_ext_nvtx)
else
ccall(:jl_set_typeinf_func, Cvoid, (Any,), Core.Compiler.typeinf_ext_toplevel)
end
end
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end

nsys = get(ENV, "JULIA_NSYS", "nsys")

run(`$nsys profile --env-var="JULIA_NVTX_CALLBACKS=gc|alloc|free" --output=$(joinpath(dirname, "basic")) --export=json,sqlite --trace=nvtx $(Base.julia_cmd()) --project=$(Base.active_project()) --threads=3 basic.jl`)
run(`$nsys profile --env-var="JULIA_NVTX_CALLBACKS=gc|alloc|free|inference" --output=$(joinpath(dirname, "basic")) --export=json,sqlite --trace=nvtx $(Base.julia_cmd()) --project=$(Base.active_project()) --threads=3 basic.jl`)

using DataFrames, SQLite, DBInterface, Colors, Test

Expand Down Expand Up @@ -84,14 +84,14 @@ julia_categories = DataFrame(DBInterface.execute(db, """
WHERE eventType = $NvtxCategory AND domainId = $julia_domainId
ORDER BY category
"""))
@test julia_categories.category == [1, 2, 3]
@test julia_categories.text == ["auto", "full", "incremental"]
@test julia_categories.category == [1, 2, 3, 11]
@test julia_categories.text == ["GC auto", "GC full", "GC incremental", "compiler inference"]

julia_ranges = DataFrame(DBInterface.execute(db, """
SELECT COALESCE(text, value) as text, category, color
FROM NVTX_EVENTS
LEFT JOIN StringIds on textId == id
WHERE eventType = $NvtxPushPopRange AND domainId = $julia_domainId AND category > 1
WHERE eventType = $NvtxPushPopRange AND domainId = $julia_domainId AND category > 1 AND category < 10
ORDER BY start
""")) # exclude auto GC
@test julia_ranges.text == ["GC" for i = 1:2]
Expand Down

2 comments on commit a1637c4

@simonbyrne
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/98731

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.4 -m "<description of version>" a1637c4aa085ae8c416825658298e504f4518d41
git push origin v0.3.4

Please sign in to comment.