Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if key supports batch API in Map() ctor #32348

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/ebpf/maps/generic_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,17 @@ func Map[K any, V any](m *ebpf.Map) (*GenericMap[K, V], error) {
return nil, err
}

// See if we can perform binary.Read on the key type. If we can't we can't use the batch API
// for this map
var kval K
keySupportsBatchAPI := canBinaryReadKey[K]()
if !keySupportsBatchAPI {
log.Warnf("Key type %T does not support binary.Read, batch API will not be used for this map", kval)
}

return &GenericMap[K, V]{
m: m,
m: m,
keySupportsBatchAPI: keySupportsBatchAPI,
}, nil
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/ebpf/maps/generic_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,25 @@ func TestIterateWithPointerKey(t *testing.T) {
require.NoError(t, it.Err())
require.Equal(t, expectedNumbers, actualNumbers)
}

func TestGenericHashMapCanUseBatchAPI(t *testing.T) {
if !BatchAPISupported() {
t.Skip("Skipping because batch API not supported")
}
usamasaqib marked this conversation as resolved.
Show resolved Hide resolved

hash, err := ebpf.NewMap(&ebpf.MapSpec{
Type: ebpf.Hash,
KeySize: 4,
ValueSize: 4,
MaxEntries: 10,
})
if err != nil {
t.Fatal(err)
}
usamasaqib marked this conversation as resolved.
Show resolved Hide resolved
t.Cleanup(func() { hash.Close() })

gm, err := Map[int32, int32](hash)
require.NoError(t, err)

require.True(t, gm.CanUseBatchAPI())
val06 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading