Skip to content

Commit 556c7b1

Browse files
Machine info introspects hugepages
1 parent 0443713 commit 556c7b1

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

info/v1/machine.go

+11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ func (self *Node) AddPerCoreCache(c Cache) {
9494
}
9595
}
9696

97+
type HugePagesInfo struct {
98+
// huge page size (in kB)
99+
PageSize uint64 `json:"page_size"`
100+
101+
// number of huge pages
102+
NumPages uint64 `json:"num_pages"`
103+
}
104+
97105
type DiskInfo struct {
98106
// device name
99107
Name string `json:"name"`
@@ -158,6 +166,9 @@ type MachineInfo struct {
158166
// The amount of memory (in bytes) in this machine
159167
MemoryCapacity uint64 `json:"memory_capacity"`
160168

169+
// HugePages on this machine.
170+
HugePages []HugePagesInfo `json:"hugepages"`
171+
161172
// The machine id
162173
MachineID string `json:"machine_id"`
163174

info/v2/machine.go

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ type Attributes struct {
5252
// The system uuid
5353
SystemUUID string `json:"system_uuid"`
5454

55+
// HugePages on this machine.
56+
HugePages []v1.HugePagesInfo `json:"hugepages"`
57+
5558
// Filesystems on this machine.
5659
Filesystems []v1.FsInfo `json:"filesystems"`
5760

machine/info.go

+47
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ package machine
1717
import (
1818
"bytes"
1919
"flag"
20+
"fmt"
2021
"io/ioutil"
2122
"path/filepath"
23+
"strconv"
2224
"strings"
2325
"syscall"
2426

@@ -31,6 +33,8 @@ import (
3133
"github.com/golang/glog"
3234
)
3335

36+
const hugepagesDirectory = "/sys/kernel/mm/hugepages/"
37+
3438
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.")
3539
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.")
3640

@@ -48,6 +52,43 @@ func getInfoFromFiles(filePaths string) string {
4852
return ""
4953
}
5054

55+
// GetHugePagesInfo returns information about pre-allocated huge pages
56+
func GetHugePagesInfo() ([]info.HugePagesInfo, error) {
57+
var hugePagesInfo []info.HugePagesInfo
58+
files, err := ioutil.ReadDir(hugepagesDirectory)
59+
if err != nil {
60+
return hugePagesInfo, err
61+
}
62+
for _, st := range files {
63+
nameArray := strings.Split(st.Name(), "-")
64+
pageSizeArray := strings.Split(nameArray[1], "kB")
65+
pageSize, err := strconv.ParseUint(string(pageSizeArray[0]), 10, 64)
66+
if err != nil {
67+
return hugePagesInfo, err
68+
}
69+
70+
numFile := hugepagesDirectory + st.Name() + "/nr_hugepages"
71+
val, err := ioutil.ReadFile(numFile)
72+
if err != nil {
73+
return hugePagesInfo, err
74+
}
75+
var numPages uint64
76+
// we use sscanf as the file as a new-line that trips up ParseUint
77+
// it returns the number of tokens successfully parsed, so if
78+
// n != 1, it means we were unable to parse a number from the file
79+
n, err := fmt.Sscanf(string(val), "%d", &numPages)
80+
if err != nil || n != 1 {
81+
return hugePagesInfo, fmt.Errorf("could not parse file %v contents %q", numFile, string(val))
82+
}
83+
84+
hugePagesInfo = append(hugePagesInfo, info.HugePagesInfo{
85+
NumPages: numPages,
86+
PageSize: pageSize,
87+
})
88+
}
89+
return hugePagesInfo, nil
90+
}
91+
5192
func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.MachineInfo, error) {
5293
rootFs := "/"
5394
if !inHostNamespace {
@@ -65,6 +106,11 @@ func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.Mach
65106
return nil, err
66107
}
67108

109+
hugePagesInfo, err := GetHugePagesInfo()
110+
if err != nil {
111+
return nil, err
112+
}
113+
68114
filesystems, err := fsInfo.GetGlobalFsInfo()
69115
if err != nil {
70116
glog.Errorf("Failed to get global filesystem information: %v", err)
@@ -99,6 +145,7 @@ func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.Mach
99145
NumCores: numCores,
100146
CpuFrequency: clockSpeed,
101147
MemoryCapacity: memoryCapacity,
148+
HugePages: hugePagesInfo,
102149
DiskMap: diskMap,
103150
NetworkDevices: netDevices,
104151
Topology: topology,

0 commit comments

Comments
 (0)