diff options
Diffstat (limited to 'vendor/github.com/KimMachineGun/automemlimit')
| -rw-r--r-- | vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go | 16 | ||||
| -rw-r--r-- | vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go | 32 |
2 files changed, 24 insertions, 24 deletions
diff --git a/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go b/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go index 73a57c3..81559e3 100644 --- a/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go +++ b/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go @@ -157,7 +157,7 @@ func getMemoryLimitV1(chs []cgroupHierarchy, mis []mountInfo) (uint64, error) { return 0, err } - // retrieve the memory limit from the memory.stats and memory.limit_in_bytes files. + // retrieve the memory limit from the memory.stat and memory.limit_in_bytes files. return readMemoryLimitV1FromPath(cgroupPath) } @@ -173,7 +173,7 @@ func getCgroupV1NoLimit() uint64 { func readMemoryLimitV1FromPath(cgroupPath string) (uint64, error) { // read hierarchical_memory_limit and memory.limit_in_bytes files. // but if hierarchical_memory_limit is not available, then use the max value as a fallback. - hml, err := readHierarchicalMemoryLimit(filepath.Join(cgroupPath, "memory.stats")) + hml, err := readHierarchicalMemoryLimit(filepath.Join(cgroupPath, "memory.stat")) if err != nil && !errors.Is(err, os.ErrNotExist) { return 0, fmt.Errorf("failed to read hierarchical_memory_limit: %w", err) } else if hml == 0 { @@ -202,8 +202,8 @@ func readMemoryLimitV1FromPath(cgroupPath string) (uint64, error) { return limit, nil } -// readHierarchicalMemoryLimit extracts hierarchical_memory_limit from memory.stats. -// this function expects the path to be memory.stats file. +// readHierarchicalMemoryLimit extracts hierarchical_memory_limit from memory.stat. +// this function expects the path to be memory.stat file. func readHierarchicalMemoryLimit(path string) (uint64, error) { file, err := os.Open(path) if err != nil { @@ -217,12 +217,12 @@ func readHierarchicalMemoryLimit(path string) (uint64, error) { fields := strings.Split(line, " ") if len(fields) < 2 { - return 0, fmt.Errorf("failed to parse memory.stats %q: not enough fields", line) + return 0, fmt.Errorf("failed to parse memory.stat %q: not enough fields", line) } if fields[0] == "hierarchical_memory_limit" { if len(fields) > 2 { - return 0, fmt.Errorf("failed to parse memory.stats %q: too many fields for hierarchical_memory_limit", line) + return 0, fmt.Errorf("failed to parse memory.stat %q: too many fields for hierarchical_memory_limit", line) } return strconv.ParseUint(fields[1], 10, 64) } @@ -276,11 +276,9 @@ func parseMountInfoLine(line string) (mountInfo, error) { fields1 = append(fields1, "") } - fields2 := strings.Split(fieldss[1], " ") + fields2 := strings.SplitN(fieldss[1], " ", 3) if len(fields2) < 3 { return mountInfo{}, fmt.Errorf("not enough fields after separator: %v", fields2) - } else if len(fields2) > 3 { - return mountInfo{}, fmt.Errorf("too many fields after separator: %v", fields2) } return mountInfo{ diff --git a/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go b/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go index cbd53ce..b23980a 100644 --- a/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go +++ b/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go @@ -169,7 +169,7 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { // set the memory limit and start refresh limit, err := updateGoMemLimit(uint64(snapshot), provider, cfg.logger) - go refresh(provider, cfg.logger, cfg.refresh) + refresh(provider, cfg.logger, cfg.refresh) if err != nil { if errors.Is(err, ErrNoLimit) { cfg.logger.Info("memory is not limited, skipping") @@ -200,7 +200,7 @@ func updateGoMemLimit(currLimit uint64, provider Provider, logger *slog.Logger) return newLimit, nil } -// refresh periodically fetches the memory limit from the provider and reapplies it if it has changed. +// refresh spawns a goroutine that runs every refresh duration and updates the GOMEMLIMIT if it has changed. // See more details in the documentation of WithRefreshInterval. func refresh(provider Provider, logger *slog.Logger, refresh time.Duration) { if refresh == 0 { @@ -210,22 +210,24 @@ func refresh(provider Provider, logger *slog.Logger, refresh time.Duration) { provider = noErrNoLimitProvider(provider) t := time.NewTicker(refresh) - for range t.C { - err := func() (_err error) { - snapshot := debug.SetMemoryLimit(-1) - defer rollbackOnPanic(logger, snapshot, &_err) - - _, err := updateGoMemLimit(uint64(snapshot), provider, logger) + go func() { + for range t.C { + err := func() (_err error) { + snapshot := debug.SetMemoryLimit(-1) + defer rollbackOnPanic(logger, snapshot, &_err) + + _, err := updateGoMemLimit(uint64(snapshot), provider, logger) + if err != nil { + return err + } + + return nil + }() if err != nil { - return err + logger.Error("failed to refresh GOMEMLIMIT", slog.Any("error", err)) } - - return nil - }() - if err != nil { - logger.Error("failed to refresh GOMEMLIMIT", slog.Any("error", err)) } - } + }() } // rollbackOnPanic rollbacks to the snapshot on panic. |
