2014-04-22 11:04:16 +08:00
|
|
|
// +build linux
|
2014-04-18 15:34:47 +08:00
|
|
|
|
2014-04-22 08:44:22 +08:00
|
|
|
package gopsutil
|
2014-04-18 15:34:47 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-04-30 14:32:05 +08:00
|
|
|
func VirtualMemory() (VirtualMemoryStat, error) {
|
2014-04-22 09:02:01 +08:00
|
|
|
ret := Virtual_memoryStat{}
|
2014-04-18 15:34:47 +08:00
|
|
|
sysinfo := &syscall.Sysinfo_t{}
|
|
|
|
|
|
|
|
if err := syscall.Sysinfo(sysinfo); err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
// TODO: platform independent
|
|
|
|
ret.Available = ret.Free + ret.Buffers + ret.Cached
|
|
|
|
|
|
|
|
ret.Used = ret.Total - ret.Free
|
|
|
|
ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) * 100.0
|
|
|
|
|
|
|
|
/*
|
|
|
|
kern := buffers + cached
|
|
|
|
ret.ActualFree = ret.Free + kern
|
|
|
|
ret.ActualUsed = ret.Used - kern
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2014-04-30 14:32:05 +08:00
|
|
|
func SwapMemory() (SwapMemoryStat, error) {
|
2014-04-22 09:02:01 +08:00
|
|
|
ret := Swap_memoryStat{}
|
2014-04-18 15:34:47 +08:00
|
|
|
sysinfo := &syscall.Sysinfo_t{}
|
|
|
|
|
|
|
|
if err := syscall.Sysinfo(sysinfo); err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|