Skip to content

Commit

Permalink
fix darwin mem_swap parse error.
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Sep 20, 2014
1 parent b2dc569 commit f53c682
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions mem_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package gopsutil

import (
"os/exec"
"strconv"
"strings"
)

Expand Down Expand Up @@ -53,21 +54,38 @@ func VirtualMemory() (*VirtualMemoryStat, error) {

// SwapMemory returns swapinfo.
func SwapMemory() (*SwapMemoryStat, error) {
swapUsage, _ := doSysctrl("vm.swapusage")

var ret *SwapMemoryStat

total := strings.Replace(swapUsage[3], "M", "", 1)
used := strings.Replace(swapUsage[6], "M", "", 1)
free := strings.Replace(swapUsage[9], "M", "", 1)
swapUsage, err := doSysctrl("vm.swapusage")
if err != nil {
return ret, err
}

total := strings.Replace(swapUsage[2], "M", "", 1)
used := strings.Replace(swapUsage[5], "M", "", 1)
free := strings.Replace(swapUsage[8], "M", "", 1)

total_v, err := strconv.ParseFloat(total, 64)
if err != nil {
return nil, err
}
used_v, err := strconv.ParseFloat(used, 64)
if err != nil {
return nil, err
}
free_v, err := strconv.ParseFloat(free, 64)
if err != nil {
return nil, err
}

u := "0"
u := ((total_v - free_v) / total_v) * 100.0

// vm.swapusage shows "M", multiply 1000
ret = &SwapMemoryStat{
Total: mustParseUint64(total),
Used: mustParseUint64(used),
Free: mustParseUint64(free),
UsedPercent: mustParseFloat64(u),
Total: uint64(total_v * 1000),
Used: uint64(used_v * 1000),
Free: uint64(free_v * 1000),
UsedPercent: u,
}

return ret, nil
Expand Down

0 comments on commit f53c682

Please sign in to comment.