Skip to content

Commit

Permalink
Don't unconditionally evaluate arguments to log functions.
Browse files Browse the repository at this point in the history
Use a macro to avoid doing so when the log level is disabled.
  • Loading branch information
maleadt committed Feb 16, 2017
1 parent 4ca6b75 commit 2b11f22
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/CUDAdrv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ include("array.jl")

function __init__()
# check validity of CUDA library
debug("Checking validity of $(libcuda_path)")
@debug("Checking validity of $(libcuda_path)")
if version() != libcuda_version
error("CUDA library version has changed. Please re-run Pkg.build(\"CUDA\") and restart Julia.")
end
Expand Down
2 changes: 1 addition & 1 deletion src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end
(::Type{CuArray{T}}){T}(len::Int) = CuArray{T,1}((len,))

function finalize(a::CuArray)
trace("Finalizing CuArray at $(Base.pointer_from_objref(a))")
@trace("Finalizing CuArray at $(Base.pointer_from_objref(a))")
Mem.free(a.devptr)
unblock_finalizer(a, a.devptr.ctx)
end
Expand Down
4 changes: 2 additions & 2 deletions src/context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ end
const context_instances = Dict{CuContext_t,CuContext}()

function finalize(ctx::CuContext)
trace("Finalizing CuContext at $(Base.pointer_from_objref(ctx))")
@trace("Finalizing CuContext at $(Base.pointer_from_objref(ctx))")
if can_finalize(ctx)
@apicall(:cuCtxDestroy, (CuContext_t,), ctx)
else
# this is due to finalizers not respecting _any_ order during process teardown
# (ie. it doesn't respect active instances carefully set-up in `gc.jl`)
# TODO: can we check this only happens during teardown?
trace("Not destroying context $ctx because of out-of-order finalizer run")
@trace("Not destroying context $ctx because of out-of-order finalizer run")
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type CuEvent
end

function finalize(e::CuEvent)
trace("Finalizing CuEvent at $(Base.pointer_from_objref(e))")
@trace("Finalizing CuEvent at $(Base.pointer_from_objref(e))")
@apicall(:cuEventDestroy, (CuEvent_t,), e)
unblock_finalizer(e, e.ctx)
end
Expand Down
8 changes: 4 additions & 4 deletions src/gc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ finalized _before_ any parent object is finalized.
"""
function block_finalizer(owner::ANY, target::ANY)
owner_id = Base.pointer_from_objref(owner)
trace("Blocking finalization of $target at $((Base.pointer_from_objref(target)))",
" by $(typeof(owner)) at $owner_id")
@trace("Blocking finalization of $target at $((Base.pointer_from_objref(target)))",
" by $(typeof(owner)) at $owner_id")
haskey(finalizer_blocks, owner_id) && error("can only issue a single call to block_finalizer")
finalizer_blocks[owner_id] = target
end
Expand All @@ -50,8 +50,8 @@ This function is meant to be called in the finalized of a child object.
"""
function unblock_finalizer(owner::ANY, target::ANY)
owner_id = Base.pointer_from_objref(owner)
trace("Unblocking finalization of $target at $((Base.pointer_from_objref(target)))",
" by $(typeof(owner)) at $owner_id")
@trace("Unblocking finalization of $target at $((Base.pointer_from_objref(target)))",
" by $(typeof(owner)) at $owner_id")
haskey(finalizer_blocks, owner_id) ||
error("no finalizer blocks found (no call to block_finalizer, or has been unblocked already)")

Expand Down
6 changes: 3 additions & 3 deletions src/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ type CuModule
@static if DEBUG
options = decode(optionKeys, optionVals)
if isempty(options[INFO_LOG_BUFFER])
debug("JIT info log is empty")
@debug("JIT info log is empty")
else
debug("JIT info log: ", repr_indented(options[INFO_LOG_BUFFER]; abbrev=false))
@debug("JIT info log: ", repr_indented(options[INFO_LOG_BUFFER]; abbrev=false))
end
end

Expand All @@ -56,7 +56,7 @@ type CuModule
end

function finalize(mod::CuModule)
trace("Finalizing CuModule at $(Base.pointer_from_objref(mod))")
@trace("Finalizing CuModule at $(Base.pointer_from_objref(mod))")
@apicall(:cuModuleUnload, (CuModule_t,), mod)
unblock_finalizer(mod, mod.ctx)
end
Expand Down
6 changes: 3 additions & 3 deletions src/module/linker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type CuLink
end

function finalize(link::CuLink)
trace("Finalizing CuLink at $(Base.pointer_from_objref(link))")
@trace("Finalizing CuLink at $(Base.pointer_from_objref(link))")
@apicall(:cuLinkDestroy, (CuLinkState_t,), link)
unblock_finalizer(link, link.ctx)
end
Expand Down Expand Up @@ -80,9 +80,9 @@ function complete(link::CuLink)
@static if DEBUG
options = decode(link.optionKeys, link.optionVals)
if isempty(options[INFO_LOG_BUFFER])
debug("JIT info log is empty")
@debug("JIT info log is empty")
else
debug("JIT info log: ", repr_indented(options[INFO_LOG_BUFFER]))
@debug("JIT info log: ", repr_indented(options[INFO_LOG_BUFFER]))
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function CuStream(flags::Integer=0)
end

function finalize(s::CuStream)
trace("Finalizing CuStream at $(Base.pointer_from_objref(s))")
@trace("Finalizing CuStream at $(Base.pointer_from_objref(s))")
@apicall(:cuStreamDestroy, (CuModule_t,), s)
unblock_finalizer(s, s.ctx)
end
Expand Down
6 changes: 6 additions & 0 deletions src/util/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ is set."
end
end
@inline trace(msg...; kwargs...) = trace(STDERR, msg...; kwargs...)
macro trace(args...)
TRACE && return esc(Expr(:call, :trace, args...))
end

const DEBUG = TRACE || haskey(ENV, "DEBUG")
"Display a debug message. Only results in actual printing if the TRACE or DEBUG environment
Expand All @@ -33,6 +36,9 @@ variable is set."
end
end
@inline debug(msg...; kwargs...) = debug(STDERR, msg...; kwargs...)
macro debug(args...)
DEBUG && return esc(Expr(:call, :trace, args...))
end

"Create an indented string from any value (instead of escaping endlines as \n)"
function repr_indented(ex; prefix=" "^7, abbrev=true)
Expand Down
2 changes: 0 additions & 2 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ CUDAdrv.@apicall(:cuDriverGetVersion, (Ptr{Cint},), Ref{Cint}())
@test_throws ErrorException @eval CUDAdrv.@apicall(:cuDummyAvailable, ())
@test_throws CUDAdrv.CuVersionError @eval CUDAdrv.@apicall(:cuDummyUnavailable, ())

CUDAdrv.trace(prefix=" ")

@test_throws ErrorException eval(
quote
foo = :bar
Expand Down

0 comments on commit 2b11f22

Please sign in to comment.