Skip to content

Commit 65c3dbe

Browse files
committed
Fix map indexing, use unpacked structs
1 parent 0eb6e56 commit 65c3dbe

File tree

2 files changed

+37
-43
lines changed

2 files changed

+37
-43
lines changed

src/host/maps.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,21 @@ Base.keys(map::H) where {K,V,H<:HostMap{K,V}} = HostMapKeySet{K,V,H}(map)
178178
Base.IteratorSize(::Type{<:HostMapKeySet}) = Base.SizeUnknown()
179179
Base.eltype(::Type{HostMapKeySet{K,V,H}}) where {K,V,H} = K
180180
function Base.iterate(hmks::HostMapKeySet{K,V,H}) where {K,V,H}
181-
fakekey_ref = Ref{K}()
182-
iterate(hmks, fakekey_ref[])
183-
end
184-
function Base.iterate(hmks::HostMapKeySet{K,V,H}, key) where {K,V,H}
185-
nkey = nextkey(hmks.map, key)
186-
if nkey === nothing
187-
return nothing
181+
fakekey_ref = ZeroInitRef(K)
182+
realkey = if haskey(hmks.map, fakekey_ref[])
183+
fakekey_ref[]
184+
else
185+
nextkey(hmks.map, fakekey_ref[])
188186
end
189-
return nkey, nkey
187+
realkey === nothing && return nothing # empty
188+
return realkey, (realkey, realkey)
190189
end
191-
function Base.length(map::HostMap)
192-
# TODO: This sucks!
193-
ctr = 0
194-
for k in keys(map)
195-
ctr += 1
190+
function Base.iterate(hmks::HostMapKeySet{K,V,H}, (lastkey, initial)) where {K,V,H}
191+
lastkey === nothing && return nothing
192+
nkey = nextkey(hmks.map, lastkey)
193+
if nkey === nothing || nkey == lastkey
194+
return nothing
196195
end
197-
ctr
196+
return nkey, (nkey == initial ? nothing : nkey, initial)
198197
end
198+
Base.length(map::HostMap) = length(keys(map))

test/runtests.jl

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,20 @@ if run_root_tests
220220
end
221221
end
222222
@test_skip "uprobe"
223-
#= FIXME
224-
@testset "uprobe" begin
225-
up = UProbe(+, Tuple{Int,Int}) do regs
226-
return 0
227-
end
228-
API.load(up)
229-
API.unload(up)
230-
@testset "retprobe" begin
231-
up = UProbe(+, Tuple{Int,Int}; retprobe=true) do regs
232-
return 0
233-
end
234-
API.load(up)
235-
API.unload(up)
236-
end
237-
end
238-
=#
223+
#@testset "uprobe" begin
224+
# up = UProbe(+, Tuple{Int,Int}) do regs
225+
# return 0
226+
# end
227+
# API.load(up)
228+
# API.unload(up)
229+
# @testset "retprobe" begin
230+
# up = UProbe(+, Tuple{Int,Int}; retprobe=true) do regs
231+
# return 0
232+
# end
233+
# API.load(up)
234+
# API.unload(up)
235+
# end
236+
#end
239237
@testset "tracepoint" begin
240238
p = Tracepoint("clk", "clk_enable") do regs
241239
return 0
@@ -456,14 +454,13 @@ if run_root_tests
456454
# TODO: get_current_comm
457455
@testset "get_stackid" begin
458456
# from BCC's stackcount
459-
# FIXME: { i64, i32 } causes verifier errors
460457
struct StackKey
461-
tgid::UInt64
458+
tgid::UInt32
462459
sid::Clong
463460
end
464461
kp = KProbe("ksys_write"; license="GPL") do x
465-
stacks = RT.RTMap(;name="stacks",maptype=API.BPF_MAP_TYPE_STACK_TRACE,keytype=UInt32,valuetype=NTuple{API.PERF_MAX_STACK_DEPTH,UInt64},maxentries=100)
466-
counts = RT.RTMap(;name="counts",maptype=API.BPF_MAP_TYPE_HASH,keytype=StackKey,valuetype=UInt32,maxentries=100)
462+
stacks = RT.RTMap(;name="stacks",maptype=API.BPF_MAP_TYPE_STACK_TRACE,keytype=UInt32,valuetype=NTuple{API.PERF_MAX_STACK_DEPTH,UInt64},maxentries=10000)
463+
counts = RT.RTMap(;name="counts",maptype=API.BPF_MAP_TYPE_HASH,keytype=StackKey,valuetype=UInt32,maxentries=10000)
467464
pid, tgid = RT.get_current_pid_tgid()
468465
sid = RT.get_stackid(x, stacks, 0)
469466
key = StackKey(tgid, sid)
@@ -475,17 +472,14 @@ if run_root_tests
475472
stacks = Host.hostmap(API.findmap(kp.obj, "stacks"); K=UInt32, V=NTuple{API.PERF_MAX_STACK_DEPTH,UInt64})
476473
counts = Host.hostmap(API.findmap(kp.obj, "counts"); K=StackKey, V=UInt32)
477474
write(test_io, "1"); flush(test_io)
478-
@test length(counts) > 0
479-
key = nothing
480-
for k in keys(counts)
481-
if k.tgid == getpid()
482-
key = k
483-
break
484-
end
485-
end
486-
@test key !== nothing
475+
ks = collect(keys(counts))
476+
@test length(ks) > 0
477+
key = first(ks)
478+
# TODO: Should we be able to find a key with getpid() == key.tgid?
487479
@test haskey(counts, key)
480+
# TODO: This is potentially racy
488481
@test haskey(stacks, key.sid)
482+
@test occursin("ksys_write", Host.stack_to_string(stacks[key.sid]))
489483
end
490484
end
491485
end

0 commit comments

Comments
 (0)