Skip to content

Commit d4e0a9a

Browse files
topolarityZentrik
authored andcommitted
Fix-up behavior for empty PerfGroup
For machines that don't have `perf` properly configured (e.g. WSL), it's common to end up with an empty PerfGroup that has an invalid `leader_fd`. This commit adds the appropriate guards to make sure that doesn't error excessively.
1 parent f0468ea commit d4e0a9a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/LinuxPerf.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ const PERF_EVENT_IOC_DISABLE = UInt64(0x2401)
369369
const PERF_EVENT_IOC_RESET = UInt64(0x2403)
370370

371371
function ioctl(group::EventGroup, x)
372+
group.leader_fd == -1 && return nothing
372373
res = ccall(:ioctl, Cint, (Cint, Clong, Clong), group.leader_fd, x, 1)
373374
Base.systemerror(:ioctl, res < 0)
374375
return nothing
@@ -383,6 +384,7 @@ function Base.close(g::EventGroup)
383384
fd == g.leader_fd && continue # close leader_fd last
384385
ccall(:close, Cint, (Cint,), fd)
385386
end
387+
g.leader_fd == -1 && return
386388
ccall(:close, Cint, (Cint,), g.leader_fd)
387389
end
388390

@@ -441,6 +443,7 @@ function counters(b::PerfBench)
441443
c = Counter[]
442444
for g in b.groups
443445
values = Vector{UInt64}(undef, length(g)+1+2)
446+
g.leader_fd == -1 && continue # empty group
444447
read!(g.leader_io, values)
445448
#?Ref@assert(length(g) == values[1])
446449
enabled, running = values[2], values[3]
@@ -801,6 +804,10 @@ function ThreadStats(b::PerfBench)
801804
groups = Vector{Counter}[]
802805
for g in b.groups
803806
values = Vector{UInt64}(undef, length(g)+1+2)
807+
if g.leader_fd == -1
808+
push!(groups, Counter[]) # empty group
809+
continue
810+
end
804811
read!(g.leader_io, values)
805812
#?Ref@assert(length(g) == values[1])
806813
enabled, running = values[2], values[3]

test/runtests.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using LinuxPerf
22
using Test
33

4-
using LinuxPerf: make_bench, enable!, disable!, reset!, reasonable_defaults, counters, EventType, EventTypeExt, parse_groups, enable_all!, disable_all!
4+
using LinuxPerf: make_bench, enable!, disable!, reset!, reasonable_defaults, counters, EventGroup, EventType, EventTypeExt, parse_groups, enable_all!, disable_all!
55

66
@testset "LinuxPerf" begin
77

@@ -128,4 +128,12 @@ end
128128
@test LinuxPerf._addcommas(typemin(Int64)) == "-9,223,372,036,854,775,808"
129129
end
130130

131+
@testset "empty EventGroup" begin
132+
# Creating an empty EventGroup should be a no-op (and not error)
133+
g = EventGroup(EventType[])
134+
@test length(g.fds) == 0
135+
@test g.leader_fd == -1
136+
close(g) # also a no-op
137+
end
138+
131139
end

0 commit comments

Comments
 (0)