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