Skip to content

Commit 9030de8

Browse files
authored
Merge pull request #1595 from tkan145/update-cpus-share-formula
THREESCALE-15465 refactor: use new cpu.requests formula from Kubernetes
2 parents ca6df0f + 7de3852 commit 9030de8

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2020
- Reduce memory consumption when returning large response that has been routed through a proxy server. [PR #1572](https://github.com/3scale/APIcast/pull/1572) [THREESCALE-12258](https://issues.redhat.com/browse/THREESCALE-12258)
2121
- Fix proxy policy doesn't send headers set by APIcast to the API Backend. [PR #1588](https://github.com/3scale/APIcast/pull/1588) [THREESCALE-10151](https://redhat.atlassian.net/browse/THREESCALE-10151)
2222
- Ensure the synchronization key is released correctly. [PR #1591](https://github.com/3scale/APIcast/pull/1590)
23+
- Use new cpu.requests formula from Kubernetes. [PR #1595](https://github.com/3scale/APIcast/pull/1595) [THREESCALE-15465](https://redhat.atlassian.net/browse/THREESCALE-15465)
2324

2425
### Added
2526
- Update APIcast schema manifest [PR #1550](https://github.com/3scale/APIcast/pull/1550)

gateway/src/apicast/cli/environment.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,24 @@ local function cpu_shares()
6060
local weight = file:read('*n')
6161
file:close()
6262

63-
shares = (((weight - 1) * 262142) / 9999) + 2
63+
-- Recent version of runc/crun change the formula to calculate CPU weight
64+
-- https://github.com/kubernetes/website/blob/main/content/en/blog/_posts/2026-01-30-new-cgroup-v1-to-v2-conversion-formula/index.md#new-conversion-formula
65+
-- Old formula:
66+
-- cpu.weight = (1 + ((cpu.shares - 2) * 9999) / 262142)
67+
--
68+
-- New formula:
69+
-- cpu.weight = ⌈ 10 ^ (L^2 / 612 + 125 * L / 612 − 7 / 34)
70+
-- where: L=log2⁡(cpu.shares) (Please verify this with the code)
71+
--
72+
-- So now we need to calculate CPU shares as follow:
73+
-- cpu.shares = 2^L
74+
-- L = (sqrt(16129 + 2448 * log10(cpu.weight)) - 125) / 2
75+
local a = math.log(weight, 10)
76+
local b = 2448 * a
77+
local c = 16129 + b
78+
local d = math.sqrt(c)
79+
local l = (d - 125)/2
80+
shares = math.ceil(2^l)
6481
end
6582
else
6683
-- Cgroups v1

0 commit comments

Comments
 (0)