Repository: cilium/ebpf
Pull request #107 (2020-06-12 08:54:24)

hasPerCPUValue function add MapType LRUCPUHash
Hi lmb,
I have added a test for LRUCPUHash, LRUCPUHash is PerCPU map that store a distinct value for each CPU, so from userspace we need to see it in the form of an array

lmb: Hi yeze,

Can you please add a test that exercises LRUCPUHash? As far as I understand it that map type doesn't fit the `hasPerCPUValue` in that from userspace you still only see a single value, not an array of values.

Patch:
diff --git a/types.go b/types.go
index 2d84417..5bc5f65 100644
--- a/types.go
+++ b/types.go
@@ -85,7 +85,7 @@ const (
 
 // hasPerCPUValue returns true if the Map stores a value per CPU.
 func (mt MapType) hasPerCPUValue() bool {
-	if mt == PerCPUHash || mt == PerCPUArray {
+	if mt == PerCPUHash || mt == PerCPUArray || mt == LRUCPUHash {
 		return true
 	}
 	return false


Test patch:
diff --git a/map_test.go b/map_test.go
index c4a2a00..20993bc 100644
--- a/map_test.go
+++ b/map_test.go
@@ -503,45 +503,50 @@ func TestIterateMapInMap(t *testing.T) {
 }
 
 func TestPerCPUMarshaling(t *testing.T) {
-	numCPU, err := internal.PossibleCPUs()
-	if err != nil {
-		t.Fatal(err)
-	}
-	if numCPU < 2 {
-		t.Skip("Test requires at least two CPUs")
-	}
+	for _, typ := range []MapType{PerCPUHash, PerCPUArray, LRUCPUHash} {
+		t.Run(typ.String(), func(t *testing.T) {
+			numCPU, err := internal.PossibleCPUs()
+			if err != nil {
+				t.Fatal(err)
+			}
+			if numCPU < 2 {
+				t.Skip("Test requires at least two CPUs")
+			}
 
-	arr, err := NewMap(&MapSpec{
-		Type:       PerCPUArray,
-		KeySize:    4,
-		ValueSize:  5,
-		MaxEntries: 1,
-	})
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer arr.Close()
+			arr, err := NewMap(&MapSpec{
+				Type:       typ,
+				KeySize:    4,
+				ValueSize:  5,
+				MaxEntries: 1,
+			})
+			if err != nil {
+				t.Fatal(err)
+			}
+			defer arr.Close()
 
-	values := []*customEncoding{
-		&customEncoding{"hello"},
-		&customEncoding{"world"},
-	}
-	if err := arr.Put(uint32(0), values); err != nil {
-		t.Fatal(err)
-	}
+			values := []*customEncoding{
+				&customEncoding{"hello"},
+				&customEncoding{"world"},
+			}
+			if err := arr.Put(uint32(0), values); err != nil {
+				t.Fatal(err)
+			}
 
-	// Make sure unmarshaling works on slices containing pointers
-	var retrieved []*customEncoding
-	if err := arr.Lookup(uint32(0), &retrieved); err != nil {
-		t.Fatal("Can't retrieve key 0:", err)
-	}
+			// Make sure unmarshaling works on slices containing pointers
+			var retrieved []*customEncoding
+			if err := arr.Lookup(uint32(0), &retrieved); err != nil {
+				t.Fatal("Can't retrieve key 0:", err)
+			}
 
-	for i, want := range []string{"HELLO", "WORLD"} {
-		if retrieved[i] == nil {
-			t.Error("First item is nil")
-		} else if have := retrieved[i].data; have != want {
-			t.Errorf("Put doesn't use BinaryMarshaler, expected %s but got %s", want, have)
-		}
+			for i, want := range []string{"HELLO", "WORLD"} {
+				if retrieved[i] == nil {
+					t.Error("First item is nil")
+				} else if have := retrieved[i].data; have != want {
+					t.Errorf("Put doesn't use BinaryMarshaler, expected %s but got %s", want, have)
+				}
+			}
+
+		})
 	}
 }