github.com/newrelic/go-agent@v3.26.0+incompatible/internal/sysinfo/memtotal_solaris_test.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 "errors" 8 "os/exec" 9 "regexp" 10 "strconv" 11 "strings" 12 "testing" 13 ) 14 15 func TestPhysicalMemoryBytes(t *testing.T) { 16 prtconf, err := prtconfMemoryBytes() 17 if err != nil { 18 t.Fatal(err) 19 } 20 21 sysconf, err := PhysicalMemoryBytes() 22 if err != nil { 23 t.Fatal(err) 24 } 25 26 // The pagesize*pages calculation, although standard (the JVM, at least, 27 // uses this approach), doesn't match up exactly with the number 28 // returned by prtconf. 29 if sysconf > prtconf || sysconf < (prtconf-prtconf/20) { 30 t.Fatal(prtconf, sysconf) 31 } 32 } 33 34 var ( 35 ptrconfRe = regexp.MustCompile(`[Mm]emory\s*size:\s*([0-9]+)\s*([a-zA-Z]+)`) 36 ) 37 38 func prtconfMemoryBytes() (uint64, error) { 39 output, err := exec.Command("/usr/sbin/prtconf").Output() 40 if err != nil { 41 return 0, err 42 } 43 44 m := ptrconfRe.FindSubmatch(output) 45 if m == nil { 46 return 0, errors.New("memory size not found in prtconf output") 47 } 48 49 size, err := strconv.ParseUint(string(m[1]), 10, 64) 50 if err != nil { 51 return 0, err 52 } 53 54 switch strings.ToLower(string(m[2])) { 55 case "megabytes", "mb": 56 return size * 1024 * 1024, nil 57 case "kilobytes", "kb": 58 return size * 1024, nil 59 default: 60 return 0, errors.New("couldn't parse memory size in prtconf output") 61 } 62 }