Skip to content

Commit

Permalink
Merge pull request #41 from topolarity/ct/all-arches
Browse files Browse the repository at this point in the history
Fix syscall numbers for non-x86_64 architectures
  • Loading branch information
vchuravy authored Sep 13, 2024
2 parents 47111c4 + 085b26a commit e19e1f2
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/LinuxPerf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,24 @@ function Base.show(io::IO, e::EventType)
end
end

const SYS_perf_event_open = 298
const SYS_perf_event_open = if Sys.ARCH === :x86_64
Clong(298)
elseif Sys.ARCH === :i686
Clong(336)
elseif Sys.ARCH === :aarch64
Clong(241)
elseif Sys.ARCH === :arm
Clong(364)
else
Clong(-1) # sentinel for unknown syscall ID
end

"""
perf_event_open(attr::perf_event_attr, pid, cpu, fd, flags)
"""
function perf_event_open(attr::perf_event_attr, pid, cpu, leader_fd, flags)
r_attr = Ref(attr)
SYS_perf_event_open == -1 && error("perf_event_open error : unknown architecture")
GC.@preserve r_attr begin
# Have to do a manual conversion, since the ABI is a vararg call
ptr = Base.unsafe_convert(Ptr{Cvoid}, Base.cconvert(Ptr{Cvoid}, r_attr))
Expand Down Expand Up @@ -325,16 +336,28 @@ function Base.show(io::IO, g::EventGroup)
print(io, "\t", g.event_types[end], ")")
end

const SYS_prctl = Clong(157)
const SYS_prctl = if Sys.ARCH === :x86_64
Clong(157)
elseif Sys.ARCH === :i686
Clong(172)
elseif Sys.ARCH === :aarch64
Clong(167)
elseif Sys.ARCH === :arm
Clong(172)
else
Clong(-1) # sentinel for unknown syscall ID
end
const PR_TASK_PERF_EVENTS_DISABLE = Cint(31)
const PR_TASK_PERF_EVENTS_ENABLE = Cint(32)

# syscall is lower overhead than calling libc's prctl
function enable_all!()
SYS_prctl == -1 && error("prctl error : unknown architecture")
res = ccall(:syscall, Cint, (Clong, Clong...), SYS_prctl, PR_TASK_PERF_EVENTS_ENABLE)
Base.systemerror(:prctl, res < 0)
end
function disable_all!()
SYS_prctl == -1 && error("prctl error : unknown architecture")
res = ccall(:syscall, Cint, (Clong, Clong...), SYS_prctl, PR_TASK_PERF_EVENTS_DISABLE)
Base.systemerror(:prctl, res < 0)
end
Expand Down Expand Up @@ -470,8 +493,10 @@ Base.close(b::PerfBenchThreaded) = foreach(close, b.data)

function make_bench_threaded(groups; threads = true)
data = PerfBench[]
warn_unsupported = true
for tid in (threads ? alltids() : zero(getpid()))
push!(data, PerfBench(tid, [EventGroup(g, pid = tid, userspace_only = false) for g in groups]))
push!(data, PerfBench(tid, [EventGroup(g; pid = tid, userspace_only = false, warn_unsupported) for g in groups]))
warn_unsupported = false # First tid's events will already have issued warnings
end
return PerfBenchThreaded(data)
end
Expand Down

0 comments on commit e19e1f2

Please sign in to comment.