error handling in linux is now unsuppressed.

However, so many err check is introduced. Some refactoring is needed.
pull/16/head
WAKAYAMA Shirou 11 years ago
parent 9d03533a0f
commit a2d4c5dcb8

@ -32,11 +32,26 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
return ret, err return ret, err
} }
user, _ := strconv.ParseFloat(cpuTime[CPUser], 32) user, err := strconv.ParseFloat(cpuTime[CPUser], 32)
nice, _ := strconv.ParseFloat(cpuTime[CPNice], 32) if err != nil {
sys, _ := strconv.ParseFloat(cpuTime[CPSys], 32) return ret, err
idle, _ := strconv.ParseFloat(cpuTime[CPIdle], 32) }
intr, _ := strconv.ParseFloat(cpuTime[CPIntr], 32) 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{ c := CPUTimesStat{
User: float32(user / ClocksPerSec), User: float32(user / ClocksPerSec),
@ -71,7 +86,12 @@ func CPUInfo() ([]CPUInfoStat, error) {
} else if strings.HasPrefix(line, "machdep.cpu.model") { } else if strings.HasPrefix(line, "machdep.cpu.model") {
c.Model = values[1] c.Model = values[1]
} else if strings.HasPrefix(line, "machdep.cpu.stepping") { } 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") { } else if strings.HasPrefix(line, "machdep.cpu.features") {
for _, v := range values[1:] { for _, v := range values[1:] {
c.Flags = append(c.Flags, strings.ToLower(v)) c.Flags = append(c.Flags, strings.ToLower(v))
@ -85,9 +105,18 @@ func CPUInfo() ([]CPUInfoStat, error) {
c.Flags = append(c.Flags, strings.ToLower(v)) c.Flags = append(c.Flags, strings.ToLower(v))
} }
} else if strings.HasPrefix(line, "machdep.cpu.core_count") { } 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") { } 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") { } else if strings.HasPrefix(line, "machdep.cpu.vendor") {
c.VendorID = values[1] c.VendorID = values[1]
} }

