Move from the `syscall` package to the `golang.org/x/sys/{unix,windows}`

pull/380/head
Sean Chittenden 8 years ago
parent e30b7839cd
commit 635b971c0e
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16

@ -9,9 +9,9 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"syscall"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/unix"
) )
// sys/sched.h // sys/sched.h
@ -100,7 +100,7 @@ func Info() ([]InfoStat, error) {
c := InfoStat{} c := InfoStat{}
v, err := syscall.Sysctl("hw.model") v, err := unix.Sysctl("hw.model")
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -4,12 +4,11 @@ package cpu
import ( import (
"fmt" "fmt"
"syscall"
"unsafe" "unsafe"
"github.com/StackExchange/wmi" "github.com/StackExchange/wmi"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/windows"
) )
type Win32_Processor struct { type Win32_Processor struct {
@ -35,7 +34,7 @@ func Times(percpu bool) ([]TimesStat, error) {
uintptr(unsafe.Pointer(&lpKernelTime)), uintptr(unsafe.Pointer(&lpKernelTime)),
uintptr(unsafe.Pointer(&lpUserTime))) uintptr(unsafe.Pointer(&lpUserTime)))
if r == 0 { if r == 0 {
return ret, syscall.GetLastError() return ret, windows.GetLastError()
} }
LOT := float64(0.0000001) LOT := float64(0.0000001)

@ -4,10 +4,10 @@ package disk
import ( import (
"path" "path"
"syscall"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/unix"
) )
func Partitions(all bool) ([]PartitionStat, error) { func Partitions(all bool) ([]PartitionStat, error) {
@ -94,7 +94,7 @@ func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
_p0 = unsafe.Pointer(&buf[0]) _p0 = unsafe.Pointer(&buf[0])
bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf)) bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
} }
r0, _, e1 := syscall.Syscall(SYS_GETFSSTAT64, uintptr(_p0), bufsize, uintptr(flags)) r0, _, e1 := unix.Syscall(SYS_GETFSSTAT64, uintptr(_p0), bufsize, uintptr(flags))
n = int(r0) n = int(r0)
if e1 != 0 { if e1 != 0 {
err = e1 err = e1
@ -102,6 +102,6 @@ func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
return return
} }
func getFsType(stat syscall.Statfs_t) string { func getFsType(stat unix.Statfs_t) string {
return common.IntToString(stat.Fstypename[:]) return common.IntToString(stat.Fstypename[:])
} }

