mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-26 13:48:59 +08:00
change various func return type to pointer.
This commit is contained in:
parent
172ac637da
commit
4899782e71
@ -18,18 +18,18 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ret = append(ret, ct)
|
ret = append(ret, *ct)
|
||||||
|
|
||||||
}
|
}
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseStatLine(line string) (CPUTimesStat, error) {
|
func parseStatLine(line string) (*CPUTimesStat, error) {
|
||||||
fields := strings.Fields(line)
|
fields := strings.Fields(line)
|
||||||
|
|
||||||
if strings.HasPrefix(fields[0], "cpu") == false {
|
if strings.HasPrefix(fields[0], "cpu") == false {
|
||||||
// return CPUTimesStat{}, e
|
// return CPUTimesStat{}, e
|
||||||
return CPUTimesStat{}, errors.New("not contain cpu")
|
return nil, errors.New("not contain cpu")
|
||||||
}
|
}
|
||||||
|
|
||||||
cpu := fields[0]
|
cpu := fields[0]
|
||||||
@ -44,7 +44,7 @@ func parseStatLine(line string) (CPUTimesStat, error) {
|
|||||||
irq, _ := strconv.ParseFloat(fields[6], 32)
|
irq, _ := strconv.ParseFloat(fields[6], 32)
|
||||||
softirq, _ := strconv.ParseFloat(fields[7], 32)
|
softirq, _ := strconv.ParseFloat(fields[7], 32)
|
||||||
stolen, _ := strconv.ParseFloat(fields[8], 32)
|
stolen, _ := strconv.ParseFloat(fields[8], 32)
|
||||||
ct := CPUTimesStat{
|
ct := &CPUTimesStat{
|
||||||
CPU: cpu,
|
CPU: cpu,
|
||||||
User: float32(user),
|
User: float32(user),
|
||||||
Nice: float32(nice),
|
Nice: float32(nice),
|
||||||
|
@ -11,15 +11,15 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HostInfo() (HostInfoStat, error) {
|
func HostInfo() (*HostInfoStat, error) {
|
||||||
ret := HostInfoStat{}
|
|
||||||
|
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
ret.Hostname = hostname
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ret, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret := &HostInfoStat{
|
||||||
|
Hostname: hostname,
|
||||||
|
}
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,29 +8,29 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadAvg() (LoadAvgStat, error) {
|
func LoadAvg() (*LoadAvgStat, error) {
|
||||||
filename := "/proc/loadavg"
|
filename := "/proc/loadavg"
|
||||||
line, err := ioutil.ReadFile(filename)
|
line, err := ioutil.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return LoadAvgStat{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
values := strings.Fields(string(line))
|
values := strings.Fields(string(line))
|
||||||
|
|
||||||
load1, err := strconv.ParseFloat(values[0], 64)
|
load1, err := strconv.ParseFloat(values[0], 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return LoadAvgStat{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
load5, err := strconv.ParseFloat(values[1], 64)
|
load5, err := strconv.ParseFloat(values[1], 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return LoadAvgStat{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
load15, err := strconv.ParseFloat(values[2], 64)
|
load15, err := strconv.ParseFloat(values[2], 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return LoadAvgStat{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := LoadAvgStat{
|
ret := &LoadAvgStat{
|
||||||
Load1: load1,
|
Load1: load1,
|
||||||
Load5: load5,
|
Load5: load5,
|
||||||
Load15: load15,
|
Load15: load15,
|
||||||
|
@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
package gopsutil
|
package gopsutil
|
||||||
|
|
||||||
func VirtualMemory() (VirtualMemoryStat, error) {
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||||
ret := VirtualMemoryStat{}
|
ret := &VirtualMemoryStat{}
|
||||||
|
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SwapMemory() (SwapMemoryStat, error) {
|
func SwapMemory() (*SwapMemoryStat, error) {
|
||||||
ret := SwapMemoryStat{}
|
ret := &SwapMemoryStat{}
|
||||||
|
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
27
mem_linux.go
27
mem_linux.go
@ -6,17 +6,19 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func VirtualMemory() (VirtualMemoryStat, error) {
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||||
ret := VirtualMemoryStat{}
|
|
||||||
sysinfo := &syscall.Sysinfo_t{}
|
sysinfo := &syscall.Sysinfo_t{}
|
||||||
|
|
||||||
if err := syscall.Sysinfo(sysinfo); err != nil {
|
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
|
ret.Used = ret.Total - ret.Free
|
||||||
|
|
||||||
@ -35,15 +37,16 @@ func VirtualMemory() (VirtualMemoryStat, error) {
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SwapMemory() (SwapMemoryStat, error) {
|
func SwapMemory() (*SwapMemoryStat, error) {
|
||||||
ret := SwapMemoryStat{}
|
|
||||||
sysinfo := &syscall.Sysinfo_t{}
|
sysinfo := &syscall.Sysinfo_t{}
|
||||||
|
|
||||||
if err := syscall.Sysinfo(sysinfo); err != nil {
|
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.Used = ret.Total - ret.Free
|
||||||
ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0
|
ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0
|
||||||
|
|
||||||
|
@ -23,25 +23,26 @@ type MEMORYSTATUSEX struct {
|
|||||||
ullAvailExtendedVirtual uint64
|
ullAvailExtendedVirtual uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func VirtualMemory() (VirtualMemoryStat, error) {
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||||
ret := VirtualMemoryStat{}
|
|
||||||
|
|
||||||
var memInfo MEMORYSTATUSEX
|
var memInfo MEMORYSTATUSEX
|
||||||
memInfo.cbSize = uint32(unsafe.Sizeof(memInfo))
|
memInfo.cbSize = uint32(unsafe.Sizeof(memInfo))
|
||||||
mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo)))
|
mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo)))
|
||||||
if mem == 0 {
|
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
|
ret.Used = ret.Total - ret.Available
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SwapMemory() (SwapMemoryStat, error) {
|
func SwapMemory() (*SwapMemoryStat, error) {
|
||||||
ret := SwapMemoryStat{}
|
ret := &SwapMemoryStat{}
|
||||||
|
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
4
net.go
4
net.go
@ -1,8 +1,8 @@
|
|||||||
package gopsutil
|
package gopsutil
|
||||||
|
|
||||||
type NetIOCountersStat struct {
|
type NetIOCountersStat struct {
|
||||||
Name string `json:"name"` // interface name
|
Name string `json:"name"` // interface name
|
||||||
BytesSent uint64 `json:"bytes_sent"` // number of bytes sent
|
BytesSent uint64 `json:"bytes_sent"` // number of bytes sent
|
||||||
BytesRecv uint64 `json:"bytes_recv"` // number of bytes received
|
BytesRecv uint64 `json:"bytes_recv"` // number of bytes received
|
||||||
PacketsSent uint64 `json:"packets_sent"` // number of packets sent
|
PacketsSent uint64 `json:"packets_sent"` // number of packets sent
|
||||||
PacketsRecv uint64 `json:"packets_recv"` // number of packets received
|
PacketsRecv uint64 `json:"packets_recv"` // number of packets received
|
||||||
|
@ -10,7 +10,7 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
|
|||||||
filename := "/proc/net/dev"
|
filename := "/proc/net/dev"
|
||||||
lines, err := readLines(filename)
|
lines, err := readLines(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return make([]NetIOCountersStat, 0), err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
statlen := len(lines) - 1
|
statlen := len(lines) - 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user