From f55af7d21f7e6f124f69009fb4b9a8ff2c91d52a Mon Sep 17 00:00:00 2001
From: Daniel Nelson <danielnelson@users.noreply.github.com>
Date: Tue, 11 Apr 2017 11:41:09 -0700
Subject: [PATCH] Use name filter for IOCounters in diskio (#2649)

Use IOCountersForNames for disk counters.
---
 CHANGELOG.md                     |  1 +
 Godeps                           |  2 +-
 plugins/inputs/system/disk.go    | 15 +--------------
 plugins/inputs/system/mock_PS.go |  2 +-
 plugins/inputs/system/ps.go      |  6 +++---
 5 files changed, 7 insertions(+), 19 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3a169821..12381152 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -97,6 +97,7 @@ be deprecated eventually.
 - [#2610](https://github.com/influxdata/telegraf/pull/2610): Fix deadlock when output cannot write
 - [#2410](https://github.com/influxdata/telegraf/issues/2410): Fix connection leak in postgresql.
 - [#2628](https://github.com/influxdata/telegraf/issues/2628): Set default measurement name for snmp input.
+- [#2649](https://github.com/influxdata/telegraf/pull/2649): Improve performance of diskio with many disks
 
 ## v1.2.1 [2017-02-01]
 
diff --git a/Godeps b/Godeps
index ab72be8f..2e04c0cd 100644
--- a/Godeps
+++ b/Godeps
@@ -44,7 +44,7 @@ github.com/prometheus/common dd2f054febf4a6c00f2343686efb775948a8bff4
 github.com/prometheus/procfs 1878d9fbb537119d24b21ca07effd591627cd160
 github.com/rcrowley/go-metrics 1f30fe9094a513ce4c700b9a54458bbb0c96996c
 github.com/samuel/go-zookeeper 1d7be4effb13d2d908342d349d71a284a7542693
-github.com/shirou/gopsutil d371ba1293cb48fedc6850526ea48b3846c54f2c
+github.com/shirou/gopsutil dfbb3e40da8d6fcd1aa0d87003e965fe0ca745ea
 github.com/soniah/gosnmp 5ad50dc75ab389f8a1c9f8a67d3a1cd85f67ed15
 github.com/streadway/amqp 63795daa9a446c920826655f26ba31c81c860fd6
 github.com/stretchr/testify 4d4bfba8f1d1027c4fdbe371823030df51419987
diff --git a/plugins/inputs/system/disk.go b/plugins/inputs/system/disk.go
index 3f6d83c1..004466f8 100644
--- a/plugins/inputs/system/disk.go
+++ b/plugins/inputs/system/disk.go
@@ -125,25 +125,12 @@ func (_ *DiskIOStats) SampleConfig() string {
 }
 
 func (s *DiskIOStats) Gather(acc telegraf.Accumulator) error {
-	diskio, err := s.ps.DiskIO()
+	diskio, err := s.ps.DiskIO(s.Devices)
 	if err != nil {
 		return fmt.Errorf("error getting disk io info: %s", err)
 	}
 
-	var restrictDevices bool
-	devices := make(map[string]bool)
-	if len(s.Devices) != 0 {
-		restrictDevices = true
-		for _, dev := range s.Devices {
-			devices[dev] = true
-		}
-	}
-
 	for _, io := range diskio {
-		_, member := devices[io.Name]
-		if restrictDevices && !member {
-			continue
-		}
 		tags := map[string]string{}
 		tags["name"] = s.diskName(io.Name)
 		for t, v := range s.diskTags(io.Name) {
diff --git a/plugins/inputs/system/mock_PS.go b/plugins/inputs/system/mock_PS.go
index e9f96a6c..a83a8b80 100644
--- a/plugins/inputs/system/mock_PS.go
+++ b/plugins/inputs/system/mock_PS.go
@@ -61,7 +61,7 @@ func (m *MockPS) NetProto() ([]net.ProtoCountersStat, error) {
 	return r0, r1
 }
 
-func (m *MockPS) DiskIO() (map[string]disk.IOCountersStat, error) {
+func (m *MockPS) DiskIO(names []string) (map[string]disk.IOCountersStat, error) {
 	ret := m.Called()
 
 	r0 := ret.Get(0).(map[string]disk.IOCountersStat)
diff --git a/plugins/inputs/system/ps.go b/plugins/inputs/system/ps.go
index b0e021e4..d2532781 100644
--- a/plugins/inputs/system/ps.go
+++ b/plugins/inputs/system/ps.go
@@ -17,7 +17,7 @@ type PS interface {
 	DiskUsage(mountPointFilter []string, fstypeExclude []string) ([]*disk.UsageStat, []*disk.PartitionStat, error)
 	NetIO() ([]net.IOCountersStat, error)
 	NetProto() ([]net.ProtoCountersStat, error)
-	DiskIO() (map[string]disk.IOCountersStat, error)
+	DiskIO(names []string) (map[string]disk.IOCountersStat, error)
 	VMStat() (*mem.VirtualMemoryStat, error)
 	SwapStat() (*mem.SwapMemoryStat, error)
 	NetConnections() ([]net.ConnectionStat, error)
@@ -120,8 +120,8 @@ func (s *systemPS) NetConnections() ([]net.ConnectionStat, error) {
 	return net.Connections("all")
 }
 
-func (s *systemPS) DiskIO() (map[string]disk.IOCountersStat, error) {
-	m, err := disk.IOCounters()
+func (s *systemPS) DiskIO(names []string) (map[string]disk.IOCountersStat, error) {
+	m, err := disk.IOCountersForNames(names)
 	if err == internal.NotImplementedError {
 		return nil, nil
 	}
-- 
GitLab