Skip to content

Commit

Permalink
mem_linux: VirtualMemory() is now from /proc/meminfo, not syscall.
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Jul 3, 2014
1 parent 8791276 commit c78563d
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions mem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,44 @@
package gopsutil

import (
"strings"
"syscall"
)

func VirtualMemory() (*VirtualMemoryStat, error) {
sysinfo := &syscall.Sysinfo_t{}

if err := syscall.Sysinfo(sysinfo); err != nil {
return nil, err
filename := "/proc/meminfo"
lines, _ := readLines(filename)

ret := &VirtualMemoryStat{}
for _, line := range lines {
fields := strings.Split(line, ":")
if len(fields) != 2 {
continue
}
key := strings.TrimSpace(fields[0])
value := strings.TrimSpace(fields[1])
value = strings.Replace(value, " kB", "", -1)

switch key {
case "MemTotal":
ret.Total = mustParseUint64(value)
case "MemFree":
ret.Free = mustParseUint64(value)
case "Buffers":
ret.Buffers = mustParseUint64(value)
case "Cached":
ret.Cached = mustParseUint64(value)
case "Active":
ret.Active = mustParseUint64(value)
case "Inactive":
ret.Inactive = mustParseUint64(value)

}
}

ret := &VirtualMemoryStat{
Total: uint64(sysinfo.Totalram),
Free: uint64(sysinfo.Freeram),
Shared: uint64(sysinfo.Sharedram),
Buffers: uint64(sysinfo.Bufferram),
}

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

Expand Down

0 comments on commit c78563d

Please sign in to comment.