File: memtotal_windows.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (26 lines) | stat: -rw-r--r-- 738 bytes parent folder | download | duplicates (2)
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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package sysinfo

import (
	"syscall"
	"unsafe"
)

// PhysicalMemoryBytes returns the total amount of host memory.
func PhysicalMemoryBytes() (uint64, error) {
	// https://msdn.microsoft.com/en-us/library/windows/desktop/cc300158(v=vs.85).aspx
	// http://stackoverflow.com/questions/30743070/query-total-physical-memory-in-windows-with-golang
	mod := syscall.NewLazyDLL("kernel32.dll")
	proc := mod.NewProc("GetPhysicallyInstalledSystemMemory")
	var memkb uint64

	ret, _, err := proc.Call(uintptr(unsafe.Pointer(&memkb)))
	// return value TRUE(1) succeeds, FAILED(0) fails
	if ret != 1 {
		return 0, err
	}

	return memkb * 1024, nil
}