Path: blob/main/components/common-go/cgroups/v2/memory.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cgroups_v256import (7"path/filepath"89"github.com/gitpod-io/gitpod/common-go/cgroups"10)1112type Memory struct {13path string14}1516func NewMemoryControllerWithMount(mountPoint, path string) *Memory {17fullPath := filepath.Join(mountPoint, path)18return &Memory{19path: fullPath,20}21}2223func NewMemoryController(path string) *Memory {24return &Memory{25path: path,26}27}2829// Current returns the total amount of memory being used by30// the cgroup and its descendants in bytes.31func (c *Memory) Current() (uint64, error) {32path := filepath.Join(c.path, "memory.current")33return cgroups.ReadSingleValue(path)34}3536// Max returns the memory usage hard limit in bytes. If the cgroup37// memory usage reaches this limit and cannot be reduced the38// OOM killer will be invoked in the cgroup. If no memory39// restriction has been placed on the cgroup, uint64.max40// will be returned41func (c *Memory) Max() (uint64, error) {42path := filepath.Join(c.path, "memory.max")43return cgroups.ReadSingleValue(path)44}4546// High returns the memory usage throttle limit in bytes. If the cgroup47// memory usage reaches this limit the processes in the cgroup48// will be put under heavy reclaim pressure.49func (c *Memory) High() (uint64, error) {50path := filepath.Join(c.path, "memory.high")51return cgroups.ReadSingleValue(path)52}5354func (m *Memory) Stat() (*cgroups.MemoryStats, error) {55path := filepath.Join(m.path, "memory.stat")56statMap, err := cgroups.ReadFlatKeyedFile(path)57if err != nil {58return nil, err59}6061return &cgroups.MemoryStats{62InactiveFileTotal: statMap["inactive_file"],63}, nil64}6566func (m *Memory) PSI() (cgroups.PSI, error) {67path := filepath.Join(m.path, "memory.pressure")68return cgroups.ReadPSIValue(path)69}707172