Skip to content

Commit

Permalink
Machine info introspects hugepages
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwaynecarr committed Aug 12, 2017
1 parent 0443713 commit 556c7b1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
11 changes: 11 additions & 0 deletions info/v1/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func (self *Node) AddPerCoreCache(c Cache) {
}
}

type HugePagesInfo struct {
// huge page size (in kB)
PageSize uint64 `json:"page_size"`

// number of huge pages
NumPages uint64 `json:"num_pages"`
}

type DiskInfo struct {
// device name
Name string `json:"name"`
Expand Down Expand Up @@ -158,6 +166,9 @@ type MachineInfo struct {
// The amount of memory (in bytes) in this machine
MemoryCapacity uint64 `json:"memory_capacity"`

// HugePages on this machine.
HugePages []HugePagesInfo `json:"hugepages"`

// The machine id
MachineID string `json:"machine_id"`

Expand Down
3 changes: 3 additions & 0 deletions info/v2/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type Attributes struct {
// The system uuid
SystemUUID string `json:"system_uuid"`

// HugePages on this machine.
HugePages []v1.HugePagesInfo `json:"hugepages"`

// Filesystems on this machine.
Filesystems []v1.FsInfo `json:"filesystems"`

Expand Down
47 changes: 47 additions & 0 deletions machine/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package machine
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"syscall"

Expand All @@ -31,6 +33,8 @@ import (
"github.com/golang/glog"
)

const hugepagesDirectory = "/sys/kernel/mm/hugepages/"

var machineIdFilePath = flag.String("machine_id_file", "/etc/machine-id,/var/lib/dbus/machine-id", "Comma-separated list of files to check for machine-id. Use the first one that exists.")
var bootIdFilePath = flag.String("boot_id_file", "/proc/sys/kernel/random/boot_id", "Comma-separated list of files to check for boot-id. Use the first one that exists.")

Expand All @@ -48,6 +52,43 @@ func getInfoFromFiles(filePaths string) string {
return ""
}

// GetHugePagesInfo returns information about pre-allocated huge pages
func GetHugePagesInfo() ([]info.HugePagesInfo, error) {
var hugePagesInfo []info.HugePagesInfo
files, err := ioutil.ReadDir(hugepagesDirectory)
if err != nil {
return hugePagesInfo, err
}
for _, st := range files {
nameArray := strings.Split(st.Name(), "-")
pageSizeArray := strings.Split(nameArray[1], "kB")
pageSize, err := strconv.ParseUint(string(pageSizeArray[0]), 10, 64)
if err != nil {
return hugePagesInfo, err
}

numFile := hugepagesDirectory + st.Name() + "/nr_hugepages"
val, err := ioutil.ReadFile(numFile)
if err != nil {
return hugePagesInfo, err
}
var numPages uint64
// we use sscanf as the file as a new-line that trips up ParseUint
// it returns the number of tokens successfully parsed, so if
// n != 1, it means we were unable to parse a number from the file
n, err := fmt.Sscanf(string(val), "%d", &numPages)
if err != nil || n != 1 {
return hugePagesInfo, fmt.Errorf("could not parse file %v contents %q", numFile, string(val))
}

hugePagesInfo = append(hugePagesInfo, info.HugePagesInfo{
NumPages: numPages,
PageSize: pageSize,
})
}
return hugePagesInfo, nil
}

func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.MachineInfo, error) {
rootFs := "/"
if !inHostNamespace {
Expand All @@ -65,6 +106,11 @@ func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.Mach
return nil, err
}

hugePagesInfo, err := GetHugePagesInfo()
if err != nil {
return nil, err
}

filesystems, err := fsInfo.GetGlobalFsInfo()
if err != nil {
glog.Errorf("Failed to get global filesystem information: %v", err)
Expand Down Expand Up @@ -99,6 +145,7 @@ func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.Mach
NumCores: numCores,
CpuFrequency: clockSpeed,
MemoryCapacity: memoryCapacity,
HugePages: hugePagesInfo,
DiskMap: diskMap,
NetworkDevices: netDevices,
Topology: topology,
Expand Down

0 comments on commit 556c7b1

Please sign in to comment.