From 7f9e55b000d903b386e1dfba000950279902b5c4 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 8 Jan 2020 09:39:05 +0000 Subject: [PATCH] Use Getfsstat from golang.org/x/sys/unix on OpenBSD Use the syscall wrapper and types from golang.org/x/sys/unix instead of implementing them locally. Also remove unused generated types and consts. Follow-up for #810 and #812 --- disk/disk_openbsd.go | 26 ++------------------- disk/disk_openbsd_386.go | 43 ----------------------------------- disk/disk_openbsd_amd64.go | 46 +------------------------------------- disk/types_openbsd.go | 26 --------------------- 4 files changed, 3 insertions(+), 138 deletions(-) diff --git a/disk/disk_openbsd.go b/disk/disk_openbsd.go index ab17eac..cd9e294 100644 --- a/disk/disk_openbsd.go +++ b/disk/disk_openbsd.go @@ -7,7 +7,6 @@ import ( "context" "encoding/binary" "path" - "unsafe" "github.com/shirou/gopsutil/internal/common" "golang.org/x/sys/unix" @@ -26,8 +25,8 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro return ret, err } - fs := make([]Statfs, count) - if _, err = Getfsstat(fs, unix.MNT_WAIT); err != nil { + fs := make([]unix.Statfs_t, count) + if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil { return ret, err } @@ -123,27 +122,6 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC // BT2LD(time) ((long double)(time).sec + (time).frac * BINTIME_SCALE) -// Getfsstat is borrowed from pkg/syscall/syscall_freebsd.go -// change Statfs_t to Statfs in order to get more information -func Getfsstat(buf []Statfs, flags int) (n int, err error) { - return GetfsstatWithContext(context.Background(), buf, flags) -} - -func GetfsstatWithContext(ctx context.Context, buf []Statfs, flags int) (n int, err error) { - var _p0 unsafe.Pointer - var bufsize uintptr - if len(buf) > 0 { - _p0 = unsafe.Pointer(&buf[0]) - bufsize = unsafe.Sizeof(Statfs{}) * uintptr(len(buf)) - } - r0, _, e1 := unix.Syscall(unix.SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags)) - n = int(r0) - if e1 != 0 { - err = e1 - } - return -} - func parseDiskstats(buf []byte) (Diskstats, error) { var ds Diskstats br := bytes.NewReader(buf) diff --git a/disk/disk_openbsd_386.go b/disk/disk_openbsd_386.go index c01ff5a..2b1c720 100644 --- a/disk/disk_openbsd_386.go +++ b/disk/disk_openbsd_386.go @@ -4,13 +4,6 @@ package disk const ( - sizeofPtr = 0x4 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x4 - sizeofLongLong = 0x8 - sizeofLongDouble = 0x8 - DEVSTAT_NO_DATA = 0x00 DEVSTAT_READ = 0x01 DEVSTAT_WRITE = 0x02 @@ -21,39 +14,6 @@ const ( sizeOfDiskstats = 0x60 ) -type ( - _C_short int16 - _C_int int32 - _C_long int32 - _C_long_long int64 - _C_long_double int64 -) - -type Statfs struct { - F_flags uint32 - F_bsize uint32 - F_iosize uint32 - F_blocks uint64 - F_bfree uint64 - F_bavail int64 - F_files uint64 - F_ffree uint64 - F_favail int64 - F_syncwrites uint64 - F_syncreads uint64 - F_asyncwrites uint64 - F_asyncreads uint64 - F_fsid Fsid - F_namemax uint32 - F_owner uint32 - F_ctime uint64 - F_fstypename [16]int8 - F_mntonname [90]int8 - F_mntfromname [90]int8 - F_mntfromspec [90]int8 - Pad_cgo_0 [2]byte - Mount_info [160]byte -} type Diskstats struct { Name [16]int8 Busy int32 @@ -66,9 +26,6 @@ type Diskstats struct { Timestamp Timeval Time Timeval } -type Fsid struct { - Val [2]int32 -} type Timeval struct { Sec int64 Usec int32 diff --git a/disk/disk_openbsd_amd64.go b/disk/disk_openbsd_amd64.go index c9dcedc..7c9ceaa 100644 --- a/disk/disk_openbsd_amd64.go +++ b/disk/disk_openbsd_amd64.go @@ -1,16 +1,9 @@ -// Created by cgo -godefs - DO NOT EDIT +// Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs types_openbsd.go package disk const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 - sizeofLongDouble = 0x8 - DEVSTAT_NO_DATA = 0x00 DEVSTAT_READ = 0x01 DEVSTAT_WRITE = 0x02 @@ -21,40 +14,6 @@ const ( sizeOfDiskstats = 0x70 ) -type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 - _C_long_double int64 -) - -type Statfs struct { - F_flags uint32 - F_bsize uint32 - F_iosize uint32 - Pad_cgo_0 [4]byte - F_blocks uint64 - F_bfree uint64 - F_bavail int64 - F_files uint64 - F_ffree uint64 - F_favail int64 - F_syncwrites uint64 - F_syncreads uint64 - F_asyncwrites uint64 - F_asyncreads uint64 - F_fsid Fsid - F_namemax uint32 - F_owner uint32 - F_ctime uint64 - F_fstypename [16]int8 - F_mntonname [90]int8 - F_mntfromname [90]int8 - F_mntfromspec [90]int8 - Pad_cgo_1 [2]byte - Mount_info [160]byte -} type Diskstats struct { Name [16]int8 Busy int32 @@ -68,9 +27,6 @@ type Diskstats struct { Timestamp Timeval Time Timeval } -type Fsid struct { - Val [2]int32 -} type Timeval struct { Sec int64 Usec int64 diff --git a/disk/types_openbsd.go b/disk/types_openbsd.go index 79122a4..90f83dd 100644 --- a/disk/types_openbsd.go +++ b/disk/types_openbsd.go @@ -11,24 +11,10 @@ package disk #include #include #include - -enum { - sizeofPtr = sizeof(void*), -}; - */ import "C" -// Machine characteristics; for internal use. - const ( - sizeofPtr = C.sizeofPtr - sizeofShort = C.sizeof_short - sizeofInt = C.sizeof_int - sizeofLong = C.sizeof_long - sizeofLongLong = C.sizeof_longlong - sizeofLongDouble = C.sizeof_longlong - DEVSTAT_NO_DATA = 0x00 DEVSTAT_READ = 0x01 DEVSTAT_WRITE = 0x02 @@ -39,19 +25,7 @@ const ( sizeOfDiskstats = C.sizeof_struct_diskstats ) -// Basic types - -type ( - _C_short C.short - _C_int C.int - _C_long C.long - _C_long_long C.longlong - _C_long_double C.longlong -) - -type Statfs C.struct_statfs type Diskstats C.struct_diskstats -type Fsid C.fsid_t type Timeval C.struct_timeval type Diskstat C.struct_diskstat