Skip to content

Commit

Permalink
Add hugepage info to v1 node structure
Browse files Browse the repository at this point in the history
Signed-off-by: sewon.oh <[email protected]>
  • Loading branch information
ohsewon committed Sep 3, 2019
1 parent 24a6a52 commit d474239
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
7 changes: 4 additions & 3 deletions info/v1/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ type FsInfo struct {
type Node struct {
Id int `json:"node_id"`
// Per-node memory
Memory uint64 `json:"memory"`
Cores []Core `json:"cores"`
Caches []Cache `json:"caches"`
Memory uint64 `json:"memory"`
HugePages []HugePagesInfo `json:"huge_pages"`
Cores []Core `json:"cores"`
Caches []Cache `json:"caches"`
}

type Core struct {
Expand Down
46 changes: 46 additions & 0 deletions machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ var (
cpuClockSpeedMHz = regexp.MustCompile(`(?:cpu MHz|clock)\s*:\s*([0-9]+\.[0-9]+)(?:MHz)?`)
memoryCapacityRegexp = regexp.MustCompile(`MemTotal:\s*([0-9]+) kB`)
swapCapacityRegexp = regexp.MustCompile(`SwapTotal:\s*([0-9]+) kB`)
hugePageSizeRegexp = regexp.MustCompile(`hugepages-\s*([0-9]+)kB`)
)

const maxFreqFile = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
const cpuBusPath = "/sys/bus/cpu/devices/"
const nodePath = "/sys/devices/system/node"

// GetClockSpeed returns the CPU clock speed, given a []byte formatted as the /proc/cpuinfo file.
func GetClockSpeed(procInfo []byte) (uint64, error) {
Expand Down Expand Up @@ -191,6 +193,43 @@ func getNodeIdFromCpuBus(cpuBusPath string, threadId int) (int, error) {
return nodeId, nil
}

/* Look for per-node huge pages info using node id */
/* Such as: /sys/devices/system/node/node%d/hugepages */
func getHugePagesInfoFromNode(nodePath string, nodeIndex int) ([]info.HugePagesInfo, error) {
hugePagesInfo := []info.HugePagesInfo{}
path := filepath.Join(nodePath, fmt.Sprintf("node%d/hugepages", nodeIndex))
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}

for _, file := range files {
fileName := file.Name()
pageSize, err := parseCapacity([]byte(fileName), hugePageSizeRegexp)
if err != nil {
return nil, err
}

file := filepath.Join(path, fileName, "nr_hugepages")
num, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}

pageNum, err := strconv.ParseUint(string(bytes.TrimSpace(num)), 10, 64)
if err != nil {
return nil, err
}

hugePagesInfo = append(hugePagesInfo, info.HugePagesInfo{
PageSize: pageSize * 1024, // Convert to kB.
NumPages: pageNum,
})
}

return hugePagesInfo, nil
}

func GetTopology(sysFs sysfs.SysFs, cpuinfo string) ([]info.Node, int, error) {
nodes := []info.Node{}

Expand Down Expand Up @@ -305,6 +344,13 @@ func GetTopology(sysFs sysfs.SysFs, cpuinfo string) ([]info.Node, int, error) {
}
// Ignore unknown caches.
}

// Add a node-level huge pages info.
hugePagesInfo, err := getHugePagesInfoFromNode(nodePath, node.Id)
if err != nil {
return nil, -1, err
}
nodes[idx].HugePages = hugePagesInfo
}
return nodes, numCores, nil
}
Expand Down

0 comments on commit d474239

Please sign in to comment.