github.com/newrelic/go-agent@v3.26.0+incompatible/internal/sysinfo/memtotal_windows.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  	"syscall"
     8  	"unsafe"
     9  )
    10  
    11  // PhysicalMemoryBytes returns the total amount of host memory.
    12  func PhysicalMemoryBytes() (uint64, error) {
    13  	// https://msdn.microsoft.com/en-us/library/windows/desktop/cc300158(v=vs.85).aspx
    14  	// http://stackoverflow.com/questions/30743070/query-total-physical-memory-in-windows-with-golang
    15  	mod := syscall.NewLazyDLL("kernel32.dll")
    16  	proc := mod.NewProc("GetPhysicallyInstalledSystemMemory")
    17  	var memkb uint64
    18  
    19  	ret, _, err := proc.Call(uintptr(unsafe.Pointer(&memkb)))
    20  	// return value TRUE(1) succeeds, FAILED(0) fails
    21  	if ret != 1 {
    22  		return 0, err
    23  	}
    24  
    25  	return memkb * 1024, nil
    26  }