From 167b8b8eb88ddeffc42d9c2c76d870df373e3bed Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Fri, 18 Sep 2015 11:03:04 -0700 Subject: [PATCH] Godep update gopsutil to get darwin mem fix --- Godeps/Godeps.json | 28 +++--- .../shirou/gopsutil/common/common_darwin.go | 4 + .../shirou/gopsutil/common/common_linux.go | 6 ++ .../shirou/gopsutil/mem/mem_darwin.go | 88 ++++++++++++++----- .../shirou/gopsutil/mem/mem_darwin_test.go | 67 ++++++++++++++ .../shirou/gopsutil/mem/mem_freebsd.go | 6 +- .../shirou/gopsutil/net/net_darwin.go | 9 +- .../shirou/gopsutil/net/net_linux.go | 8 +- 8 files changed, 173 insertions(+), 43 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_darwin_test.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 3c8e342c9..c436e3513 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -143,38 +143,38 @@ }, { "ImportPath": "github.com/shirou/gopsutil/common", - "Comment": "1.0.0-148-ga369a88", - "Rev": "a369a8857c47ba9bcf8bbcf316897fa123d73639" + "Comment": "1.0.0-153-gc1313e7", + "Rev": "c1313e76341b18456212c5645d1daa7f132ac50e" }, { "ImportPath": "github.com/shirou/gopsutil/cpu", - "Comment": "1.0.0-148-ga369a88", - "Rev": "a369a8857c47ba9bcf8bbcf316897fa123d73639" + "Comment": "1.0.0-153-gc1313e7", + "Rev": "c1313e76341b18456212c5645d1daa7f132ac50e" }, { "ImportPath": "github.com/shirou/gopsutil/disk", - "Comment": "1.0.0-148-ga369a88", - "Rev": "a369a8857c47ba9bcf8bbcf316897fa123d73639" + "Comment": "1.0.0-153-gc1313e7", + "Rev": "c1313e76341b18456212c5645d1daa7f132ac50e" }, { "ImportPath": "github.com/shirou/gopsutil/docker", - "Comment": "1.0.0-148-ga369a88", - "Rev": "a369a8857c47ba9bcf8bbcf316897fa123d73639" + "Comment": "1.0.0-153-gc1313e7", + "Rev": "c1313e76341b18456212c5645d1daa7f132ac50e" }, { "ImportPath": "github.com/shirou/gopsutil/load", - "Comment": "1.0.0-148-ga369a88", - "Rev": "a369a8857c47ba9bcf8bbcf316897fa123d73639" + "Comment": "1.0.0-153-gc1313e7", + "Rev": "c1313e76341b18456212c5645d1daa7f132ac50e" }, { "ImportPath": "github.com/shirou/gopsutil/mem", - "Comment": "1.0.0-148-ga369a88", - "Rev": "a369a8857c47ba9bcf8bbcf316897fa123d73639" + "Comment": "1.0.0-153-gc1313e7", + "Rev": "c1313e76341b18456212c5645d1daa7f132ac50e" }, { "ImportPath": "github.com/shirou/gopsutil/net", - "Comment": "1.0.0-148-ga369a88", - "Rev": "a369a8857c47ba9bcf8bbcf316897fa123d73639" + "Comment": "1.0.0-153-gc1313e7", + "Rev": "c1313e76341b18456212c5645d1daa7f132ac50e" }, { "ImportPath": "github.com/streadway/amqp", diff --git a/Godeps/_workspace/src/github.com/shirou/gopsutil/common/common_darwin.go b/Godeps/_workspace/src/github.com/shirou/gopsutil/common/common_darwin.go index cc746018c..6cdefb4f8 100644 --- a/Godeps/_workspace/src/github.com/shirou/gopsutil/common/common_darwin.go +++ b/Godeps/_workspace/src/github.com/shirou/gopsutil/common/common_darwin.go @@ -74,6 +74,10 @@ func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) { } out, err := invoke.Command(lsof, cmd...) if err != nil { + // if not pid found, lsof returnes code 1 + if err.Error() == "exit status 1" && len(out) == 0 { + return []string{}, nil + } return []string{}, err } lines := strings.Split(string(out), "\n") diff --git a/Godeps/_workspace/src/github.com/shirou/gopsutil/common/common_linux.go b/Godeps/_workspace/src/github.com/shirou/gopsutil/common/common_linux.go index 3c760e5c8..bfd96a935 100644 --- a/Godeps/_workspace/src/github.com/shirou/gopsutil/common/common_linux.go +++ b/Godeps/_workspace/src/github.com/shirou/gopsutil/common/common_linux.go @@ -8,6 +8,8 @@ import ( "strings" ) +// CallLsof invokes lsof to get connection informations. +// This is same as darwin currently. func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) { var cmd []string if pid == 0 { // will get from all processes. @@ -22,6 +24,10 @@ func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) { } out, err := invoke.Command(lsof, cmd...) if err != nil { + // if not pid found, lsof returnes code 1 + if err.Error() == "exit status 1" && len(out) == 0 { + return []string{}, nil + } return []string{}, err } lines := strings.Split(string(out), "\n") diff --git a/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_darwin.go b/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_darwin.go index bd6d2e0d8..921d87096 100644 --- a/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_darwin.go +++ b/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_darwin.go @@ -20,45 +20,89 @@ func getPageSize() (uint64, error) { if err != nil { return 0, err } - return p, nil } +// Runs vm_stat and returns Free and inactive pages +func getVmStat(pagesize uint64, vms *VirtualMemoryStat) error { + out, err := exec.Command("vm_stat").Output() + if err != nil { + return err + } + return parseVmStat(string(out), pagesize, vms) +} + +func parseVmStat(out string, pagesize uint64, vms *VirtualMemoryStat) error { + var err error + + lines := strings.Split(out, "\n") + for _, line := range lines { + fields := strings.Split(line, ":") + if len(fields) < 2 { + continue + } + key := strings.TrimSpace(fields[0]) + value := strings.Trim(fields[1], " .") + switch key { + case "Pages free": + free, e := strconv.ParseUint(value, 10, 64) + if e != nil { + err = e + } + vms.Free = free * pagesize + case "Pages inactive": + inactive, e := strconv.ParseUint(value, 10, 64) + if e != nil { + err = e + } + vms.Cached += inactive * pagesize + vms.Inactive = inactive * pagesize + case "Pages active": + active, e := strconv.ParseUint(value, 10, 64) + if e != nil { + err = e + } + vms.Active = active * pagesize + case "Pages wired down": + wired, e := strconv.ParseUint(value, 10, 64) + if e != nil { + err = e + } + vms.Wired = wired * pagesize + case "Pages purgeable": + purgeable, e := strconv.ParseUint(value, 10, 64) + if e != nil { + err = e + } + vms.Cached += purgeable * pagesize + } + } + return err +} + // VirtualMemory returns VirtualmemoryStat. func VirtualMemory() (*VirtualMemoryStat, error) { + ret := &VirtualMemoryStat{} + p, err := getPageSize() if err != nil { return nil, err } - - total, err := common.DoSysctrl("hw.memsize") + t, err := common.DoSysctrl("hw.memsize") if err != nil { return nil, err } - free, err := common.DoSysctrl("vm.page_free_count") + total, err := strconv.ParseUint(t[0], 10, 64) if err != nil { return nil, err } - parsed := make([]uint64, 0, 7) - vv := []string{ - total[0], - free[0], - } - for _, target := range vv { - t, err := strconv.ParseUint(target, 10, 64) - if err != nil { - return nil, err - } - parsed = append(parsed, t) + err = getVmStat(p, ret) + if err != nil { + return nil, err } - ret := &VirtualMemoryStat{ - Total: parsed[0], - Free: parsed[1] * p, - } - - // TODO: platform independent (worked freebsd?) - ret.Available = ret.Free + ret.Buffers + ret.Cached + ret.Available = ret.Free + ret.Cached + ret.Total = total ret.Used = ret.Total - ret.Free ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) * 100.0 diff --git a/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_darwin_test.go b/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_darwin_test.go new file mode 100644 index 000000000..c52e7d43e --- /dev/null +++ b/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_darwin_test.go @@ -0,0 +1,67 @@ +// +build darwin + +package mem + +import ( + "testing" +) + +var vm_stat_out = ` +Mach Virtual Memory Statistics: (page size of 4096 bytes) +Pages free: 105885. +Pages active: 725641. +Pages inactive: 449242. +Pages speculative: 6155. +Pages throttled: 0. +Pages wired down: 560835. +Pages purgeable: 128967. +"Translation faults": 622528839. +Pages copy-on-write: 17697839. +Pages zero filled: 311034413. +Pages reactivated: 4705104. +Pages purged: 5605610. +File-backed pages: 349192. +Anonymous pages: 831846. +Pages stored in compressor: 876507. +Pages occupied by compressor: 249167. +Decompressions: 4555025. +Compressions: 7524729. +Pageins: 40532443. +Pageouts: 126496. +Swapins: 2988073. +Swapouts: 3283599. +` + +func TestParseVmStat(t *testing.T) { + ret := &VirtualMemoryStat{} + err := parseVmStat(vm_stat_out, 4096, ret) + + if err != nil { + t.Errorf("Expected no error, got %s\n", err.Error()) + } + + if ret.Free != uint64(105885*4096) { + t.Errorf("Free pages, actual: %d, expected: %d", ret.Free, + 105885*4096) + } + + if ret.Inactive != uint64(449242*4096) { + t.Errorf("Inactive pages, actual: %d, expected: %d", ret.Inactive, + 449242*4096) + } + + if ret.Active != uint64(725641*4096) { + t.Errorf("Active pages, actual: %d, expected: %d", ret.Active, + 725641*4096) + } + + if ret.Wired != uint64(560835*4096) { + t.Errorf("Wired pages, actual: %d, expected: %d", ret.Wired, + 560835*4096) + } + + if ret.Cached != uint64(128967*4096+449242.*4096) { + t.Errorf("Cached pages, actual: %d, expected: %d", ret.Cached, + 128967*4096+449242.*4096) + } +} diff --git a/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_freebsd.go b/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_freebsd.go index a380519f9..2791fca82 100644 --- a/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_freebsd.go +++ b/Godeps/_workspace/src/github.com/shirou/gopsutil/mem/mem_freebsd.go @@ -77,10 +77,8 @@ func VirtualMemory() (*VirtualMemoryStat, error) { Wired: parsed[6] * p, } - // TODO: platform independent (worked freebsd?) - ret.Available = ret.Free + ret.Buffers + ret.Cached - - ret.Used = ret.Total - ret.Free + ret.Available = ret.Inactive + ret.Cached + ret.Free + ret.Used = ret.Active + ret.Wired + ret.Cached ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) * 100.0 return ret, nil diff --git a/Godeps/_workspace/src/github.com/shirou/gopsutil/net/net_darwin.go b/Godeps/_workspace/src/github.com/shirou/gopsutil/net/net_darwin.go index da722dc6e..bcf2915fa 100644 --- a/Godeps/_workspace/src/github.com/shirou/gopsutil/net/net_darwin.go +++ b/Godeps/_workspace/src/github.com/shirou/gopsutil/net/net_darwin.go @@ -91,8 +91,13 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { return ret, nil } -// Return a list of network connections opened by a process +// Return a list of network connections opened. func NetConnections(kind string) ([]NetConnectionStat, error) { + return NetConnectionsPid(kind, 0) +} + +// Return a list of network connections opened by a process. +func NetConnectionsPid(kind string, pid int32) ([]NetConnectionStat, error) { var ret []NetConnectionStat args := []string{"-i"} @@ -126,7 +131,7 @@ func NetConnections(kind string) ([]NetConnectionStat, error) { } // we can not use -F filter to get all of required information at once. - r, err := common.CallLsof(invoke, 0, args...) + r, err := common.CallLsof(invoke, pid, args...) if err != nil { return nil, err } diff --git a/Godeps/_workspace/src/github.com/shirou/gopsutil/net/net_linux.go b/Godeps/_workspace/src/github.com/shirou/gopsutil/net/net_linux.go index de0d65a46..6e542449f 100644 --- a/Godeps/_workspace/src/github.com/shirou/gopsutil/net/net_linux.go +++ b/Godeps/_workspace/src/github.com/shirou/gopsutil/net/net_linux.go @@ -90,7 +90,13 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { return ret, nil } +// Return a list of network connections opened. func NetConnections(kind string) ([]NetConnectionStat, error) { + return NetConnectionsPid(kind, 0) +} + +// Return a list of network connections opened by a process. +func NetConnectionsPid(kind string, pid int32) ([]NetConnectionStat, error) { var ret []NetConnectionStat args := []string{"-i"} @@ -124,7 +130,7 @@ func NetConnections(kind string) ([]NetConnectionStat, error) { } // we can not use -F filter to get all of required information at once. - r, err := common.CallLsof(invoke, 0, args...) + r, err := common.CallLsof(invoke, pid, args...) if err != nil { return nil, err }