1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-15 04:57:56 +00:00

source/memory: refactor numa detection into a separate func

This commit is contained in:
Markus Lehtonen 2019-01-25 16:29:20 +02:00
parent c91af2a2f4
commit c52d01a6cb

View file

@ -17,8 +17,8 @@ limitations under the License.
package memory package memory
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"log"
"strings" "strings"
"sigs.k8s.io/node-feature-discovery/source" "sigs.k8s.io/node-feature-discovery/source"
@ -34,20 +34,33 @@ func (s Source) Name() string { return "memory" }
func (s Source) Discover() (source.Features, error) { func (s Source) Discover() (source.Features, error) {
features := source.Features{} features := source.Features{}
// Detect NUMA
numa, err := isNuma()
if err != nil {
log.Printf("ERROR: failed to detect NUMA topology: %s", err)
} else if numa {
features["numa"] = true
}
return features, nil
}
// Detect if the platform has NUMA topology
func isNuma() (bool, error) {
// Find out how many nodes are online // Find out how many nodes are online
// Multiple nodes is a sign of NUMA // Multiple nodes is a sign of NUMA
bytes, err := ioutil.ReadFile("/sys/devices/system/node/online") bytes, err := ioutil.ReadFile("/sys/devices/system/node/online")
if err != nil { if err != nil {
return nil, fmt.Errorf("can't read /sys/devices/system/node/online: %s", err.Error()) return false, err
} }
// File content is expected to be: // File content is expected to be:
// "0\n" in one-node case // "0\n" in one-node case
// "0-K\n" in N-node case where K=N-1 // "0-K\n" in N-node case where K=N-1
// presence of newline requires TrimSpace // presence of newline requires TrimSpace
if strings.TrimSpace(string(bytes)) != "0" { if strings.TrimSpace(string(bytes)) != "0" {
// more than one node means NUMA // more than one node means NUMA
features["numa"] = true return true, nil
} }
return false, nil
return features, nil
} }