Skip to content

Instantly share code, notes, and snippets.

@florianl
Created June 28, 2022 18:58
Show Gist options
  • Save florianl/3ea394b47fe1e2ce2339647c0c866e7c to your computer and use it in GitHub Desktop.
Save florianl/3ea394b47fe1e2ce2339647c0c866e7c to your computer and use it in GitHub Desktop.
continous map lookups
package main
import (
"fmt"
"math/rand"
"os"
"unsafe"
"github.com/cilium/ebpf"
)
func main() {
rand.Seed(73)
hash, err := ebpf.NewMap(&ebpf.MapSpec{
Type: ebpf.Hash,
KeySize: 4, // size of uint32
ValueSize: 4, // size of uint32
MaxEntries: 2097152, // 2^21
})
if err != nil {
fmt.Fprintf(os.Stderr, "Loading map failed: %v\n", err)
return
}
defer func() {
if err := hash.Close(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to remove map: %v\n", err)
}
}()
var key, value uint32
for {
// Get a random key to lookup in the map
key = rand.Uint32()
if err := hash.Lookup(unsafe.Pointer(&key), unsafe.Pointer(&value)); err != nil {
fmt.Fprintf(os.Stderr, "Failed to lookkup %d: %v\n", key, err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment