https://github.com/fangwentong/gogctuner
port MaxRAMPercentage to Golang, adjust GC parameters(SetGCPercent/SetMemoryLimit) based on the target memory usage percentage, optimize the GC overhead of Go runtime.
https://github.com/fangwentong/gogctuner
cpu-optimization gc golang
Last synced: 2 months ago
JSON representation
port MaxRAMPercentage to Golang, adjust GC parameters(SetGCPercent/SetMemoryLimit) based on the target memory usage percentage, optimize the GC overhead of Go runtime.
- Host: GitHub
- URL: https://github.com/fangwentong/gogctuner
- Owner: fangwentong
- License: mit
- Created: 2024-01-15T12:04:01.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-11-25T08:13:56.000Z (over 1 year ago)
- Last Synced: 2024-11-25T09:20:45.701Z (over 1 year ago)
- Topics: cpu-optimization, gc, golang
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gogctuner: GC Tuner for Go Applications
`gogctuner` is a Go library designed to automatically adjust the garbage collection (GC) parameters to optimize CPU
usage based on the target memory usage percentage.
## How it works
- **Pre Go1.19 Versions**: Utilizes a tuning strategy inspired
by [Uber's article](https://www.uber.com/blog/how-we-saved-70k-cores-across-30-mission-critical-services/), which
adjusts the `GOGC` parameter based on the live heap size and target memory limit.
- **Go1.19 and Later**: Takes advantage of
the [soft memory limit feature](https://github.com/golang/proposal/blob/master/design/48409-soft-memory-limit.md)
introduced in Go 1.19, offering more granular control over GC behavior.
## Features
- **Memory Usage Based Tuning**: Automatically adjusts GC parameters to maintain a specified percentage of memory usage.
- **Cross-Version Compatibility**: Compatible with Go versions below 1.19 and leverages the `SetMemoryLimit` features in
Go 1.19 and above.
- **cgroups Support**: Works seamlessly with both cgroups and cgroupsv2 for enhanced resource management.
- **Cross-OS Compatibility**: Ensures functionality across multiple operating systems.
## Usage
### Static Configuration
Use static configuration by setting the `MaxRAMPercentage` at the initialization of your application:
```go
package main
import (
"github.com/fangwentong/gogctuner"
)
func main() {
gogctuner.EnableGCTuner(
gogctuner.WithStaticConfig(gogctuner.Config{MaxRAMPercentage: 90}),
)
// Your application code here
}
```
### Dynamic Configuration
For dynamic configuration that allows runtime updates, you can use a configurator:
```go
package main
import (
"github.com/fangwentong/gogctuner"
)
func main() {
configurator := gogctuner.NewGcConfigurator()
// Integrate with your dynamic config implementation here:
conf := readFromYourConfigCenter("your_config_key")
configurator.SetConfig(conf) // set initial config
// register config updates callback
registerConfigUpdateCallback("your_config_key", func(conf gogctuner.Config) {
configurator.SetConfig(conf)
})
gogctuner.EnableGCTuner(
gogctuner.WithConfigurator(configurator),
)
// Your application code here
}
```
### Reference
- Golang GC Guide: https://tip.golang.org/doc/gc-guide
- How We Saved 70K Cores Across 30 Mission-Critical Services (Large-Scale, Semi-Automated Go GC Tuning
@Uber): https://www.uber.com/blog/how-we-saved-70k-cores-across-30-mission-critical-services/
- https://github.com/cch123/gogctuner
- https://github.com/VictoriaMetrics/VictoriaMetrics/tree/master/lib/cgroup
### License
This project is licensed under the MIT License, see the [LICENSE](LICENSE) file for details.