From 4899782e71c018b51faee129360379658687b58b Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Thu, 1 May 2014 12:01:30 +0900 Subject: [PATCH] change various func return type to pointer. --- cpu_linux.go | 8 ++++---- host_linux.go | 10 +++++----- load_linux.go | 12 ++++++------ mem_freebsd.go | 8 ++++---- mem_linux.go | 27 +++++++++++++++------------ mem_windows.go | 19 ++++++++++--------- net.go | 4 ++-- net_linux.go | 2 +- 8 files changed, 47 insertions(+), 43 deletions(-) diff --git a/cpu_linux.go b/cpu_linux.go index b7b2073..3b66d1f 100644 --- a/cpu_linux.go +++ b/cpu_linux.go @@ -18,18 +18,18 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { if err != nil { continue } - ret = append(ret, ct) + ret = append(ret, *ct) } return ret, nil } -func parseStatLine(line string) (CPUTimesStat, error) { +func parseStatLine(line string) (*CPUTimesStat, error) { fields := strings.Fields(line) if strings.HasPrefix(fields[0], "cpu") == false { // return CPUTimesStat{}, e - return CPUTimesStat{}, errors.New("not contain cpu") + return nil, errors.New("not contain cpu") } cpu := fields[0] @@ -44,7 +44,7 @@ func parseStatLine(line string) (CPUTimesStat, error) { irq, _ := strconv.ParseFloat(fields[6], 32) softirq, _ := strconv.ParseFloat(fields[7], 32) stolen, _ := strconv.ParseFloat(fields[8], 32) - ct := CPUTimesStat{ + ct := &CPUTimesStat{ CPU: cpu, User: float32(user), Nice: float32(nice), diff --git a/host_linux.go b/host_linux.go index d3b0e76..c857558 100644 --- a/host_linux.go +++ b/host_linux.go @@ -11,15 +11,15 @@ import ( "unsafe" ) -func HostInfo() (HostInfoStat, error) { - ret := HostInfoStat{} - +func HostInfo() (*HostInfoStat, error) { hostname, err := os.Hostname() - ret.Hostname = hostname if err != nil { - return ret, err + return nil, err } + ret := &HostInfoStat{ + Hostname: hostname, + } return ret, nil } diff --git a/load_linux.go b/load_linux.go index 00eaf70..238ae30 100644 --- a/load_linux.go +++ b/load_linux.go @@ -8,29 +8,29 @@ import ( "strings" ) -func LoadAvg() (LoadAvgStat, error) { +func LoadAvg() (*LoadAvgStat, error) { filename := "/proc/loadavg" line, err := ioutil.ReadFile(filename) if err != nil { - return LoadAvgStat{}, err + return nil, err } values := strings.Fields(string(line)) load1, err := strconv.ParseFloat(values[0], 64) if err != nil { - return LoadAvgStat{}, err + return nil, err } load5, err := strconv.ParseFloat(values[1], 64) if err != nil { - return LoadAvgStat{}, err + return nil, err } load15, err := strconv.ParseFloat(values[2], 64) if err != nil { - return LoadAvgStat{}, err + return nil, err } - ret := LoadAvgStat{ + ret := &LoadAvgStat{ Load1: load1, Load5: load5, Load15: load15, diff --git a/mem_freebsd.go b/mem_freebsd.go index 7535118..8a4ca05 100644 --- a/mem_freebsd.go +++ b/mem_freebsd.go @@ -2,14 +2,14 @@ package gopsutil -func VirtualMemory() (VirtualMemoryStat, error) { - ret := VirtualMemoryStat{} +func VirtualMemory() (*VirtualMemoryStat, error) { + ret := &VirtualMemoryStat{} return ret, nil } -func SwapMemory() (SwapMemoryStat, error) { - ret := SwapMemoryStat{} +func SwapMemory() (*SwapMemoryStat, error) { + ret := &SwapMemoryStat{} return ret, nil } diff --git a/mem_linux.go b/mem_linux.go index 08aa2b0..cfe1d0c 100644 --- a/mem_linux.go +++ b/mem_linux.go @@ -6,17 +6,19 @@ import ( "syscall" ) -func VirtualMemory() (VirtualMemoryStat, error) { - ret := VirtualMemoryStat{} +func VirtualMemory() (*VirtualMemoryStat, error) { sysinfo := &syscall.Sysinfo_t{} if err := syscall.Sysinfo(sysinfo); err != nil { - return ret, err + return nil, err + } + + ret := &VirtualMemoryStat{ + Total: uint64(sysinfo.Totalram), + Free: uint64(sysinfo.Freeram), + Shared: uint64(sysinfo.Sharedram), + Buffers: uint64(sysinfo.Bufferram), } - ret.Total = uint64(sysinfo.Totalram) - ret.Free = uint64(sysinfo.Freeram) - ret.Shared = uint64(sysinfo.Sharedram) - ret.Buffers = uint64(sysinfo.Bufferram) ret.Used = ret.Total - ret.Free @@ -35,15 +37,16 @@ func VirtualMemory() (VirtualMemoryStat, error) { return ret, nil } -func SwapMemory() (SwapMemoryStat, error) { - ret := SwapMemoryStat{} +func SwapMemory() (*SwapMemoryStat, error) { sysinfo := &syscall.Sysinfo_t{} if err := syscall.Sysinfo(sysinfo); err != nil { - return ret, err + return nil, err + } + ret := &SwapMemoryStat{ + Total: sysinfo.Totalswap, + Free: sysinfo.Freeswap, } - ret.Total = sysinfo.Totalswap - ret.Free = sysinfo.Freeswap ret.Used = ret.Total - ret.Free ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0 diff --git a/mem_windows.go b/mem_windows.go index b946fc2..07d16e9 100644 --- a/mem_windows.go +++ b/mem_windows.go @@ -23,25 +23,26 @@ type MEMORYSTATUSEX struct { ullAvailExtendedVirtual uint64 } -func VirtualMemory() (VirtualMemoryStat, error) { - ret := VirtualMemoryStat{} - +func VirtualMemory() (*VirtualMemoryStat, error) { var memInfo MEMORYSTATUSEX memInfo.cbSize = uint32(unsafe.Sizeof(memInfo)) mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo))) if mem == 0 { - return ret, syscall.GetLastError() + return nil, syscall.GetLastError() + } + + ret := &VirtualMemoryStat{ + Total: memInfo.ullTotalPhys, + Available: memInfo.ullAvailPhys, + UsedPercent: float64(memInfo.dwMemoryLoad), } - ret.Total = memInfo.ullTotalPhys - ret.Available = memInfo.ullAvailPhys - ret.UsedPercent = float64(memInfo.dwMemoryLoad) ret.Used = ret.Total - ret.Available return ret, nil } -func SwapMemory() (SwapMemoryStat, error) { - ret := SwapMemoryStat{} +func SwapMemory() (*SwapMemoryStat, error) { + ret := &SwapMemoryStat{} return ret, nil } diff --git a/net.go b/net.go index 373b968..4bca1c9 100644 --- a/net.go +++ b/net.go @@ -1,8 +1,8 @@ package gopsutil type NetIOCountersStat struct { - Name string `json:"name"` // interface name - BytesSent uint64 `json:"bytes_sent"` // number of bytes sent + Name string `json:"name"` // interface name + BytesSent uint64 `json:"bytes_sent"` // number of bytes sent BytesRecv uint64 `json:"bytes_recv"` // number of bytes received PacketsSent uint64 `json:"packets_sent"` // number of packets sent PacketsRecv uint64 `json:"packets_recv"` // number of packets received diff --git a/net_linux.go b/net_linux.go index 871e8e0..20c08a6 100644 --- a/net_linux.go +++ b/net_linux.go @@ -10,7 +10,7 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { filename := "/proc/net/dev" lines, err := readLines(filename) if err != nil { - return make([]NetIOCountersStat, 0), err + return nil, err } statlen := len(lines) - 1