-
Notifications
You must be signed in to change notification settings - Fork 58
/
os_darwin.go
230 lines (199 loc) · 5.51 KB
/
os_darwin.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
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
//go:build darwin
// +build darwin
package kgo
import (
"encoding/binary"
"fmt"
"golang.org/x/sys/unix"
"os/exec"
"strconv"
"strings"
"sync/atomic"
"time"
)
// cachedBootTime must be accessed via atomic.Load/StoreUint64
var cachedBootTime uint64
// 系统IO信息
var cacheIOInfos []byte
// bootTime 获取系统启动时间,秒.
func bootTime() (uint64, error) {
t := atomic.LoadUint64(&cachedBootTime)
if t != 0 {
return t, nil
}
tv, err := unix.SysctlTimeval("kern.boottime")
if err != nil {
return 0, err
}
atomic.StoreUint64(&cachedBootTime, uint64(tv.Sec))
return uint64(tv.Sec), nil
}
// getIOInfos 获取系统IO信息
func (ko *LkkOS) getIOInfos() []byte {
if len(cacheIOInfos) == 0 {
_, cacheIOInfos, _ = ko.System("ioreg -l")
}
return cacheIOInfos
}
// MemoryUsage 获取内存使用率,单位字节.
// 参数 virtual(仅支持linux),是否取虚拟内存.
// used为已用,
// free为空闲,
// total为总数.
func (ko *LkkOS) MemoryUsage(virtual bool) (used, free, total uint64) {
totalStr, err := unix.Sysctl("hw.memsize")
if err != nil {
return
}
vm_stat, err := exec.LookPath("vm_stat")
if err != nil {
return
}
_, out, _ := ko.Exec(vm_stat)
lines := strings.Split(string(out), "\n")
pagesize := uint64(unix.Getpagesize())
var inactive uint64
for _, line := range lines {
fields := strings.Split(line, ":")
if len(fields) < 2 {
continue
}
key := strings.TrimSpace(fields[0])
value := strings.Trim(fields[1], " .")
switch key {
case "Pages free":
f, e := strconv.ParseUint(value, 10, 64)
if e != nil {
err = e
}
free = f * pagesize
case "Pages inactive":
ina, e := strconv.ParseUint(value, 10, 64)
if e != nil {
err = e
}
inactive = ina * pagesize
}
}
// unix.sysctl() helpfully assumes the result is a null-terminated string and
// removes the last byte of the result if it's 0 :/
totalStr += "\x00"
total = uint64(binary.LittleEndian.Uint64([]byte(totalStr)))
used = total - (free + inactive)
return
}
// DiskUsage 获取磁盘(目录)使用情况,单位字节.参数path为路径.
// used为已用,
// free为空闲,
// total为总数.
func (ko *LkkOS) DiskUsage(path string) (used, free, total uint64) {
stat := unix.Statfs_t{}
err := unix.Statfs(path, &stat)
if err != nil {
return
}
total = uint64(stat.Blocks) * uint64(stat.Bsize)
free = uint64(stat.Bavail) * uint64(stat.Bsize)
used = (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(stat.Bsize)
return
}
// Uptime 获取系统运行时间,秒.
func (ko *LkkOS) Uptime() (uint64, error) {
boot, err := bootTime()
if err != nil {
return 0, err
}
res := uint64(time.Now().Unix()) - boot
return res, nil
}
// GetBiosInfo 获取BIOS信息.
// 注意:Mac机器没有BIOS信息,它使用EFI,所以该函数将返回空信息.
func (ko *LkkOS) GetBiosInfo() *BiosInfo {
res := &BiosInfo{
Vendor: "",
Version: "",
Date: "",
}
return res
}
// GetBoardInfo 获取Board信息.
func (ko *LkkOS) GetBoardInfo() *BoardInfo {
res := &BoardInfo{
Name: "",
Vendor: "",
Version: "",
Serial: "",
AssetTag: "",
}
infos := ko.getIOInfos()
if len(infos) > 0 {
infoStr := string(infos)
res.Name = trim(KStr.GetEquationValue(infoStr, "product-name"), "<", ">", `"`, `'`)
res.Version = trim(KStr.GetEquationValue(infoStr, "board-id"), "<", ">", `"`, `'`)
res.Serial = trim(KStr.GetEquationValue(infoStr, "IOPlatformUUID"), "<", ">", `"`, `'`)
}
return res
}
// GetCpuInfo 获取CPU信息.
func (ko *LkkOS) GetCpuInfo() *CpuInfo {
var res = &CpuInfo{
Vendor: "",
Model: "",
Speed: "",
Cache: 0,
Cpus: 0,
Cores: 0,
Threads: 0,
}
//调用系统命令获取,例如
//$ sysctl machdep.cpu
//machdep.cpu.cores_per_package: 10
//machdep.cpu.core_count: 10
//machdep.cpu.logical_per_package: 10
//machdep.cpu.thread_count: 10
//machdep.cpu.brand_string: Apple M1 Pro
//machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTSE64 MON DSCPL VMX EST TM2 SSSE3 CX16 TPR PDCM SSE4.1 SSE4.2 AES SEGLIM64
//machdep.cpu.feature_bits: 151121000215084031
//machdep.cpu.family: 6
//打印CPU信息
//cpuInfos, _ := unix.Sysctl("machdep.cpu")
//println("cpuInfos: ", cpuInfos)
res.Model, _ = unix.Sysctl("machdep.cpu.brand_string")
res.Vendor, _ = unix.Sysctl("machdep.cpu.vendor") //有可能为空
cacheSize, _ := unix.SysctlUint32("machdep.cpu.cache.size") //有可能为空
cpus, _ := unix.SysctlUint32("hw.physicalcpu")
cores, _ := unix.SysctlUint32("machdep.cpu.core_count")
threads, _ := unix.SysctlUint32("machdep.cpu.thread_count")
res.Cache = uint(cacheSize)
res.Cpus = uint(cpus)
res.Cores = uint(cores)
res.Threads = uint(threads)
// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
if err == nil {
speed := float64(cpuFrequency) / 1000000.0
res.Speed = KNum.NumberFormat(speed, 2, ".", "")
}
return res
}
// GetPidByPort 根据端口号获取监听的进程PID.
// linux可能要求root权限;
// darwin依赖lsof;
// windows依赖netstat.
func (ko *LkkOS) GetPidByPort(port int) (pid int) {
command := fmt.Sprintf("lsof -i tcp:%d", port)
_, out, _ := ko.System(command)
lines := strings.Split(string(out), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) >= 9 && isNumeric(fields[1]) {
p := toInt(fields[1])
if p > 0 {
pid = p
break
}
}
}
return
}