Skip to content

Commit a0b0f94

Browse files
authored
Merge pull request #578 from kubernetes/partitions
Reduce the number of reads to /proc/partitions file and gofmt.
2 parents 220f0b0 + 7b51a90 commit a0b0f94

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

pkg/systemstatsmonitor/disk_collector.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,15 @@ func (dc *diskCollector) collect() {
244244
if dc.config.IncludeRootBlk {
245245
devices = append(devices, listRootBlockDevices(dc.config.LsblkTimeout)...)
246246
}
247+
248+
partitions, err := disk.Partitions(false)
249+
if err != nil {
250+
glog.Errorf("Failed to list disk partitions: %v", err)
251+
return
252+
}
253+
247254
if dc.config.IncludeAllAttachedBlk {
248-
devices = append(devices, listAttachedBlockDevices()...)
255+
devices = append(devices, listAttachedBlockDevices(partitions)...)
249256
}
250257

251258
// Fetch metrics from /proc, /sys.
@@ -254,11 +261,6 @@ func (dc *diskCollector) collect() {
254261
glog.Errorf("Failed to retrieve disk IO counters: %v", err)
255262
return
256263
}
257-
partitions, err := disk.Partitions(false)
258-
if err != nil {
259-
glog.Errorf("Failed to list disk partitions: %v", err)
260-
return
261-
}
262264
sampleTime := time.Now()
263265
defer func() { dc.lastSampleTime = sampleTime }()
264266

@@ -310,15 +312,9 @@ func listRootBlockDevices(timeout time.Duration) []string {
310312
}
311313

312314
// listAttachedBlockDevices lists all currently attached block devices.
313-
func listAttachedBlockDevices() []string {
315+
func listAttachedBlockDevices(partitions []disk.PartitionStat) []string {
314316
blks := []string{}
315317

316-
partitions, err := disk.Partitions(false)
317-
if err != nil {
318-
glog.Errorf("Failed to retrieve the list of disk partitions: %v", err)
319-
return blks
320-
}
321-
322318
for _, partition := range partitions {
323319
blks = append(blks, partition.Device)
324320
}

pkg/systemstatsmonitor/system_stats_monitor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ func TestRegistration(t *testing.T) {
2828
assert.NotPanics(t,
2929
func() { problemdaemon.GetProblemDaemonHandlerOrDie(SystemStatsMonitorName) },
3030
"System stats monitor failed to register itself as a problem daemon.")
31-
}
31+
}

0 commit comments

Comments
 (0)