Update vendor

This commit is contained in:
vcaesar 2018-02-25 16:17:15 +08:00
parent bdcad6d55d
commit a62a48767d
38 changed files with 692 additions and 82 deletions

View File

@ -1,6 +1,7 @@
package cpu package cpu
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"runtime" "runtime"
@ -12,6 +13,9 @@ import (
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
) )
// TimesStat contains the amounts of time the CPU has spent performing different
// kinds of work. Time units are in USER_HZ or Jiffies (typically hundredths of
// a second). It is based on linux /proc/stat file.
type TimesStat struct { type TimesStat struct {
CPU string `json:"cpu"` CPU string `json:"cpu"`
User float64 `json:"user"` User float64 `json:"user"`
@ -61,6 +65,10 @@ func init() {
} }
func Counts(logical bool) (int, error) { func Counts(logical bool) (int, error) {
return CountsWithContext(context.Background(), logical)
}
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
return runtime.NumCPU(), nil return runtime.NumCPU(), nil
} }
@ -134,6 +142,10 @@ func calculateAllBusy(t1, t2 []TimesStat) ([]float64, error) {
// If an interval of 0 is given it will compare the current cpu times against the last call. // If an interval of 0 is given it will compare the current cpu times against the last call.
// Returns one value per cpu, or a single value if percpu is set to false. // Returns one value per cpu, or a single value if percpu is set to false.
func Percent(interval time.Duration, percpu bool) ([]float64, error) { func Percent(interval time.Duration, percpu bool) ([]float64, error) {
return PercentWithContext(context.Background(), interval, percpu)
}
func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool) ([]float64, error) {
if interval <= 0 { if interval <= 0 {
return percentUsedFromLastCall(percpu) return percentUsedFromLastCall(percpu)
} }

View File

@ -3,6 +3,7 @@
package cpu package cpu
import ( import (
"context"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
@ -22,6 +23,10 @@ const (
var ClocksPerSec = float64(128) var ClocksPerSec = float64(128)
func Times(percpu bool) ([]TimesStat, error) { func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu)
}
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
if percpu { if percpu {
return perCPUTimes() return perCPUTimes()
} }
@ -31,6 +36,10 @@ func Times(percpu bool) ([]TimesStat, error) {
// Returns only one CPUInfoStat on FreeBSD // Returns only one CPUInfoStat on FreeBSD
func Info() ([]InfoStat, error) { func Info() ([]InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
var ret []InfoStat var ret []InfoStat
sysctl, err := exec.LookPath("/usr/sbin/sysctl") sysctl, err := exec.LookPath("/usr/sbin/sysctl")
if err != nil { if err != nil {

View File

@ -3,13 +3,23 @@
package cpu package cpu
import ( import (
"context"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
) )
func Times(percpu bool) ([]TimesStat, error) { func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu)
}
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
return []TimesStat{}, common.ErrNotImplementedError return []TimesStat{}, common.ErrNotImplementedError
} }
func Info() ([]InfoStat, error) { func Info() ([]InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
return []InfoStat{}, common.ErrNotImplementedError return []InfoStat{}, common.ErrNotImplementedError
} }

View File

@ -1,6 +1,7 @@
package cpu package cpu
import ( import (
"context"
"fmt" "fmt"
"os/exec" "os/exec"
"reflect" "reflect"
@ -50,6 +51,10 @@ func timeStat(name string, t *cpuTimes) *TimesStat {
} }
func Times(percpu bool) ([]TimesStat, error) { func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu)
}
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
if percpu { if percpu {
buf, err := unix.SysctlRaw("kern.cp_times") buf, err := unix.SysctlRaw("kern.cp_times")
if err != nil { if err != nil {
@ -87,6 +92,10 @@ func Times(percpu bool) ([]TimesStat, error) {
// count, however is accurate and it is assumed that all InfoStat attributes // count, however is accurate and it is assumed that all InfoStat attributes
// are the same across CPUs. // are the same across CPUs.
func Info() ([]InfoStat, error) { func Info() ([]InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
const dmesgBoot = "/var/run/dmesg.boot" const dmesgBoot = "/var/run/dmesg.boot"
c, num, err := parseDmesgBoot(dmesgBoot) c, num, err := parseDmesgBoot(dmesgBoot)

View File

@ -3,6 +3,7 @@
package cpu package cpu
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"os/exec" "os/exec"
@ -30,6 +31,10 @@ func init() {
} }
func Times(percpu bool) ([]TimesStat, error) { func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu)
}
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
filename := common.HostProc("stat") filename := common.HostProc("stat")
var lines = []string{} var lines = []string{}
if percpu { if percpu {
@ -104,6 +109,10 @@ func finishCPUInfo(c *InfoStat) error {
// For example a single socket board with two cores each with HT will // For example a single socket board with two cores each with HT will
// return 4 CPUInfoStat structs on Linux and the "Cores" field set to 1. // return 4 CPUInfoStat structs on Linux and the "Cores" field set to 1.
func Info() ([]InfoStat, error) { func Info() ([]InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
filename := common.HostProc("cpuinfo") filename := common.HostProc("cpuinfo")
lines, _ := common.ReadLines(filename) lines, _ := common.ReadLines(filename)

View File

@ -4,6 +4,7 @@ package cpu
import ( import (
"bytes" "bytes"
"context"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"os/exec" "os/exec"
@ -49,6 +50,10 @@ func init() {
} }
func Times(percpu bool) ([]TimesStat, error) { func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu)
}
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
var ret []TimesStat var ret []TimesStat
var ncpu int var ncpu int
@ -96,6 +101,10 @@ func Times(percpu bool) ([]TimesStat, error) {
// Returns only one (minimal) CPUInfoStat on OpenBSD // Returns only one (minimal) CPUInfoStat on OpenBSD
func Info() ([]InfoStat, error) { func Info() ([]InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
var ret []InfoStat var ret []InfoStat
c := InfoStat{} c := InfoStat{}

View File

@ -1,6 +1,7 @@
package cpu package cpu
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"os/exec" "os/exec"
@ -30,10 +31,18 @@ func init() {
} }
func Times(percpu bool) ([]TimesStat, error) { func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu)
}
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
return []TimesStat{}, common.ErrNotImplementedError return []TimesStat{}, common.ErrNotImplementedError
} }
func Info() ([]InfoStat, error) { func Info() ([]InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
psrInfo, err := exec.LookPath("/usr/sbin/psrinfo") psrInfo, err := exec.LookPath("/usr/sbin/psrinfo")
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot find psrinfo: %s", err) return nil, fmt.Errorf("cannot find psrinfo: %s", err)

View File

@ -3,6 +3,7 @@
package cpu package cpu
import ( import (
"context"
"fmt" "fmt"
"unsafe" "unsafe"
@ -45,6 +46,10 @@ type Win32_PerfFormattedData_PerfOS_System struct {
// Times returns times stat per cpu and combined for all CPUs // Times returns times stat per cpu and combined for all CPUs
func Times(percpu bool) ([]TimesStat, error) { func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu)
}
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
if percpu { if percpu {
return perCPUTimes() return perCPUTimes()
} }
@ -78,11 +83,14 @@ func Times(percpu bool) ([]TimesStat, error) {
} }
func Info() ([]InfoStat, error) { func Info() ([]InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
var ret []InfoStat var ret []InfoStat
var dst []Win32_Processor var dst []Win32_Processor
q := wmi.CreateQuery(&dst, "") q := wmi.CreateQuery(&dst, "")
err := wmi.Query(q, &dst) if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
if err != nil {
return ret, err return ret, err
} }
@ -112,18 +120,34 @@ func Info() ([]InfoStat, error) {
// PerfInfo returns the performance counter's instance value for ProcessorInformation. // PerfInfo returns the performance counter's instance value for ProcessorInformation.
// Name property is the key by which overall, per cpu and per core metric is known. // Name property is the key by which overall, per cpu and per core metric is known.
func PerfInfo() ([]Win32_PerfFormattedData_Counters_ProcessorInformation, error) { func PerfInfo() ([]Win32_PerfFormattedData_Counters_ProcessorInformation, error) {
return PerfInfoWithContext(context.Background())
}
func PerfInfoWithContext(ctx context.Context) ([]Win32_PerfFormattedData_Counters_ProcessorInformation, error) {
var ret []Win32_PerfFormattedData_Counters_ProcessorInformation var ret []Win32_PerfFormattedData_Counters_ProcessorInformation
q := wmi.CreateQuery(&ret, "") q := wmi.CreateQuery(&ret, "")
err := wmi.Query(q, &ret) err := common.WMIQueryWithContext(ctx, q, &ret)
if err != nil {
return []Win32_PerfFormattedData_Counters_ProcessorInformation{}, err
}
return ret, err return ret, err
} }
// ProcInfo returns processes count and processor queue length in the system. // ProcInfo returns processes count and processor queue length in the system.
// There is a single queue for processor even on multiprocessors systems. // There is a single queue for processor even on multiprocessors systems.
func ProcInfo() ([]Win32_PerfFormattedData_PerfOS_System, error) { func ProcInfo() ([]Win32_PerfFormattedData_PerfOS_System, error) {
return ProcInfoWithContext(context.Background())
}
func ProcInfoWithContext(ctx context.Context) ([]Win32_PerfFormattedData_PerfOS_System, error) {
var ret []Win32_PerfFormattedData_PerfOS_System var ret []Win32_PerfFormattedData_PerfOS_System
q := wmi.CreateQuery(&ret, "") q := wmi.CreateQuery(&ret, "")
err := wmi.Query(q, &ret) err := common.WMIQueryWithContext(ctx, q, &ret)
if err != nil {
return []Win32_PerfFormattedData_PerfOS_System{}, err
}
return ret, err return ret, err
} }

View File

@ -4,6 +4,7 @@ package host
import ( import (
"bytes" "bytes"
"context"
"encoding/binary" "encoding/binary"
"io/ioutil" "io/ioutil"
"os" "os"
@ -23,6 +24,10 @@ import (
const USER_PROCESS = 7 const USER_PROCESS = 7
func Info() (*InfoStat, error) { func Info() (*InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) (*InfoStat, error) {
ret := &InfoStat{ ret := &InfoStat{
OS: runtime.GOOS, OS: runtime.GOOS,
PlatformFamily: "darwin", PlatformFamily: "darwin",
@ -77,6 +82,10 @@ func Info() (*InfoStat, error) {
var cachedBootTime uint64 var cachedBootTime uint64
func BootTime() (uint64, error) { func BootTime() (uint64, error) {
return BootTimeWithContext(context.Background())
}
func BootTimeWithContext(ctx context.Context) (uint64, error) {
t := atomic.LoadUint64(&cachedBootTime) t := atomic.LoadUint64(&cachedBootTime)
if t != 0 { if t != 0 {
return t, nil return t, nil
@ -102,6 +111,10 @@ func uptime(boot uint64) uint64 {
} }
func Uptime() (uint64, error) { func Uptime() (uint64, error) {
return UptimeWithContext(context.Background())
}
func UptimeWithContext(ctx context.Context) (uint64, error) {
boot, err := BootTime() boot, err := BootTime()
if err != nil { if err != nil {
return 0, err return 0, err
@ -110,6 +123,10 @@ func Uptime() (uint64, error) {
} }
func Users() ([]UserStat, error) { func Users() ([]UserStat, error) {
return UsersWithContext(context.Background())
}
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
utmpfile := "/var/run/utmpx" utmpfile := "/var/run/utmpx"
var ret []UserStat var ret []UserStat
@ -154,6 +171,10 @@ func Users() ([]UserStat, error) {
} }
func PlatformInformation() (string, string, string, error) { func PlatformInformation() (string, string, string, error) {
return PlatformInformationWithContext(context.Background())
}
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
platform := "" platform := ""
family := "" family := ""
pver := "" pver := ""
@ -181,10 +202,18 @@ func PlatformInformation() (string, string, string, error) {
} }
func Virtualization() (string, string, error) { func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError return "", "", common.ErrNotImplementedError
} }
func KernelVersion() (string, error) { func KernelVersion() (string, error) {
return KernelVersionWithContext(context.Background())
}
func KernelVersionWithContext(ctx context.Context) (string, error) {
_, _, version, err := PlatformInformation() _, _, version, err := PlatformInformation()
return version, err return version, err
} }

View File

@ -6,8 +6,13 @@ package host
// #cgo LDFLAGS: -framework IOKit // #cgo LDFLAGS: -framework IOKit
// #include "include/smc.c" // #include "include/smc.c"
import "C" import "C"
import "context"
func SensorsTemperatures() ([]TemperatureStat, error) { func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
temperatureKeys := []string{ temperatureKeys := []string{
C.AMBIENT_AIR_0, C.AMBIENT_AIR_0,
C.AMBIENT_AIR_1, C.AMBIENT_AIR_1,

View File

@ -3,8 +3,16 @@
package host package host
import "github.com/shirou/gopsutil/internal/common" import (
"context"
"github.com/shirou/gopsutil/internal/common"
)
func SensorsTemperatures() ([]TemperatureStat, error) { func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return []TemperatureStat{}, common.ErrNotImplementedError return []TemperatureStat{}, common.ErrNotImplementedError
} }

View File

@ -2,28 +2,56 @@
package host package host
import "github.com/shirou/gopsutil/internal/common" import (
"context"
"github.com/shirou/gopsutil/internal/common"
)
func Info() (*InfoStat, error) { func Info() (*InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) (*InfoStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func BootTime() (uint64, error) { func BootTime() (uint64, error) {
return BootTimeWithContext(context.Background())
}
func BootTimeWithContext(ctx context.Context) (uint64, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func Uptime() (uint64, error) { func Uptime() (uint64, error) {
return UptimeWithContext(context.Background())
}
func UptimeWithContext(ctx context.Context) (uint64, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func Users() ([]UserStat, error) { func Users() ([]UserStat, error) {
return UsersWithContext(context.Background())
}
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
return []UserStat{}, common.ErrNotImplementedError return []UserStat{}, common.ErrNotImplementedError
} }
func Virtualization() (string, string, error) { func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError return "", "", common.ErrNotImplementedError
} }
func KernelVersion() (string, error) { func KernelVersion() (string, error) {
return KernelVersionWithContext(context.Background())
}
func KernelVersionWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }

View File

@ -4,6 +4,7 @@ package host
import ( import (
"bytes" "bytes"
"context"
"encoding/binary" "encoding/binary"
"io/ioutil" "io/ioutil"
"os" "os"
@ -27,6 +28,10 @@ const (
) )
func Info() (*InfoStat, error) { func Info() (*InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) (*InfoStat, error) {
ret := &InfoStat{ ret := &InfoStat{
OS: runtime.GOOS, OS: runtime.GOOS,
PlatformFamily: "freebsd", PlatformFamily: "freebsd",
@ -74,6 +79,10 @@ func Info() (*InfoStat, error) {
var cachedBootTime uint64 var cachedBootTime uint64
func BootTime() (uint64, error) { func BootTime() (uint64, error) {
return BootTimeWithContext(context.Background())
}
func BootTimeWithContext(ctx context.Context) (uint64, error) {
t := atomic.LoadUint64(&cachedBootTime) t := atomic.LoadUint64(&cachedBootTime)
if t != 0 { if t != 0 {
return t, nil return t, nil
@ -94,6 +103,10 @@ func uptime(boot uint64) uint64 {
} }
func Uptime() (uint64, error) { func Uptime() (uint64, error) {
return UptimeWithContext(context.Background())
}
func UptimeWithContext(ctx context.Context) (uint64, error) {
boot, err := BootTime() boot, err := BootTime()
if err != nil { if err != nil {
return 0, err return 0, err
@ -102,6 +115,10 @@ func Uptime() (uint64, error) {
} }
func Users() ([]UserStat, error) { func Users() ([]UserStat, error) {
return UsersWithContext(context.Background())
}
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
utmpfile := "/var/run/utx.active" utmpfile := "/var/run/utx.active"
if !common.PathExists(utmpfile) { if !common.PathExists(utmpfile) {
utmpfile = "/var/run/utmp" // before 9.0 utmpfile = "/var/run/utmp" // before 9.0
@ -147,6 +164,10 @@ func Users() ([]UserStat, error) {
} }
func PlatformInformation() (string, string, string, error) { func PlatformInformation() (string, string, string, error) {
return PlatformInformationWithContext(context.Background())
}
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
platform := "" platform := ""
family := "" family := ""
version := "" version := ""
@ -169,6 +190,10 @@ func PlatformInformation() (string, string, string, error) {
} }
func Virtualization() (string, string, error) { func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError return "", "", common.ErrNotImplementedError
} }
@ -212,10 +237,18 @@ func getUsersFromUtmp(utmpfile string) ([]UserStat, error) {
} }
func SensorsTemperatures() ([]TemperatureStat, error) { func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return []TemperatureStat{}, common.ErrNotImplementedError return []TemperatureStat{}, common.ErrNotImplementedError
} }
func KernelVersion() (string, error) { func KernelVersion() (string, error) {
return KernelVersionWithContext(context.Background())
}
func KernelVersionWithContext(ctx context.Context) (string, error) {
_, _, version, err := PlatformInformation() _, _, version, err := PlatformInformation()
return version, err return version, err
} }

View File

@ -4,6 +4,7 @@ package host
import ( import (
"bytes" "bytes"
"context"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -31,6 +32,10 @@ type LSB struct {
const USER_PROCESS = 7 const USER_PROCESS = 7
func Info() (*InfoStat, error) { func Info() (*InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) (*InfoStat, error) {
ret := &InfoStat{ ret := &InfoStat{
OS: runtime.GOOS, OS: runtime.GOOS,
} }
@ -91,11 +96,26 @@ var cachedBootTime uint64
// BootTime returns the system boot time expressed in seconds since the epoch. // BootTime returns the system boot time expressed in seconds since the epoch.
func BootTime() (uint64, error) { func BootTime() (uint64, error) {
return BootTimeWithContext(context.Background())
}
func BootTimeWithContext(ctx context.Context) (uint64, error) {
t := atomic.LoadUint64(&cachedBootTime) t := atomic.LoadUint64(&cachedBootTime)
if t != 0 { if t != 0 {
return t, nil return t, nil
} }
filename := common.HostProc("stat")
system, role, err := Virtualization()
if err != nil {
return 0, err
}
statFile := "stat"
if system == "lxc" && role == "guest" {
// if lxc, /proc/uptime is used.
statFile = "uptime"
}
filename := common.HostProc(statFile)
lines, err := common.ReadLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
return 0, err return 0, err
@ -124,6 +144,10 @@ func uptime(boot uint64) uint64 {
} }
func Uptime() (uint64, error) { func Uptime() (uint64, error) {
return UptimeWithContext(context.Background())
}
func UptimeWithContext(ctx context.Context) (uint64, error) {
boot, err := BootTime() boot, err := BootTime()
if err != nil { if err != nil {
return 0, err return 0, err
@ -132,6 +156,10 @@ func Uptime() (uint64, error) {
} }
func Users() ([]UserStat, error) { func Users() ([]UserStat, error) {
return UsersWithContext(context.Background())
}
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
utmpfile := common.HostVar("run/utmp") utmpfile := common.HostVar("run/utmp")
file, err := os.Open(utmpfile) file, err := os.Open(utmpfile)
@ -249,6 +277,10 @@ func getLSB() (*LSB, error) {
} }
func PlatformInformation() (platform string, family string, version string, err error) { func PlatformInformation() (platform string, family string, version string, err error) {
return PlatformInformationWithContext(context.Background())
}
func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) {
lsb, err := getLSB() lsb, err := getLSB()
if err != nil { if err != nil {
@ -371,6 +403,10 @@ func PlatformInformation() (platform string, family string, version string, err
} }
func KernelVersion() (version string, err error) { func KernelVersion() (version string, err error) {
return KernelVersionWithContext(context.Background())
}
func KernelVersionWithContext(ctx context.Context) (version string, err error) {
filename := common.HostProc("sys/kernel/osrelease") filename := common.HostProc("sys/kernel/osrelease")
if common.PathExists(filename) { if common.PathExists(filename) {
contents, err := common.ReadLines(filename) contents, err := common.ReadLines(filename)
@ -430,6 +466,10 @@ func getSusePlatform(contents []string) string {
} }
func Virtualization() (string, string, error) { func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
var system string var system string
var role string var role string
@ -533,6 +573,10 @@ func Virtualization() (string, string, error) {
} }
func SensorsTemperatures() ([]TemperatureStat, error) { func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
var temperatures []TemperatureStat var temperatures []TemperatureStat
files, err := filepath.Glob(common.HostSys("/class/hwmon/hwmon*/temp*_*")) files, err := filepath.Glob(common.HostSys("/class/hwmon/hwmon*/temp*_*"))
if err != nil { if err != nil {
@ -541,28 +585,52 @@ func SensorsTemperatures() ([]TemperatureStat, error) {
if len(files) == 0 { if len(files) == 0 {
// CentOS has an intermediate /device directory: // CentOS has an intermediate /device directory:
// https://github.com/giampaolo/psutil/issues/971 // https://github.com/giampaolo/psutil/issues/971
files, err = filepath.Glob(common.HostSys("/class/hwmon/hwmon*/temp*_*")) files, err = filepath.Glob(common.HostSys("/class/hwmon/hwmon*/device/temp*_*"))
if err != nil { if err != nil {
return temperatures, err return temperatures, err
} }
} }
for _, match := range files { // example directory
match = strings.Split(match, "_")[0] // device/ temp1_crit_alarm temp2_crit_alarm temp3_crit_alarm temp4_crit_alarm temp5_crit_alarm temp6_crit_alarm temp7_crit_alarm
name, err := ioutil.ReadFile(filepath.Join(filepath.Dir(match), "name")) // name temp1_input temp2_input temp3_input temp4_input temp5_input temp6_input temp7_input
// power/ temp1_label temp2_label temp3_label temp4_label temp5_label temp6_label temp7_label
// subsystem/ temp1_max temp2_max temp3_max temp4_max temp5_max temp6_max temp7_max
// temp1_crit temp2_crit temp3_crit temp4_crit temp5_crit temp6_crit temp7_crit uevent
for _, file := range files {
filename := strings.Split(filepath.Base(file), "_")
if filename[1] == "label" {
// Do not try to read the temperature of the label file
continue
}
// Get the label of the temperature you are reading
var label string
c, _ := ioutil.ReadFile(filepath.Join(filepath.Dir(file), filename[0]+"_label"))
if c != nil {
//format the label from "Core 0" to "core0_"
label = fmt.Sprintf("%s_", strings.Join(strings.Split(strings.TrimSpace(strings.ToLower(string(c))), " "), ""))
}
// Get the name of the tempearture you are reading
name, err := ioutil.ReadFile(filepath.Join(filepath.Dir(file), "name"))
if err != nil { if err != nil {
return temperatures, err return temperatures, err
} }
current, err := ioutil.ReadFile(match + "_input")
// Get the temperature reading
current, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
return temperatures, err return temperatures, err
} }
temperature, err := strconv.ParseFloat(string(current), 64) temperature, err := strconv.ParseFloat(strings.TrimSpace(string(current)), 64)
if err != nil { if err != nil {
continue continue
} }
tempName := strings.TrimSpace(strings.ToLower(string(strings.Join(filename[1:], ""))))
temperatures = append(temperatures, TemperatureStat{ temperatures = append(temperatures, TemperatureStat{
SensorKey: string(name), SensorKey: fmt.Sprintf("%s_%s%s", strings.TrimSpace(string(name)), label, tempName),
Temperature: temperature / 1000.0, Temperature: temperature / 1000.0,
}) })
} }

View File

@ -4,6 +4,7 @@ package host
import ( import (
"bytes" "bytes"
"context"
"encoding/binary" "encoding/binary"
"io/ioutil" "io/ioutil"
"os" "os"
@ -25,6 +26,10 @@ const (
) )
func Info() (*InfoStat, error) { func Info() (*InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) (*InfoStat, error) {
ret := &InfoStat{ ret := &InfoStat{
OS: runtime.GOOS, OS: runtime.GOOS,
PlatformFamily: "openbsd", PlatformFamily: "openbsd",
@ -62,6 +67,10 @@ func Info() (*InfoStat, error) {
} }
func BootTime() (uint64, error) { func BootTime() (uint64, error) {
return BootTimeWithContext(context.Background())
}
func BootTimeWithContext(ctx context.Context) (uint64, error) {
val, err := common.DoSysctrl("kern.boottime") val, err := common.DoSysctrl("kern.boottime")
if err != nil { if err != nil {
return 0, err return 0, err
@ -80,6 +89,10 @@ func uptime(boot uint64) uint64 {
} }
func Uptime() (uint64, error) { func Uptime() (uint64, error) {
return UptimeWithContext(context.Background())
}
func UptimeWithContext(ctx context.Context) (uint64, error) {
boot, err := BootTime() boot, err := BootTime()
if err != nil { if err != nil {
return 0, err return 0, err
@ -88,6 +101,10 @@ func Uptime() (uint64, error) {
} }
func PlatformInformation() (string, string, string, error) { func PlatformInformation() (string, string, string, error) {
return PlatformInformationWithContext(context.Background())
}
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
platform := "" platform := ""
family := "" family := ""
version := "" version := ""
@ -110,10 +127,18 @@ func PlatformInformation() (string, string, string, error) {
} }
func Virtualization() (string, string, error) { func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError return "", "", common.ErrNotImplementedError
} }
func Users() ([]UserStat, error) { func Users() ([]UserStat, error) {
return UsersWithContext(context.Background())
}
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
var ret []UserStat var ret []UserStat
utmpfile := "/var/run/utmp" utmpfile := "/var/run/utmp"
file, err := os.Open(utmpfile) file, err := os.Open(utmpfile)
@ -153,10 +178,18 @@ func Users() ([]UserStat, error) {
} }
func SensorsTemperatures() ([]TemperatureStat, error) { func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return []TemperatureStat{}, common.ErrNotImplementedError return []TemperatureStat{}, common.ErrNotImplementedError
} }
func KernelVersion() (string, error) { func KernelVersion() (string, error) {
return KernelVersionWithContext(context.Background())
}
func KernelVersionWithContext(ctx context.Context) (string, error) {
_, _, version, err := PlatformInformation() _, _, version, err := PlatformInformation()
return version, err return version, err
} }

View File

@ -3,6 +3,7 @@ package host
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -17,6 +18,10 @@ import (
) )
func Info() (*InfoStat, error) { func Info() (*InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) (*InfoStat, error) {
result := &InfoStat{ result := &InfoStat{
OS: runtime.GOOS, OS: runtime.GOOS,
} }
@ -142,6 +147,10 @@ func Info() (*InfoStat, error) {
var kstatMatch = regexp.MustCompile(`([^\s]+)[\s]+([^\s]*)`) var kstatMatch = regexp.MustCompile(`([^\s]+)[\s]+([^\s]*)`)
func BootTime() (uint64, error) { func BootTime() (uint64, error) {
return BootTimeWithContext(context.Background())
}
func BootTimeWithContext(ctx context.Context) (uint64, error) {
kstat, err := exec.LookPath("/usr/bin/kstat") kstat, err := exec.LookPath("/usr/bin/kstat")
if err != nil { if err != nil {
return 0, err return 0, err
@ -161,6 +170,10 @@ func BootTime() (uint64, error) {
} }
func Uptime() (uint64, error) { func Uptime() (uint64, error) {
return UptimeWithContext(context.Background())
}
func UptimeWithContext(ctx context.Context) (uint64, error) {
bootTime, err := BootTime() bootTime, err := BootTime()
if err != nil { if err != nil {
return 0, err return 0, err
@ -173,18 +186,34 @@ func uptimeSince(since uint64) uint64 {
} }
func Users() ([]UserStat, error) { func Users() ([]UserStat, error) {
return UsersWithContext(context.Background())
}
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
return []UserStat{}, common.ErrNotImplementedError return []UserStat{}, common.ErrNotImplementedError
} }
func SensorsTemperatures() ([]TemperatureStat, error) { func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return []TemperatureStat{}, common.ErrNotImplementedError return []TemperatureStat{}, common.ErrNotImplementedError
} }
func Virtualization() (string, string, error) { func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError return "", "", common.ErrNotImplementedError
} }
func KernelVersion() (string, error) { func KernelVersion() (string, error) {
return KernelVersionWithContext(context.Background())
}
func KernelVersionWithContext(ctx context.Context) (string, error) {
// Parse versions from output of `uname(1)` // Parse versions from output of `uname(1)`
uname, err := exec.LookPath("/usr/bin/uname") uname, err := exec.LookPath("/usr/bin/uname")
if err != nil { if err != nil {

View File

@ -3,6 +3,7 @@
package host package host
import ( import (
"context"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
@ -31,6 +32,10 @@ type Win32_OperatingSystem struct {
} }
func Info() (*InfoStat, error) { func Info() (*InfoStat, error) {
return InfoWithContext(context.Background())
}
func InfoWithContext(ctx context.Context) (*InfoStat, error) {
ret := &InfoStat{ ret := &InfoStat{
OS: runtime.GOOS, OS: runtime.GOOS,
} }
@ -43,7 +48,7 @@ func Info() (*InfoStat, error) {
} }
{ {
platform, family, version, err := PlatformInformation() platform, family, version, err := PlatformInformationWithContext(ctx)
if err == nil { if err == nil {
ret.Platform = platform ret.Platform = platform
ret.PlatformFamily = family ret.PlatformFamily = family
@ -107,9 +112,13 @@ func getMachineGuid() (string, error) {
} }
func GetOSInfo() (Win32_OperatingSystem, error) { func GetOSInfo() (Win32_OperatingSystem, error) {
return GetOSInfoWithContext(context.Background())
}
func GetOSInfoWithContext(ctx context.Context) (Win32_OperatingSystem, error) {
var dst []Win32_OperatingSystem var dst []Win32_OperatingSystem
q := wmi.CreateQuery(&dst, "") q := wmi.CreateQuery(&dst, "")
err := wmi.Query(q, &dst) err := common.WMIQueryWithContext(ctx, q, &dst)
if err != nil { if err != nil {
return Win32_OperatingSystem{}, err return Win32_OperatingSystem{}, err
} }
@ -120,8 +129,12 @@ func GetOSInfo() (Win32_OperatingSystem, error) {
} }
func Uptime() (uint64, error) { func Uptime() (uint64, error) {
return UptimeWithContext(context.Background())
}
func UptimeWithContext(ctx context.Context) (uint64, error) {
if osInfo == nil { if osInfo == nil {
_, err := GetOSInfo() _, err := GetOSInfoWithContext(ctx)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -139,6 +152,10 @@ func bootTime(up uint64) uint64 {
var cachedBootTime uint64 var cachedBootTime uint64
func BootTime() (uint64, error) { func BootTime() (uint64, error) {
return BootTimeWithContext(context.Background())
}
func BootTimeWithContext(ctx context.Context) (uint64, error) {
t := atomic.LoadUint64(&cachedBootTime) t := atomic.LoadUint64(&cachedBootTime)
if t != 0 { if t != 0 {
return t, nil return t, nil
@ -153,8 +170,12 @@ func BootTime() (uint64, error) {
} }
func PlatformInformation() (platform string, family string, version string, err error) { func PlatformInformation() (platform string, family string, version string, err error) {
return PlatformInformationWithContext(context.Background())
}
func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) {
if osInfo == nil { if osInfo == nil {
_, err = GetOSInfo() _, err = GetOSInfoWithContext(ctx)
if err != nil { if err != nil {
return return
} }
@ -180,20 +201,36 @@ func PlatformInformation() (platform string, family string, version string, err
} }
func Users() ([]UserStat, error) { func Users() ([]UserStat, error) {
return UsersWithContext(context.Background())
}
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
var ret []UserStat var ret []UserStat
return ret, nil return ret, nil
} }
func SensorsTemperatures() ([]TemperatureStat, error) { func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return []TemperatureStat{}, common.ErrNotImplementedError return []TemperatureStat{}, common.ErrNotImplementedError
} }
func Virtualization() (string, string, error) { func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError return "", "", common.ErrNotImplementedError
} }
func KernelVersion() (string, error) { func KernelVersion() (string, error) {
return KernelVersionWithContext(context.Background())
}
func KernelVersionWithContext(ctx context.Context) (string, error) {
_, _, version, err := PlatformInformation() _, _, version, err := PlatformInformation()
return version, err return version, err
} }

View File

@ -115,6 +115,12 @@ func CreateCounter(query windows.Handle, pname, cname string) (*CounterInfo, err
// WMIQueryWithContext - wraps wmi.Query with a timed-out context to avoid hanging // WMIQueryWithContext - wraps wmi.Query with a timed-out context to avoid hanging
func WMIQueryWithContext(ctx context.Context, query string, dst interface{}, connectServerArgs ...interface{}) error { func WMIQueryWithContext(ctx context.Context, query string, dst interface{}, connectServerArgs ...interface{}) error {
if _, ok := ctx.Deadline(); !ok {
ctxTimeout, cancel := context.WithTimeout(ctx, Timeout)
defer cancel()
ctx = ctxTimeout
}
errChan := make(chan error, 1) errChan := make(chan error, 1)
go func() { go func() {
errChan <- wmi.Query(query, dst, connectServerArgs...) errChan <- wmi.Query(query, dst, connectServerArgs...)

View File

@ -3,6 +3,7 @@
package mem package mem
import ( import (
"context"
"encoding/binary" "encoding/binary"
"strconv" "strconv"
"strings" "strings"
@ -28,6 +29,10 @@ func getHwMemsize() (uint64, error) {
// SwapMemory returns swapinfo. // SwapMemory returns swapinfo.
func SwapMemory() (*SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
return SwapMemoryWithContext(context.Background())
}
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
var ret *SwapMemoryStat var ret *SwapMemoryStat
swapUsage, err := common.DoSysctrl("vm.swapusage") swapUsage, err := common.DoSysctrl("vm.swapusage")

View File

@ -9,6 +9,7 @@ package mem
import "C" import "C"
import ( import (
"context"
"fmt" "fmt"
"unsafe" "unsafe"
@ -17,6 +18,10 @@ import (
// VirtualMemory returns VirtualmemoryStat. // VirtualMemory returns VirtualmemoryStat.
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
return VirtualMemoryWithContext(context.Background())
}
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
count := C.mach_msg_type_number_t(C.HOST_VM_INFO_COUNT) count := C.mach_msg_type_number_t(C.HOST_VM_INFO_COUNT)
var vmstat C.vm_statistics_data_t var vmstat C.vm_statistics_data_t

View File

@ -4,6 +4,7 @@
package mem package mem
import ( import (
"context"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
@ -68,6 +69,10 @@ func parseVMStat(out string, vms *VirtualMemoryStat) error {
// VirtualMemory returns VirtualmemoryStat. // VirtualMemory returns VirtualmemoryStat.
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
return VirtualMemoryWithContext(context.Background())
}
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
ret := &VirtualMemoryStat{} ret := &VirtualMemoryStat{}
total, err := getHwMemsize() total, err := getHwMemsize()

View File

@ -2,12 +2,24 @@
package mem package mem
import "github.com/shirou/gopsutil/internal/common" import (
"context"
"github.com/shirou/gopsutil/internal/common"
)
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
return VirtualMemoryWithContext(context.Background())
}
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func SwapMemory() (*SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
return SwapMemoryWithContext(context.Background())
}
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }

View File

@ -3,6 +3,7 @@
package mem package mem
import ( import (
"context"
"errors" "errors"
"os/exec" "os/exec"
"strconv" "strconv"
@ -12,6 +13,10 @@ import (
) )
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
return VirtualMemoryWithContext(context.Background())
}
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
pageSize, err := unix.SysctlUint32("vm.stats.vm.v_page_size") pageSize, err := unix.SysctlUint32("vm.stats.vm.v_page_size")
if err != nil { if err != nil {
return nil, err return nil, err
@ -66,6 +71,10 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
// Return swapinfo // Return swapinfo
// FreeBSD can have multiple swap devices. but use only first device // FreeBSD can have multiple swap devices. but use only first device
func SwapMemory() (*SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
return SwapMemoryWithContext(context.Background())
}
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
swapinfo, err := exec.LookPath("swapinfo") swapinfo, err := exec.LookPath("swapinfo")
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -3,6 +3,7 @@
package mem package mem
import ( import (
"context"
"strconv" "strconv"
"strings" "strings"
@ -11,6 +12,10 @@ import (
) )
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
return VirtualMemoryWithContext(context.Background())
}
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
filename := common.HostProc("meminfo") filename := common.HostProc("meminfo")
lines, _ := common.ReadLines(filename) lines, _ := common.ReadLines(filename)
// flag if MemAvailable is in /proc/meminfo (kernel 3.14+) // flag if MemAvailable is in /proc/meminfo (kernel 3.14+)
@ -72,6 +77,10 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
} }
func SwapMemory() (*SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
return SwapMemoryWithContext(context.Background())
}
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
sysinfo := &unix.Sysinfo_t{} sysinfo := &unix.Sysinfo_t{}
if err := unix.Sysinfo(sysinfo); err != nil { if err := unix.Sysinfo(sysinfo); err != nil {

View File

@ -4,14 +4,20 @@ package mem
import ( import (
"bytes" "bytes"
"context"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"github.com/shirou/gopsutil/internal/common"
"os/exec" "os/exec"
"github.com/shirou/gopsutil/internal/common"
) )
func GetPageSize() (uint64, error) { func GetPageSize() (uint64, error) {
return GetPageSizeWithContext(context.Background())
}
func GetPageSizeWithContext(ctx context.Context) (uint64, error) {
mib := []int32{CTLVm, VmUvmexp} mib := []int32{CTLVm, VmUvmexp}
buf, length, err := common.CallSyscall(mib) buf, length, err := common.CallSyscall(mib)
if err != nil { if err != nil {
@ -30,6 +36,10 @@ func GetPageSize() (uint64, error) {
} }
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
return VirtualMemoryWithContext(context.Background())
}
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
mib := []int32{CTLVm, VmUvmexp} mib := []int32{CTLVm, VmUvmexp}
buf, length, err := common.CallSyscall(mib) buf, length, err := common.CallSyscall(mib)
if err != nil { if err != nil {
@ -80,6 +90,10 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
// Return swapctl summary info // Return swapctl summary info
func SwapMemory() (*SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
return SwapMemoryWithContext(context.Background())
}
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
swapctl, err := exec.LookPath("swapctl") swapctl, err := exec.LookPath("swapctl")
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -1,6 +1,7 @@
package mem package mem
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"os/exec" "os/exec"
@ -14,6 +15,10 @@ import (
// VirtualMemory for Solaris is a minimal implementation which only returns // VirtualMemory for Solaris is a minimal implementation which only returns
// what Nomad needs. It does take into account global vs zone, however. // what Nomad needs. It does take into account global vs zone, however.
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
return VirtualMemoryWithContext(context.Background())
}
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
result := &VirtualMemoryStat{} result := &VirtualMemoryStat{}
zoneName, err := zoneName() zoneName, err := zoneName()
@ -39,6 +44,10 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
} }
func SwapMemory() (*SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
return SwapMemoryWithContext(context.Background())
}
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }

View File

@ -3,6 +3,7 @@
package mem package mem
import ( import (
"context"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
@ -27,6 +28,10 @@ type memoryStatusEx struct {
} }
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
return VirtualMemoryWithContext(context.Background())
}
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
var memInfo memoryStatusEx var memInfo memoryStatusEx
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)))
@ -62,6 +67,10 @@ type performanceInformation struct {
} }
func SwapMemory() (*SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
return SwapMemoryWithContext(context.Background())
}
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
var perfInfo performanceInformation var perfInfo performanceInformation
perfInfo.cb = uint32(unsafe.Sizeof(perfInfo)) perfInfo.cb = uint32(unsafe.Sizeof(perfInfo))
mem, _, _ := procGetPerformanceInfo.Call(uintptr(unsafe.Pointer(&perfInfo)), uintptr(perfInfo.cb)) mem, _, _ := procGetPerformanceInfo.Call(uintptr(unsafe.Pointer(&perfInfo)), uintptr(perfInfo.cb))
@ -80,4 +89,3 @@ func SwapMemory() (*SwapMemoryStat, error) {
return ret, nil return ret, nil
} }

View File

@ -1,6 +1,7 @@
package net package net
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net" "net"
@ -111,6 +112,10 @@ func (n InterfaceAddr) String() string {
} }
func Interfaces() ([]InterfaceStat, error) { func Interfaces() ([]InterfaceStat, error) {
return InterfacesWithContext(context.Background())
}
func InterfacesWithContext(ctx context.Context) ([]InterfaceStat, error) {
is, err := net.Interfaces() is, err := net.Interfaces()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -3,6 +3,7 @@
package net package net
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"os/exec" "os/exec"
@ -164,6 +165,10 @@ func (min mapInterfaceNameUsage) notTruncated() []string {
// lo0 16384 ::1/128 ::1 869107 - 169411755 869107 - 169411755 - - // lo0 16384 ::1/128 ::1 869107 - 169411755 869107 - 169411755 - -
// lo0 16384 127 127.0.0.1 869107 - 169411755 869107 - 169411755 - - // lo0 16384 127 127.0.0.1 869107 - 169411755 869107 - 169411755 - -
func IOCounters(pernic bool) ([]IOCountersStat, error) { func IOCounters(pernic bool) ([]IOCountersStat, error) {
return IOCountersWithContext(context.Background(), pernic)
}
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
var ( var (
ret []IOCountersStat ret []IOCountersStat
retIndex int retIndex int
@ -251,10 +256,18 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
// NetIOCountersByFile is an method which is added just a compatibility for linux. // NetIOCountersByFile is an method which is added just a compatibility for linux.
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
return IOCountersByFileWithContext(context.Background(), pernic, filename)
}
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
return IOCounters(pernic) return IOCounters(pernic)
} }
func FilterCounters() ([]FilterStat, error) { func FilterCounters() ([]FilterStat, error) {
return FilterCountersWithContext(context.Background())
}
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
return nil, errors.New("NetFilterCounters not implemented for darwin") return nil, errors.New("NetFilterCounters not implemented for darwin")
} }
@ -263,5 +276,9 @@ func FilterCounters() ([]FilterStat, error) {
// just the protocols in the list are returned. // just the protocols in the list are returned.
// Not Implemented for Darwin // Not Implemented for Darwin
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
return ProtoCountersWithContext(context.Background(), protocols)
}
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
return nil, errors.New("NetProtoCounters not implemented for darwin") return nil, errors.New("NetProtoCounters not implemented for darwin")
} }

View File

@ -2,24 +2,48 @@
package net package net
import "github.com/shirou/gopsutil/internal/common" import (
"context"
"github.com/shirou/gopsutil/internal/common"
)
func IOCounters(pernic bool) ([]IOCountersStat, error) { func IOCounters(pernic bool) ([]IOCountersStat, error) {
return IOCountersWithContext(context.Background(), pernic)
}
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
return []IOCountersStat{}, common.ErrNotImplementedError return []IOCountersStat{}, common.ErrNotImplementedError
} }
func FilterCounters() ([]FilterStat, error) { func FilterCounters() ([]FilterStat, error) {
return FilterCountersWithContext(context.Background())
}
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
return []FilterStat{}, common.ErrNotImplementedError return []FilterStat{}, common.ErrNotImplementedError
} }
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
return ProtoCountersWithContext(context.Background(), protocols)
}
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
return []ProtoCountersStat{}, common.ErrNotImplementedError return []ProtoCountersStat{}, common.ErrNotImplementedError
} }
func Connections(kind string) ([]ConnectionStat, error) { func Connections(kind string) ([]ConnectionStat, error) {
return ConnectionsWithContext(context.Background(), kind)
}
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
return []ConnectionStat{}, common.ErrNotImplementedError return []ConnectionStat{}, common.ErrNotImplementedError
} }
func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
return ConnectionsMaxWithContext(context.Background(), kind, max)
}
func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
return []ConnectionStat{}, common.ErrNotImplementedError return []ConnectionStat{}, common.ErrNotImplementedError
} }

View File

@ -3,6 +3,7 @@
package net package net
import ( import (
"context"
"errors" "errors"
"os/exec" "os/exec"
"strconv" "strconv"
@ -12,6 +13,10 @@ import (
) )
func IOCounters(pernic bool) ([]IOCountersStat, error) { func IOCounters(pernic bool) ([]IOCountersStat, error) {
return IOCountersWithContext(context.Background(), pernic)
}
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
netstat, err := exec.LookPath("/usr/bin/netstat") netstat, err := exec.LookPath("/usr/bin/netstat")
if err != nil { if err != nil {
return nil, err return nil, err
@ -92,10 +97,18 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
// NetIOCountersByFile is an method which is added just a compatibility for linux. // NetIOCountersByFile is an method which is added just a compatibility for linux.
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
return IOCountersByFileWithContext(context.Background(), pernic, filename)
}
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
return IOCounters(pernic) return IOCounters(pernic)
} }
func FilterCounters() ([]FilterStat, error) { func FilterCounters() ([]FilterStat, error) {
return FilterCountersWithContext(context.Background())
}
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
return nil, errors.New("NetFilterCounters not implemented for freebsd") return nil, errors.New("NetFilterCounters not implemented for freebsd")
} }
@ -104,5 +117,9 @@ func FilterCounters() ([]FilterStat, error) {
// just the protocols in the list are returned. // just the protocols in the list are returned.
// Not Implemented for FreeBSD // Not Implemented for FreeBSD
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
return ProtoCountersWithContext(context.Background(), protocols)
}
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
return nil, errors.New("NetProtoCounters not implemented for freebsd") return nil, errors.New("NetProtoCounters not implemented for freebsd")
} }

View File

@ -4,6 +4,7 @@ package net
import ( import (
"bytes" "bytes"
"context"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
@ -23,11 +24,19 @@ import (
// every network interface installed on the system is returned // every network interface installed on the system is returned
// separately. // separately.
func IOCounters(pernic bool) ([]IOCountersStat, error) { func IOCounters(pernic bool) ([]IOCountersStat, error) {
return IOCountersWithContext(context.Background(), pernic)
}
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
filename := common.HostProc("net/dev") filename := common.HostProc("net/dev")
return IOCountersByFile(pernic, filename) return IOCountersByFile(pernic, filename)
} }
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
return IOCountersByFileWithContext(context.Background(), pernic, filename)
}
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
lines, err := common.ReadLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
return nil, err return nil, err
@ -132,6 +141,10 @@ var netProtocols = []string{
// Available protocols: // Available protocols:
// ip,icmp,icmpmsg,tcp,udp,udplite // ip,icmp,icmpmsg,tcp,udp,udplite
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
return ProtoCountersWithContext(context.Background(), protocols)
}
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
if len(protocols) == 0 { if len(protocols) == 0 {
protocols = netProtocols protocols = netProtocols
} }
@ -191,6 +204,10 @@ func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
// the currently in use conntrack count and the max. // the currently in use conntrack count and the max.
// If the file does not exist or is invalid it will return nil. // If the file does not exist or is invalid it will return nil.
func FilterCounters() ([]FilterStat, error) { func FilterCounters() ([]FilterStat, error) {
return FilterCountersWithContext(context.Background())
}
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
countfile := common.HostProc("sys/net/netfilter/nf_conntrack_count") countfile := common.HostProc("sys/net/netfilter/nf_conntrack_count")
maxfile := common.HostProc("sys/net/netfilter/nf_conntrack_max") maxfile := common.HostProc("sys/net/netfilter/nf_conntrack_max")
@ -294,17 +311,29 @@ type connTmp struct {
// Return a list of network connections opened. // Return a list of network connections opened.
func Connections(kind string) ([]ConnectionStat, error) { func Connections(kind string) ([]ConnectionStat, error) {
return ConnectionsWithContext(context.Background(), kind)
}
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
return ConnectionsPid(kind, 0) return ConnectionsPid(kind, 0)
} }
// Return a list of network connections opened returning at most `max` // Return a list of network connections opened returning at most `max`
// connections for each running process. // connections for each running process.
func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
return ConnectionsMaxWithContext(context.Background(), kind, max)
}
func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
return ConnectionsPidMax(kind, 0, max) return ConnectionsPidMax(kind, 0, max)
} }
// Return a list of network connections opened by a process. // Return a list of network connections opened by a process.
func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error) { func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error) {
return ConnectionsPidWithContext(context.Background(), kind, pid)
}
func ConnectionsPidWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) {
tmap, ok := netConnectionKindMap[kind] tmap, ok := netConnectionKindMap[kind]
if !ok { if !ok {
return nil, fmt.Errorf("invalid kind, %s", kind) return nil, fmt.Errorf("invalid kind, %s", kind)
@ -329,6 +358,10 @@ func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error) {
// Return up to `max` network connections opened by a process. // Return up to `max` network connections opened by a process.
func ConnectionsPidMax(kind string, pid int32, max int) ([]ConnectionStat, error) { func ConnectionsPidMax(kind string, pid int32, max int) ([]ConnectionStat, error) {
return ConnectionsPidMaxWithContext(context.Background(), kind, pid, max)
}
func ConnectionsPidMaxWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) {
tmap, ok := netConnectionKindMap[kind] tmap, ok := netConnectionKindMap[kind]
if !ok { if !ok {
return nil, fmt.Errorf("invalid kind, %s", kind) return nil, fmt.Errorf("invalid kind, %s", kind)
@ -459,6 +492,10 @@ func getProcInodes(root string, pid int32, max int) (map[string][]inodeMap, erro
// FIXME: Import process occures import cycle. // FIXME: Import process occures import cycle.
// move to common made other platform breaking. Need consider. // move to common made other platform breaking. Need consider.
func Pids() ([]int32, error) { func Pids() ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
var ret []int32 var ret []int32
d, err := os.Open(common.HostProc()) d, err := os.Open(common.HostProc())
@ -541,8 +578,8 @@ func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) {
for _, pid := range pids { for _, pid := range pids {
t, err := getProcInodes(root, pid, max) t, err := getProcInodes(root, pid, max)
if err != nil { if err != nil {
// skip if permission error // skip if permission error or no longer exists
if os.IsPermission(err) { if os.IsPermission(err) || os.IsNotExist(err) {
continue continue
} }
return ret, err return ret, err
@ -592,6 +629,10 @@ func decodeAddress(family uint32, src string) (Addr, error) {
// Reverse reverses array of bytes. // Reverse reverses array of bytes.
func Reverse(s []byte) []byte { func Reverse(s []byte) []byte {
return ReverseWithContext(context.Background(), s)
}
func ReverseWithContext(ctx context.Context, s []byte) []byte {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i] s[i], s[j] = s[j], s[i]
} }

View File

@ -3,6 +3,7 @@
package net package net
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"os/exec" "os/exec"
@ -97,6 +98,10 @@ func ParseNetstat(output string, mode string,
} }
func IOCounters(pernic bool) ([]IOCountersStat, error) { func IOCounters(pernic bool) ([]IOCountersStat, error) {
return IOCountersWithContext(context.Background(), pernic)
}
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
netstat, err := exec.LookPath("netstat") netstat, err := exec.LookPath("netstat")
if err != nil { if err != nil {
return nil, err return nil, err
@ -136,10 +141,18 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
// NetIOCountersByFile is an method which is added just a compatibility for linux. // NetIOCountersByFile is an method which is added just a compatibility for linux.
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
return IOCountersByFileWithContext(context.Background(), pernic, filename)
}
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
return IOCounters(pernic) return IOCounters(pernic)
} }
func FilterCounters() ([]FilterStat, error) { func FilterCounters() ([]FilterStat, error) {
return FilterCountersWithContext(context.Background())
}
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
return nil, errors.New("NetFilterCounters not implemented for openbsd") return nil, errors.New("NetFilterCounters not implemented for openbsd")
} }
@ -148,6 +161,10 @@ func FilterCounters() ([]FilterStat, error) {
// just the protocols in the list are returned. // just the protocols in the list are returned.
// Not Implemented for OpenBSD // Not Implemented for OpenBSD
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
return ProtoCountersWithContext(context.Background(), protocols)
}
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
return nil, errors.New("NetProtoCounters not implemented for openbsd") return nil, errors.New("NetProtoCounters not implemented for openbsd")
} }
@ -233,6 +250,10 @@ func parseNetstatAddr(local string, remote string, family uint32) (laddr Addr, r
// Return a list of network connections opened. // Return a list of network connections opened.
func Connections(kind string) ([]ConnectionStat, error) { func Connections(kind string) ([]ConnectionStat, error) {
return ConnectionsWithContext(context.Background(), kind)
}
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
var ret []ConnectionStat var ret []ConnectionStat
args := []string{"-na"} args := []string{"-na"}

View File

@ -3,6 +3,7 @@
package net package net
import ( import (
"context"
"strings" "strings"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
@ -10,17 +11,29 @@ import (
// Return a list of network connections opened. // Return a list of network connections opened.
func Connections(kind string) ([]ConnectionStat, error) { func Connections(kind string) ([]ConnectionStat, error) {
return ConnectionsWithContext(context.Background(), kind)
}
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
return ConnectionsPid(kind, 0) return ConnectionsPid(kind, 0)
} }
// Return a list of network connections opened returning at most `max` // Return a list of network connections opened returning at most `max`
// connections for each running process. // connections for each running process.
func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
return ConnectionsMaxWithContext(context.Background(), kind, max)
}
func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
return []ConnectionStat{}, common.ErrNotImplementedError return []ConnectionStat{}, common.ErrNotImplementedError
} }
// Return a list of network connections opened by a process. // Return a list of network connections opened by a process.
func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error) { func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error) {
return ConnectionsPidWithContext(context.Background(), kind, pid)
}
func ConnectionsPidWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) {
var ret []ConnectionStat var ret []ConnectionStat
args := []string{"-i"} args := []string{"-i"}
@ -75,5 +88,9 @@ func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error) {
// Return up to `max` network connections opened by a process. // Return up to `max` network connections opened by a process.
func ConnectionsPidMax(kind string, pid int32, max int) ([]ConnectionStat, error) { func ConnectionsPidMax(kind string, pid int32, max int) ([]ConnectionStat, error) {
return ConnectionsPidMaxWithContext(context.Background(), kind, pid, max)
}
func ConnectionsPidMaxWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) {
return []ConnectionStat{}, common.ErrNotImplementedError return []ConnectionStat{}, common.ErrNotImplementedError
} }

View File

@ -3,6 +3,7 @@
package net package net
import ( import (
"context"
"errors" "errors"
"net" "net"
"os" "os"
@ -30,6 +31,10 @@ const (
) )
func IOCounters(pernic bool) ([]IOCountersStat, error) { func IOCounters(pernic bool) ([]IOCountersStat, error) {
return IOCountersWithContext(context.Background(), pernic)
}
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
ifs, err := net.Interfaces() ifs, err := net.Interfaces()
if err != nil { if err != nil {
return nil, err return nil, err
@ -66,11 +71,19 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
// NetIOCountersByFile is an method which is added just a compatibility for linux. // NetIOCountersByFile is an method which is added just a compatibility for linux.
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
return IOCountersByFileWithContext(context.Background(), pernic, filename)
}
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
return IOCounters(pernic) return IOCounters(pernic)
} }
// Return a list of network connections opened by a process // Return a list of network connections opened by a process
func Connections(kind string) ([]ConnectionStat, error) { func Connections(kind string) ([]ConnectionStat, error) {
return ConnectionsWithContext(context.Background(), kind)
}
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
var ret []ConnectionStat var ret []ConnectionStat
return ret, common.ErrNotImplementedError return ret, common.ErrNotImplementedError
@ -79,10 +92,18 @@ func Connections(kind string) ([]ConnectionStat, error) {
// Return a list of network connections opened returning at most `max` // Return a list of network connections opened returning at most `max`
// connections for each running process. // connections for each running process.
func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
return ConnectionsMaxWithContext(context.Background(), kind, max)
}
func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
return []ConnectionStat{}, common.ErrNotImplementedError return []ConnectionStat{}, common.ErrNotImplementedError
} }
func FilterCounters() ([]FilterStat, error) { func FilterCounters() ([]FilterStat, error) {
return FilterCountersWithContext(context.Background())
}
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
return nil, errors.New("NetFilterCounters not implemented for windows") return nil, errors.New("NetFilterCounters not implemented for windows")
} }
@ -91,5 +112,9 @@ func FilterCounters() ([]FilterStat, error) {
// just the protocols in the list are returned. // just the protocols in the list are returned.
// Not Implemented for Windows // Not Implemented for Windows
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
return ProtoCountersWithContext(context.Background(), protocols)
}
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
return nil, errors.New("NetProtoCounters not implemented for windows") return nil, errors.New("NetProtoCounters not implemented for windows")
} }

View File

@ -235,10 +235,15 @@ func (p *Process) Terminal() (string, error) {
} }
func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
terminal, _, _, _, _, _, err := p.fillFromStat() t, _, _, _, _, _, err := p.fillFromStat()
if err != nil { if err != nil {
return "", err return "", err
} }
termmap, err := getTerminalMap()
if err != nil {
return "", err
}
terminal := termmap[t]
return terminal, nil return terminal, nil
} }
@ -1115,11 +1120,11 @@ func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
return nil return nil
} }
func (p *Process) fillFromTIDStat(tid int32) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromTIDStat(tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, error) {
return p.fillFromTIDStatWithContext(context.Background(), tid) return p.fillFromTIDStatWithContext(context.Background(), tid)
} }
func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, error) {
pid := p.Pid pid := p.Pid
var statPath string var statPath string
@ -1131,7 +1136,7 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (st
contents, err := ioutil.ReadFile(statPath) contents, err := ioutil.ReadFile(statPath)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
} }
fields := strings.Fields(string(contents)) fields := strings.Fields(string(contents))
@ -1140,28 +1145,23 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (st
i++ i++
} }
termmap, err := getTerminalMap() terminal, err := strconv.ParseUint(fields[i+5], 10, 64)
terminal := ""
if err == nil {
t, err := strconv.ParseUint(fields[i+5], 10, 64)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
}
terminal = termmap[t]
} }
ppid, err := strconv.ParseInt(fields[i+2], 10, 32) ppid, err := strconv.ParseInt(fields[i+2], 10, 32)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
} }
utime, err := strconv.ParseFloat(fields[i+12], 64) utime, err := strconv.ParseFloat(fields[i+12], 64)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
} }
stime, err := strconv.ParseFloat(fields[i+13], 64) stime, err := strconv.ParseFloat(fields[i+13], 64)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
} }
cpuTimes := &cpu.TimesStat{ cpuTimes := &cpu.TimesStat{
@ -1173,7 +1173,7 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (st
bootTime, _ := host.BootTime() bootTime, _ := host.BootTime()
t, err := strconv.ParseUint(fields[i+20], 10, 64) t, err := strconv.ParseUint(fields[i+20], 10, 64)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
} }
ctime := (t / uint64(ClockTicks)) + uint64(bootTime) ctime := (t / uint64(ClockTicks)) + uint64(bootTime)
createTime := int64(ctime * 1000) createTime := int64(ctime * 1000)
@ -1193,11 +1193,11 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (st
return terminal, int32(ppid), cpuTimes, createTime, uint32(rtpriority), nice, nil return terminal, int32(ppid), cpuTimes, createTime, uint32(rtpriority), nice, nil
} }
func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromStat() (uint64, int32, *cpu.TimesStat, int64, uint32, int32, error) {
return p.fillFromStatWithContext(context.Background()) return p.fillFromStatWithContext(context.Background())
} }
func (p *Process) fillFromStatWithContext(ctx context.Context) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromStatWithContext(ctx context.Context) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, error) {
return p.fillFromTIDStat(-1) return p.fillFromTIDStat(-1)
} }

View File

@ -144,8 +144,6 @@ func GetWin32ProcWithContext(ctx context.Context, pid int32) ([]Win32_Process, e
var dst []Win32_Process var dst []Win32_Process
query := fmt.Sprintf("WHERE ProcessId = %d", pid) query := fmt.Sprintf("WHERE ProcessId = %d", pid)
q := wmi.CreateQuery(&dst, query) q := wmi.CreateQuery(&dst, query)
ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
defer cancel()
err := common.WMIQueryWithContext(ctx, q, &dst) err := common.WMIQueryWithContext(ctx, q, &dst)
if err != nil { if err != nil {
return []Win32_Process{}, fmt.Errorf("could not get win32Proc: %s", err) return []Win32_Process{}, fmt.Errorf("could not get win32Proc: %s", err)
@ -457,8 +455,6 @@ func (p *Process) Children() ([]*Process, error) {
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
var dst []Win32_Process var dst []Win32_Process
query := wmi.CreateQuery(&dst, fmt.Sprintf("Where ParentProcessId = %d", p.Pid)) query := wmi.CreateQuery(&dst, fmt.Sprintf("Where ParentProcessId = %d", p.Pid))
ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
defer cancel()
err := common.WMIQueryWithContext(ctx, query, &dst) err := common.WMIQueryWithContext(ctx, query, &dst)
if err != nil { if err != nil {
return nil, err return nil, err

40
vendor/vendor.json vendored
View File

@ -45,40 +45,40 @@
"revisionTime": "2017-07-14T06:33:53Z" "revisionTime": "2017-07-14T06:33:53Z"
}, },
{ {
"checksumSHA1": "D4ykpIWCkigwlqfT/fdWrUFKG3g=", "checksumSHA1": "PHwqGzRGjJ81TtD7aBgcppCjeRg=",
"path": "github.com/shirou/gopsutil/cpu", "path": "github.com/shirou/gopsutil/cpu",
"revision": "a8bc26299477e53c4ca89f7ec60160a74d4c0890", "revision": "12ab94e8042b4639d3cbd6bdafd0be9be8a33e88",
"revisionTime": "2017-11-12T16:40:41Z" "revisionTime": "2018-02-21T07:26:18Z"
}, },
{ {
"checksumSHA1": "9hoAGAesAy8x1d/G9eQfzCmuHc8=", "checksumSHA1": "sy4twPdTy18BS0k2PxntChG2FcE=",
"path": "github.com/shirou/gopsutil/host", "path": "github.com/shirou/gopsutil/host",
"revision": "a8bc26299477e53c4ca89f7ec60160a74d4c0890", "revision": "12ab94e8042b4639d3cbd6bdafd0be9be8a33e88",
"revisionTime": "2017-11-12T16:40:41Z" "revisionTime": "2018-02-21T07:26:18Z"
}, },
{ {
"checksumSHA1": "jWpwWWcywJPNhKTYxi4RXds+amQ=", "checksumSHA1": "xPMr7RbEnFd1XxOFpLOSdX4cEO0=",
"path": "github.com/shirou/gopsutil/internal/common", "path": "github.com/shirou/gopsutil/internal/common",
"revision": "6a368fb7cd1221fa6ea90facc9447c9a2234c255", "revision": "12ab94e8042b4639d3cbd6bdafd0be9be8a33e88",
"revisionTime": "2018-01-11T02:47:13Z" "revisionTime": "2018-02-21T07:26:18Z"
}, },
{ {
"checksumSHA1": "UuwHornIODuEq7fYnsZwdVgERLk=", "checksumSHA1": "Cgm7wMq9rJpnUeZFV3OD8qkTKOM=",
"path": "github.com/shirou/gopsutil/mem", "path": "github.com/shirou/gopsutil/mem",
"revision": "27389f01ec9364f60d6dba4fbe9751d3b7058d46", "revision": "12ab94e8042b4639d3cbd6bdafd0be9be8a33e88",
"revisionTime": "2017-12-14T06:29:47Z" "revisionTime": "2018-02-21T07:26:18Z"
}, },
{ {
"checksumSHA1": "AiC1wzY1Rjxs7iitVBvn4YM886k=", "checksumSHA1": "Z7FjZvR5J5xh6Ne572gD7tRUsc8=",
"path": "github.com/shirou/gopsutil/net", "path": "github.com/shirou/gopsutil/net",
"revision": "a8bc26299477e53c4ca89f7ec60160a74d4c0890", "revision": "12ab94e8042b4639d3cbd6bdafd0be9be8a33e88",
"revisionTime": "2017-11-12T16:40:41Z" "revisionTime": "2018-02-21T07:26:18Z"
}, },
{ {
"checksumSHA1": "a5m6WLMC9Y0N96wxwM1n4JoIIyg=", "checksumSHA1": "Ylp6t7kozHBFREv3tBcK4B1SMI4=",
"path": "github.com/shirou/gopsutil/process", "path": "github.com/shirou/gopsutil/process",
"revision": "6a368fb7cd1221fa6ea90facc9447c9a2234c255", "revision": "12ab94e8042b4639d3cbd6bdafd0be9be8a33e88",
"revisionTime": "2018-01-11T02:47:13Z" "revisionTime": "2018-02-21T07:26:18Z"
}, },
{ {
"checksumSHA1": "Nve7SpDmjsv6+rhkXAkfg/UQx94=", "checksumSHA1": "Nve7SpDmjsv6+rhkXAkfg/UQx94=",
@ -86,6 +86,10 @@
"revision": "bb4de0191aa41b5507caa14b0650cdbddcd9280b", "revision": "bb4de0191aa41b5507caa14b0650cdbddcd9280b",
"revisionTime": "2016-09-30T03:27:40Z" "revisionTime": "2016-09-30T03:27:40Z"
}, },
{
"path": "github.com/shirou/win32",
"revision": ""
},
{ {
"checksumSHA1": "L8WNMYHKGlN21LnrVhG05OWxwAA=", "checksumSHA1": "L8WNMYHKGlN21LnrVhG05OWxwAA=",
"path": "github.com/stretchr/testify/assert", "path": "github.com/stretchr/testify/assert",