Skip to content

Commit

Permalink
Add GetProcessMemoryInfo (#2)
Browse files Browse the repository at this point in the history
* Fix typo in uft16.go filename

* Add GetProcessMemoryInfo

* Add changelog
  • Loading branch information
axw authored and andrewkroh committed Jul 12, 2018
1 parent a730c8b commit 5d078fa
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Changelog

* Add GetProcessMemoryInfo: [#2](https://github.com/elastic/go-windows/pull/2)
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ package windows

// Use "GOOS=windows go generate -v -x" to generate the sources.
// Add -trace to enable debug prints around syscalls.
//go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -systemdll=false -output zsyscall_windows.go kernel32.go version.go
//go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -systemdll=false -output zsyscall_windows.go kernel32.go version.go psapi.go
62 changes: 62 additions & 0 deletions psapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// +build windows

package windows

import (
"syscall"
"unsafe"

"github.com/pkg/errors"
)

// Syscalls
//sys _GetProcessMemoryInfo(handle syscall.Handle, psmemCounters *ProcessMemoryCountersEx, cb uint32) (err error) = psapi.GetProcessMemoryInfo

var (
sizeofProcessMemoryCountersEx = uint32(unsafe.Sizeof(ProcessMemoryCountersEx{}))
)

// ProcessMemoryCountersEx is an equivalent representation of
// PROCESS_MEMORY_COUNTERS_EX in the Windows API. It contains information about
// the memory usage of a process.
// https://docs.microsoft.com/en-au/windows/desktop/api/psapi/ns-psapi-_process_memory_counters_ex
type ProcessMemoryCountersEx struct {
cb uint32
PageFaultCount uint32
PeakWorkingSetSize uintptr
WorkingSetSize uintptr
QuotaPeakPagedPoolUsage uintptr
QuotaPagedPoolUsage uintptr
QuotaPeakNonPagedPoolUsage uintptr
QuotaNonPagedPoolUsage uintptr
PagefileUsage uintptr
PeakPagefileUsage uintptr
PrivateUsage uintptr
}

// GetProcessMemoryInfo retrieves memory info for the given process handle.
// https://docs.microsoft.com/en-us/windows/desktop/api/psapi/nf-psapi-getprocessmemoryinfo
func GetProcessMemoryInfo(process syscall.Handle) (ProcessMemoryCountersEx, error) {
var info ProcessMemoryCountersEx
if err := _GetProcessMemoryInfo(process, &info, sizeofProcessMemoryCountersEx); err != nil {
return ProcessMemoryCountersEx{}, errors.Wrap(err, "GetProcessMemoryInfo failed")
}
return info, nil
}
File renamed without changes.
14 changes: 14 additions & 0 deletions zsyscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func errnoErr(e syscall.Errno) error {
var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
modversion = syscall.NewLazyDLL("version.dll")
modpsapi = syscall.NewLazyDLL("psapi.dll")

procGetNativeSystemInfo = modkernel32.NewProc("GetNativeSystemInfo")
procGetTickCount64 = modkernel32.NewProc("GetTickCount64")
Expand All @@ -45,6 +46,7 @@ var (
procGetFileVersionInfoW = modversion.NewProc("GetFileVersionInfoW")
procGetFileVersionInfoSizeW = modversion.NewProc("GetFileVersionInfoSizeW")
procVerQueryValueW = modversion.NewProc("VerQueryValueW")
procGetProcessMemoryInfo = modpsapi.NewProc("GetProcessMemoryInfo")
)

func _GetNativeSystemInfo(systemInfo *SystemInfo) (err error) {
Expand Down Expand Up @@ -161,3 +163,15 @@ func __VerQueryValueW(data *byte, subBlock *uint16, pBuffer *uintptr, len *uint3
}
return
}

func _GetProcessMemoryInfo(handle syscall.Handle, psmemCounters *ProcessMemoryCountersEx, cb uint32) (err error) {
r1, _, e1 := syscall.Syscall(procGetProcessMemoryInfo.Addr(), 3, uintptr(handle), uintptr(unsafe.Pointer(psmemCounters)), uintptr(cb))
if r1 == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}

0 comments on commit 5d078fa

Please sign in to comment.