| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Keep the original Uber license. |
| 5 |
|
| 6 |
// Copyright (c) 2017 Uber Technologies, Inc. |
| 7 |
// |
| 8 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
// of this software and associated documentation files (the "Software"), to deal |
| 10 |
// in the Software without restriction, including without limitation the rights |
| 11 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
// copies of the Software, and to permit persons to whom the Software is |
| 13 |
// furnished to do so, subject to the following conditions: |
| 14 |
// |
| 15 |
// The above copyright notice and this permission notice shall be included in |
| 16 |
// all copies or substantial portions of the Software. |
| 17 |
// |
| 18 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
// THE SOFTWARE. |
| 25 |
|
| 26 |
//go:build linux |
| 27 |
|
| 28 |
package cgroups // import "go.opentelemetry.io/collector/internal/memorylimiter/cgroups" |
| 29 |
import ( |
| 30 |
"bufio" |
| 31 |
"io" |
| 32 |
"os" |
| 33 |
"path/filepath" |
| 34 |
"strconv" |
| 35 |
"strings" |
| 36 |
) |
| 37 |
|
| 38 |
const ( |
| 39 |
// _cgroupFSType is the Linux CGroup file system type used in |
| 40 |
// `/proc/$PID/mountinfo`. |
| 41 |
_cgroupFSType = "cgroup" |
| 42 |
// _cgroupSubsysCPU is the CPU CGroup subsystem. |
| 43 |
_cgroupSubsysCPU = "cpu" |
| 44 |
// _cgroupSubsysCPUAcct is the CPU accounting CGroup subsystem. |
| 45 |
_cgroupSubsysCPUAcct = "cpuacct" |
| 46 |
// _cgroupSubsysCPUSet is the CPUSet CGroup subsystem. |
| 47 |
_cgroupSubsysCPUSet = "cpuset" |
| 48 |
// _cgroupSubsysMemory is the Memory CGroup subsystem. |
| 49 |
_cgroupSubsysMemory = "memory" |
| 50 |
|
| 51 |
_cgroupMemoryLimitBytes = "memory.limit_in_bytes" |
| 52 |
|
| 53 |
// _cgroupv2MemoryMax is the file name for the CGroup-V2 Memory max |
| 54 |
// parameter. |
| 55 |
_cgroupv2MemoryMax = "memory.max" |
| 56 |
// _cgroupFSType is the Linux CGroup-V2 file system type used in |
| 57 |
// `/proc/$PID/mountinfo`. |
| 58 |
_cgroupv2FSType = "cgroup2" |
| 59 |
) |
| 60 |
|
| 61 |
const ( |
| 62 |
_procPathCGroup = "/proc/self/cgroup" |
| 63 |
_procPathMountInfo = "/proc/self/mountinfo" |
| 64 |
_cgroupv2MountPoint = "/sys/fs/cgroup" |
| 65 |
) |
| 66 |
|
| 67 |
// CGroups is a map that associates each CGroup with its subsystem name. |
| 68 |
type CGroups map[string]*CGroup |
| 69 |
|
| 70 |
// NewCGroups returns a new *CGroups from given `mountinfo` and `cgroup` files |
| 71 |
// under for some process under `/proc` file system (see also proc(5) for more |
| 72 |
// information). |
| 73 |
func NewCGroups(procPathMountInfo, procPathCGroup string) (CGroups, error) { |
| 74 |
cgroupSubsystems, err := parseCGroupSubsystems(procPathCGroup) |
| 75 |
if err != nil { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
|
| 79 |
cgroups := make(CGroups) |
| 80 |
newMountPoint := func(mp *MountPoint) error { |
| 81 |
if mp.FSType != _cgroupFSType { |
| 82 |
return nil |
| 83 |
} |
| 84 |
|
| 85 |
for _, opt := range mp.SuperOptions { |
| 86 |
subsys, exists := cgroupSubsystems[opt] |
| 87 |
if !exists { |
| 88 |
continue |
| 89 |
} |
| 90 |
|
| 91 |
cgroupPath, err := mp.Translate(subsys.Name) |
| 92 |
if err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
if strings.HasPrefix(cgroupPath, "/sys") { |
| 96 |
cgroups[opt] = NewCGroup(cgroupPath) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return nil |
| 101 |
} |
| 102 |
|
| 103 |
if err := parseMountInfo(procPathMountInfo, newMountPoint); err != nil { |
| 104 |
return nil, err |
| 105 |
} |
| 106 |
return cgroups, nil |
| 107 |
} |
| 108 |
|
| 109 |
// NewCGroupsForCurrentProcess returns a new *CGroups instance for the current |
| 110 |
// process. |
| 111 |
func NewCGroupsForCurrentProcess() (CGroups, error) { |
| 112 |
return NewCGroups(_procPathMountInfo, _procPathCGroup) |
| 113 |
} |
| 114 |
|
| 115 |
// MemoryQuota returns the total memory limit of the process |
| 116 |
// It is a result of `memory.limit_in_bytes`. If the value of |
| 117 |
// `memory.limit_in_bytes` was not set (-1) or (9223372036854771712), the method returns `(-1, false, nil)`. |
| 118 |
func (cg CGroups) MemoryQuota() (int64, bool, error) { |
| 119 |
memCGroup, exists := cg[_cgroupSubsysMemory] |
| 120 |
if !exists { |
| 121 |
return -1, false, nil |
| 122 |
} |
| 123 |
|
| 124 |
memLimitBytes, err := memCGroup.readInt(_cgroupMemoryLimitBytes) |
| 125 |
if defined := memLimitBytes > 0; err != nil || !defined { |
| 126 |
return -1, defined, err |
| 127 |
} |
| 128 |
return memLimitBytes, true, nil |
| 129 |
} |
| 130 |
|
| 131 |
// IsCGroupV2 returns true if the system supports and uses cgroup2. |
| 132 |
// It gets the required information for deciding from mountinfo file. |
| 133 |
func IsCGroupV2() (bool, error) { |
| 134 |
return isCGroupV2(_procPathMountInfo) |
| 135 |
} |
| 136 |
|
| 137 |
func isCGroupV2(procPathMountInfo string) (bool, error) { |
| 138 |
isV2 := false |
| 139 |
newMountPoint := func(mp *MountPoint) error { |
| 140 |
if mp.FSType == _cgroupv2FSType && mp.MountPoint == _cgroupv2MountPoint { |
| 141 |
isV2 = true |
| 142 |
} |
| 143 |
return nil |
| 144 |
} |
| 145 |
if err := parseMountInfo(procPathMountInfo, newMountPoint); err != nil { |
| 146 |
return false, err |
| 147 |
} |
| 148 |
return isV2, nil |
| 149 |
} |
| 150 |
|
| 151 |
// MemoryQuotaV2 returns the total memory limit of the process |
| 152 |
// It is a result of cgroupv2 `memory.max`. If the value of |
| 153 |
// `memory.max` was not set (max), the method returns `(-1, false, nil)`. |
| 154 |
func MemoryQuotaV2() (int64, bool, error) { |
| 155 |
return memoryQuotaV2(_cgroupv2MountPoint, _cgroupv2MemoryMax) |
| 156 |
} |
| 157 |
|
| 158 |
func memoryQuotaV2(cgroupv2MountPoint, cgroupv2MemoryMax string) (int64, bool, error) { |
| 159 |
memoryMaxParams, err := os.Open(filepath.Clean(filepath.Join(cgroupv2MountPoint, cgroupv2MemoryMax))) |
| 160 |
if err != nil { |
| 161 |
if os.IsNotExist(err) { |
| 162 |
return -1, false, nil |
| 163 |
} |
| 164 |
return -1, false, err |
| 165 |
} |
| 166 |
scanner := bufio.NewScanner(memoryMaxParams) |
| 167 |
if scanner.Scan() { |
| 168 |
value := strings.TrimSpace(scanner.Text()) |
| 169 |
if value == "max" { |
| 170 |
return -1, false, nil |
| 171 |
} |
| 172 |
maxVal, err := strconv.ParseInt(value, 10, 64) |
| 173 |
if err != nil { |
| 174 |
return -1, false, err |
| 175 |
} |
| 176 |
return maxVal, true, nil |
| 177 |
} |
| 178 |
if err := scanner.Err(); err != nil { |
| 179 |
return -1, false, err |
| 180 |
} |
| 181 |
return -1, false, io.ErrUnexpectedEOF |
| 182 |
} |