1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package sysinfo
import (
"errors"
"os/exec"
"regexp"
"strconv"
"testing"
)
var re = regexp.MustCompile(`hw\.memsize:\s*(\d+)`)
func darwinSysctlMemoryBytes() (uint64, error) {
out, err := exec.Command("/usr/sbin/sysctl", "hw.memsize").Output()
if err != nil {
return 0, err
}
match := re.FindSubmatch(out)
if match == nil {
return 0, errors.New("memory size not found in sysctl output")
}
bts, err := strconv.ParseUint(string(match[1]), 10, 64)
if err != nil {
return 0, err
}
return bts, nil
}
func TestPhysicalMemoryBytes(t *testing.T) {
mem, err := PhysicalMemoryBytes()
if err != nil {
t.Fatal(err)
}
mem2, err := darwinSysctlMemoryBytes()
if nil != err {
t.Fatal(err)
}
if mem != mem2 {
t.Error(mem, mem2)
}
}
|