Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/common-go/cgroups/v2/memory.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cgroups_v2
6
7
import (
8
"path/filepath"
9
10
"github.com/gitpod-io/gitpod/common-go/cgroups"
11
)
12
13
type Memory struct {
14
path string
15
}
16
17
func NewMemoryControllerWithMount(mountPoint, path string) *Memory {
18
fullPath := filepath.Join(mountPoint, path)
19
return &Memory{
20
path: fullPath,
21
}
22
}
23
24
func NewMemoryController(path string) *Memory {
25
return &Memory{
26
path: path,
27
}
28
}
29
30
// Current returns the total amount of memory being used by
31
// the cgroup and its descendants in bytes.
32
func (c *Memory) Current() (uint64, error) {
33
path := filepath.Join(c.path, "memory.current")
34
return cgroups.ReadSingleValue(path)
35
}
36
37
// Max returns the memory usage hard limit in bytes. If the cgroup
38
// memory usage reaches this limit and cannot be reduced the
39
// OOM killer will be invoked in the cgroup. If no memory
40
// restriction has been placed on the cgroup, uint64.max
41
// will be returned
42
func (c *Memory) Max() (uint64, error) {
43
path := filepath.Join(c.path, "memory.max")
44
return cgroups.ReadSingleValue(path)
45
}
46
47
// High returns the memory usage throttle limit in bytes. If the cgroup
48
// memory usage reaches this limit the processes in the cgroup
49
// will be put under heavy reclaim pressure.
50
func (c *Memory) High() (uint64, error) {
51
path := filepath.Join(c.path, "memory.high")
52
return cgroups.ReadSingleValue(path)
53
}
54
55
func (m *Memory) Stat() (*cgroups.MemoryStats, error) {
56
path := filepath.Join(m.path, "memory.stat")
57
statMap, err := cgroups.ReadFlatKeyedFile(path)
58
if err != nil {
59
return nil, err
60
}
61
62
return &cgroups.MemoryStats{
63
InactiveFileTotal: statMap["inactive_file"],
64
}, nil
65
}
66
67
func (m *Memory) PSI() (cgroups.PSI, error) {
68
path := filepath.Join(m.path, "memory.pressure")
69
return cgroups.ReadPSIValue(path)
70
}
71
72