Skip to content

Commit

Permalink
Check if key supports batch API in Map() ctor (#32348)
Browse files Browse the repository at this point in the history
  • Loading branch information
usamasaqib authored Dec 19, 2024
1 parent 3c2d6cb commit 091e10e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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
16 changes: 16 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,19 @@ func TestIterateWithPointerKey(t *testing.T) {
require.NoError(t, it.Err())
require.Equal(t, expectedNumbers, actualNumbers)
}

func TestGenericHashMapCanUseBatchAPI(t *testing.T) {
hash, err := ebpf.NewMap(&ebpf.MapSpec{
Type: ebpf.Hash,
KeySize: 4,
ValueSize: 4,
MaxEntries: 10,
})
require.NoError(t, err)
t.Cleanup(func() { hash.Close() })

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

require.Equal(t, BatchAPISupported(), gm.CanUseBatchAPI())
}

0 comments on commit 091e10e

Please sign in to comment.