|
| 1 | +// +build linux |
| 2 | + |
| 3 | +package intelrdt |
| 4 | + |
| 5 | +import ( |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestParseMonFeatures(t *testing.T) { |
| 15 | + t.Run("All features available", func(t *testing.T) { |
| 16 | + parsedMonFeatures, err := parseMonFeatures( |
| 17 | + strings.NewReader("mbm_total_bytes\nmbm_local_bytes\nllc_occupancy")) |
| 18 | + if err != nil { |
| 19 | + t.Errorf("Error while parsing mon features err = %v", err) |
| 20 | + } |
| 21 | + |
| 22 | + expectedMonFeatures := monFeatures{true, true, true} |
| 23 | + |
| 24 | + if parsedMonFeatures != expectedMonFeatures { |
| 25 | + t.Error("Cannot gather all features!") |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + t.Run("No features available", func(t *testing.T) { |
| 30 | + parsedMonFeatures, err := parseMonFeatures(strings.NewReader("")) |
| 31 | + |
| 32 | + if err != nil { |
| 33 | + t.Errorf("Error while parsing mon features err = %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + expectedMonFeatures := monFeatures{false, false, false} |
| 37 | + |
| 38 | + if parsedMonFeatures != expectedMonFeatures { |
| 39 | + t.Error("Expected no features available but there is any!") |
| 40 | + } |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +func mockMBM(NUMANodes []string, mocks map[string]uint64) (string, error) { |
| 45 | + testDir, err := ioutil.TempDir("", "rdt_mbm_test") |
| 46 | + if err != nil { |
| 47 | + return "", err |
| 48 | + } |
| 49 | + monDataPath := filepath.Join(testDir, "mon_data") |
| 50 | + |
| 51 | + for _, numa := range NUMANodes { |
| 52 | + numaPath := filepath.Join(monDataPath, numa) |
| 53 | + err = os.MkdirAll(numaPath, os.ModePerm) |
| 54 | + if err != nil { |
| 55 | + return "", err |
| 56 | + } |
| 57 | + |
| 58 | + for fileName, value := range mocks { |
| 59 | + err := ioutil.WriteFile(filepath.Join(numaPath, fileName), []byte(strconv.FormatUint(value, 10)), 777) |
| 60 | + if err != nil { |
| 61 | + return "", err |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + return testDir, nil |
| 68 | +} |
| 69 | + |
| 70 | +func TestGetMbmStats(t *testing.T) { |
| 71 | + mocksNUMANodesToCreate := []string{"mon_l3_00", "mon_l3_01"} |
| 72 | + |
| 73 | + mocksFilesToCreate := map[string]uint64{ |
| 74 | + "mbm_total_bytes": 9123911, |
| 75 | + "mbm_local_bytes": 2361361, |
| 76 | + "llc_occupancy": 123013, |
| 77 | + } |
| 78 | + |
| 79 | + mockedMBM, err := mockMBM(mocksNUMANodesToCreate, mocksFilesToCreate) |
| 80 | + |
| 81 | + defer func() { |
| 82 | + err := os.RemoveAll(mockedMBM) |
| 83 | + if err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + }() |
| 87 | + |
| 88 | + if err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + |
| 92 | + t.Run("Gather mbm", func(t *testing.T) { |
| 93 | + enabledMonFeatures.mbmTotalBytes = true |
| 94 | + enabledMonFeatures.mbmLocalBytes = true |
| 95 | + enabledMonFeatures.llcOccupancy = true |
| 96 | + |
| 97 | + stats, err := getMBMStats(mockedMBM) |
| 98 | + if err != nil { |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + |
| 102 | + if len(*stats) != len(mocksNUMANodesToCreate) { |
| 103 | + t.Fatalf("Wrong number of stats slices from NUMA nodes. Expected: %v but got: %v", |
| 104 | + len(mocksNUMANodesToCreate), len(*stats)) |
| 105 | + } |
| 106 | + |
| 107 | + checkStatCorrection := func(got MBMNumaNodeStats, expected MBMNumaNodeStats, t *testing.T) { |
| 108 | + if got.MBMTotalBytes != expected.MBMTotalBytes { |
| 109 | + t.Fatalf("Wrong value of mbm_total_bytes. Expected: %v but got: %v", |
| 110 | + expected.MBMTotalBytes, |
| 111 | + got.MBMTotalBytes) |
| 112 | + } |
| 113 | + |
| 114 | + if got.MBMLocalBytes != expected.MBMLocalBytes { |
| 115 | + t.Fatalf("Wrong value of mbm_local_bytes. Expected: %v but got: %v", |
| 116 | + expected.MBMLocalBytes, |
| 117 | + got.MBMLocalBytes) |
| 118 | + } |
| 119 | + |
| 120 | + if got.LLCOccupancy != expected.LLCOccupancy { |
| 121 | + t.Fatalf("Wrong value of llc_occupancy. Expected: %v but got: %v", |
| 122 | + expected.LLCOccupancy, |
| 123 | + got.LLCOccupancy) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + expectedStats := MBMNumaNodeStats{ |
| 128 | + MBMTotalBytes: mocksFilesToCreate["mbm_total_bytes"], |
| 129 | + MBMLocalBytes: mocksFilesToCreate["mbm_local_bytes"], |
| 130 | + LLCOccupancy: mocksFilesToCreate["llc_occupancy"], |
| 131 | + } |
| 132 | + |
| 133 | + checkStatCorrection((*stats)[0], expectedStats, t) |
| 134 | + checkStatCorrection((*stats)[1], expectedStats, t) |
| 135 | + }) |
| 136 | + |
| 137 | +} |
0 commit comments