-
Notifications
You must be signed in to change notification settings - Fork 194
/
sigar_freebsd.go
441 lines (374 loc) · 8.78 KB
/
sigar_freebsd.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//go:build freebsd
// +build freebsd
package sigar
import (
"bytes"
"encoding/binary"
"io"
"io/ioutil"
"os"
"strconv"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
const (
kernProc = 14 // KERN_PROC in sysctl((3)
kernProcPid = 1 // KERN_PROC_PID in sysctl(3)
)
// kinfo_proc as returned by kinfo_getproc() in libutil
// see also /usr/include/sys/user.h
type procInfo struct {
Structsize int32
Layout int32
Args int64
Paddr int64
Addr int64
Tracep int64
Textvp int64
Fd int64
Vmspace int64
Wchan int64
Pid int32
Ppid int32
Pgid int32
Tpgid int32
Sid int32
Tsid int32
Jobc [2]byte
Spare_short1 [2]byte
Tdev int32
Siglist [16]byte
Sigmask [16]byte
Sigignore [16]byte
Sigcatch [16]byte
Uid int32
Ruid int32
Svuid int32
Rgid int32
Svgid int32
Ngroups [2]byte
Spare_short2 [2]byte
Groups [64]byte
Size int64
Rssize int64
Swrss int64
Tsize int64
Dsize int64
Ssize int64
Xstat [2]byte
Acflag [2]byte
Pctcpu int32
Estcpu int32
Slptime int32
Swtime int32
Cow int32
Runtime int64
Start [16]byte
Childtime [16]byte
Flag int64
Kiflag int64
Traceflag int32
Stat [1]byte
Nice [1]byte
Lock [1]byte
Rqindex [1]byte
Oncpu [1]byte
Lastcpu [1]byte
Ocomm [17]byte
Wmesg [9]byte
Login [18]byte
Lockname [9]byte
Comm [20]byte
Emul [17]byte
Sparestrings [68]byte
Spareints [36]byte
Cr_flags int32
Jid int32
Numthreads int32
Tid int32
Pri int32
Rusage [144]byte
Rusage_ch [144]byte
Pcb int64
Kstack int64
Udata int64
Tdaddr int64
Spareptrs [48]byte
Spareint64s [96]byte
Sflag int64
Tdflags int64
}
// See /usr/include/sys/proc.h
var (
procStateIdle = [1]uint8{0x1} // SIDL
procStateRun = [1]uint8{0x2} // SRUN
procStateSleep = [1]uint8{0x3} // SSLEEP
procStateStop = [1]uint8{0x4} // SSTOP
procStateZombie = [1]uint8{0x5} // SZOMB
procStateWait = [1]uint8{0x6} // SWAIT
procStateLock = [1]uint8{0x7} // SLOCK
)
var (
Procd string = "/proc"
)
type loadStruct struct {
Ldavg [3]uint32
Fscale uint64
}
func getProcInfo(pid int) (procInfo, error) {
var pinfo procInfo
pinfoRaw, err := unix.SysctlRaw("kern", kernProc, kernProcPid, pid)
if err != nil {
return procInfo{}, err
}
pinfoReader := bytes.NewReader(pinfoRaw)
err = binary.Read(pinfoReader, binary.LittleEndian, &pinfo)
if err != nil {
return procInfo{}, err
}
return pinfo, nil
}
func procFileName(pid int, name string) string {
return Procd + "/" + strconv.Itoa(pid) + "/" + name
}
func readProcFile(pid int, name string) ([]byte, error) {
path := procFileName(pid, name)
contents, err := ioutil.ReadFile(path)
if err != nil {
if perr, ok := err.(*os.PathError); ok {
if perr.Err == unix.ENOENT {
return nil, unix.ESRCH
}
}
}
return contents, err
}
func (self *Uptime) Get() error {
var tv unix.Timeval
boottimeRaw, err := unix.SysctlRaw("kern.boottime")
if err != nil {
return err
}
tv = *(*unix.Timeval)(unsafe.Pointer(&boottimeRaw[0]))
self.Length = time.Since(time.Unix(int64(tv.Sec), int64(tv.Usec)*1000)).Seconds()
return nil
}
func (self *LoadAverage) Get() error {
avgRaw, err := unix.SysctlRaw("vm.loadavg")
if err != nil {
return err
}
avg := *(*loadStruct)(unsafe.Pointer(&avgRaw[0]))
fscale := float64(avg.Fscale)
self.One = float64(avg.Ldavg[0]) / fscale
self.Five = float64(avg.Ldavg[1]) / fscale
self.Fifteen = float64(avg.Ldavg[2]) / fscale
return nil
}
func (self *ProcList) Get() error {
dir, err := os.Open(Procd)
if err != nil {
return err
}
defer dir.Close()
const readAllDirnames = -1 // see os.File.Readdirnames doc
names, err := dir.Readdirnames(readAllDirnames)
if err != nil {
return err
}
capacity := len(names)
list := make([]int, 0, capacity)
for _, name := range names {
if name[0] < '0' || name[0] > '9' {
continue
}
pid, err := strconv.Atoi(name)
if err == nil {
list = append(list, pid)
}
}
self.List = list
return nil
}
func (self *ProcState) Get(pid int) error {
pinfo, err := getProcInfo(pid)
if err != nil {
return err
}
self.Name = string(bytes.Trim(pinfo.Comm[:], "\x00"))
self.Ppid = int(pinfo.Ppid)
switch pinfo.Stat {
case procStateRun:
self.State = RunStateRun
case procStateIdle:
self.State = RunStateIdle
case procStateSleep:
self.State = RunStateSleep
case procStateStop:
self.State = RunStateStop
case procStateZombie:
self.State = RunStateZombie
default:
self.State = RunStateUnknown
}
return nil
}
func (self *FileSystemList) Get() error {
n, err := unix.Getfsstat(nil, unix.MNT_NOWAIT)
if err != nil {
return err
}
fslist := make([]FileSystem, 0, n)
buf := make([]unix.Statfs_t, n)
unix.Getfsstat(buf, unix.MNT_NOWAIT)
for _, f := range buf {
fs := FileSystem{}
fs.DirName = string(bytes.Trim(f.Mntonname[:], "\x00"))
fs.DevName = string(bytes.Trim(f.Mntfromname[:], "\x00"))
fs.SysTypeName = string(bytes.Trim(f.Fstypename[:], "\x00"))
fslist = append(fslist, fs)
}
self.List = fslist
return nil
}
func (self *FileSystemUsage) Get(path string) error {
stat := unix.Statfs_t{}
err := unix.Statfs(path, &stat)
if err != nil {
return err
}
bsize := stat.Bsize / 512
self.Total = (uint64(stat.Blocks) * uint64(bsize)) >> 1
self.Free = (uint64(stat.Bfree) * uint64(bsize)) >> 1
self.Avail = (uint64(stat.Bavail) * uint64(bsize)) >> 1
self.Used = self.Total - self.Free
self.Files = stat.Files
self.FreeFiles = uint64(stat.Ffree)
return nil
}
func (self *ProcTime) Get(pid int) error {
rusage := unix.Rusage{}
unix.Getrusage(pid, &rusage)
self.User = uint64(rusage.Utime.Nano() / 1e6)
self.Sys = uint64(rusage.Stime.Nano() / 1e6)
self.Total = self.User + self.Sys
var tv unix.Timeval
pinfo, err := getProcInfo(pid)
if err != nil {
return err
}
tv = *(*unix.Timeval)(unsafe.Pointer(&pinfo.Start[0]))
self.StartTime = (uint64(tv.Sec) * 1000) + (uint64(tv.Usec) / 1000)
return nil
}
func (self *Cpu) Get() error {
cpTime, err := unix.SysctlRaw("kern.cp_time")
if err != nil {
return err
}
cpuRaw := *(*cpuStat)(unsafe.Pointer(&cpTime[0]))
self.User = uint64(cpuRaw.user)
self.Nice = uint64(cpuRaw.nice)
self.Sys = uint64(cpuRaw.sys)
self.Irq = uint64(cpuRaw.irq)
self.Idle = uint64(cpuRaw.idle)
return nil
}
func (self *Mem) Get() error {
var err error
self.Total, err = unix.SysctlUint64("hw.physmem")
if err != nil {
return err
}
pageSize, err := unix.SysctlUint32("hw.pagesize")
if err != nil {
return err
}
freePages, err := unix.SysctlUint32("vm.stats.vm.v_free_count")
if err != nil {
return err
}
self.Free = uint64(freePages) * uint64(pageSize)
self.Used = self.Total - self.Free
return nil
}
func (self *Mem) GetIgnoringCGroups() error {
return self.Get()
}
func (self *Swap) Get() error {
var err error
self.Total, err = unix.SysctlUint64("vm.swap_total")
if err != nil {
return err
}
return nil
}
type cpuStat struct {
user int64
nice int64
sys int64
irq int64
idle int64
}
func (self *CpuList) Get() error {
cpTimes, err := unix.SysctlRaw("kern.cp_times")
if err != nil {
return err
}
// 5 values of 8 bytes (int64) per CPU
ncpu := len(cpTimes) / 8 / 5
cpulist := make([]Cpu, 0, ncpu)
for i := 0; i < ncpu; i++ {
cpuRaw := *(*cpuStat)(unsafe.Pointer(&cpTimes[i*8*5]))
cpu := Cpu{}
cpu.User = uint64(cpuRaw.user)
cpu.Nice = uint64(cpuRaw.nice)
cpu.Sys = uint64(cpuRaw.sys)
cpu.Irq = uint64(cpuRaw.irq)
cpu.Idle = uint64(cpuRaw.idle)
cpulist = append(cpulist, cpu)
}
self.List = cpulist
return nil
}
func (self *ProcMem) Get(pid int) error {
pageSize, err := unix.SysctlUint32("hw.pagesize")
if err != nil {
return err
}
rusage := unix.Rusage{}
unix.Getrusage(pid, &rusage)
self.Resident = (uint64(rusage.Ixrss) + uint64(rusage.Idrss)) * uint64(pageSize)
self.Share = uint64(rusage.Isrss) * uint64(pageSize)
self.Size = self.Resident + self.Share
self.MinorFaults = uint64(rusage.Minflt)
self.MajorFaults = uint64(rusage.Majflt)
self.PageFaults = self.MinorFaults + self.MajorFaults
return nil
}
func (self *ProcArgs) Get(pid int) error {
contents, err := readProcFile(pid, "cmdline")
if err != nil {
return err
}
bbuf := bytes.NewBuffer(contents)
var args []string
for {
arg, err := bbuf.ReadBytes(0)
if err == io.EOF {
break
}
args = append(args, string(chop(arg)))
}
self.List = args
return nil
}
func (self *ProcExe) Get(pid int) error {
var err error
self.Name, err = os.Readlink(procFileName(pid, "file"))
if err != nil {
return err
}
return nil
}