-
Notifications
You must be signed in to change notification settings - Fork 2
/
phymem_windows.go
73 lines (64 loc) · 2.06 KB
/
phymem_windows.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package phymem
import (
"syscall"
"unsafe"
"github.com/koron-go/phymem/internal/psapi"
)
// for test.
const providedCurrent = true
// Current get physical memory which used by current process.
func Current() (uint, error) {
m, err := currentPMC()
if err != nil {
return 0, err
}
return uint(m.WorkingSetSize), nil
}
var zeroPMC = psapi.ProcessMemoryCounters{}
func currentPMC() (psapi.ProcessMemoryCounters, error) {
p, err := syscall.GetCurrentProcess()
if err != nil {
return zeroPMC, err
}
var m psapi.ProcessMemoryCounters
err = psapi.GetProcessMemoryInfo(p, &m, uint32(unsafe.Sizeof(m)))
if err != nil {
return zeroPMC, err
}
return m, nil
}
// ProcessMemoryCounters is set of counters related memory of the process.
// This type is provided for Windows only.
type ProcessMemoryCounters struct {
PageFaultCount uint32
PeakWorkingSetSize uint
WorkingSetSize uint
QuotaPeakPagedPoolUsage uint
QuotaPagedPoolUsage uint
QuotaPeakNonPagedPoolUsage uint
QuotaNonPagedPoolUsage uint
PagefileUsage uint
PeakPagefileUsage uint
}
func toProcessMemoryCounters(src psapi.ProcessMemoryCounters) ProcessMemoryCounters {
return ProcessMemoryCounters{
PageFaultCount: src.PageFaultCount,
PeakWorkingSetSize: uint(src.PeakWorkingSetSize),
WorkingSetSize: uint(src.WorkingSetSize),
QuotaPeakPagedPoolUsage: uint(src.QuotaPeakPagedPoolUsage),
QuotaPagedPoolUsage: uint(src.QuotaPagedPoolUsage),
QuotaPeakNonPagedPoolUsage: uint(src.QuotaPeakNonPagedPoolUsage),
QuotaNonPagedPoolUsage: uint(src.QuotaNonPagedPoolUsage),
PagefileUsage: uint(src.PagefileUsage),
PeakPagefileUsage: uint(src.PeakPagefileUsage),
}
}
// CurrentProcessMemoryCounters get ProcessMemoryCounters for current process.
// This function is provided for Windows only.
func CurrentProcessMemoryCounters() (ProcessMemoryCounters, error) {
m, err := currentPMC()
if err != nil {
return ProcessMemoryCounters{}, err
}
return toProcessMemoryCounters(m), nil
}