Files
team/server/channels/app/platform/container_limits_linux.go
2381664c84 [MM-67975] Add container CPU and memory limits to support packet diagnostics (#35835)
* MM-67975: Add container CPU and memory limits to support packet diagnostics

Add two new optional fields to SupportPacketDiagnostics.Server:
- container_cpu_limit (float64): effective CPU limit in CPUs (e.g. 0.5, 2.0)
- container_memory_limit_mb (uint64): memory limit in MB

Both fields are populated from cgroups v2 (/sys/fs/cgroup/memory.max and
/sys/fs/cgroup/cpu.max) on Linux. Fields use omitempty so they are absent
from the output on bare metal or when no container limits are configured,
preserving backwards compatibility.

Non-Linux builds compile and produce no output for these fields via a
container_limits_other.go stub, following the existing memory_linux.go /
memory_other.go pattern.

* Fix golangci-lint errors: govet shadow and gofmt formatting

- Fix govet shadow: use var memBytes uint64 + err = instead of memBytes, err :=
  to avoid shadowing the outer err variable (which is reused after this block)
- Fix gofmt: add extra space before // 512 MB comment to align with
  the longer v2CPUMax line in the same assignment block

* fix(platform): ceil memory MB conversion and return zero-values for missing cgroup files

- Use ceiling division for MemoryLimitMB so sub-1MB limits map to 1 instead
  of 0 (which omitempty would silently drop)
- Absorb os.ErrNotExist in getContainerLimits so non-v2/bare-metal hosts
  return ContainerLimits{}, nil as the function comment promises
- Update test: missing cgroup file now asserts zero-values, not an error

* test(platform): add subtest locking ceil-to-MB behavior for sub-MB memory limits

* test(platform): add subtest for missing cpu.max returning zero values

Locks in the os.ErrNotExist branch for cpu.max (lines 57-60 in
container_limits_linux.go), which was previously unreachable via the
existing missing-memory test since that test returns early before
reading cpu.max.

---------

Co-authored-by: Hanzo Dev <[email protected]>
2026-04-23 13:25:34 +00:00

90 lines
2.2 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//go:build linux
package platform
import (
"errors"
"os"
"strconv"
"strings"
)
// cgroupPaths holds the file paths used to read cgroup v2 resource limits.
// These are package-level variables so they can be overridden in tests.
var cgroupPaths = struct {
v2MemoryMax string
v2CPUMax string
}{
v2MemoryMax: "/sys/fs/cgroup/memory.max",
v2CPUMax: "/sys/fs/cgroup/cpu.max",
}
// ContainerLimits holds effective CPU and memory limits for the current container.
// Zero values indicate no limit is set (fields will be omitted from YAML output).
type ContainerLimits struct {
CPULimit float64
MemoryLimitMB uint64
}
// getContainerLimits reads container resource limits from cgroups v2.
// Returns zero values when no limits are detected (bare metal, unlimited, or non-v2 system).
func getContainerLimits() (ContainerLimits, error) {
var limits ContainerLimits
// Memory limit: "max" means unlimited, otherwise bytes
memStr, err := readTrimmed(cgroupPaths.v2MemoryMax)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return limits, nil
}
return limits, err
}
if memStr != "max" {
var memBytes uint64
memBytes, err = strconv.ParseUint(memStr, 10, 64)
if err != nil {
return limits, err
}
limits.MemoryLimitMB = (memBytes + 1024*1024 - 1) / (1024 * 1024)
}
// CPU limit: format is "quota period"; quota is "max" when unlimited
cpuStr, err := readTrimmed(cgroupPaths.v2CPUMax)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return limits, nil
}
return limits, err
}
parts := strings.Fields(cpuStr)
if len(parts) != 2 {
return limits, errors.New("unexpected format in cpu.max")
}
if parts[0] != "max" {
quota, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return limits, err
}
period, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return limits, err
}
if period > 0 {
limits.CPULimit = quota / period
}
}
return limits, nil
}
func readTrimmed(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", err
}
return strings.TrimSpace(string(data)), nil
}