@ -7,9 +7,10 @@ import (
"encoding/binary" "encoding/binary"
"path" "path"
"strconv" "strconv"
"syscall"
"unsafe" "unsafe"
"golang.org/x/sys/unix"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
) )
@ -17,7 +18,7 @@ func Partitions(all bool) ([]PartitionStat, error) {
var ret []PartitionStat var ret []PartitionStat
// get length // get length
count, err := syscall.Getfsstat(nil, MNT_WAIT) count, err := unix.Getfsstat(nil, MNT_WAIT)
if err != nil { if err != nil {
return ret, err return ret, err
} }
@ -99,7 +100,7 @@ func IOCounters(names ...string) (map[string]IOCountersStat, error) {
// /usr/include/devinfo.h // /usr/include/devinfo.h
ret := make(map[string]IOCountersStat) ret := make(map[string]IOCountersStat)
r, err := syscall.Sysctl("kern.devstat.all") r, err := unix.Sysctl("kern.devstat.all")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -155,7 +156,7 @@ func Getfsstat(buf []Statfs, flags int) (n int, err error) {
_p0 = unsafe.Pointer(&buf[0]) _p0 = unsafe.Pointer(&buf[0])
bufsize = unsafe.Sizeof(Statfs{}) * uintptr(len(buf)) bufsize = unsafe.Sizeof(Statfs{}) * uintptr(len(buf))
} }
r0, _, e1 := syscall.Syscall(syscall.SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags)) r0, _, e1 := unix.Syscall(unix.SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
n = int(r0) n = int(r0)
if e1 != 0 { if e1 != 0 {
err = e1 err = e1
@ -175,6 +176,6 @@ func parseDevstat(buf []byte) (Devstat, error) {
return ds, nil return ds, nil
} }
func getFsType(stat syscall.Statfs_t) string { func getFsType(stat unix.Statfs_t) string {
return common.IntToString(stat.Fstypename[:]) return common.IntToString(stat.Fstypename[:])
} }

@ -7,7 +7,8 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"syscall"
"golang.org/x/sys/unix"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
) )
@ -386,7 +387,7 @@ func GetDiskSerialNumber(name string) string {
return "" return ""
} }
func getFsType(stat syscall.Statfs_t) string { func getFsType(stat unix.Statfs_t) string {
t := int64(stat.Type) t := int64(stat.Type)
ret, ok := fsTypeMap[t] ret, ok := fsTypeMap[t]
if !ok { if !ok {

@ -6,17 +6,17 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"path" "path"
"syscall"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/unix"
) )
func Partitions(all bool) ([]PartitionStat, error) { func Partitions(all bool) ([]PartitionStat, error) {
var ret []PartitionStat var ret []PartitionStat
// get length // get length
count, err := syscall.Getfsstat(nil, MNT_WAIT) count, err := unix.Getfsstat(nil, MNT_WAIT)
if err != nil { if err != nil {
return ret, err return ret, err
} }
@ -66,7 +66,7 @@ func Partitions(all bool) ([]PartitionStat, error) {
func IOCounters(names ...string) (map[string]IOCountersStat, error) { func IOCounters(names ...string) (map[string]IOCountersStat, error) {
ret := make(map[string]IOCountersStat) ret := make(map[string]IOCountersStat)
r, err := syscall.Sysctl("hw.diskstats") r, err := unix.Sysctl("hw.diskstats")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -112,7 +112,7 @@ func Getfsstat(buf []Statfs, flags int) (n int, err error) {
_p0 = unsafe.Pointer(&buf[0]) _p0 = unsafe.Pointer(&buf[0])
bufsize = unsafe.Sizeof(Statfs{}) * uintptr(len(buf)) bufsize = unsafe.Sizeof(Statfs{}) * uintptr(len(buf))
} }
r0, _, e1 := syscall.Syscall(syscall.SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags)) r0, _, e1 := unix.Syscall(unix.SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
n = int(r0) n = int(r0)
if e1 != 0 { if e1 != 0 {
err = e1 err = e1
@ -133,8 +133,8 @@ func parseDiskstats(buf []byte) (Diskstats, error) {
} }
func Usage(path string) (*UsageStat, error) { func Usage(path string) (*UsageStat, error) {
stat := syscall.Statfs_t{} stat := unix.Statfs_t{}
err := syscall.Statfs(path, &stat) err := unix.Statfs(path, &stat)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -157,6 +157,6 @@ func Usage(path string) (*UsageStat, error) {
return ret, nil return ret, nil
} }
func getFsType(stat syscall.Statfs_t) string { func getFsType(stat unix.Statfs_t) string {
return common.IntToString(stat.F_fstypename[:]) return common.IntToString(stat.F_fstypename[:])
} }

@ -2,11 +2,11 @@
package disk package disk
import "syscall" import "golang.org/x/sys/unix"
func Usage(path string) (*UsageStat, error) { func Usage(path string) (*UsageStat, error) {
stat := syscall.Statfs_t{} stat := unix.Statfs_t{}
err := syscall.Statfs(path, &stat) err := unix.Statfs(path, &stat)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -4,12 +4,11 @@ package disk
import ( import (
"bytes" "bytes"
"syscall"
"unsafe" "unsafe"
"github.com/StackExchange/wmi" "github.com/StackExchange/wmi"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/windows"
) )
var ( var (
@ -43,7 +42,7 @@ func Usage(path string) (*UsageStat, error) {
lpTotalNumberOfBytes := int64(0) lpTotalNumberOfBytes := int64(0)
lpTotalNumberOfFreeBytes := int64(0) lpTotalNumberOfFreeBytes := int64(0)
diskret, _, err := procGetDiskFreeSpaceExW.Call( diskret, _, err := procGetDiskFreeSpaceExW.Call(
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))), uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(path))),
uintptr(unsafe.Pointer(&lpFreeBytesAvailable)), uintptr(unsafe.Pointer(&lpFreeBytesAvailable)),
uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)), uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)),
uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes))) uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)))
@ -79,10 +78,10 @@ func Partitions(all bool) ([]PartitionStat, error) {
if path == "A:" || path == "B:" { // skip floppy drives if path == "A:" || path == "B:" { // skip floppy drives
continue continue
} }
typepath, _ := syscall.UTF16PtrFromString(path) typepath, _ := windows.UTF16PtrFromString(path)
typeret, _, _ := procGetDriveType.Call(uintptr(unsafe.Pointer(typepath))) typeret, _, _ := procGetDriveType.Call(uintptr(unsafe.Pointer(typepath)))
if typeret == 0 { if typeret == 0 {
return ret, syscall.GetLastError() return ret, windows.GetLastError()
} }
// 2: DRIVE_REMOVABLE 3: DRIVE_FIXED 4: DRIVE_REMOTE 5: DRIVE_CDROM // 2: DRIVE_REMOVABLE 3: DRIVE_FIXED 4: DRIVE_REMOTE 5: DRIVE_CDROM
@ -92,7 +91,7 @@ func Partitions(all bool) ([]PartitionStat, error) {
lpMaximumComponentLength := int64(0) lpMaximumComponentLength := int64(0)
lpFileSystemFlags := int64(0) lpFileSystemFlags := int64(0)
lpFileSystemNameBuffer := make([]byte, 256) lpFileSystemNameBuffer := make([]byte, 256)
volpath, _ := syscall.UTF16PtrFromString(string(v) + ":/") volpath, _ := windows.UTF16PtrFromString(string(v) + ":/")
driveret, _, err := provGetVolumeInformation.Call( driveret, _, err := provGetVolumeInformation.Call(
uintptr(unsafe.Pointer(volpath)), uintptr(unsafe.Pointer(volpath)),
uintptr(unsafe.Pointer(&lpVolumeNameBuffer[0])), uintptr(unsafe.Pointer(&lpVolumeNameBuffer[0])),

@ -8,14 +8,13 @@ import (
"runtime" "runtime"
"strings" "strings"
"sync/atomic" "sync/atomic"
"syscall"
"time" "time"
"unsafe" "unsafe"
"github.com/StackExchange/wmi" "github.com/StackExchange/wmi"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
process "github.com/shirou/gopsutil/process" process "github.com/shirou/gopsutil/process"
"golang.org/x/sys/windows"
) )
var ( var (
@ -80,12 +79,12 @@ func Info() (*InfoStat, error) {
} }
func getMachineGuid() (string, error) { func getMachineGuid() (string, error) {
var h syscall.Handle var h windows.Handle
err := syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, syscall.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, syscall.KEY_READ|syscall.KEY_WOW64_64KEY, &h) err := windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE, windows.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, windows.KEY_READ|windows.KEY_WOW64_64KEY, &h)
if err != nil { if err != nil {
return "", err return "", err
} }
defer syscall.RegCloseKey(h) defer windows.RegCloseKey(h)
const windowsRegBufLen = 74 // len(`{`) + len(`abcdefgh-1234-456789012-123345456671` * 2) + len(`}`) // 2 == bytes/UTF16 const windowsRegBufLen = 74 // len(`{`) + len(`abcdefgh-1234-456789012-123345456671` * 2) + len(`}`) // 2 == bytes/UTF16
const uuidLen = 36 const uuidLen = 36
@ -93,12 +92,12 @@ func getMachineGuid() (string, error) {
var regBuf [windowsRegBufLen]uint16 var regBuf [windowsRegBufLen]uint16
bufLen := uint32(windowsRegBufLen) bufLen := uint32(windowsRegBufLen)
var valType uint32 var valType uint32
err = syscall.RegQueryValueEx(h, syscall.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen) err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
if err != nil { if err != nil {
return "", err return "", err
} }
hostID := syscall.UTF16ToString(regBuf[:]) hostID := windows.UTF16ToString(regBuf[:])
hostIDLen := len(hostID) hostIDLen := len(hostID)
if hostIDLen != uuidLen { if hostIDLen != uuidLen {
return "", fmt.Errorf("HostID incorrect: %q\n", hostID) return "", fmt.Errorf("HostID incorrect: %q\n", hostID)

@ -6,8 +6,9 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"syscall"
"unsafe" "unsafe"
"golang.org/x/sys/unix"
) )
func DoSysctrl(mib string) ([]string, error) { func DoSysctrl(mib string) ([]string, error) {
@ -36,8 +37,8 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) {
// get required buffer size // get required buffer size
length := uint64(0) length := uint64(0)
_, _, err := syscall.Syscall6( _, _, err := unix.Syscall6(
syscall.SYS___SYSCTL, unix.SYS___SYSCTL,
uintptr(unsafe.Pointer(&mib[0])), uintptr(unsafe.Pointer(&mib[0])),
uintptr(miblen), uintptr(miblen),
0, 0,
@ -54,8 +55,8 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) {
} }
// get proc info itself // get proc info itself
buf := make([]byte, length) buf := make([]byte, length)
_, _, err = syscall.Syscall6( _, _, err = unix.Syscall6(
syscall.SYS___SYSCTL, unix.SYS___SYSCTL,
uintptr(unsafe.Pointer(&mib[0])), uintptr(unsafe.Pointer(&mib[0])),
uintptr(miblen), uintptr(miblen),
uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&buf[0])),

@ -6,8 +6,9 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"syscall"
"unsafe" "unsafe"
"golang.org/x/sys/unix"
) )
func DoSysctrl(mib string) ([]string, error) { func DoSysctrl(mib string) ([]string, error) {
@ -36,8 +37,8 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) {
// get required buffer size // get required buffer size
length := uint64(0) length := uint64(0)
_, _, err := syscall.Syscall6( _, _, err := unix.Syscall6(
syscall.SYS___SYSCTL, unix.SYS___SYSCTL,
uintptr(mibptr), uintptr(mibptr),
uintptr(miblen), uintptr(miblen),
0, 0,
@ -54,8 +55,8 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) {
} }
// get proc info itself // get proc info itself
buf := make([]byte, length) buf := make([]byte, length)
_, _, err = syscall.Syscall6( _, _, err = unix.Syscall6(
syscall.SYS___SYSCTL, unix.SYS___SYSCTL,
uintptr(mibptr), uintptr(mibptr),
uintptr(miblen), uintptr(miblen),
uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&buf[0])),

@ -6,8 +6,9 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"syscall"
"unsafe" "unsafe"
"golang.org/x/sys/unix"
) )
func DoSysctrl(mib string) ([]string, error) { func DoSysctrl(mib string) ([]string, error) {
@ -36,8 +37,8 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) {
// get required buffer size // get required buffer size
length := uint64(0) length := uint64(0)
_, _, err := syscall.Syscall6( _, _, err := unix.Syscall6(
syscall.SYS___SYSCTL, unix.SYS___SYSCTL,
uintptr(mibptr), uintptr(mibptr),
uintptr(miblen), uintptr(miblen),
0, 0,
@ -54,8 +55,8 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) {
} }
// get proc info itself // get proc info itself
buf := make([]byte, length) buf := make([]byte, length)
_, _, err = syscall.Syscall6( _, _, err = unix.Syscall6(
syscall.SYS___SYSCTL, unix.SYS___SYSCTL,
uintptr(mibptr), uintptr(mibptr),
uintptr(miblen), uintptr(miblen),
uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&buf[0])),

@ -3,8 +3,9 @@
package common package common
import ( import (
"syscall"
"unsafe" "unsafe"
"golang.org/x/sys/windows"
) )
// for double values // for double values
@ -44,9 +45,9 @@ const (
) )
var ( var (
Modkernel32 = syscall.NewLazyDLL("kernel32.dll") Modkernel32 = windows.NewLazyDLL("kernel32.dll")
ModNt = syscall.NewLazyDLL("ntdll.dll") ModNt = windows.NewLazyDLL("ntdll.dll")
ModPdh = syscall.NewLazyDLL("pdh.dll") ModPdh = windows.NewLazyDLL("pdh.dll")
ProcGetSystemTimes = Modkernel32.NewProc("GetSystemTimes") ProcGetSystemTimes = Modkernel32.NewProc("GetSystemTimes")
ProcNtQuerySystemInformation = ModNt.NewProc("NtQuerySystemInformation") ProcNtQuerySystemInformation = ModNt.NewProc("NtQuerySystemInformation")
@ -77,13 +78,13 @@ func BytePtrToString(p *uint8) string {
type CounterInfo struct { type CounterInfo struct {
PostName string PostName string
CounterName string CounterName string
Counter syscall.Handle Counter windows.Handle
} }
// CreateQuery XXX // CreateQuery XXX
// copied from https://github.com/mackerelio/mackerel-agent/ // copied from https://github.com/mackerelio/mackerel-agent/
func CreateQuery() (syscall.Handle, error) { func CreateQuery() (windows.Handle, error) {
var query syscall.Handle var query windows.Handle
r, _, err := PdhOpenQuery.Call(0, 0, uintptr(unsafe.Pointer(&query))) r, _, err := PdhOpenQuery.Call(0, 0, uintptr(unsafe.Pointer(&query)))
if r != 0 { if r != 0 {
return 0, err return 0, err
@ -92,11 +93,11 @@ func CreateQuery() (syscall.Handle, error) {
} }
// CreateCounter XXX // CreateCounter XXX
func CreateCounter(query syscall.Handle, pname, cname string) (*CounterInfo, error) { func CreateCounter(query windows.Handle, pname, cname string) (*CounterInfo, error) {
var counter syscall.Handle var counter windows.Handle
r, _, err := PdhAddCounter.Call( r, _, err := PdhAddCounter.Call(
uintptr(query), uintptr(query),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(cname))), uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(cname))),
0, 0,
uintptr(unsafe.Pointer(&counter))) uintptr(unsafe.Pointer(&counter)))
if r != 0 { if r != 0 {

@ -6,18 +6,18 @@ import (
"encoding/binary" "encoding/binary"
"strconv" "strconv"
"strings" "strings"
"syscall"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/unix"
) )
func getHwMemsize() (uint64, error) { func getHwMemsize() (uint64, error) {
totalString, err := syscall.Sysctl("hw.memsize") totalString, err := unix.Sysctl("hw.memsize")
if err != nil { if err != nil {
return 0, err return 0, err
} }
// syscall.sysctl() helpfully assumes the result is a null-terminated string and // unix.sysctl() helpfully assumes the result is a null-terminated string and
// removes the last byte of the result if it's 0 :/ // removes the last byte of the result if it's 0 :/
totalString += "\x00" totalString += "\x00"

@ -10,8 +10,9 @@ import "C"
import ( import (
"fmt" "fmt"
"syscall"
"unsafe" "unsafe"
"golang.org/x/sys/unix"
) )
// VirtualMemory returns VirtualmemoryStat. // VirtualMemory returns VirtualmemoryStat.
@ -28,7 +29,7 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
return nil, fmt.Errorf("host_statistics error=%d", status) return nil, fmt.Errorf("host_statistics error=%d", status)
} }
pageSize := uint64(syscall.Getpagesize()) pageSize := uint64(unix.Getpagesize())
total, err := getHwMemsize() total, err := getHwMemsize()
if err != nil { if err != nil {
return nil, err return nil, err

@ -7,7 +7,8 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"syscall"
"golang.org/x/sys/unix"
) )
// Runs vm_stat and returns Free and inactive pages // Runs vm_stat and returns Free and inactive pages
@ -27,7 +28,7 @@ func parseVMStat(out string, vms *VirtualMemoryStat) error {
var err error var err error
lines := strings.Split(out, "\n") lines := strings.Split(out, "\n")
pagesize := uint64(syscall.Getpagesize()) pagesize := uint64(unix.Getpagesize())
for _, line := range lines { for _, line := range lines {
fields := strings.Split(line, ":") fields := strings.Split(line, ":")
if len(fields) < 2 { if len(fields) < 2 {

@ -5,9 +5,9 @@ package mem
import ( import (
"strconv" "strconv"
"strings" "strings"
"syscall"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/unix"
) )
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
@ -72,9 +72,9 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
} }
func SwapMemory() (*SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
sysinfo := &syscall.Sysinfo_t{} sysinfo := &unix.Sysinfo_t{}
if err := syscall.Sysinfo(sysinfo); err != nil { if err := unix.Sysinfo(sysinfo); err != nil {
return nil, err return nil, err
} }
ret := &SwapMemoryStat{ ret := &SwapMemoryStat{

@ -3,10 +3,10 @@
package mem package mem
import ( import (
"syscall"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/windows"
) )
var ( var (
@ -30,7 +30,7 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
memInfo.cbSize = uint32(unsafe.Sizeof(memInfo)) memInfo.cbSize = uint32(unsafe.Sizeof(memInfo))
mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo))) mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo)))
if mem == 0 { if mem == 0 {
return nil, syscall.GetLastError() return nil, windows.GetLastError()
} }
ret := &VirtualMemoryStat{ ret := &VirtualMemoryStat{

@ -6,13 +6,13 @@ import (
"errors" "errors"
"net" "net"
"os" "os"
"syscall"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/windows"
) )
var ( var (
modiphlpapi = syscall.NewLazyDLL("iphlpapi.dll") modiphlpapi = windows.NewLazyDLL("iphlpapi.dll")
procGetExtendedTCPTable = modiphlpapi.NewProc("GetExtendedTcpTable") procGetExtendedTCPTable = modiphlpapi.NewProc("GetExtendedTcpTable")
procGetExtendedUDPTable = modiphlpapi.NewProc("GetExtendedUdpTable") procGetExtendedUDPTable = modiphlpapi.NewProc("GetExtendedUdpTable")
) )
@ -41,8 +41,8 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
Name: ifi.Name, Name: ifi.Name,
} }
row := syscall.MibIfRow{Index: uint32(ifi.Index)} row := windows.MibIfRow{Index: uint32(ifi.Index)}
e := syscall.GetIfEntry(&row) e := windows.GetIfEntry(&row)
if e != nil { if e != nil {
return nil, os.NewSyscallError("GetIfEntry", e) return nil, os.NewSyscallError("GetIfEntry", e)
} }

@ -9,13 +9,13 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"syscall"
"time" "time"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/net" "github.com/shirou/gopsutil/net"
"golang.org/x/sys/unix"
) )
// copied from sys/sysctl.h // copied from sys/sysctl.h
@ -443,8 +443,8 @@ func (p *Process) getKProc() (*KinfoProc, error) {
procK := KinfoProc{} procK := KinfoProc{}
length := uint64(unsafe.Sizeof(procK)) length := uint64(unsafe.Sizeof(procK))
buf := make([]byte, length) buf := make([]byte, length)
_, _, syserr := syscall.Syscall6( _, _, syserr := unix.Syscall6(
syscall.SYS___SYSCTL, unix.SYS___SYSCTL,
uintptr(unsafe.Pointer(&mib[0])), uintptr(unsafe.Pointer(&mib[0])),
uintptr(len(mib)), uintptr(len(mib)),
uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&buf[0])),

@ -6,11 +6,11 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"strings" "strings"
"syscall"
cpu "github.com/shirou/gopsutil/cpu" cpu "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
net "github.com/shirou/gopsutil/net" net "github.com/shirou/gopsutil/net"
"golang.org/x/sys/unix"
) )
// MemoryInfoExStat is different between OSes // MemoryInfoExStat is different between OSes
@ -223,7 +223,7 @@ func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
v, err := syscall.Sysctl("vm.stats.vm.v_page_size") v, err := unix.Sysctl("vm.stats.vm.v_page_size")
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -14,12 +14,12 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"syscall"
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host" "github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/net" "github.com/shirou/gopsutil/net"
"golang.org/x/sys/unix"
) )
var ( var (
@ -857,7 +857,7 @@ func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, int32, e
// p.Nice = mustParseInt32(fields[18]) // p.Nice = mustParseInt32(fields[18])
// use syscall instead of parse Stat file // use syscall instead of parse Stat file
snice, _ := syscall.Getpriority(PrioProcess, int(pid)) snice, _ := unix.Getpriority(PrioProcess, int(pid))
nice := int32(snice) // FIXME: is this true? nice := int32(snice) // FIXME: is this true?
return terminal, int32(ppid), cpuTimes, createTime, nice, nil return terminal, int32(ppid), cpuTimes, createTime, nice, nil

@ -7,13 +7,13 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"strings" "strings"
"syscall"
"unsafe" "unsafe"
cpu "github.com/shirou/gopsutil/cpu" cpu "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
mem "github.com/shirou/gopsutil/mem" mem "github.com/shirou/gopsutil/mem"
net "github.com/shirou/gopsutil/net" net "github.com/shirou/gopsutil/net"
"golang.org/x/sys/unix"
) )
// MemoryInfoExStat is different between OSes // MemoryInfoExStat is different between OSes
@ -328,8 +328,8 @@ func CallKernProcSyscall(op int32, arg int32) ([]byte, uint64, error) {
mibptr := unsafe.Pointer(&mib[0]) mibptr := unsafe.Pointer(&mib[0])
miblen := uint64(len(mib)) miblen := uint64(len(mib))
length := uint64(0) length := uint64(0)
_, _, err := syscall.Syscall6( _, _, err := unix.Syscall6(
syscall.SYS___SYSCTL, unix.SYS___SYSCTL,
uintptr(mibptr), uintptr(mibptr),
uintptr(miblen), uintptr(miblen),
0, 0,
@ -346,8 +346,8 @@ func CallKernProcSyscall(op int32, arg int32) ([]byte, uint64, error) {
miblen = uint64(len(mib)) miblen = uint64(len(mib))
// get proc info itself // get proc info itself
buf := make([]byte, length) buf := make([]byte, length)
_, _, err = syscall.Syscall6( _, _, err = unix.Syscall6(
syscall.SYS___SYSCTL, unix.SYS___SYSCTL,
uintptr(mibptr), uintptr(mibptr),
uintptr(miblen), uintptr(miblen),
uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&buf[0])),

@ -9,6 +9,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
"golang.org/x/sys/unix"
) )
// POSIX // POSIX
@ -50,8 +52,8 @@ func getTerminalMap() (map[uint64]string, error) {
} }
for _, name := range termfiles { for _, name := range termfiles {
stat := syscall.Stat_t{} stat := unix.Stat_t{}
if err = syscall.Stat(name, &stat); err != nil { if err = unix.Stat(name, &stat); err != nil {
return nil, err return nil, err
} }
rdev := uint64(stat.Rdev) rdev := uint64(stat.Rdev)
@ -60,7 +62,7 @@ func getTerminalMap() (map[uint64]string, error) {
return ret, nil return ret, nil
} }
// SendSignal sends a syscall.Signal to the process. // SendSignal sends a unix.Signal to the process.
// Currently, SIGSTOP, SIGCONT, SIGTERM and SIGKILL are supported. // Currently, SIGSTOP, SIGCONT, SIGTERM and SIGKILL are supported.
func (p *Process) SendSignal(sig syscall.Signal) error { func (p *Process) SendSignal(sig syscall.Signal) error {
process, err := os.FindProcess(int(p.Pid)) process, err := os.FindProcess(int(p.Pid))
@ -78,22 +80,22 @@ func (p *Process) SendSignal(sig syscall.Signal) error {
// Suspend sends SIGSTOP to the process. // Suspend sends SIGSTOP to the process.
func (p *Process) Suspend() error { func (p *Process) Suspend() error {
return p.SendSignal(syscall.SIGSTOP) return p.SendSignal(unix.SIGSTOP)
} }
// Resume sends SIGCONT to the process. // Resume sends SIGCONT to the process.
func (p *Process) Resume() error { func (p *Process) Resume() error {
return p.SendSignal(syscall.SIGCONT) return p.SendSignal(unix.SIGCONT)
} }
// Terminate sends SIGTERM to the process. // Terminate sends SIGTERM to the process.
func (p *Process) Terminate() error { func (p *Process) Terminate() error {
return p.SendSignal(syscall.SIGTERM) return p.SendSignal(unix.SIGTERM)
} }
// Kill sends SIGKILL to the process. // Kill sends SIGKILL to the process.
func (p *Process) Kill() error { func (p *Process) Kill() error {
return p.SendSignal(syscall.SIGKILL) return p.SendSignal(unix.SIGKILL)
} }
// Username returns a username of the process. // Username returns a username of the process.

@ -4,15 +4,16 @@ package process
import ( import (
"os" "os"
"syscall"
"testing" "testing"
"golang.org/x/sys/unix"
) )
func Test_SendSignal(t *testing.T) { func Test_SendSignal(t *testing.T) {
checkPid := os.Getpid() checkPid := os.Getpid()
p, _ := NewProcess(int32(checkPid)) p, _ := NewProcess(int32(checkPid))
err := p.SendSignal(syscall.SIGCONT) err := p.SendSignal(unix.SIGCONT)
if err != nil { if err != nil {
t.Errorf("send signal %v", err) t.Errorf("send signal %v", err)
} }

@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
"syscall"
"time" "time"
"unsafe" "unsafe"
@ -16,6 +15,7 @@ import (
cpu "github.com/shirou/gopsutil/cpu" cpu "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
net "github.com/shirou/gopsutil/net" net "github.com/shirou/gopsutil/net"
"golang.org/x/sys/windows"
) )
const ( const (
@ -24,7 +24,7 @@ const (
) )
var ( var (
modpsapi = syscall.NewLazyDLL("psapi.dll") modpsapi = windows.NewLazyDLL("psapi.dll")
procGetProcessMemoryInfo = modpsapi.NewProc("GetProcessMemoryInfo") procGetProcessMemoryInfo = modpsapi.NewProc("GetProcessMemoryInfo")
) )
@ -298,7 +298,7 @@ func NewProcess(pid int32) (*Process, error) {
return p, nil return p, nil
} }
func (p *Process) SendSignal(sig syscall.Signal) error { func (p *Process) SendSignal(sig windows.Signal) error {
return common.ErrNotImplementedError return common.ErrNotImplementedError
} }
@ -316,7 +316,7 @@ func (p *Process) Terminate() error {
w32.CloseHandle(proc) w32.CloseHandle(proc)
if ret == false { if ret == false {
return syscall.GetLastError() return windows.GetLastError()
} else { } else {
return nil return nil
} }
@ -329,23 +329,23 @@ func (p *Process) Kill() error {
func (p *Process) getFromSnapProcess(pid int32) (int32, int32, string, error) { func (p *Process) getFromSnapProcess(pid int32) (int32, int32, string, error) {
snap := w32.CreateToolhelp32Snapshot(w32.TH32CS_SNAPPROCESS, uint32(pid)) snap := w32.CreateToolhelp32Snapshot(w32.TH32CS_SNAPPROCESS, uint32(pid))
if snap == 0 { if snap == 0 {
return 0, 0, "", syscall.GetLastError() return 0, 0, "", windows.GetLastError()
} }
defer w32.CloseHandle(snap) defer w32.CloseHandle(snap)
var pe32 w32.PROCESSENTRY32 var pe32 w32.PROCESSENTRY32
pe32.DwSize = uint32(unsafe.Sizeof(pe32)) pe32.DwSize = uint32(unsafe.Sizeof(pe32))
if w32.Process32First(snap, &pe32) == false { if w32.Process32First(snap, &pe32) == false {
return 0, 0, "", syscall.GetLastError() return 0, 0, "", windows.GetLastError()
} }
if pe32.Th32ProcessID == uint32(pid) { if pe32.Th32ProcessID == uint32(pid) {
szexe := syscall.UTF16ToString(pe32.SzExeFile[:]) szexe := windows.UTF16ToString(pe32.SzExeFile[:])
return int32(pe32.Th32ParentProcessID), int32(pe32.CntThreads), szexe, nil return int32(pe32.Th32ParentProcessID), int32(pe32.CntThreads), szexe, nil
} }
for w32.Process32Next(snap, &pe32) { for w32.Process32Next(snap, &pe32) {
if pe32.Th32ProcessID == uint32(pid) { if pe32.Th32ProcessID == uint32(pid) {
szexe := syscall.UTF16ToString(pe32.SzExeFile[:]) szexe := windows.UTF16ToString(pe32.SzExeFile[:])
return int32(pe32.Th32ParentProcessID), int32(pe32.CntThreads), szexe, nil return int32(pe32.Th32ParentProcessID), int32(pe32.CntThreads), szexe, nil
} }
} }
@ -388,22 +388,22 @@ func getProcInfo(pid int32) (*SystemProcessInformation, error) {
uintptr(unsafe.Pointer(&bufferSize)), uintptr(unsafe.Pointer(&bufferSize)),
uintptr(unsafe.Pointer(&bufferSize))) uintptr(unsafe.Pointer(&bufferSize)))
if ret != 0 { if ret != 0 {
return nil, syscall.GetLastError() return nil, windows.GetLastError()
} }
return &sysProcInfo, nil return &sysProcInfo, nil
} }
func getRusage(pid int32) (*syscall.Rusage, error) { func getRusage(pid int32) (*windows.Rusage, error) {
var CPU syscall.Rusage var CPU windows.Rusage
c, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, uint32(pid)) c, err := windows.OpenProcess(windows.PROCESS_QUERY_INFORMATION, false, uint32(pid))
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer syscall.CloseHandle(c) defer windows.CloseHandle(c)
if err := syscall.GetProcessTimes(c, &CPU.CreationTime, &CPU.ExitTime, &CPU.KernelTime, &CPU.UserTime); err != nil { if err := windows.GetProcessTimes(c, &CPU.CreationTime, &CPU.ExitTime, &CPU.KernelTime, &CPU.UserTime); err != nil {
return nil, err return nil, err
} }
@ -412,11 +412,11 @@ func getRusage(pid int32) (*syscall.Rusage, error) {
func getMemoryInfo(pid int32) (PROCESS_MEMORY_COUNTERS, error) { func getMemoryInfo(pid int32) (PROCESS_MEMORY_COUNTERS, error) {
var mem PROCESS_MEMORY_COUNTERS var mem PROCESS_MEMORY_COUNTERS
c, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, uint32(pid)) c, err := windows.OpenProcess(windows.PROCESS_QUERY_INFORMATION, false, uint32(pid))
if err != nil { if err != nil {
return mem, err return mem, err
} }
defer syscall.CloseHandle(c) defer windows.CloseHandle(c)
if err := getProcessMemoryInfo(c, &mem); err != nil { if err := getProcessMemoryInfo(c, &mem); err != nil {
return mem, err return mem, err
} }
@ -424,13 +424,13 @@ func getMemoryInfo(pid int32) (PROCESS_MEMORY_COUNTERS, error) {
return mem, err return mem, err
} }
func getProcessMemoryInfo(h syscall.Handle, mem *PROCESS_MEMORY_COUNTERS) (err error) { func getProcessMemoryInfo(h windows.Handle, mem *PROCESS_MEMORY_COUNTERS) (err error) {
r1, _, e1 := syscall.Syscall(procGetProcessMemoryInfo.Addr(), 3, uintptr(h), uintptr(unsafe.Pointer(mem)), uintptr(unsafe.Sizeof(*mem))) r1, _, e1 := windows.Syscall(procGetProcessMemoryInfo.Addr(), 3, uintptr(h), uintptr(unsafe.Pointer(mem)), uintptr(unsafe.Sizeof(*mem)))
if r1 == 0 { if r1 == 0 {
if e1 != 0 { if e1 != 0 {
err = error(e1) err = error(e1)
} else { } else {
err = syscall.EINVAL err = windows.EINVAL
} }
} }
return return

Loading…
Cancel
Save