@ -46,7 +46,11 @@ func CPUInfo() ([]CPUInfoStat, error) {
switch key { switch key {
case "processor": case "processor":
c = CPUInfoStat{} 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": case "vendor_id":
c.VendorID = value c.VendorID = value
case "cpu family": case "cpu family":
@ -56,17 +60,33 @@ func CPUInfo() ([]CPUInfoStat, error) {
case "model name": case "model name":
c.ModelName = value c.ModelName = value
case "stepping": 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": 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": 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": case "physical id":
c.PhysicalID = value c.PhysicalID = value
case "core id": case "core id":
c.CoreID = value c.CoreID = value
case "cpu cores": 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": case "flags":
c.Flags = strings.Split(value, ",") c.Flags = strings.Split(value, ",")
} }

@ -31,7 +31,7 @@ type DiskIOCountersStat struct {
ReadTime uint64 `json:"readTime"` ReadTime uint64 `json:"readTime"`
WriteTime uint64 `json:"writeTime"` WriteTime uint64 `json:"writeTime"`
Name string `json:"name"` Name string `json:"name"`
IoTime uint64 `json:"ioTime"` IoTime uint64 `json:"ioTime"`
} }
func (d DiskUsageStat) String() string { func (d DiskUsageStat) String() string {

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

@ -3,6 +3,7 @@
package gopsutil package gopsutil
import ( import (
"strconv"
"strings" "strings"
"syscall" "syscall"
) )
@ -24,17 +25,42 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
switch key { switch key {
case "MemTotal": case "MemTotal":
ret.Total = mustParseUint64(value) * 1000 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": case "MemFree":
ret.Free = mustParseUint64(value) * 1000 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": 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": 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": 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": 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 ret.Available = ret.Free + ret.Buffers + ret.Cached

@ -3,6 +3,7 @@
package gopsutil package gopsutil
import ( import (
"strconv"
"strings" "strings"
) )
@ -22,16 +23,50 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
if fields[0] == "" { if fields[0] == "" {
continue 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{ nic := NetIOCountersStat{
Name: strings.Trim(fields[0], ":"), Name: strings.Trim(fields[0], ":"),
BytesRecv: mustParseUint64(fields[1]), BytesRecv: bytesRecv,
PacketsRecv: mustParseUint64(fields[2]), PacketsRecv: packetsRecv,
Errin: mustParseUint64(fields[3]), Errin: errIn,
Dropin: mustParseUint64(fields[4]), Dropin: dropIn,
BytesSent: mustParseUint64(fields[9]), BytesSent: bytesSent,
PacketsSent: mustParseUint64(fields[10]), PacketsSent: packetsSent,
Errout: mustParseUint64(fields[11]), Errout: errOut,
Dropout: mustParseUint64(fields[12]), Dropout: dropOut,
} }
ret = append(ret, nic) ret = append(ret, nic)
} }

@ -5,13 +5,13 @@ import (
) )
type Process struct { type Process struct {
Pid int32 `json:"pid"` Pid int32 `json:"pid"`
name string name string
status string status string
numCtxSwitches *NumCtxSwitchesStat numCtxSwitches *NumCtxSwitchesStat
uids []int32 uids []int32
gids []int32 gids []int32
numThreads int32 numThreads int32
} }
type OpenFilesStat struct { type OpenFilesStat struct {

@ -195,7 +195,7 @@ func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
lines := strings.Split(string(contents), "\n") lines := strings.Split(string(contents), "\n")
// function of parsing a block // function of parsing a block
getBlock := func(first_line []string, block []string) MemoryMapsStat { getBlock := func(first_line []string, block []string) (MemoryMapsStat, error) {
m := MemoryMapsStat{} m := MemoryMapsStat{}
m.Path = first_line[len(first_line)-1] m.Path = first_line[len(first_line)-1]
@ -205,30 +205,35 @@ func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
continue continue
} }
v := strings.Trim(field[1], " kB") // remove last "kB" v := strings.Trim(field[1], " kB") // remove last "kB"
t, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return m, err
}
switch field[0] { switch field[0] {
case "Size": case "Size":
m.Size = mustParseUint64(v) m.Size = t
case "Rss": case "Rss":
m.Rss = mustParseUint64(v) m.Rss = t
case "Pss": case "Pss":
m.Pss = mustParseUint64(v) m.Pss = t
case "Shared_Clean": case "Shared_Clean":
m.SharedClean = mustParseUint64(v) m.SharedClean = t
case "Shared_Dirty": case "Shared_Dirty":
m.SharedDirty = mustParseUint64(v) m.SharedDirty = t
case "Private_Clean": case "Private_Clean":
m.PrivateClean = mustParseUint64(v) m.PrivateClean = t
case "Private_Dirty": case "Private_Dirty":
m.PrivateDirty = mustParseUint64(v) m.PrivateDirty = t
case "Referenced": case "Referenced":
m.Referenced = mustParseUint64(v) m.Referenced = t
case "Anonymous": case "Anonymous":
m.Anonymous = mustParseUint64(v) m.Anonymous = t
case "Swap": case "Swap":
m.Swap = mustParseUint64(v) m.Swap = t
} }
} }
return m return m, nil
} }
blocks := make([]string, 16) blocks := make([]string, 16)
@ -237,7 +242,11 @@ func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
if strings.HasSuffix(field[0], ":") == false { if strings.HasSuffix(field[0], ":") == false {
// new block section // new block section
if len(blocks) > 0 { if len(blocks) > 0 {
ret = append(ret, getBlock(field, blocks)) g, err := getBlock(field, blocks)
if err != nil {
return &ret, err
}
ret = append(ret, g)
} }
// starts new block // starts new block
blocks = make([]string, 16) blocks = make([]string, 16)
@ -272,9 +281,13 @@ func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) {
if err != nil { if err != nil {
continue continue
} }
t, err := strconv.ParseUint(fd, 10, 64)
if err != nil {
return numFDs, openfiles, err
}
o := &OpenFilesStat{ o := &OpenFilesStat{
Path: filepath, Path: filepath,
Fd: mustParseUint64(fd), Fd: t,
} }
openfiles = append(openfiles, o) openfiles = append(openfiles, o)
} }
@ -338,15 +351,20 @@ func (p *Process) fillFromIO() (*IOCountersStat, error) {
if len(field) < 2 { if len(field) < 2 {
continue continue
} }
t, err := strconv.ParseInt(strings.Trim(field[1], " \t"), 10, 32)
if err != nil {
return nil, err
}
switch field[0] { switch field[0] {
case "rchar": case "rchar":
ret.ReadCount = mustParseInt32(strings.Trim(field[1], " \t")) ret.ReadCount = int32(t)
case "wchar": case "wchar":
ret.WriteCount = mustParseInt32(strings.Trim(field[1], " \t")) ret.WriteCount = int32(t)
case "read_bytes": case "read_bytes":
ret.ReadBytes = mustParseInt32(strings.Trim(field[1], " \t")) ret.ReadBytes = int32(t)
case "write_bytes": case "write_bytes":
ret.WriteBytes = mustParseInt32(strings.Trim(field[1], " \t")) ret.WriteBytes = int32(t)
} }
} }
@ -363,19 +381,43 @@ func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) {
} }
fields := strings.Split(string(contents), " ") fields := strings.Split(string(contents), " ")
vms := mustParseUint64(fields[0]) * PageSize vms, err := strconv.ParseUint(fields[0], 10, 64)
rss := mustParseUint64(fields[1]) * PageSize if err != nil {
return nil, nil, err
}
rss, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
return nil, nil, err
}
memInfo := &MemoryInfoStat{ memInfo := &MemoryInfoStat{
RSS: rss, RSS: rss * PageSize,
VMS: vms, VMS: vms * PageSize,
}
shared, err := strconv.ParseUint(fields[2], 10, 64)
if err != nil {
return nil, nil, err
}
text, err := strconv.ParseUint(fields[3], 10, 64)
if err != nil {
return nil, nil, err
} }
lib, err := strconv.ParseUint(fields[4], 10, 64)
if err != nil {
return nil, nil, err
}
dirty, err := strconv.ParseUint(fields[5], 10, 64)
if err != nil {
return nil, nil, err
}
memInfoEx := &MemoryInfoExStat{ memInfoEx := &MemoryInfoExStat{
RSS: rss, RSS: rss * PageSize,
VMS: vms, VMS: vms * PageSize,
Shared: mustParseUint64(fields[2]) * PageSize, Shared: shared * PageSize,
Text: mustParseUint64(fields[3]) * PageSize, Text: text * PageSize,
Lib: mustParseUint64(fields[4]) * PageSize, Lib: lib * PageSize,
Dirty: mustParseUint64(fields[5]) * PageSize, Dirty: dirty * PageSize,
} }
return memInfo, memInfoEx, nil return memInfo, memInfoEx, nil
@ -458,12 +500,25 @@ func (p *Process) fillFromStat() (string, int32, *CPUTimesStat, int64, int32, er
termmap, err := getTerminalMap() termmap, err := getTerminalMap()
terminal := "" terminal := ""
if err == nil { if err == nil {
terminal = termmap[mustParseUint64(fields[6])] t, err := strconv.ParseUint(fields[6], 10, 64)
if err != nil {
return "", 0, nil, 0, 0, err
}
terminal = termmap[t]
} }
ppid := mustParseInt32(fields[3]) ppid, err := strconv.ParseInt(fields[3], 10, 32)
utime, _ := strconv.ParseFloat(fields[13], 64) if err != nil {
stime, _ := strconv.ParseFloat(fields[14], 64) return "", 0, nil, 0, 0, err
}
utime, err := strconv.ParseFloat(fields[13], 64)
if err != nil {
return "", 0, nil, 0, 0, err
}
stime, err := strconv.ParseFloat(fields[14], 64)
if err != nil {
return "", 0, nil, 0, 0, err
}
cpuTimes := &CPUTimesStat{ cpuTimes := &CPUTimesStat{
CPU: "cpu", CPU: "cpu",
@ -472,7 +527,12 @@ func (p *Process) fillFromStat() (string, int32, *CPUTimesStat, int64, int32, er
} }
bootTime, _ := BootTime() bootTime, _ := BootTime()
ctime := ((mustParseUint64(fields[21]) / uint64(ClockTicks)) + uint64(bootTime)) * 1000 t, err := strconv.ParseUint(fields[21], 10, 64)
if err != nil {
return "", 0, nil, 0, 0, err
}
ctime := ((t / uint64(ClockTicks)) + uint64(bootTime)) * 1000
createTime := int64(ctime) createTime := int64(ctime)
// p.Nice = mustParseInt32(fields[18]) // p.Nice = mustParseInt32(fields[18])
@ -480,7 +540,7 @@ func (p *Process) fillFromStat() (string, int32, *CPUTimesStat, int64, int32, er
snice, _ := syscall.Getpriority(PrioProcess, int(pid)) snice, _ := syscall.Getpriority(PrioProcess, int(pid))
nice := int32(snice) // FIXME: is this true? nice := int32(snice) // FIXME: is this true?
return terminal, ppid, cpuTimes, createTime, nice, nil return terminal, int32(ppid), cpuTimes, createTime, nice, nil
} }
func Pids() ([]int32, error) { func Pids() ([]int32, error) {

@ -4,8 +4,8 @@ package gopsutil
import ( import (
"os" "os"
"os/user"
"os/exec" "os/exec"
"os/user"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"

Loading…
Cancel
Save