Skip to content

Commit

Permalink
Merge pull request #347 from danielnelson/io-counters-for-names
Browse files Browse the repository at this point in the history
Add disk.IOCountersForNames function
  • Loading branch information
shirou authored Apr 10, 2017
2 parents e49a95f + ab6db76 commit f5781ca
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 7 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{})
}
8 changes: 7 additions & 1 deletion disk/disk_darwin_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ import (
"errors"
"strings"
"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 {
return nil, errors.New("Unable to fetch disk list")
}
Expand Down Expand Up @@ -78,6 +80,10 @@ func IOCounters() (map[string]IOCountersStat, error) {
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
}

Expand Down
2 changes: 1 addition & 1 deletion disk/disk_darwin_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ 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
}
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 f5781ca

Please sign in to comment.