gctuner

package
v0.0.0-...-b628505 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

gctuner

Introduction

Inspired by How We Saved 70K Cores Across 30 Mission-Critical Services (Large-Scale, Semi-Automated Go GC Tuning @Uber) .

 _______________  => limit: host/cgroup memory hard limit
|               |
|---------------| => gc_trigger: heap_live + heap_live * GCPercent / 100
|               |
|---------------|
|   heap_live   |
|_______________|

Go runtime trigger GC when hit gc_trigger which affected by GCPercent and heap_live.

Assuming that we have stable traffic, our application will always have 100 MB live heap, so the runtime will trigger GC once heap hits 200 MB(by default GOGC=100). The heap size will be changed like: 100MB => 200MB => 100MB => 200MB => ....

But in real production, our application may have 4 GB memory resources, so no need to GC so frequently.

The gctuner helps to change the GOGC(GCPercent) dynamically at runtime, set the appropriate GCPercent according to current memory usage.

How it works
 _______________  => limit: host/cgroup memory hard limit
|               |
|---------------| => threshold: increase GCPercent when gc_trigger < threshold
|               |
|---------------| => gc_trigger: heap_live + heap_live * GCPercent / 100
|               |
|---------------|
|   heap_live   |
|_______________|

threshold = inuse + inuse * (gcPercent / 100)
=> gcPercent = (threshold - inuse) / inuse * 100

if threshold < 2*inuse, so gcPercent < 100, and GC positively to avoid OOM
if threshold > 2*inuse, so gcPercent > 100, and GC negatively to reduce GC times

Usage

The recommended threshold is 70% of the memory limit.


// Get mem limit from the host machine or cgroup file.
limit := 4 * 1024 * 1024 * 1024
threshold := limit * 0.7

gctuner.Tuning(threshold)

// Friendly input
gctuner.TuningWithFromHuman("1g")

// Auto
// There may be problems with multiple services in one pod.
gctuner.TuningWithAuto(false) // Is it a container? Incoming Boolean

Documentation

Overview

Package gctuner implements https://github.com/bytedance/gopkg

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGCPercent

func GetGCPercent() uint32

GetGCPercent returns the current GCPercent.

func GetMaxGCPercent

func GetMaxGCPercent() uint32

GetMaxGCPercent returns the max gc percent value.

func GetMinGCPercent

func GetMinGCPercent() uint32

GetMinGCPercent returns the min gc percent value.

func SetMaxGCPercent

func SetMaxGCPercent(n uint32) uint32

SetMaxGCPercent sets the new max gc percent value.

func SetMinGCPercent

func SetMinGCPercent(n uint32) uint32

SetMinGCPercent sets the new min gc percent value.

func Tuning

func Tuning(threshold uint64)

Tuning sets the threshold of heap which will be respect by gc tuner. When Tuning, the env GOGC will not be take effect. threshold: disable tuning if threshold == 0

func TuningWithAuto

func TuningWithAuto(isContainer bool)

TuningWithAuto By automatic calculation of the total amount

func TuningWithFromHuman

func TuningWithFromHuman(threshold string)

TuningWithFromHuman eg. "b/B", "k/K" "kb/Kb" "mb/Mb", "gb/Gb" "tb/Tb" "pb/Pb".

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL