Skip to content

Commit

Permalink
feat(memlimit): handle default value of cgroup's memory limit
Browse files Browse the repository at this point in the history
  • Loading branch information
KimMachineGun committed Jan 10, 2024
1 parent 3f5343b commit 2b8484a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions memlimit/cgroups_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package memlimit

import (
"math"
"os"
"path/filepath"

"github.com/containerd/cgroups/v3"
Expand Down Expand Up @@ -42,13 +44,18 @@ func FromCgroupV1() (uint64, error) {
return 0, err
}

if limit := metrics.GetMemory().GetHierarchicalMemoryLimit(); limit != 0 {
if limit := metrics.GetMemory().GetHierarchicalMemoryLimit(); limit != 0 && limit != getCgroupV1NoLimit() {
return limit, nil
}

return 0, ErrNoLimit
}

func getCgroupV1NoLimit() uint64 {
ps := uint64(os.Getpagesize())
return math.MaxInt64 / ps * ps
}

// FromCgroupHybrid returns the memory limit from the cgroup v1 or v2.
// It checks the cgroup v2 first, and if it fails, it falls back to cgroup v1.
func FromCgroupHybrid() (uint64, error) {
Expand Down Expand Up @@ -83,7 +90,7 @@ func fromCgroupV2(mountPoint string) (uint64, error) {
return 0, err
}

if limit := stats.GetMemory().GetUsageLimit(); limit != 0 {
if limit := stats.GetMemory().GetUsageLimit(); limit != 0 && limit != math.MaxUint64 {
return limit, nil
}

Expand Down
4 changes: 4 additions & 0 deletions memlimit/memlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) {

limit, err := setGoMemLimit(ApplyRatio(cfg.provider, ratio))
if err != nil {
if errors.Is(err, ErrNoLimit) {
cfg.logger.Printf("memory is not limited, skipping: %v\n", err)
return 0, nil
}
return 0, fmt.Errorf("failed to set GOMEMLIMIT: %w", err)
}

Expand Down

0 comments on commit 2b8484a

Please sign in to comment.