disk_linux: DiskIOCounter now return all partitions except empty.

pull/4/head
Shirou WAKAYAMA 11 years ago
parent 409b0c74ed
commit 3bd7ad99b2

@ -4,7 +4,6 @@ package gopsutil
import ( import (
"strings" "strings"
"unicode"
) )
const ( const (
@ -37,35 +36,14 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
} }
func DiskIOCounters() (map[string]DiskIOCountersStat, error) { func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
// determine partitions we want to look for filename := "/proc/diskstats"
filename := "/proc/partitions"
lines, err := readLines(filename) lines, err := readLines(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
partitions := make([]string, 0, len(lines)-2)
for _, line := range lines[2:] {
fields := strings.Fields(line)
name := []rune(fields[3])
if unicode.IsDigit(name[len(name)-1]) {
partitions = append(partitions, fields[3])
} else {
// http://code.google.com/p/psutil/issues/detail?id=338
lenpart := len(partitions)
if lenpart == 0 || strings.HasPrefix(partitions[lenpart-1], fields[3]) {
partitions = append(partitions, fields[3])
}
}
}
filename = "/proc/diskstats"
lines, err = readLines(filename)
if err != nil {
return nil, err
}
ret := make(map[string]DiskIOCountersStat, 0) ret := make(map[string]DiskIOCountersStat, 0)
empty := DiskIOCountersStat{}
for _, line := range lines { for _, line := range lines {
fields := strings.Fields(line) fields := strings.Fields(line)
name := fields[2] name := fields[2]
@ -75,19 +53,20 @@ func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
writes := mustParseUint64(fields[7]) writes := mustParseUint64(fields[7])
wbytes := mustParseUint64(fields[9]) wbytes := mustParseUint64(fields[9])
wtime := mustParseUint64(fields[10]) wtime := mustParseUint64(fields[10])
if stringContains(partitions, name) { d := DiskIOCountersStat{
d := DiskIOCountersStat{ ReadBytes: rbytes * SECTOR_SIZE,
Name: name, WriteBytes: wbytes * SECTOR_SIZE,
ReadBytes: rbytes * SECTOR_SIZE, ReadCount: reads,
WriteBytes: wbytes * SECTOR_SIZE, WriteCount: writes,
ReadCount: reads, ReadTime: rtime,
WriteCount: writes, WriteTime: wtime,
ReadTime: rtime, }
WriteTime: wtime, if d == empty {
} continue
ret[name] = d
} }
d.Name = name
ret[name] = d
} }
return ret, nil return ret, nil
} }

@ -39,9 +39,9 @@ func TestDisk_io_counters(t *testing.T) {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
empty := DiskIOCountersStat{} empty := DiskIOCountersStat{}
for _, io := range ret { for part, io := range ret {
if io == empty { if io == empty {
t.Errorf("io_counter error %v", io) t.Errorf("io_counter error %v, %v", part, io)
} }
} }
} }

Loading…
Cancel
Save