1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00

Merge pull request #347 from danielnelson/io-counters-for-names

Add disk.IOCountersForNames function
This commit is contained in:
shirou 2017-04-10 21:52:16 +09:00 committed by GitHub
commit f5781cab54
8 changed files with 35 additions and 7 deletions

View File

@ -62,3 +62,7 @@ func (d IOCountersStat) String() string {
s, _ := json.Marshal(d) s, _ := json.Marshal(d)
return string(s) return string(s)
} }
func IOCounters() (map[string]IOCountersStat, error) {
return IOCountersForNames([]string{})
}

View File

@ -30,9 +30,11 @@ import (
"errors" "errors"
"strings" "strings"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/internal/common"
) )
func IOCounters() (map[string]IOCountersStat, error) { func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
if C.StartIOCounterFetch() == 0 { if C.StartIOCounterFetch() == 0 {
return nil, errors.New("Unable to fetch disk list") return nil, errors.New("Unable to fetch disk list")
} }
@ -78,6 +80,10 @@ func IOCounters() (map[string]IOCountersStat, error) {
Name: strings.TrimFunc(C.GoStringN(&di.DiskName[0], C.MAX_DISK_NAME), isRuneNull), Name: strings.TrimFunc(C.GoStringN(&di.DiskName[0], C.MAX_DISK_NAME), isRuneNull),
} }
if len(names) > 0 && !common.StringsHas(names, d.Name) {
continue
}
ret[d.Name] = d ret[d.Name] = d
} }

View File

@ -5,6 +5,6 @@ package disk
import "github.com/shirou/gopsutil/internal/common" import "github.com/shirou/gopsutil/internal/common"
func IOCounters() (map[string]IOCountersStat, error) { func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }

View File

@ -4,7 +4,7 @@ package disk
import "github.com/shirou/gopsutil/internal/common" import "github.com/shirou/gopsutil/internal/common"
func IOCounters() (map[string]IOCountersStat, error) { func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }

View File

@ -94,7 +94,7 @@ func Partitions(all bool) ([]PartitionStat, error) {
return ret, nil return ret, nil
} }
func IOCounters() (map[string]IOCountersStat, error) { func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
// statinfo->devinfo->devstat // statinfo->devinfo->devstat
// /usr/include/devinfo.h // /usr/include/devinfo.h
ret := make(map[string]IOCountersStat) ret := make(map[string]IOCountersStat)
@ -119,6 +119,10 @@ func IOCounters() (map[string]IOCountersStat, error) {
un := strconv.Itoa(int(d.Unit_number)) un := strconv.Itoa(int(d.Unit_number))
name := common.IntToString(d.Device_name[:]) + un name := common.IntToString(d.Device_name[:]) + un
if len(names) > 0 && !common.StringsHas(names, name) {
continue
}
ds := IOCountersStat{ ds := IOCountersStat{
ReadCount: d.Operations[DEVSTAT_READ], ReadCount: d.Operations[DEVSTAT_READ],
WriteCount: d.Operations[DEVSTAT_WRITE], WriteCount: d.Operations[DEVSTAT_WRITE],

View File

@ -272,7 +272,7 @@ func getFileSystems() ([]string, error) {
return ret, nil return ret, nil
} }
func IOCounters() (map[string]IOCountersStat, error) { func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
filename := common.HostProc("diskstats") filename := common.HostProc("diskstats")
lines, err := common.ReadLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
@ -288,6 +288,11 @@ func IOCounters() (map[string]IOCountersStat, error) {
continue continue
} }
name := fields[2] name := fields[2]
if len(names) > 0 && !common.StringsHas(names, name) {
continue
}
reads, err := strconv.ParseUint((fields[3]), 10, 64) reads, err := strconv.ParseUint((fields[3]), 10, 64)
if err != nil { if err != nil {
return ret, err return ret, err

View File

@ -63,7 +63,7 @@ func Partitions(all bool) ([]PartitionStat, error) {
return ret, nil return ret, nil
} }
func IOCounters() (map[string]IOCountersStat, error) { func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
ret := make(map[string]IOCountersStat) ret := make(map[string]IOCountersStat)
r, err := syscall.Sysctl("hw.diskstats") r, err := syscall.Sysctl("hw.diskstats")
@ -84,6 +84,10 @@ func IOCounters() (map[string]IOCountersStat, error) {
} }
name := common.IntToString(d.Name[:]) name := common.IntToString(d.Name[:])
if len(names) > 0 && !common.StringsHas(names, name) {
continue
}
ds := IOCountersStat{ ds := IOCountersStat{
ReadCount: d.Rxfer, ReadCount: d.Rxfer,
WriteCount: d.Wxfer, WriteCount: d.Wxfer,

View File

@ -129,7 +129,7 @@ func Partitions(all bool) ([]PartitionStat, error) {
return ret, nil return ret, nil
} }
func IOCounters() (map[string]IOCountersStat, error) { func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
ret := make(map[string]IOCountersStat, 0) ret := make(map[string]IOCountersStat, 0)
var dst []Win32_PerfFormattedData var dst []Win32_PerfFormattedData
@ -141,6 +141,11 @@ func IOCounters() (map[string]IOCountersStat, error) {
if len(d.Name) > 3 { // not get _Total or Harddrive if len(d.Name) > 3 { // not get _Total or Harddrive
continue continue
} }
if len(names) > 0 && !common.StringsHas(names, name) {
continue
}
ret[d.Name] = IOCountersStat{ ret[d.Name] = IOCountersStat{
Name: d.Name, Name: d.Name,
ReadCount: uint64(d.AvgDiskReadQueueLength), ReadCount: uint64(d.AvgDiskReadQueueLength),