Skip to content

Commit

Permalink
move linuxperf into an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
willow-ahrens committed Dec 26, 2024
1 parent 1afc4bd commit 126a1a7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
39 changes: 39 additions & 0 deletions ext/LinuxPerfExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module LinuxPerfExt

import LinuxPerf: LinuxPerf, PerfBench, EventGroup, EventType
import LinuxPerf: enable!, disable!, enable_all!, disable_all!, close, read!

export LinuxPerfParameters

Base.@kwdef struct LinuxPerfParameters
g = EventGroup([EventType(:hw, :instructions), EventType(:hw, :branches)])
params = BenchmarkTools.Parameters()
end

function BenchmarkTools.prehook(evals, params::LinuxPerfParameters)
state = BenchmarkTools.prehook(evals, params.params)
bench = PerfBench(0, params.g)
enable!(bench)
(state, bench)
end

function BenchmarkTools.posthook((state, bench), evals, params::LinuxPerfParameters)
disable!(bench)
result = BenchmarkTools.posthook(state, evals, params.params)
(N, time_enabled, time_running, insts, branches) = read!(
bench.groups.leader_io, Vector{UInt64}(undef, 5)
)
if 2 * time_running <= time_enabled
# enabled less than 50% of the time
# (most likely due to PMU contention with other perf events)
return (NaN, NaN)
else
# account for partially-active measurement
k = time_enabled / time_running
estimated_instructions = Float64(insts) * k
estimated_branches = Float64(branches) * k
return (estimated_instructions, estimated_branches)
end
close(bench)
return (__sample_instructions, __sample_branches, result)
end
3 changes: 0 additions & 3 deletions src/LinuxPerf.jl

This file was deleted.

11 changes: 6 additions & 5 deletions src/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ function _run(b::Benchmark, p::Parameters; verbose=false, pad="", warmup=true, k
params = Parameters(p; kwargs...)
@assert params.seconds > 0.0 "time limit must be greater than 0.0"
if warmup
b.samplefunc(b.quote_vals, Parameters(params; evals=1)) #warmup sample
b.samplefunc(1, b.quote_vals, Parameters(params; evals=1)) #warmup sample
end
trial = Trial(params)
params.gctrial && gcscrub()
start_time = Base.time()
s = b.samplefunc(b.quote_vals, params)
s = b.samplefunc(params.evals, b.quote_vals, params)
push!(trial, s[1:(end - 1)]...)
return_val = s[end]
iters = 2
while (Base.time() - start_time) < params.seconds && iters params.samples
params.gcsample && gcscrub()
push!(trial, b.samplefunc(b.quote_vals, params)[1:(end - 1)]...)
push!(trial, b.samplefunc(params.evals, b.quote_vals, params)[1:(end - 1)]...)
iters += 1
end
return trial, return_val
Expand Down Expand Up @@ -183,13 +183,13 @@ function _lineartrial(b::Benchmark, p::Parameters=b.params; maxevals=RESOLUTION,
estimates = zeros(maxevals)
completed = 0
params.evals = 1
b.samplefunc(b.quote_vals, params) #warmup sample
b.samplefunc(params.evals, b.quote_vals, params) #warmup sample
params.gctrial && gcscrub()
start_time = time()
for evals in eachindex(estimates)
params.gcsample && gcscrub()
params.evals = evals
estimates[evals] = first(b.samplefunc(b.quote_vals, params))
estimates[evals] = first(b.samplefunc(params.evals, b.quote_vals, params))
completed += 1
((time() - start_time) > params.seconds) && break
end
Expand Down Expand Up @@ -600,6 +600,7 @@ function posthook((gc_start, start_time), evals, params::Parameters)
return time, gctime, memory, allocs, return_val
end


######################
# convenience macros #
######################
Expand Down

0 comments on commit 126a1a7

Please sign in to comment.