change various func return type to pointer.

This commit is contained in:
Shirou WAKAYAMA 2014-05-01 12:01:30 +09:00
parent 172ac637da
commit 4899782e71
8 changed files with 47 additions and 43 deletions

View File

@ -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),

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -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

View File

@ -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
}

4
net.go
View File

@ -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

View File

@ -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