diff --git a/cpu_darwin.go b/cpu_darwin.go index 8a61de0a2..81705fd8d 100644 --- a/cpu_darwin.go +++ b/cpu_darwin.go @@ -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), @@ -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)) @@ -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] } diff --git a/cpu_linux.go b/cpu_linux.go index 5ec5f88f6..ada85e638 100644 --- a/cpu_linux.go +++ b/cpu_linux.go @@ -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": @@ -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, ",") } diff --git a/disk.go b/disk.go index ae54a0b55..a76771ee5 100644 --- a/disk.go +++ b/disk.go @@ -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 { diff --git a/disk_linux.go b/disk_linux.go index 1e33c5fa0..81e48116b 100644 --- a/disk_linux.go +++ b/disk_linux.go @@ -3,6 +3,7 @@ package gopsutil import ( + "strconv" "strings" ) @@ -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], @@ -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 diff --git a/mem_linux.go b/mem_linux.go index 9163a01b0..e2fa8fbec 100644 --- a/mem_linux.go +++ b/mem_linux.go @@ -3,6 +3,7 @@ package gopsutil import ( + "strconv" "strings" "syscall" ) @@ -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 diff --git a/net_linux.go b/net_linux.go index 97903377b..9cc38aeb6 100644 --- a/net_linux.go +++ b/net_linux.go @@ -3,6 +3,7 @@ package gopsutil import ( + "strconv" "strings" ) @@ -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) } diff --git a/process.go b/process.go index 8ad2071c0..aaade8bd3 100644 --- a/process.go +++ b/process.go @@ -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 { diff --git a/process_linux.go b/process_linux.go index 8e60793c1..ad088bf51 100644 --- a/process_linux.go +++ b/process_linux.go @@ -195,7 +195,7 @@ func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { lines := strings.Split(string(contents), "\n") // 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.Path = first_line[len(first_line)-1] @@ -205,30 +205,35 @@ func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { continue } 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] { case "Size": - m.Size = mustParseUint64(v) + m.Size = t case "Rss": - m.Rss = mustParseUint64(v) + m.Rss = t case "Pss": - m.Pss = mustParseUint64(v) + m.Pss = t case "Shared_Clean": - m.SharedClean = mustParseUint64(v) + m.SharedClean = t case "Shared_Dirty": - m.SharedDirty = mustParseUint64(v) + m.SharedDirty = t case "Private_Clean": - m.PrivateClean = mustParseUint64(v) + m.PrivateClean = t case "Private_Dirty": - m.PrivateDirty = mustParseUint64(v) + m.PrivateDirty = t case "Referenced": - m.Referenced = mustParseUint64(v) + m.Referenced = t case "Anonymous": - m.Anonymous = mustParseUint64(v) + m.Anonymous = t case "Swap": - m.Swap = mustParseUint64(v) + m.Swap = t } } - return m + return m, nil } blocks := make([]string, 16) @@ -237,7 +242,11 @@ func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { if strings.HasSuffix(field[0], ":") == false { // new block section 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 blocks = make([]string, 16) @@ -272,9 +281,13 @@ func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) { if err != nil { continue } + t, err := strconv.ParseUint(fd, 10, 64) + if err != nil { + return numFDs, openfiles, err + } o := &OpenFilesStat{ Path: filepath, - Fd: mustParseUint64(fd), + Fd: t, } openfiles = append(openfiles, o) } @@ -338,15 +351,20 @@ func (p *Process) fillFromIO() (*IOCountersStat, error) { if len(field) < 2 { continue } + t, err := strconv.ParseInt(strings.Trim(field[1], " \t"), 10, 32) + if err != nil { + return nil, err + } + switch field[0] { case "rchar": - ret.ReadCount = mustParseInt32(strings.Trim(field[1], " \t")) + ret.ReadCount = int32(t) case "wchar": - ret.WriteCount = mustParseInt32(strings.Trim(field[1], " \t")) + ret.WriteCount = int32(t) case "read_bytes": - ret.ReadBytes = mustParseInt32(strings.Trim(field[1], " \t")) + ret.ReadBytes = int32(t) 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), " ") - vms := mustParseUint64(fields[0]) * PageSize - rss := mustParseUint64(fields[1]) * PageSize + vms, err := strconv.ParseUint(fields[0], 10, 64) + if err != nil { + return nil, nil, err + } + rss, err := strconv.ParseUint(fields[1], 10, 64) + if err != nil { + return nil, nil, err + } memInfo := &MemoryInfoStat{ - RSS: rss, - VMS: vms, + RSS: rss * PageSize, + 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{ - RSS: rss, - VMS: vms, - Shared: mustParseUint64(fields[2]) * PageSize, - Text: mustParseUint64(fields[3]) * PageSize, - Lib: mustParseUint64(fields[4]) * PageSize, - Dirty: mustParseUint64(fields[5]) * PageSize, + RSS: rss * PageSize, + VMS: vms * PageSize, + Shared: shared * PageSize, + Text: text * PageSize, + Lib: lib * PageSize, + Dirty: dirty * PageSize, } return memInfo, memInfoEx, nil @@ -458,12 +500,25 @@ func (p *Process) fillFromStat() (string, int32, *CPUTimesStat, int64, int32, er termmap, err := getTerminalMap() terminal := "" 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]) - utime, _ := strconv.ParseFloat(fields[13], 64) - stime, _ := strconv.ParseFloat(fields[14], 64) + ppid, err := strconv.ParseInt(fields[3], 10, 32) + if err != nil { + 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{ CPU: "cpu", @@ -472,7 +527,12 @@ func (p *Process) fillFromStat() (string, int32, *CPUTimesStat, int64, int32, er } 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) // 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)) 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) { diff --git a/process_posix.go b/process_posix.go index 1514dc725..4e2625a23 100644 --- a/process_posix.go +++ b/process_posix.go @@ -4,8 +4,8 @@ package gopsutil import ( "os" - "os/user" "os/exec" + "os/user" "strconv" "strings" "syscall"