-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Paweł Szulik <[email protected]>
- Loading branch information
Paweł Szulik
committed
Apr 9, 2020
1 parent
7fa13b2
commit d2b4e6b
Showing
6 changed files
with
305 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// +build linux | ||
|
||
package intelrdt | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
// The flag to indicate if Intel RDT/MBM is enabled | ||
isMbmEnabled bool | ||
|
||
enabledMonFeatures monFeatures | ||
) | ||
|
||
type monFeatures struct { | ||
mbmTotalBytes bool | ||
mbmLocalBytes bool | ||
llcOccupancy bool | ||
} | ||
|
||
// Check if Intel RDT/MBM is enabled | ||
func IsMbmEnabled() bool { | ||
return isMbmEnabled | ||
} | ||
|
||
func getMonFeatures(intelRdtRoot string) (monFeatures, error) { | ||
file, err := os.Open(filepath.Join(intelRdtRoot, "info", "L3_MON", "mon_features")) | ||
defer file.Close() | ||
if err != nil { | ||
return monFeatures{}, err | ||
} | ||
return parseMonFeatures(file) | ||
} | ||
|
||
func parseMonFeatures(reader io.Reader) (monFeatures, error) { | ||
scanner := bufio.NewScanner(reader) | ||
|
||
monFeatures := monFeatures{} | ||
|
||
for scanner.Scan() { | ||
|
||
switch feature := scanner.Text(); feature { | ||
|
||
case "mbm_total_bytes": | ||
monFeatures.mbmTotalBytes = true | ||
case "mbm_local_bytes": | ||
monFeatures.mbmLocalBytes = true | ||
case "llc_occupancy": | ||
monFeatures.llcOccupancy = true | ||
default: | ||
logrus.Warnf(fmt.Sprintf("Unsupported RDT Memory Bandwidth Monitoring (MBM) feature: %s", feature)) | ||
} | ||
} | ||
|
||
return monFeatures, scanner.Err() | ||
} | ||
|
||
func getMBMStats(containerPath string) (*[]MBMNumaNodeStats, error) { | ||
mbmStats := []MBMNumaNodeStats{} | ||
|
||
numaFiles, err := ioutil.ReadDir(filepath.Join(containerPath, "mon_data")) | ||
if err != nil { | ||
return &mbmStats, err | ||
} | ||
|
||
for _, file := range numaFiles { | ||
if file.IsDir() { | ||
numaStats, err := getMBMNumaNodeStats(filepath.Join(containerPath, "mon_data", file.Name())) | ||
if err != nil { | ||
return &mbmStats, nil | ||
} | ||
mbmStats = append(mbmStats, *numaStats) | ||
} | ||
} | ||
|
||
return &mbmStats, nil | ||
} | ||
|
||
func getMBMNumaNodeStats(numaPath string) (*MBMNumaNodeStats, error) { | ||
stats := &MBMNumaNodeStats{} | ||
if enabledMonFeatures.mbmTotalBytes { | ||
mbmTotalBytes, err := getIntelRdtParamUint(numaPath, "mbm_total_bytes") | ||
if err != nil { | ||
return nil, err | ||
} | ||
stats.MBMTotalBytes = mbmTotalBytes | ||
} | ||
|
||
if enabledMonFeatures.mbmLocalBytes { | ||
mbmLocalBytes, err := getIntelRdtParamUint(numaPath, "mbm_local_bytes") | ||
if err != nil { | ||
return nil, err | ||
} | ||
stats.MBMLocalBytes = mbmLocalBytes | ||
} | ||
|
||
if enabledMonFeatures.llcOccupancy { | ||
llcOccupancy, err := getIntelRdtParamUint(numaPath, "llc_occupancy") | ||
if err != nil { | ||
return nil, err | ||
} | ||
stats.LLCOccupancy = llcOccupancy | ||
} | ||
return stats, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// +build linux | ||
|
||
package intelrdt | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestParseMonFeatures(t *testing.T) { | ||
t.Run("All features available", func(t *testing.T) { | ||
parsedMonFeatures, err := parseMonFeatures( | ||
strings.NewReader("mbm_total_bytes\nmbm_local_bytes\nllc_occupancy")) | ||
if err != nil { | ||
t.Errorf("Error while parsing mon features err = %v", err) | ||
} | ||
|
||
expectedMonFeatures := monFeatures{true, true, true} | ||
|
||
if parsedMonFeatures != expectedMonFeatures { | ||
t.Error("Cannot gather all features!") | ||
} | ||
}) | ||
|
||
t.Run("No features available", func(t *testing.T) { | ||
parsedMonFeatures, err := parseMonFeatures(strings.NewReader("")) | ||
|
||
if err != nil { | ||
t.Errorf("Error while parsing mon features err = %v", err) | ||
} | ||
|
||
expectedMonFeatures := monFeatures{false, false, false} | ||
|
||
if parsedMonFeatures != expectedMonFeatures { | ||
t.Error("Expected no features available but there is any!") | ||
} | ||
}) | ||
} | ||
|
||
func mockMBM(NUMANodes []string, mocks map[string]uint64) (string, error) { | ||
testDir, err := ioutil.TempDir("", "rdt_mbm_test") | ||
if err != nil { | ||
return "", err | ||
} | ||
monDataPath := filepath.Join(testDir, "mon_data") | ||
|
||
for _, numa := range NUMANodes { | ||
numaPath := filepath.Join(monDataPath, numa) | ||
err = os.MkdirAll(numaPath, os.ModePerm) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for fileName, value := range mocks { | ||
err := ioutil.WriteFile(filepath.Join(numaPath, fileName), []byte(strconv.FormatUint(value, 10)), 777) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
} | ||
|
||
return testDir, nil | ||
} | ||
|
||
func TestGetMbmStats(t *testing.T) { | ||
mocksNUMANodesToCreate := []string{"mon_l3_00", "mon_l3_01"} | ||
|
||
mocksFilesToCreate := map[string]uint64{ | ||
"mbm_total_bytes": 9123911, | ||
"mbm_local_bytes": 2361361, | ||
"llc_occupancy": 123013, | ||
} | ||
|
||
mockedMBM, err := mockMBM(mocksNUMANodesToCreate, mocksFilesToCreate) | ||
|
||
defer func() { | ||
err := os.RemoveAll(mockedMBM) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
t.Run("Gather mbm", func(t *testing.T) { | ||
enabledMonFeatures.mbmTotalBytes = true | ||
enabledMonFeatures.mbmLocalBytes = true | ||
enabledMonFeatures.llcOccupancy = true | ||
|
||
stats, err := getMBMStats(mockedMBM) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(*stats) != len(mocksNUMANodesToCreate) { | ||
t.Fatalf("Wrong number of stats slices from NUMA nodes. Expected: %v but got: %v", | ||
len(mocksNUMANodesToCreate), len(*stats)) | ||
} | ||
|
||
checkStatCorrection := func(got MBMNumaNodeStats, expected MBMNumaNodeStats, t *testing.T) { | ||
if got.MBMTotalBytes != expected.MBMTotalBytes { | ||
t.Fatalf("Wrong value of mbm_total_bytes. Expected: %v but got: %v", | ||
expected.MBMTotalBytes, | ||
got.MBMTotalBytes) | ||
} | ||
|
||
if got.MBMLocalBytes != expected.MBMLocalBytes { | ||
t.Fatalf("Wrong value of mbm_local_bytes. Expected: %v but got: %v", | ||
expected.MBMLocalBytes, | ||
got.MBMLocalBytes) | ||
} | ||
|
||
if got.LLCOccupancy != expected.LLCOccupancy { | ||
t.Fatalf("Wrong value of llc_occupancy. Expected: %v but got: %v", | ||
expected.LLCOccupancy, | ||
got.LLCOccupancy) | ||
} | ||
} | ||
|
||
expectedStats := MBMNumaNodeStats{ | ||
MBMTotalBytes: mocksFilesToCreate["mbm_total_bytes"], | ||
MBMLocalBytes: mocksFilesToCreate["mbm_local_bytes"], | ||
LLCOccupancy: mocksFilesToCreate["llc_occupancy"], | ||
} | ||
|
||
checkStatCorrection((*stats)[0], expectedStats, t) | ||
checkStatCorrection((*stats)[1], expectedStats, t) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters