Skip to content

runtime:change gcBackgroundUtilization optional - #167

Open
lxq015 wants to merge 2 commits into
go1.26.5-zte-devfrom
gcBackgroundUtilization
Open

lxq015 wants to merge 2 commits into
go1.26.5-zte-devfrom
gcBackgroundUtilization

Conversation

@lxq015

@lxq015 lxq015 commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

Port of https://gitcode.com/openeuler/golang/pull/70 to the ZTE RISC-V toolchain.

The concurrent GC previously reserved a fixed 25% of CPU for background marking. This change introduces the GOGCRATIO environment variable (1–99, default 25) to let users trade mutator CPU for marking throughput per workload.

Implementation: gcController.gcRatio stores the configured utilization; readGOGCRATIO parses and clamps to [0.01, 0.99]; pacer (startCycle/endCycle) and GC CPU limiter (mgclimit) use the dynamic value. Tests updated; new benchmark BenchmarkGCBackground added.

Benchmarks

  • goos: linux
  • goarch: riscv64
  • cpu: Spacemit(R) X60
  • GOGC regimes: 100 (default), 20 (moderate), 5 (dense)
  • GOMAXPROCS: 1, 4, 8
  • Baseline: GOGCRATIO=25

Benchstat comparisons below:

GOGCRATIO=25 vs 50

                                     │ GOGCRATIO=25 │              GOGCRATIO=50               │
                                     │   sec/op    │   sec/op     vs base                     │
GCBackgroundDefault/gcbound            2.938µ ± 2%   3.076µ ± 4%  +4.68% (p=0.002 n=6)
GCBackgroundDefault/gcbound-large      2.949µ ± 4%   3.140µ ± 3%  +6.48% (p=0.009 n=6)
GCBackgroundDefault/gcbound-dense      3.232µ ± 3%   3.599µ ± 5% +11.37% (p=0.002 n=6)
GCBackgroundModerate/gcbound           7.756µ ± 8%   6.483µ ±40%        ~ (p=0.065 n=6)
GCBackgroundModerate/gcbound-large     7.231µ ±22%   7.601µ ±17%        ~ (p=0.093 n=6)
GCBackgroundModerate/gcbound-dense     7.869µ ±18%   7.567µ ±11%        ~ (p=0.310 n=6)
GCBackgroundDense/gcbound              9.329µ ± 5%   9.341µ ±20%        ~ (p=0.937 n=6)
GCBackgroundDense/gcbound-large        9.582µ ± 7%  12.885µ ±32%        ~ (p=0.065 n=6)
GCBackgroundDense/gcbound-dense        9.517µ ±11%   9.763µ ±23%        ~ (p=0.310 n=6)
GCBackgroundDefault/gcbound-4          1.885µ ± 2%   1.891µ ± 1%        ~ (p=0.329 n=6)
GCBackgroundDefault/gcbound-large-4    1.929µ ± 2%   1.921µ ± 1%        ~ (p=0.699 n=6)
GCBackgroundDefault/gcbound-dense-4    1.951µ ± 2%   1.982µ ± 2%        ~ (p=0.394 n=6)
GCBackgroundModerate/gcbound-4         3.886µ ± 5%   3.645µ ±12%        ~ (p=0.132 n=6)
GCBackgroundModerate/gcbound-large-4   3.703µ ± 2%   3.385µ ± 1%  -8.59% (p=0.002 n=6)
GCBackgroundModerate/gcbound-dense-4   4.361µ ± 7%   3.495µ ± 2% -19.86% (p=0.002 n=6)
GCBackgroundDense/gcbound-4           10.655µ ±13%   6.684µ ± 6% -37.27% (p=0.002 n=6)
GCBackgroundDense/gcbound-large-4      8.588µ ±14%   5.315µ ± 1% -38.11% (p=0.002 n=6)
GCBackgroundDense/gcbound-dense-4      9.318µ ±12%   5.544µ ± 3% -40.50% (p=0.002 n=6)
GCBackgroundDefault/gcbound-8          1.852µ ± 1%   1.862µ ± 2%        ~ (p=0.582 n=6)
GCBackgroundDefault/gcbound-large-8    1.894µ ± 1%   1.889µ ± 3%        ~ (p=0.937 n=6)
GCBackgroundDefault/gcbound-dense-8    1.925µ ± 2%   1.901µ ± 2%  -1.27% (p=0.002 n=6)
GCBackgroundModerate/gcbound-8         3.490µ ± 1%   3.257µ ± 8%  -6.68% (p=0.028 n=6)
GCBackgroundModerate/gcbound-large-8   3.161µ ± 7%   3.045µ ± 2%  -3.69% (p=0.017 n=6)
GCBackgroundModerate/gcbound-dense-8   3.652µ ± 3%   3.223µ ± 1% -11.75% (p=0.002 n=6)
GCBackgroundDense/gcbound-8            8.290µ ±15%   5.929µ ± 2% -28.48% (p=0.002 n=6)
GCBackgroundDense/gcbound-large-8      5.859µ ± 7%   5.039µ ± 2% -13.99% (p=0.002 n=6)
GCBackgroundDense/gcbound-dense-8      7.406µ ± 6%   5.769µ ± 8% -22.10% (p=0.002 n=6)
geomean                                4.482µ        4.096µ        -8.61%

(All B/op and allocs/op unchanged at 832.0 B and 3.000 allocs/op across all benchmarks.)

GOGCRATIO=25 vs 75

                                     │ GOGCRATIO=25 │              GOGCRATIO=75               │
                                     │   sec/op    │   sec/op     vs base                     │
GCBackgroundDefault/gcbound            2.938µ ± 2%   3.242µ ± 4% +10.33% (p=0.002 n=6)
GCBackgroundDefault/gcbound-large      2.949µ ± 4%   3.226µ ± 9%  +9.41% (p=0.002 n=6)
GCBackgroundDefault/gcbound-dense      3.232µ ± 3%   3.633µ ± 1% +12.42% (p=0.002 n=6)
GCBackgroundModerate/gcbound           7.756µ ± 8%   7.407µ ±30%        ~ (p=0.310 n=6)
GCBackgroundModerate/gcbound-large     7.231µ ±22%   8.415µ ± 7% +16.37% (p=0.002 n=6)
GCBackgroundModerate/gcbound-dense     7.869µ ±18%   8.585µ ± 6%        ~ (p=0.093 n=6)
GCBackgroundDense/gcbound              9.329µ ± 5%  10.491µ ± 6% +12.46% (p=0.002 n=6)
GCBackgroundDense/gcbound-large        9.582µ ± 7%  12.307µ ±13% +28.44% (p=0.002 n=6)
GCBackgroundDense/gcbound-dense        9.517µ ±11%  10.789µ ± 3% +13.37% (p=0.004 n=6)
GCBackgroundDefault/gcbound-4          1.885µ ± 2%   1.881µ ± 1%        ~ (p=0.485 n=6)
GCBackgroundDefault/gcbound-large-4    1.929µ ± 2%   1.919µ ± 2%        ~ (p=0.485 n=6)
GCBackgroundDefault/gcbound-dense-4    1.951µ ± 2%   1.938µ ± 3%        ~ (p=0.509 n=6)
GCBackgroundModerate/gcbound-4         3.886µ ± 5%   3.217µ ±21% -17.22% (p=0.015 n=6)
GCBackgroundModerate/gcbound-large-4   3.703µ ± 2%   3.206µ ± 1% -13.42% (p=0.002 n=6)
GCBackgroundModerate/gcbound-dense-4   4.361µ ± 7%   3.261µ ± 1% -25.22% (p=0.002 n=6)
GCBackgroundDense/gcbound-4           10.655µ ±13%   5.290µ ± 5% -50.35% (p=0.002 n=6)
GCBackgroundDense/gcbound-large-4      8.588µ ±14%   4.557µ ± 3% -46.94% (p=0.002 n=6)
GCBackgroundDense/gcbound-dense-4      9.318µ ±12%   4.626µ ± 4% -50.36% (p=0.002 n=6)
GCBackgroundDefault/gcbound-8          1.852µ ± 1%   1.868µ ± 2%        ~ (p=0.394 n=6)
GCBackgroundDefault/gcbound-large-8    1.894µ ± 1%   1.880µ ± 1%  -0.74% (p=0.028 n=6)
GCBackgroundDefault/gcbound-dense-8    1.925µ ± 2%   1.879µ ± 1%  -2.39% (p=0.002 n=6)
GCBackgroundModerate/gcbound-8         3.490µ ± 1%   3.057µ ± 4% -12.39% (p=0.002 n=6)
GCBackgroundModerate/gcbound-large-8   3.161µ ± 7%   2.991µ ± 2%  -5.39% (p=0.004 n=6)
GCBackgroundModerate/gcbound-dense-8   3.652µ ± 3%   3.087µ ± 3% -15.46% (p=0.002 n=6)
GCBackgroundDense/gcbound-8            8.290µ ±15%   5.437µ ± 7% -34.42% (p=0.002 n=6)
GCBackgroundDense/gcbound-large-8      5.859µ ± 7%   4.737µ ± 3% -19.13% (p=0.002 n=6)
GCBackgroundDense/gcbound-dense-8      7.406µ ± 6%   5.320µ ± 6% -28.17% (p=0.002 n=6)
geomean                                4.482µ        4.007µ       -10.60%

