Skip to content

Commit

Permalink
error handling in linux is now unsuppressed.
Browse files Browse the repository at this point in the history
However, so many err check is introduced. Some refactoring is needed.
  • Loading branch information
shirou committed Sep 19, 2014
1 parent 9d03533 commit a2d4c5d
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 83 deletions.
45 changes: 37 additions & 8 deletions cpu_darwin.go
Expand Up @@ -32,11 +32,26 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
return ret, err
}

user, _ := strconv.ParseFloat(cpuTime[CPUser], 32)
nice, _ := strconv.ParseFloat(cpuTime[CPNice], 32)
sys, _ := strconv.ParseFloat(cpuTime[CPSys], 32)
idle, _ := strconv.ParseFloat(cpuTime[CPIdle], 32)
intr, _ := strconv.ParseFloat(cpuTime[CPIntr], 32)
user, err := strconv.ParseFloat(cpuTime[CPUser], 32)
if err != nil {
return ret, err
}
nice, err := strconv.ParseFloat(cpuTime[CPNice], 32)
if err != nil {
return ret, err
}
sys, err := strconv.ParseFloat(cpuTime[CPSys], 32)
if err != nil {
return ret, err
}
idle, err := strconv.ParseFloat(cpuTime[CPIdle], 32)
if err != nil {
return ret, err
}
intr, err := strconv.ParseFloat(cpuTime[CPIntr], 32)
if err != nil {
return ret, err
}

c := CPUTimesStat{
User: float32(user / ClocksPerSec),
Expand Down Expand Up @@ -71,7 +86,12 @@ func CPUInfo() ([]CPUInfoStat, error) {
} else if strings.HasPrefix(line, "machdep.cpu.model") {
c.Model = values[1]
} else if strings.HasPrefix(line, "machdep.cpu.stepping") {
c.Stepping = mustParseInt32(values[1])
t, err := strconv.ParseInt(values[1], 10, 32)
if err != nil {
return ret, err
}
c.Stepping = int32(t)

} else if strings.HasPrefix(line, "machdep.cpu.features") {
for _, v := range values[1:] {
c.Flags = append(c.Flags, strings.ToLower(v))
Expand All @@ -85,9 +105,18 @@ func CPUInfo() ([]CPUInfoStat, error) {
c.Flags = append(c.Flags, strings.ToLower(v))
}
} else if strings.HasPrefix(line, "machdep.cpu.core_count") {
c.Cores = mustParseInt32(values[1])
t, err := strconv.ParseInt(values[1], 10, 32)
if err != nil {
return ret, err
}
c.Cores = t
} else if strings.HasPrefix(line, "machdep.cpu.cache.size") {
c.CacheSize = mustParseInt32(values[1])
t, err := strconv.ParseInt(values[1], 10, 32)
if err != nil {
return ret, err
}

c.CacheSize = t
} else if strings.HasPrefix(line, "machdep.cpu.vendor") {
c.VendorID = values[1]
}
Expand Down
30 changes: 25 additions & 5 deletions cpu_linux.go
Expand Up @@ -46,7 +46,11 @@ func CPUInfo() ([]CPUInfoStat, error) {
switch key {
case "processor":
c = CPUInfoStat{}
c.CPU = mustParseInt32(value)
t, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return ret, err
}
c.CPU = int32(t)
case "vendor_id":
c.VendorID = value
case "cpu family":
Expand All @@ -56,17 +60,33 @@ func CPUInfo() ([]CPUInfoStat, error) {
case "model name":
c.ModelName = value
case "stepping":
c.Stepping = mustParseInt32(value)
t, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return ret, err
}
c.Stepping = int32(t)
case "cpu MHz":
c.Mhz = mustParseFloat64(value)
t, err := strconv.ParseFloat(value, 64)
if err != nil {
return ret, err
}
c.Mhz = t
case "cache size":
c.CacheSize = mustParseInt32(strings.Replace(value, " KB", "", 1))
t, err := strconv.ParseInt(strings.Replace(value, " KB", "", 1), 10, 32)
if err != nil {
return ret, err
}
c.CacheSize = int32(t)
case "physical id":
c.PhysicalID = value
case "core id":
c.CoreID = value
case "cpu cores":
c.Cores = mustParseInt32(value)
t, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return ret, err
}
c.Cores = int32(t)
case "flags":
c.Flags = strings.Split(value, ",")
}
Expand Down
2 changes: 1 addition & 1 deletion disk.go
Expand Up @@ -31,7 +31,7 @@ type DiskIOCountersStat struct {
ReadTime uint64 `json:"readTime"`
WriteTime uint64 `json:"writeTime"`
Name string `json:"name"`
IoTime uint64 `json:"ioTime"`
IoTime uint64 `json:"ioTime"`
}

func (d DiskUsageStat) String() string {
Expand Down
52 changes: 37 additions & 15 deletions disk_linux.go
Expand Up @@ -3,6 +3,7 @@
package gopsutil

import (
"strconv"
"strings"
)

Expand All @@ -25,7 +26,7 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
for _, line := range lines {
fields := strings.Fields(line)
d := DiskPartitionStat{
Device: fields[0],
Device: fields[0],
Mountpoint: fields[1],
Fstype: fields[2],
Opts: fields[3],
Expand All @@ -48,21 +49,42 @@ func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
for _, line := range lines {
fields := strings.Fields(line)
name := fields[2]
reads := mustParseUint64(fields[3])
rbytes := mustParseUint64(fields[5])
rtime := mustParseUint64(fields[6])
writes := mustParseUint64(fields[7])
wbytes := mustParseUint64(fields[9])
wtime := mustParseUint64(fields[10])
iotime := mustParseUint64(fields[12])
reads, err := strconv.ParseUint((fields[3]), 10, 64)
if err != nil {
return ret, err
}
rbytes, err := strconv.ParseUint((fields[5]), 10, 64)
if err != nil {
return ret, err
}
rtime, err := strconv.ParseUint((fields[6]), 10, 64)
if err != nil {
return ret, err
}
writes, err := strconv.ParseUint((fields[7]), 10, 64)
if err != nil {
return ret, err
}
wbytes, err := strconv.ParseUint((fields[9]), 10, 64)
if err != nil {
return ret, err
}
wtime, err := strconv.ParseUint((fields[10]), 10, 64)
if err != nil {
return ret, err
}
iotime, err := strconv.ParseUint((fields[12]), 10, 64)
if err != nil {
return ret, err
}
d := DiskIOCountersStat{
ReadBytes: rbytes * SectorSize,
WriteBytes: wbytes * SectorSize,
ReadCount: reads,
WriteCount: writes,
ReadTime: rtime,
WriteTime: wtime,
IoTime: iotime,
ReadBytes: uint64(rbytes) * SectorSize,
WriteBytes: uint64(wbytes) * SectorSize,
ReadCount: uint64(reads),
WriteCount: uint64(writes),
ReadTime: uint64(rtime),
WriteTime: uint64(wtime),
IoTime: uint64(iotime),
}
if d == empty {
continue
Expand Down
36 changes: 31 additions & 5 deletions mem_linux.go
Expand Up @@ -3,6 +3,7 @@
package gopsutil

import (
"strconv"
"strings"
"syscall"
)
Expand All @@ -24,17 +25,42 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
switch key {
case "MemTotal":
ret.Total = mustParseUint64(value) * 1000
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
ret.Total = uint64(t) * 1000
case "MemFree":
ret.Free = mustParseUint64(value) * 1000
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
ret.Free = uint64(t) * 1000
case "Buffers":
ret.Buffers = mustParseUint64(value) * 1000
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
ret.Buffers = uint64(t) * 1000
case "Cached":
ret.Cached = mustParseUint64(value) * 1000
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
ret.Cached = uint64(t) * 1000
case "Active":
ret.Active = mustParseUint64(value) * 1000
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
ret.Active = uint64(t) * 1000
case "Inactive":
ret.Inactive = mustParseUint64(value) * 1000

t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
ret.Inactive = uint64(t) * 1000
}
}
ret.Available = ret.Free + ret.Buffers + ret.Cached
Expand Down
51 changes: 43 additions & 8 deletions net_linux.go
Expand Up @@ -3,6 +3,7 @@
package gopsutil

import (
"strconv"
"strings"
)

Expand All @@ -22,16 +23,50 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
if fields[0] == "" {
continue
}

bytesRecv, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
return ret, err
}
packetsRecv, err := strconv.ParseUint(fields[2], 10, 64)
if err != nil {
return ret, err
}
errIn, err := strconv.ParseUint(fields[3], 10, 64)
if err != nil {
return ret, err
}
dropIn, err := strconv.ParseUint(fields[4], 10, 64)
if err != nil {
return ret, err
}
bytesSent, err := strconv.ParseUint(fields[9], 10, 64)
if err != nil {
return ret, err
}
packetsSent, err := strconv.ParseUint(fields[10], 10, 64)
if err != nil {
return ret, err
}
errOut, err := strconv.ParseUint(fields[11], 10, 64)
if err != nil {
return ret, err
}
dropOut, err := strconv.ParseUint(fields[14], 10, 64)
if err != nil {
return ret, err
}

nic := NetIOCountersStat{
Name: strings.Trim(fields[0], ":"),
BytesRecv: mustParseUint64(fields[1]),
PacketsRecv: mustParseUint64(fields[2]),
Errin: mustParseUint64(fields[3]),
Dropin: mustParseUint64(fields[4]),
BytesSent: mustParseUint64(fields[9]),
PacketsSent: mustParseUint64(fields[10]),
Errout: mustParseUint64(fields[11]),
Dropout: mustParseUint64(fields[12]),
BytesRecv: bytesRecv,
PacketsRecv: packetsRecv,
Errin: errIn,
Dropin: dropIn,
BytesSent: bytesSent,
PacketsSent: packetsSent,
Errout: errOut,
Dropout: dropOut,
}
ret = append(ret, nic)
}
Expand Down
12 changes: 6 additions & 6 deletions process.go
Expand Up @@ -5,13 +5,13 @@ import (
)

type Process struct {
Pid int32 `json:"pid"`
name string
status string
Pid int32 `json:"pid"`
name string
status string
numCtxSwitches *NumCtxSwitchesStat
uids []int32
gids []int32
numThreads int32
uids []int32
gids []int32
numThreads int32
}

type OpenFilesStat struct {
Expand Down

0 comments on commit a2d4c5d

Please sign in to comment.