1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-29 13:49:21 +08:00

Merge pull request #636 from shirou/feature/update_xswdev_version_freebsd_12

[mem]freebsd: update xswdev_version to adapt FreeBSD 12.
This commit is contained in:
shirou 2019-02-16 01:14:15 +09:00 committed by GitHub
commit 9294781cc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -84,11 +84,22 @@ func SwapMemory() (*SwapMemoryStat, error) {
// Constants from vm/vm_param.h // Constants from vm/vm_param.h
// nolint: golint // nolint: golint
const ( const (
XSWDEV_VERSION = 1 XSWDEV_VERSION11 = 1
XSWDEV_VERSION = 2
) )
// Types from vm/vm_param.h // Types from vm/vm_param.h
type xswdev struct { type xswdev struct {
Version uint32 // Version is the version
Dev uint64 // Dev is the device identifier
Flags int32 // Flags is the swap flags applied to the device
NBlks int32 // NBlks is the total number of blocks
Used int32 // Used is the number of blocks used
}
// xswdev11 is a compatiblity for under FreeBSD 11
// sys/vm/swap_pager.c
type xswdev11 struct {
Version uint32 // Version is the version Version uint32 // Version is the version
Dev uint32 // Dev is the device identifier Dev uint32 // Dev is the device identifier
Flags int32 // Flags is the swap flags applied to the device Flags int32 // Flags is the swap flags applied to the device
@ -123,12 +134,23 @@ func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
return nil, err return nil, err
} }
// first, try to parse with version 2
xsw := (*xswdev)(unsafe.Pointer(&buf[0])) xsw := (*xswdev)(unsafe.Pointer(&buf[0]))
if xsw.Version != XSWDEV_VERSION { if xsw.Version == XSWDEV_VERSION11 {
return nil, errors.New("xswdev version mismatch") // this is version 1, so try to parse again
xsw := (*xswdev11)(unsafe.Pointer(&buf[0]))
if xsw.Version != XSWDEV_VERSION11 {
return nil, errors.New("xswdev version mismatch(11)")
} }
s.Total += uint64(xsw.NBlks) s.Total += uint64(xsw.NBlks)
s.Used += uint64(xsw.Used) s.Used += uint64(xsw.Used)
} else if xsw.Version != XSWDEV_VERSION {
return nil, errors.New("xswdev version mismatch")
} else {
s.Total += uint64(xsw.NBlks)
s.Used += uint64(xsw.Used)
}
} }
if s.Total != 0 { if s.Total != 0 {