github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/sysinfo/memtotal.go (about) 1 package sysinfo 2 3 import ( 4 "bufio" 5 "errors" 6 "io" 7 "regexp" 8 "strconv" 9 ) 10 11 // BytesToMebibytes converts bytes into mebibytes. 12 func BytesToMebibytes(bts uint64) uint64 { 13 return bts / ((uint64)(1024 * 1024)) 14 } 15 16 var ( 17 meminfoRe = regexp.MustCompile(`^MemTotal:\s+([0-9]+)\s+[kK]B$`) 18 errMemTotalNotFound = errors.New("supported MemTotal not found in /proc/meminfo") 19 ) 20 21 // parseProcMeminfo is used to parse Linux's "/proc/meminfo". It is located 22 // here so that the relevant cross agent tests will be run on all platforms. 23 func parseProcMeminfo(f io.Reader) (uint64, error) { 24 scanner := bufio.NewScanner(f) 25 for scanner.Scan() { 26 if m := meminfoRe.FindSubmatch(scanner.Bytes()); m != nil { 27 kb, err := strconv.ParseUint(string(m[1]), 10, 64) 28 if err != nil { 29 return 0, err 30 } 31 return kb * 1024, nil 32 } 33 } 34 35 err := scanner.Err() 36 if err == nil { 37 err = errMemTotalNotFound 38 } 39 return 0, err 40 }