format the code

pull/1530/head
Justin Yang 2 years ago
parent 4a46201e00
commit def3572629

@ -16,9 +16,9 @@ import (
const ( const (
// sys/sysctl.h // sys/sysctl.h
ctlKern = 1 // "high kernel": proc, limits ctlKern = 1 // "high kernel": proc, limits
ctlHw = 6 // CTL_HW ctlHw = 6 // CTL_HW
kernCpTime = 51 // KERN_CPTIME kernCpTime = 51 // KERN_CPTIME
) )
var ClocksPerSec = float64(100) var ClocksPerSec = float64(100)
@ -67,7 +67,7 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
return ret, err return ret, err
} }
stats := (*cpuTimes)(unsafe.Pointer(&buf[0])) stats := (*cpuTimes)(unsafe.Pointer(&buf[0]))
ret = append(ret, TimesStat{ ret = append(ret, TimesStat{
CPU: fmt.Sprintf("cpu%d", i), CPU: fmt.Sprintf("cpu%d", i),
User: float64(stats.User), User: float64(stats.User),
@ -96,7 +96,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, err = fmt.Sscanf(mhz, "%f", &c.Mhz) _, err = fmt.Sscanf(mhz, "%f", &c.Mhz)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -1,9 +1,9 @@
package cpu package cpu
type cpuTimes struct { type cpuTimes struct {
User uint64 User uint64
Nice uint64 Nice uint64
Sys uint64 Sys uint64
Intr uint64 Intr uint64
Idle uint64 Idle uint64
} }

@ -5,58 +5,58 @@ package disk
import ( import (
"context" "context"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
const ( const (
// see sys/fstypes.h and `man 5 statvfs` // see sys/fstypes.h and `man 5 statvfs`
MNT_RDONLY = 0x00000001 /* read only filesystem */ MNT_RDONLY = 0x00000001 /* read only filesystem */
MNT_SYNCHRONOUS = 0x00000002 /* file system written synchronously */ MNT_SYNCHRONOUS = 0x00000002 /* file system written synchronously */
MNT_NOEXEC = 0x00000004 /* can't exec from filesystem */ MNT_NOEXEC = 0x00000004 /* can't exec from filesystem */
MNT_NOSUID = 0x00000008 /* don't honor setuid bits on fs */ MNT_NOSUID = 0x00000008 /* don't honor setuid bits on fs */
MNT_NODEV = 0x00000010 /* don't interpret special files */ MNT_NODEV = 0x00000010 /* don't interpret special files */
MNT_ASYNC = 0x00000040 /* file system written asynchronously */ MNT_ASYNC = 0x00000040 /* file system written asynchronously */
MNT_NOATIME = 0x04000000 /* Never update access times in fs */ MNT_NOATIME = 0x04000000 /* Never update access times in fs */
MNT_SOFTDEP = 0x80000000 /* Use soft dependencies */ MNT_SOFTDEP = 0x80000000 /* Use soft dependencies */
) )
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
var ret []PartitionStat var ret []PartitionStat
flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h
// get required buffer size // get required buffer size
emptyBufSize := 0 emptyBufSize := 0
r, _, err := unix.Syscall( r, _, err := unix.Syscall(
483, // SYS___getvfsstat90 syscall 483, // SYS___getvfsstat90 syscall
uintptr(unsafe.Pointer(nil)), uintptr(unsafe.Pointer(nil)),
uintptr(unsafe.Pointer(&emptyBufSize)), uintptr(unsafe.Pointer(&emptyBufSize)),
uintptr(unsafe.Pointer(&flag)), uintptr(unsafe.Pointer(&flag)),
) )
if err != 0 { if err != 0 {
return ret, err return ret, err
} }
mountedFsCount := uint64(r) mountedFsCount := uint64(r)
// calculate the buffer size // calculate the buffer size
bufSize := sizeOfStatvfs * mountedFsCount bufSize := sizeOfStatvfs * mountedFsCount
buf := make([]Statvfs, mountedFsCount) buf := make([]Statvfs, mountedFsCount)
// request agian to get desired mount data // request agian to get desired mount data
_, _, err = unix.Syscall( _, _, err = unix.Syscall(
483, // SYS___getvfsstat90 syscall 483, // SYS___getvfsstat90 syscall
uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&bufSize)), uintptr(unsafe.Pointer(&bufSize)),
uintptr(unsafe.Pointer(&flag)), uintptr(unsafe.Pointer(&flag)),
) )
if err != 0 { if err != 0 {
return ret, err return ret, err
} }
for _, stat := range buf { for _, stat := range buf {
opts := []string{"rw"} opts := []string{"rw"}
if stat.Flag&MNT_RDONLY != 0 { if stat.Flag&MNT_RDONLY != 0 {
opts = []string{"rw"} opts = []string{"rw"}
@ -102,26 +102,26 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
} }
func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
stat := Statvfs{} stat := Statvfs{}
flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h
_path, e := unix.BytePtrFromString(path) _path, e := unix.BytePtrFromString(path)
if e != nil { if e != nil {
return nil, e return nil, e
} }
_, _, err := unix.Syscall( _, _, err := unix.Syscall(
484, // SYS___statvfs190, see sys/syscall.h 484, // SYS___statvfs190, see sys/syscall.h
uintptr(unsafe.Pointer(_path)), uintptr(unsafe.Pointer(_path)),
uintptr(unsafe.Pointer(&stat)), uintptr(unsafe.Pointer(&stat)),
uintptr(unsafe.Pointer(&flag)), uintptr(unsafe.Pointer(&flag)),
) )
if err != 0 { if err != 0 {
return nil, err return nil, err
} }
// frsize is the real block size on NetBSD. See discuss here: https://bugzilla.samba.org/show_bug.cgi?id=11810 // frsize is the real block size on NetBSD. See discuss here: https://bugzilla.samba.org/show_bug.cgi?id=11810
bsize := stat.Frsize bsize := stat.Frsize
ret := &UsageStat{ ret := &UsageStat{
Path: path, Path: path,
Fstype: getFsType(stat), Fstype: getFsType(stat),

@ -12,34 +12,34 @@ const (
type ( type (
Statvfs struct { Statvfs struct {
Flag uint64 Flag uint64
Bsize uint64 Bsize uint64
Frsize uint64 Frsize uint64
Iosize uint64 Iosize uint64
Blocks uint64 Blocks uint64
Bfree uint64 Bfree uint64
Bavail uint64 Bavail uint64
Bresvd uint64 Bresvd uint64
Files uint64 Files uint64
Ffree uint64 Ffree uint64
Favail uint64 Favail uint64
Fresvd uint64 Fresvd uint64
Syncreads uint64 Syncreads uint64
Syncwrites uint64 Syncwrites uint64
Asyncreads uint64 Asyncreads uint64
Asyncwrites uint64 Asyncwrites uint64
Fsidx _Ctype_struct___0 Fsidx _Ctype_struct___0
Fsid uint64 Fsid uint64
Namemax uint64 Namemax uint64
Owner uint32 Owner uint32
Spare [4]uint64 Spare [4]uint64
Fstypename [32]uint8 Fstypename [32]uint8
Mntonname [1024]uint8 Mntonname [1024]uint8
Mntfromname [1024]uint8 Mntfromname [1024]uint8
Mntfromlabel [1024]uint8 Mntfromlabel [1024]uint8
} }
) )
type _Ctype_struct___0 struct { type _Ctype_struct___0 struct {
FsidVal [2]int32 FsidVal [2]int32
} }

@ -47,7 +47,7 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
ret.Used = ret.Total - ret.Available ret.Used = ret.Total - ret.Available
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0 ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0
// Get buffers from vm.bufmem sysctl // Get buffers from vm.bufmem sysctl
ret.Buffers, err = unix.SysctlUint64("vm.bufmem") ret.Buffers, err = unix.SysctlUint64("vm.bufmem")
if err != nil { if err != nil {
return nil, err return nil, err

Loading…
Cancel
Save