@@ -17,8 +17,10 @@ package machine
17
17
import (
18
18
"bytes"
19
19
"flag"
20
+ "fmt"
20
21
"io/ioutil"
21
22
"path/filepath"
23
+ "strconv"
22
24
"strings"
23
25
"syscall"
24
26
@@ -31,6 +33,8 @@ import (
31
33
"github.com/golang/glog"
32
34
)
33
35
36
+ const hugepagesDirectory = "/sys/kernel/mm/hugepages/"
37
+
34
38
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." )
35
39
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." )
36
40
@@ -48,6 +52,43 @@ func getInfoFromFiles(filePaths string) string {
48
52
return ""
49
53
}
50
54
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
+
51
92
func Info (sysFs sysfs.SysFs , fsInfo fs.FsInfo , inHostNamespace bool ) (* info.MachineInfo , error ) {
52
93
rootFs := "/"
53
94
if ! inHostNamespace {
@@ -65,6 +106,11 @@ func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.Mach
65
106
return nil , err
66
107
}
67
108
109
+ hugePagesInfo , err := GetHugePagesInfo ()
110
+ if err != nil {
111
+ return nil , err
112
+ }
113
+
68
114
filesystems , err := fsInfo .GetGlobalFsInfo ()
69
115
if err != nil {
70
116
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
99
145
NumCores : numCores ,
100
146
CpuFrequency : clockSpeed ,
101
147
MemoryCapacity : memoryCapacity ,
148
+ HugePages : hugePagesInfo ,
102
149
DiskMap : diskMap ,
103
150
NetworkDevices : netDevices ,
104
151
Topology : topology ,
0 commit comments