|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +//go:build integration && linux |
| 4 | +// +build integration,linux |
| 5 | + |
| 6 | +// Privileged access is required to set cgroup's memory and cpu max values |
| 7 | + |
| 8 | +package cgroupruntimeextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/cgroupruntimeextension" |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "fmt" |
| 13 | + "math" |
| 14 | + "os" |
| 15 | + "path" |
| 16 | + "path/filepath" |
| 17 | + "runtime" |
| 18 | + "runtime/debug" |
| 19 | + "strconv" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/containerd/cgroups/v3/cgroup2" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + "go.opentelemetry.io/collector/component/componenttest" |
| 27 | + "go.opentelemetry.io/collector/extension/extensiontest" |
| 28 | + "golang.org/x/sys/unix" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + defaultCgroup2Path = "/sys/fs/cgroup" |
| 33 | +) |
| 34 | + |
| 35 | +// checkCgroupSystem skips the test if is not run in a cgroupv2 system |
| 36 | +func checkCgroupSystem(tb testing.TB) { |
| 37 | + var st unix.Statfs_t |
| 38 | + err := unix.Statfs(defaultCgroup2Path, &st) |
| 39 | + if err != nil { |
| 40 | + tb.Skip("cannot statfs cgroup root") |
| 41 | + } |
| 42 | + |
| 43 | + isUnified := st.Type == unix.CGROUP2_SUPER_MAGIC |
| 44 | + if !isUnified { |
| 45 | + tb.Skip("System running in hybrid or cgroupv1 mode") |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// cgroupMaxCpu returns the CPU max definition for a given cgroup slice path |
| 50 | +// File format: cpu_quote cpu_period |
| 51 | +func cgroupMaxCpu(filename string) (quota int64, period uint64, err error) { |
| 52 | + out, err := os.ReadFile(filepath.Join(defaultCgroup2Path, filename, "cpu.max")) |
| 53 | + if err != nil { |
| 54 | + return 0, 0, err |
| 55 | + } |
| 56 | + values := strings.Split(strings.TrimSpace(string(out)), " ") |
| 57 | + if values[0] == "max" { |
| 58 | + quota = math.MaxInt64 |
| 59 | + } else { |
| 60 | + quota, _ = strconv.ParseInt(values[0], 10, 64) |
| 61 | + } |
| 62 | + period, _ = strconv.ParseUint(values[1], 10, 64) |
| 63 | + return quota, period, err |
| 64 | +} |
| 65 | + |
| 66 | +func TestCgroupV2SudoIntegration(t *testing.T) { |
| 67 | + checkCgroupSystem(t) |
| 68 | + pointerInt64 := func(val int64) *int64 { |
| 69 | + return &val |
| 70 | + } |
| 71 | + pointerUint64 := func(uval uint64) *uint64 { |
| 72 | + return &uval |
| 73 | + } |
| 74 | + |
| 75 | + tests := []struct { |
| 76 | + name string |
| 77 | + // nil CPU quota == "max" cgroup string value |
| 78 | + cgroupCpuQuota *int64 |
| 79 | + cgroupCpuPeriod uint64 |
| 80 | + cgroupMaxMemory int64 |
| 81 | + config *Config |
| 82 | + expectedGoMaxProcs int |
| 83 | + expectedGoMemLimit int64 |
| 84 | + }{ |
| 85 | + { |
| 86 | + name: "90% the max cgroup memory and 12 GOMAXPROCS", |
| 87 | + cgroupCpuQuota: pointerInt64(100000), |
| 88 | + cgroupCpuPeriod: 8000, |
| 89 | + // 128 Mb |
| 90 | + cgroupMaxMemory: 134217728, |
| 91 | + config: &Config{ |
| 92 | + GoMaxProcs: GoMaxProcsConfig{ |
| 93 | + Enabled: true, |
| 94 | + }, |
| 95 | + GoMemLimit: GoMemLimitConfig{ |
| 96 | + Enabled: true, |
| 97 | + Ratio: 0.9, |
| 98 | + }, |
| 99 | + }, |
| 100 | + // 100000 / 8000 |
| 101 | + expectedGoMaxProcs: 12, |
| 102 | + // 134217728 * 0.9 |
| 103 | + expectedGoMemLimit: 120795955, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "50% of the max cgroup memory and 1 GOMAXPROCS", |
| 107 | + cgroupCpuQuota: pointerInt64(100000), |
| 108 | + cgroupCpuPeriod: 100000, |
| 109 | + // 128 Mb |
| 110 | + cgroupMaxMemory: 134217728, |
| 111 | + config: &Config{ |
| 112 | + GoMaxProcs: GoMaxProcsConfig{ |
| 113 | + Enabled: true, |
| 114 | + }, |
| 115 | + GoMemLimit: GoMemLimitConfig{ |
| 116 | + Enabled: true, |
| 117 | + Ratio: 0.5, |
| 118 | + }, |
| 119 | + }, |
| 120 | + // 100000 / 100000 |
| 121 | + expectedGoMaxProcs: 1, |
| 122 | + // 134217728 * 0.5 |
| 123 | + expectedGoMemLimit: 67108864, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "10% of the max cgroup memory, max cpu, default GOMAXPROCS", |
| 127 | + cgroupCpuQuota: nil, |
| 128 | + cgroupCpuPeriod: 100000, |
| 129 | + // 128 Mb |
| 130 | + cgroupMaxMemory: 134217728, |
| 131 | + config: &Config{ |
| 132 | + GoMaxProcs: GoMaxProcsConfig{ |
| 133 | + Enabled: true, |
| 134 | + }, |
| 135 | + GoMemLimit: GoMemLimitConfig{ |
| 136 | + Enabled: true, |
| 137 | + Ratio: 0.1, |
| 138 | + }, |
| 139 | + }, |
| 140 | + // GOMAXPROCS is set to the value of `cpu.max / cpu.period` |
| 141 | + // If cpu.max is set to max, GOMAXPROCS should not be |
| 142 | + // modified |
| 143 | + expectedGoMaxProcs: runtime.GOMAXPROCS(-1), |
| 144 | + // 134217728 * 0.1 |
| 145 | + expectedGoMemLimit: 13421772, |
| 146 | + }, |
| 147 | + } |
| 148 | + |
| 149 | + cgroupPath, err := cgroup2.PidGroupPath(os.Getpid()) |
| 150 | + assert.NoError(t, err) |
| 151 | + manager, err := cgroup2.Load(cgroupPath) |
| 152 | + assert.NoError(t, err) |
| 153 | + |
| 154 | + stats, err := manager.Stat() |
| 155 | + require.NoError(t, err) |
| 156 | + |
| 157 | + // Startup resource values |
| 158 | + initialMaxMemory := stats.GetMemory().GetUsageLimit() |
| 159 | + memoryCgroupCleanUp := func() { |
| 160 | + err = manager.Update(&cgroup2.Resources{ |
| 161 | + Memory: &cgroup2.Memory{ |
| 162 | + Max: pointerInt64(int64(initialMaxMemory)), |
| 163 | + }, |
| 164 | + }) |
| 165 | + assert.NoError(t, err) |
| 166 | + } |
| 167 | + |
| 168 | + if initialMaxMemory == math.MaxUint64 { |
| 169 | + // fallback solution to set cgroup's max memory to "max" |
| 170 | + memoryCgroupCleanUp = func() { |
| 171 | + err = os.WriteFile(path.Join(defaultCgroup2Path, cgroupPath, "memory.max"), []byte("max"), 0o600) |
| 172 | + assert.NoError(t, err) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + initialCpuQuota, initialCpuPeriod, err := cgroupMaxCpu(cgroupPath) |
| 177 | + require.NoError(t, err) |
| 178 | + cpuCgroupCleanUp := func() { |
| 179 | + fmt.Println(initialCpuQuota) |
| 180 | + err = manager.Update(&cgroup2.Resources{ |
| 181 | + CPU: &cgroup2.CPU{ |
| 182 | + Max: cgroup2.NewCPUMax(pointerInt64(initialCpuQuota), pointerUint64(initialCpuPeriod)), |
| 183 | + }, |
| 184 | + }) |
| 185 | + assert.NoError(t, err) |
| 186 | + } |
| 187 | + |
| 188 | + if initialCpuQuota == math.MaxInt64 { |
| 189 | + // fallback solution to set cgroup's max cpu to "max" |
| 190 | + cpuCgroupCleanUp = func() { |
| 191 | + err = os.WriteFile(path.Join(defaultCgroup2Path, cgroupPath, "cpu.max"), []byte("max"), 0o600) |
| 192 | + assert.NoError(t, err) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + initialGoMem := debug.SetMemoryLimit(-1) |
| 197 | + initialGoProcs := runtime.GOMAXPROCS(-1) |
| 198 | + |
| 199 | + for _, test := range tests { |
| 200 | + t.Run(test.name, func(t *testing.T) { |
| 201 | + // restore startup cgroup initial resource values |
| 202 | + t.Cleanup(func() { |
| 203 | + debug.SetMemoryLimit(initialGoMem) |
| 204 | + runtime.GOMAXPROCS(initialGoProcs) |
| 205 | + memoryCgroupCleanUp() |
| 206 | + cpuCgroupCleanUp() |
| 207 | + }) |
| 208 | + |
| 209 | + err = manager.Update(&cgroup2.Resources{ |
| 210 | + Memory: &cgroup2.Memory{ |
| 211 | + // Default max memory must be |
| 212 | + // overwritten |
| 213 | + // to automemlimit change the GOMEMLIMIT |
| 214 | + // value |
| 215 | + Max: pointerInt64(test.cgroupMaxMemory), |
| 216 | + }, |
| 217 | + CPU: &cgroup2.CPU{ |
| 218 | + Max: cgroup2.NewCPUMax(test.cgroupCpuQuota, pointerUint64(test.cgroupCpuPeriod)), |
| 219 | + }, |
| 220 | + }) |
| 221 | + require.NoError(t, err) |
| 222 | + |
| 223 | + factory := NewFactory() |
| 224 | + ctx := context.Background() |
| 225 | + extension, err := factory.Create(ctx, extensiontest.NewNopSettings(), test.config) |
| 226 | + require.NoError(t, err) |
| 227 | + |
| 228 | + err = extension.Start(ctx, componenttest.NewNopHost()) |
| 229 | + require.NoError(t, err) |
| 230 | + |
| 231 | + assert.Equal(t, test.expectedGoMaxProcs, runtime.GOMAXPROCS(-1)) |
| 232 | + assert.Equal(t, test.expectedGoMemLimit, debug.SetMemoryLimit(-1)) |
| 233 | + }) |
| 234 | + } |
| 235 | +} |
0 commit comments