(B/op and allocs/op unchanged.)

GOGCRATIO=25 vs 100

                                     │ GOGCRATIO=25 │              GOGCRATIO=100              │
                                     │   sec/op    │   sec/op     vs base                     │
GCBackgroundDefault/gcbound            2.938µ ± 2%   3.077µ ± 3%  +4.70% (p=0.002 n=6)
GCBackgroundDefault/gcbound-large      2.949µ ± 4%   3.209µ ±10%         ~ (p=0.065 n=6)
GCBackgroundDefault/gcbound-dense      3.232µ ± 3%   3.616µ ± 5% +11.90% (p=0.002 n=6)
GCBackgroundModerate/gcbound           7.756µ ± 8%   6.214µ ± 5% -19.88% (p=0.002 n=6)
GCBackgroundModerate/gcbound-large     7.231µ ±22%  10.961µ ±14% +51.58% (p=0.002 n=6)
GCBackgroundModerate/gcbound-dense     7.869µ ±18%   8.985µ ±18% +14.18% (p=0.026 n=6)
GCBackgroundDense/gcbound              9.329µ ± 5%  10.870µ ± 9% +16.52% (p=0.002 n=6)
GCBackgroundDense/gcbound-large        9.582µ ± 7%  22.967µ ±10% +139.69% (p=0.002 n=6)
GCBackgroundDense/gcbound-dense        9.517µ ±11%  16.279µ ±23% +71.06% (p=0.002 n=6)
GCBackgroundDefault/gcbound-4          1.885µ ± 2%   2.031µ ± 2%  +7.77% (p=0.002 n=6)
GCBackgroundDefault/gcbound-large-4    1.929µ ± 2%   2.077µ ± 3%  +7.70% (p=0.002 n=6)
GCBackgroundDefault/gcbound-dense-4    1.951µ ± 2%   2.197µ ± 3% +12.58% (p=0.002 n=6)
GCBackgroundModerate/gcbound-4         3.886µ ± 5%   3.911µ ± 5%         ~ (p=0.589 n=6)
GCBackgroundModerate/gcbound-large-4   3.703µ ± 2%   4.149µ ± 4% +12.06% (p=0.002 n=6)
GCBackgroundModerate/gcbound-dense-4   4.361µ ± 7%   4.768µ ± 5%  +9.32% (p=0.004 n=6)
GCBackgroundDense/gcbound-4           10.655µ ±13%   8.416µ ±11% -21.01% (p=0.002 n=6)
GCBackgroundDense/gcbound-large-4      8.588µ ±14%   8.341µ ±25%         ~ (p=0.589 n=6)
GCBackgroundDense/gcbound-dense-4      9.318µ ±12%  11.229µ ± 9% +20.52% (p=0.009 n=6)
GCBackgroundDefault/gcbound-8          1.852µ ± 1%   1.901µ ± 2%  +2.67% (p=0.013 n=6)
GCBackgroundDefault/gcbound-large-8    1.894µ ± 1%   1.974µ ± 6%  +4.25% (p=0.002 n=6)
GCBackgroundDefault/gcbound-dense-8    1.925µ ± 2%   1.993µ ± 4%  +3.53% (p=0.002 n=6)
GCBackgroundModerate/gcbound-8         3.490µ ± 1%   3.210µ ± 1%  -8.02% (p=0.002 n=6)
GCBackgroundModerate/gcbound-large-8   3.161µ ± 7%   3.354µ ± 3%  +6.11% (p=0.015 n=6)
GCBackgroundModerate/gcbound-dense-8   3.652µ ± 3%   3.716µ ± 3%         ~ (p=0.485 n=6)
GCBackgroundDense/gcbound-8            8.290µ ±15%   5.895µ ± 4% -28.89% (p=0.002 n=6)
GCBackgroundDense/gcbound-large-8      5.859µ ± 7%   5.376µ ±12%         ~ (p=0.065 n=6)
GCBackgroundDense/gcbound-dense-8      7.406µ ± 6%   7.302µ ± 7%         ~ (p=0.699 n=6)
geomean                                4.482µ        4.861µ        +8.44%

(B/op and allocs/op unchanged.)

Conclusion

The knob works as intended:

Default GOGC=100 (common case): raising to 50/75 does not hurt on multi-core; only 100 causes slowdown due to GC CPU limiter engagement, confirming no regression.

GC-bound workloads (GOGC=5, dense): 75 is near-optimal, cutting time by 19–50% on 4–8 cores; 100 gives back some gain (limiter kicks in), 25 is under-provisioned.

Single core: higher GOGCRATIO always hurts (up to +140% at 100) because no parallelism exists for extra mark workers.

Geomean across all 27 configs: 25→50 -8.61%, 25→75 -10.60%, 25→100 +8.44%. The GC CPU limiter provides a safety ceiling against oversubscription.

@BoyaoWang430

Copy link
Copy Markdown
Collaborator

You can read the Garbage collection part of this document first. https://github.com/golang/proposal/blob/master/design/48409-soft-memory-limit.md

This implementation allows GOGCRATIO values from 1% to 99%, while the GC CPU limiter is designed around a 50% threshold. Above 50%, background mark workers alone can keep GC CPU usage over the limit. The limiter only disables GC assists; it does not reduce dedicated mark workers.

@lxq015

lxq015 commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator Author

You can read the Garbage collection part of this document first. https://github.com/golang/proposal/blob/master/design/48409-soft-memory-limit.md

This implementation allows GOGCRATIO values from 1% to 99%, while the GC CPU limiter is designed around a 50% threshold. Above 50%, background mark workers alone can keep GC CPU usage over the limit. The limiter only disables GC assists; it does not reduce dedicated mark workers.

@BoyaoWang430 Thanks for the information and the reference. This PR is backported from the openEuler(https://gitcode.com/openeuler/golang/pull/70). Would it be acceptable to tighten the upper limit of GOGCRATIO from 99 to 50, to keep it consistent with the GC CPU limiter's 50% threshold?

@BoyaoWang430

Copy link
Copy Markdown
Collaborator

You can read the Garbage collection part of this document first. https://github.com/golang/proposal/blob/master/design/48409-soft-memory-limit.md
This implementation allows GOGCRATIO values from 1% to 99%, while the GC CPU limiter is designed around a 50% threshold. Above 50%, background mark workers alone can keep GC CPU usage over the limit. The limiter only disables GC assists; it does not reduce dedicated mark workers.

@BoyaoWang430 Thanks for the information and the reference. This PR is backported from the openEuler(https://gitcode.com/openeuler/golang/pull/70). Would it be acceptable to tighten the upper limit of GOGCRATIO from 99 to 50, to keep it consistent with the GC CPU limiter's 50% threshold?

With the existing rounding rule and its 30% relative-error tolerance, 38% is the highest integer setting that guarantees dedicated workers use strictly less than half of the P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants