Documentation
¶
Index ¶
- Variables
- func FromCgroup() (uint64, error)
- func FromCgroupHybrid() (uint64, error)
- func FromCgroupV1() (uint64, error)
- func FromCgroupV2() (uint64, error)
- func FromSystem() (uint64, error)
- func Limit(limit uint64) func() (uint64, error)
- func SetGoMemLimit(ratio float64) (int64, error)
- func SetGoMemLimitWithEnv()
- func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error)
- func SetGoMemLimitWithProvider(provider Provider, ratio float64) (int64, error)
- type Experiments
- type Option
- type Provider
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoCgroup is returned when the process is not in cgroup. ErrNoCgroup = errors.New("process is not in cgroup") // ErrCgroupsNotSupported is returned when the system does not support cgroups. ErrCgroupsNotSupported = errors.New("cgroups is not supported on this system") )
var ErrNoLimit = errors.New("memory is not limited")
ErrNoLimit is returned when the memory limit is not set.
Functions ¶
func FromCgroup ¶
FromCgroup retrieves the memory limit from the cgroup.
func FromCgroupHybrid ¶ added in v0.4.0
FromCgroupHybrid retrieves the memory limit from the cgroup v2 and v1 controller sequentially, basically, it is equivalent to FromCgroup. After v1.0.0, this function could be removed and FromCgroup should be used instead.
func FromCgroupV1 ¶
FromCgroupV1 retrieves the memory limit from the cgroup v1 controller. After v1.0.0, this function could be removed and FromCgroup should be used instead.
func FromCgroupV2 ¶
FromCgroupV2 retrieves the memory limit from the cgroup v2 controller. After v1.0.0, this function could be removed and FromCgroup should be used instead.
func FromSystem ¶ added in v0.5.0
FromSystem returns the total memory of the system.
func SetGoMemLimit ¶
SetGoMemLimit sets GOMEMLIMIT with the value from the cgroup's memory limit and given ratio.
func SetGoMemLimitWithEnv ¶ added in v0.2.0
func SetGoMemLimitWithEnv()
SetGoMemLimitWithEnv sets GOMEMLIMIT with the value from the environment variables. Since WithEnv is deprecated, this function is equivalent to SetGoMemLimitWithOpts(). Deprecated: use SetGoMemLimitWithOpts instead.
func SetGoMemLimitWithOpts ¶ added in v0.3.0
SetGoMemLimitWithOpts sets GOMEMLIMIT with options and environment variables.
You can configure how much memory of the cgroup's memory limit to set as GOMEMLIMIT through AUTOMEMLIMIT environment variable in the half-open range (0.0,1.0].
If AUTOMEMLIMIT is not set, it defaults to 0.9. (10% is the headroom for memory sources the Go runtime is unaware of.) If GOMEMLIMIT is already set or AUTOMEMLIMIT=off, this function does nothing.
If AUTOMEMLIMIT_EXPERIMENT is set, it enables experimental features. Please see the documentation of Experiments for more details.
Options:
- WithRatio
- WithProvider
- WithLogger
Types ¶
type Experiments ¶ added in v0.5.0
type Experiments struct { // System enables fallback to system memory limit. System bool }
Experiments is a set of experiment flags. It is used to enable experimental features.
You can set the flags by setting the environment variable AUTOMEMLIMIT_EXPERIMENT. The value of the environment variable is a comma-separated list of experiment names.
The following experiment names are known:
- none: disable all experiments
- system: enable fallback to system memory limit
type Option ¶ added in v0.3.0
type Option func(cfg *config)
Option is a function that configures the behavior of SetGoMemLimitWithOptions.
func WithLogger ¶ added in v0.6.0
WithLogger configures the logger. It automatically attaches the "package" attribute to the logs.
Default: slog.New(noopLogger{})
func WithRatio ¶ added in v0.3.0
WithRatio configures the ratio of the memory limit to set as GOMEMLIMIT.
Default: 0.9
func WithRefreshInterval ¶ added in v0.7.0
WithRefreshInterval configures the refresh interval for automemlimit. If a refresh interval is greater than 0, automemlimit periodically fetches the memory limit from the provider and reapplies it if it has changed. If the provider returns an error, it logs the error and continues. ErrNoLimit is treated as math.MaxInt64.
Default: 0 (no refresh)
type Provider ¶
Provider is a function that returns the memory limit.
func ApplyFallback ¶ added in v0.4.0
ApplyFallback is a helper Provider function that sets the fallback provider.
func ApplyRatio ¶ added in v0.4.0
ApplyRationA is a helper Provider function that applies the given ratio to the given provider.