-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathsigar_windows.go
More file actions
235 lines (202 loc) · 6.69 KB
/
sigar_windows.go
File metadata and controls
235 lines (202 loc) · 6.69 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package sigar
import (
"errors"
"fmt"
"syscall"
"time"
"unsafe"
"github.com/cloudfoundry/gosigar/sys/windows"
)
var (
kernel32DLL = syscall.MustLoadDLL("kernel32")
procGetDiskFreeSpace = kernel32DLL.MustFindProc("GetDiskFreeSpaceW")
procGetSystemTimes = kernel32DLL.MustFindProc("GetSystemTimes")
procGetTickCount64 = kernel32DLL.MustFindProc("GetTickCount64")
procGlobalMemoryStatusEx = kernel32DLL.MustFindProc("GlobalMemoryStatusEx")
// processQueryLimitedInfoAccess is set to PROCESS_QUERY_INFORMATION for Windows
// 2003 and XP where PROCESS_QUERY_LIMITED_INFORMATION is unknown. For all newer
// OS versions it is set to PROCESS_QUERY_LIMITED_INFORMATION.
processQueryLimitedInfoAccess = windows.PROCESS_QUERY_LIMITED_INFORMATION
)
func (la *LoadAverage) Get() error { //nolint:staticcheck
return ErrNotImplemented
}
func (u *Uptime) Get() error { //nolint:staticcheck
r1, _, e1 := syscall.SyscallN(procGetTickCount64.Addr())
if e1 != 0 {
return error(e1)
}
u.Length = (time.Duration(r1) * time.Millisecond).Seconds()
return nil
}
type memorystatusex struct {
Length uint32
MemoryLoad uint32
TotalPhys uint64
AvailPhys uint64
TotalPageFile uint64
AvailPageFile uint64
TotalVirtual uint64
AvailVirtual uint64
AvailExtendedVirtual uint64
}
func (m *Mem) Get() error { //nolint:staticcheck
var x memorystatusex
x.Length = uint32(unsafe.Sizeof(x))
r1, _, e1 := syscall.SyscallN(procGlobalMemoryStatusEx.Addr(),
uintptr(unsafe.Pointer(&x)),
)
if err := checkErrno(r1, e1); err != nil {
return fmt.Errorf("GlobalMemoryStatusEx: %s", err)
}
m.Total = x.TotalPhys
m.Free = x.AvailPhys
m.ActualFree = m.Free
m.Used = m.Total - m.Free
m.ActualUsed = m.Used
return nil
}
func (m *Mem) GetIgnoringCGroups() error { //nolint:staticcheck
return m.Get()
}
func (s *Swap) Get() error { //nolint:staticcheck
memoryStatusEx, err := windows.GlobalMemoryStatusEx()
if err != nil {
return fmt.Errorf("GlobalMemoryStatusEx: %w", err)
}
s.Total = memoryStatusEx.TotalPageFile
s.Free = memoryStatusEx.AvailPageFile
s.Used = s.Total - s.Free
return nil
}
func (c *Cpu) Get() error { //nolint:staticcheck
var (
idleTime syscall.Filetime
kernelTime syscall.Filetime // Includes kernel and idle time.
userTime syscall.Filetime
)
r1, _, e1 := syscall.SyscallN(procGetSystemTimes.Addr(),
uintptr(unsafe.Pointer(&idleTime)),
uintptr(unsafe.Pointer(&kernelTime)),
uintptr(unsafe.Pointer(&userTime)),
)
if err := checkErrno(r1, e1); err != nil {
return fmt.Errorf("GetSystemTimes: %s", err)
}
c.Idle = uint64(idleTime.Nanoseconds())
c.Sys = uint64(kernelTime.Nanoseconds()) - c.Idle
c.User = uint64(userTime.Nanoseconds())
return nil
}
func (cl *CpuList) Get() error { //nolint:staticcheck
return ErrNotImplemented
}
func (fsl *FileSystemList) Get() error { //nolint:staticcheck
return ErrNotImplemented
}
func (pl *ProcList) Get() error { //nolint:staticcheck
return ErrNotImplemented
}
func (ps *ProcState) Get(pid int) error { //nolint:staticcheck
return ErrNotImplemented
}
func (pm *ProcMem) Get(pid int) error { //nolint:staticcheck
handle, err := syscall.OpenProcess(processQueryLimitedInfoAccess|windows.PROCESS_VM_READ, false, uint32(pid))
if err != nil {
return fmt.Errorf("OpenProcess failed for pid=%v %w", pid, err)
}
defer syscall.CloseHandle(handle) //nolint:errcheck
counters, err := windows.GetProcessMemoryInfo(handle)
if err != nil {
return fmt.Errorf("GetProcessMemoryInfo failed for pid=%v %w", pid, err)
}
pm.Resident = uint64(counters.WorkingSetSize)
pm.Size = uint64(counters.PrivateUsage)
return nil
}
func (pt *ProcTime) Get(pid int) error { //nolint:staticcheck
handle, err := syscall.OpenProcess(processQueryLimitedInfoAccess, false, uint32(pid))
if err != nil {
return fmt.Errorf("OpenProcess failed for pid=%v %w", pid, err)
}
defer syscall.CloseHandle(handle) //nolint:errcheck
var CPU syscall.Rusage
if err := syscall.GetProcessTimes(handle, &CPU.CreationTime, &CPU.ExitTime, &CPU.KernelTime, &CPU.UserTime); err != nil {
return fmt.Errorf("GetProcessTimes failed for pid=%v %w", pid, err)
}
// Windows epoch times are expressed as time elapsed since midnight on
// January 1, 1601, at Greenwich, England. This converts the Filetime to
// unix epoch in milliseconds.
pt.StartTime = uint64(CPU.CreationTime.Nanoseconds() / 1e6)
// Convert to millis.
pt.User = uint64(windows.FiletimeToDuration(&CPU.UserTime).Nanoseconds() / 1e6)
pt.Sys = uint64(windows.FiletimeToDuration(&CPU.KernelTime).Nanoseconds() / 1e6)
pt.Total = pt.User + pt.Sys
return nil
}
func (pa *ProcArgs) Get(pid int) error { //nolint:staticcheck
handle, err := syscall.OpenProcess(processQueryLimitedInfoAccess|windows.PROCESS_VM_READ, false, uint32(pid))
if err != nil {
return fmt.Errorf("OpenProcess failed for pid=%v %w", pid, err)
}
defer syscall.CloseHandle(handle) //nolint:errcheck
pbi, err := windows.NtQueryProcessBasicInformation(handle)
if err != nil {
return fmt.Errorf("NtQueryProcessBasicInformation failed for pid=%v %w", pid, err)
}
userProcParams, err := windows.GetUserProcessParams(handle, pbi)
if err != nil {
return nil
}
argsW, err := windows.ReadProcessUnicodeString(handle, &userProcParams.CommandLine)
if err == nil {
pa.List, err = windows.ByteSliceToStringSlice(argsW)
if err != nil {
return err
}
}
return nil
}
func (pe *ProcExe) Get(pid int) error { //nolint:staticcheck
return ErrNotImplemented
}
func (fs *FileSystemUsage) Get(path string) error { //nolint:staticcheck
root, err := syscall.UTF16PtrFromString(path)
if err != nil {
return fmt.Errorf("FileSystemUsage (%s): %s", path, err)
}
var (
SectorsPerCluster uint32
BytesPerSector uint32
// NumberOfFreeClusters available to the user associated with the calling thread.
NumberOfFreeClusters uint32
// TotalNumberOfClusters available to the user associated with the calling thread.
TotalNumberOfClusters uint32
)
r1, _, e1 := syscall.SyscallN(procGetDiskFreeSpace.Addr(),
uintptr(unsafe.Pointer(root)),
uintptr(unsafe.Pointer(&SectorsPerCluster)),
uintptr(unsafe.Pointer(&BytesPerSector)),
uintptr(unsafe.Pointer(&NumberOfFreeClusters)),
uintptr(unsafe.Pointer(&TotalNumberOfClusters)),
)
if err := checkErrno(r1, e1); err != nil {
return fmt.Errorf("FileSystemUsage (%s): %s", path, err)
}
m := uint64(SectorsPerCluster * BytesPerSector / 1024)
fs.Total = uint64(TotalNumberOfClusters) * m
fs.Free = uint64(NumberOfFreeClusters) * m
fs.Avail = fs.Free
fs.Used = fs.Total - fs.Free
return nil
}
func checkErrno(r1 uintptr, e1 error) error {
if r1 == 0 {
var e syscall.Errno
if errors.As(e1, &e) && e != 0 {
return e1
}
return syscall.EINVAL
}
return nil
}