Source File
memory.go
Belonging Package
golang.org/x/pkgsite/internal/worker
package worker
import (
)
func () (systemMemStats, error) {
, := os.Open("/proc/meminfo")
if != nil {
return systemMemStats{},
}
defer .Close()
:= func( *uint64, []string) {
if len() != 3 || [2] != "kB" {
= fmt.Errorf("got %+v, want 3 words, third is 'kB'", )
return
}
var uint64
, = strconv.ParseUint([1], 10, 64)
if == nil {
* = * 1024
}
}
:= bufio.NewScanner()
var systemMemStats
for .Scan() {
:= strings.Fields(.Text())
switch [0] {
case "MemTotal:":
(&.Total, )
case "MemFree:":
(&.Free, )
case "MemAvailable:":
(&.Available, )
case "Buffers:":
(&.Buffers, )
case "Cached:":
(&.Cached, )
}
}
if == nil {
= .Err()
}
if != nil {
return systemMemStats{},
}
.Used = .Total - .Free - .Buffers - .Cached // see `man free`
return , nil
}
type processMemStats struct {
VSize uint64 // virtual memory size
RSS uint64 // resident set size (physical memory in use)
}
func () (processMemStats, error) {
, := os.Open("/proc/self/stat")
if != nil {
return processMemStats{},
}
var (
int
string
byte
, uint64
)
_, = fmt.Fscanf(, "%d %s %c %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
&, &, &, &, &, &, &, &, &, &, &, &, &, &, &, &, &, &, &, &, &, &, &, &)
if != nil {
return processMemStats{},
}
const = 4 * 1024 // Linux page size, from `getconf PAGESIZE`
return processMemStats{
VSize: ,
RSS: * ,
}, nil
}
func () map[string]uint64 {
, := getCgroupMemStatsErr()
if != nil {
log.Warningf(context.Background(), "getCgroupMemStats: %v", )
return nil
:= ["usage"]
:= ["total_inactive_file"]
if > {
= 0
} else {
-=
}
["trueRSS"] = ["rss"] + ["mapped_file"]
return
}
func () (map[string]uint64, error) {
const = "/sys/fs/cgroup/memory"
:= func( string) (uint64, error) {
, := ioutil.ReadFile(filepath.Join(, ))
if != nil {
return 0,
}
, := strconv.ParseUint(strings.TrimSpace(string()), 10, 64)
if != nil {
return 0,
}
return , nil
}
:= map[string]uint64{}
var error
["limit"], = ("memory.limit_in_bytes")
if != nil {
return nil,
}
["usage"], = ("memory.usage_in_bytes")
if != nil {
return nil,
}
, := os.Open(filepath.Join(, "memory.stat"))
if != nil {
return nil,
}
defer .Close()
:= bufio.NewScanner()
for .Scan() {
:= strings.Fields(.Text())
if len() != 2 {
return nil, fmt.Errorf("memory.stat: %q: not two fields", .Text())
}
, := strconv.ParseUint([1], 10, 64)
if != nil {
return nil,
}
[[0]] =
}
if := .Err(); != nil {
return nil,
}
return , nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |