forked from cloudfoundry/gosigar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigar_windows.go
201 lines (174 loc) · 4.56 KB
/
sigar_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
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
// Copyright (c) 2012 VMware, Inc.
package sigar
import (
"bytes"
"errors"
"fmt"
"os/exec"
"strconv"
"syscall"
"time"
"unsafe"
)
var (
kernel32DLL = syscall.MustLoadDLL("kernel32")
procGetDiskFreeSpace = kernel32DLL.MustFindProc("GetDiskFreeSpaceW")
procGetSystemTimes = kernel32DLL.MustFindProc("GetSystemTimes")
procGetTickCount64 = kernel32DLL.MustFindProc("GetTickCount64")
procGlobalMemoryStatusEx = kernel32DLL.MustFindProc("GlobalMemoryStatusEx")
)
func (self *LoadAverage) Get() error {
return ErrNotImplemented
}
func (u *Uptime) Get() error {
r1, _, e1 := syscall.Syscall(procGetTickCount64.Addr(), 0, 0, 0, 0)
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 {
var x memorystatusex
x.Length = uint32(unsafe.Sizeof(x))
r1, _, e1 := syscall.Syscall(procGlobalMemoryStatusEx.Addr(), 1,
uintptr(unsafe.Pointer(&x)),
0,
0,
)
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 (s *Swap) Get() error {
const MB = 1024 * 1024
out, err := exec.Command("wmic", "pagefile", "list", "full").Output()
if err != nil {
return err
}
total, err := parseWmicOutput(out, []byte("AllocatedBaseSize"))
if err != nil {
return err
}
used, err := parseWmicOutput(out, []byte("CurrentUsage"))
if err != nil {
return err
}
s.Total = total * MB
s.Used = used * MB
s.Free = s.Total - s.Used
return nil
}
func parseWmicOutput(s, sep []byte) (uint64, error) {
bb := bytes.Split(s, []byte("\n"))
for i := 0; i < len(bb); i++ {
b := bytes.TrimSpace(bb[i])
n := bytes.IndexByte(b, '=')
if n > 0 && bytes.Equal(sep, b[:n]) {
return strconv.ParseUint(string(b[n+1:]), 10, 64)
}
}
return 0, errors.New("parseWmicOutput: missing field: " + string(sep))
}
func (c *Cpu) Get() error {
var (
idleTime syscall.Filetime
kernelTime syscall.Filetime // Includes kernel and idle time.
userTime syscall.Filetime
)
r1, _, e1 := syscall.Syscall(procGetSystemTimes.Addr(), 3,
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 (self *CpuList) Get() error {
return ErrNotImplemented
}
func (self *FileSystemList) Get() error {
return ErrNotImplemented
}
func (self *ProcList) Get() error {
return ErrNotImplemented
}
func (self *ProcState) Get(pid int) error {
return ErrNotImplemented
}
func (self *ProcMem) Get(pid int) error {
return ErrNotImplemented
}
func (self *ProcTime) Get(pid int) error {
return ErrNotImplemented
}
func (self *ProcArgs) Get(pid int) error {
return ErrNotImplemented
}
func (self *ProcExe) Get(pid int) error {
return ErrNotImplemented
}
func (fs *FileSystemUsage) Get(path string) error {
root, err := syscall.UTF16PtrFromString(path)
if err != nil {
return fmt.Errorf("FileSystemUsage (%s): %s", path, err)
}
var (
SectorsPerCluster uint32
BytesPerSector uint32
// Free clusters available to the user
// associated with the calling thread.
NumberOfFreeClusters uint32
// Total clusters available to the user
// associated with the calling thread.
TotalNumberOfClusters uint32
)
r1, _, e1 := syscall.Syscall6(procGetDiskFreeSpace.Addr(), 5,
uintptr(unsafe.Pointer(root)),
uintptr(unsafe.Pointer(&SectorsPerCluster)),
uintptr(unsafe.Pointer(&BytesPerSector)),
uintptr(unsafe.Pointer(&NumberOfFreeClusters)),
uintptr(unsafe.Pointer(&TotalNumberOfClusters)),
0,
)
if err := checkErrno(r1, e1); err != nil {
return fmt.Errorf("FileSystemUsage (%s): %s", path, err)
}
m := uint64(SectorsPerCluster * BytesPerSector)
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 {
if e, ok := e1.(syscall.Errno); ok && e != 0 {
return e1
}
return syscall.EINVAL
}
return nil
}