Skip to content

Commit

Permalink
add swap support in nfd
Browse files Browse the repository at this point in the history
  • Loading branch information
kannon92 committed Feb 13, 2024
1 parent e0b8a52 commit 7e36011
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/utils/hostpath/hostpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var (
VarDir = HostDir(pathPrefix + "var")
// LibDir is where the /lib directory of the system to be inspected is located
LibDir = HostDir(pathPrefix + "lib")
// ProcDir is where the /proc directory of the system to be inspected is located
ProcDir = HostDir(pathPrefix + "proc")
)

// HostDir is a helper for handling host system directories
Expand Down
39 changes: 39 additions & 0 deletions source/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package memory

import (
"encoding/csv"
"fmt"
"os"
"path/filepath"
Expand All @@ -40,6 +41,9 @@ const NvFeature = "nv"
// NumaFeature is the name of the feature set that holds all NUMA related features.
const NumaFeature = "numa"

// SwapFeature is the name of the feature set that holds all Swap related features
const SwapFeature = "swap"

// memorySource implements the FeatureSource and LabelSource interfaces.
type memorySource struct {
features *nfdv1alpha1.Features
Expand Down Expand Up @@ -68,6 +72,11 @@ func (s *memorySource) GetLabels() (source.FeatureLabels, error) {
labels["numa"] = true
}

// Swap
if isSwap, ok := features.Attributes[SwapFeature].Elements["is_swap"]; ok && isSwap == "true" {
labels["swap"] = true
}

// NVDIMM
if len(features.Instances[NvFeature].Elements) > 0 {
labels["nv.present"] = true
Expand All @@ -93,6 +102,13 @@ func (s *memorySource) Discover() error {
s.features.Attributes[NumaFeature] = nfdv1alpha1.AttributeFeatureSet{Elements: numa}
}

// Detect Swap
if swap, err := detectSwap(); err != nil {
klog.ErrorS(err, "failed to detect Swap nodes")
} else {
s.features.Attributes[SwapFeature] = nfdv1alpha1.AttributeFeatureSet{Elements: swap}
}

// Detect NVDIMM
if nv, err := detectNv(); err != nil {
klog.ErrorS(err, "failed to detect nvdimm devices")
Expand All @@ -113,6 +129,29 @@ func (s *memorySource) GetFeatures() *nfdv1alpha1.Features {
return s.features
}

// detectSwap detects Swap node information
func detectSwap() (map[string]string, error) {
procBasePath := hostpath.ProcDir.Path("swaps")
file, err := os.Open(procBasePath)
defer file.Close()
if err != nil {
return nil, err
}

// Read the CSV data
reader := csv.NewReader(file)
reader.FieldsPerRecord = -1 // Allow variable number of fields
data, err := reader.ReadAll()
if err != nil {
return nil, err
}
// /proc/swaps has a header row
// If there is more than a header then we assume we have swap.
return map[string]string{
"is_swap": strconv.FormatBool(len(data[0]) > 1),
}, nil
}

// detectNuma detects NUMA node information
func detectNuma() (map[string]string, error) {
sysfsBasePath := hostpath.SysfsDir.Path("bus/node/devices")
Expand Down

0 comments on commit 7e36011

Please sign in to comment.