MCPcopy
hub / github.com/google/cadvisor / GetMachineMemoryByType

Function GetMachineMemoryByType

lib/machine/machine.go:194–241  ·  view source on GitHub ↗

GetMachineMemoryByType returns information about memory capacity and number of DIMMs. Information is retrieved from sysfs edac per-DIMM API (/sys/devices/system/edac/mc/) introduced in kernel 3.6. Documentation can be found at https://www.kernel.org/doc/Documentation/admin-guide/ras.rst. Full list o

(edacPath string)

Source from the content-addressed store, hash-verified

192// Full list of memory types can be found in edac_mc.c
193// (https://github.com/torvalds/linux/blob/v5.5/drivers/edac/edac_mc.c#L198)
194func GetMachineMemoryByType(edacPath string) (map[string]*info.MemoryInfo, error) {
195 memory := map[string]*info.MemoryInfo{}
196 names, err := os.ReadDir(edacPath)
197 // On some architectures (such as ARM) memory controller device may not exist.
198 // If this is the case then we ignore error and return empty slice.
199 _, ok := err.(*os.PathError)
200 if err != nil && ok {
201 return memory, nil
202 } else if err != nil {
203 return memory, err
204 }
205 for _, controllerDir := range names {
206 controller := controllerDir.Name()
207 if !isMemoryController.MatchString(controller) {
208 continue
209 }
210 dimms, err := os.ReadDir(path.Join(edacPath, controllerDir.Name()))
211 if err != nil {
212 return map[string]*info.MemoryInfo{}, err
213 }
214 for _, dimmDir := range dimms {
215 dimm := dimmDir.Name()
216 if !isDimm.MatchString(dimm) {
217 continue
218 }
219 memType, err := os.ReadFile(path.Join(edacPath, controller, dimm, memTypeFileName))
220 if err != nil {
221 return map[string]*info.MemoryInfo{}, err
222 }
223 readableMemType := strings.TrimSpace(string(memType))
224 if _, exists := memory[readableMemType]; !exists {
225 memory[readableMemType] = &info.MemoryInfo{}
226 }
227 size, err := os.ReadFile(path.Join(edacPath, controller, dimm, sizeFileName))
228 if err != nil {
229 return map[string]*info.MemoryInfo{}, err
230 }
231 capacity, err := strconv.Atoi(strings.TrimSpace(string(size)))
232 if err != nil {
233 return map[string]*info.MemoryInfo{}, err
234 }
235 memory[readableMemType].Capacity += uint64(mbToBytes(capacity))
236 memory[readableMemType].DimmCount++
237 }
238 }
239
240 return memory, nil
241}
242
243func mbToBytes(megabytes int) int {
244 return megabytes * 1024 * 1024

Callers 3

InfoFunction · 0.85
TestMemoryInfoFunction · 0.85

Calls 3

mbToBytesFunction · 0.85
ReadFileMethod · 0.80
NameMethod · 0.65

Used in the wild real call sites across dependent graphs

searching dependent graphs…