Skip to content

Commit

Permalink
Add disk.IOCountersForNames function
Browse files Browse the repository at this point in the history
Operates like disk.IOCounters, but accepts an array of names to limit
the results.
  • Loading branch information
danielnelson committed Apr 7, 2017
1 parent e49a95f commit 3f35f00
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ func (d IOCountersStat) String() string {
s, _ := json.Marshal(d)
return string(s)
}

func IOCounters() (map[string]IOCountersStat, error) {
return IOCountersForNames([]string{})
}
4 changes: 4 additions & 0 deletions disk/disk_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func Partitions(all bool) ([]PartitionStat, error) {
return ret, nil
}

func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
return nil, common.ErrNotImplementedError
}

func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
var _p0 unsafe.Pointer
var bufsize uintptr
Expand Down
2 changes: 1 addition & 1 deletion disk/disk_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package disk

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
}

Expand Down
6 changes: 5 additions & 1 deletion disk/disk_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func Partitions(all bool) ([]PartitionStat, error) {
return ret, nil
}

func IOCounters() (map[string]IOCountersStat, error) {
func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
// statinfo->devinfo->devstat
// /usr/include/devinfo.h
ret := make(map[string]IOCountersStat)
Expand All @@ -119,6 +119,10 @@ func IOCounters() (map[string]IOCountersStat, error) {
un := strconv.Itoa(int(d.Unit_number))
name := common.IntToString(d.Device_name[:]) + un

if len(names) > 0 && !common.StringsHas(names, name) {
continue
}

ds := IOCountersStat{
ReadCount: d.Operations[DEVSTAT_READ],
WriteCount: d.Operations[DEVSTAT_WRITE],
Expand Down
7 changes: 6 additions & 1 deletion disk/disk_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func getFileSystems() ([]string, error) {
return ret, nil
}

func IOCounters() (map[string]IOCountersStat, error) {
func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
filename := common.HostProc("diskstats")
lines, err := common.ReadLines(filename)
if err != nil {
Expand All @@ -288,6 +288,11 @@ func IOCounters() (map[string]IOCountersStat, error) {
continue
}
name := fields[2]

if len(names) > 0 && !common.StringsHas(names, name) {
continue
}

reads, err := strconv.ParseUint((fields[3]), 10, 64)
if err != nil {
return ret, err
Expand Down
6 changes: 5 additions & 1 deletion disk/disk_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Partitions(all bool) ([]PartitionStat, error) {
return ret, nil
}

func IOCounters() (map[string]IOCountersStat, error) {
func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
ret := make(map[string]IOCountersStat)

r, err := syscall.Sysctl("hw.diskstats")
Expand All @@ -84,6 +84,10 @@ func IOCounters() (map[string]IOCountersStat, error) {
}
name := common.IntToString(d.Name[:])

if len(names) > 0 && !common.StringsHas(names, name) {
continue
}

ds := IOCountersStat{
ReadCount: d.Rxfer,
WriteCount: d.Wxfer,
Expand Down
7 changes: 6 additions & 1 deletion disk/disk_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func Partitions(all bool) ([]PartitionStat, error) {
return ret, nil
}

func IOCounters() (map[string]IOCountersStat, error) {
func IOCountersForNames(names []string) (map[string]IOCountersStat, error) {
ret := make(map[string]IOCountersStat, 0)
var dst []Win32_PerfFormattedData

Expand All @@ -141,6 +141,11 @@ func IOCounters() (map[string]IOCountersStat, error) {
if len(d.Name) > 3 { // not get _Total or Harddrive
continue
}

if len(names) > 0 && !common.StringsHas(names, name) {
continue
}

ret[d.Name] = IOCountersStat{
Name: d.Name,
ReadCount: uint64(d.AvgDiskReadQueueLength),
Expand Down

0 comments on commit 3f35f00

Please sign in to comment.