Skip to content

Commit e0b5e1e

Browse files
committed
merge #4897 into opencontainers/runc:release-1.3
(Backported by Jared Ledvina.) dependabot[bot]: build(deps): bump github.com/opencontainers/cgroups from 0.0.3 to 0.0.4 Kir Kolyshkin (4): deps: bump cgroups to v0.0.3, fix tests libct: State: ensure Resources is not nil deps: bump opencontainers/cgroups to v0.0.2 tests/int: simplify using check_cpu_quota LGTMs: kolyshkin cyphar
2 parents df25d7c + 2845c53 commit e0b5e1e

File tree

26 files changed

+286
-142
lines changed

26 files changed

+286
-142
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/moby/sys/user v0.3.0
1515
github.com/moby/sys/userns v0.1.0
1616
github.com/mrunalp/fileutils v0.5.1
17-
github.com/opencontainers/cgroups v0.0.1
17+
github.com/opencontainers/cgroups v0.0.4
1818
github.com/opencontainers/runtime-spec v1.2.1
1919
github.com/opencontainers/selinux v1.11.1
2020
github.com/seccomp/libseccomp-golang v0.10.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g
4949
github.com/moby/sys/userns v0.1.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28=
5050
github.com/mrunalp/fileutils v0.5.1 h1:F+S7ZlNKnrwHfSwdlgNSkKo67ReVf8o9fel6C3dkm/Q=
5151
github.com/mrunalp/fileutils v0.5.1/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
52-
github.com/opencontainers/cgroups v0.0.1 h1:MXjMkkFpKv6kpuirUa4USFBas573sSAY082B4CiHEVA=
53-
github.com/opencontainers/cgroups v0.0.1/go.mod h1:s8lktyhlGUqM7OSRL5P7eAW6Wb+kWPNvt4qvVfzA5vs=
52+
github.com/opencontainers/cgroups v0.0.4 h1:XVj8P/IHVms/j+7eh8ggdkTLAxjz84ZzuFyGoE28DR4=
53+
github.com/opencontainers/cgroups v0.0.4/go.mod h1:s8lktyhlGUqM7OSRL5P7eAW6Wb+kWPNvt4qvVfzA5vs=
5454
github.com/opencontainers/runtime-spec v1.2.1 h1:S4k4ryNgEpxW1dzyqffOmhI1BHYcjzU8lpJfSlR0xww=
5555
github.com/opencontainers/runtime-spec v1.2.1/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
5656
github.com/opencontainers/selinux v1.11.1 h1:nHFvthhM0qY8/m+vfhJylliSshm8G1jJ2jDMcgULaH8=

libcontainer/factory_linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func loadState(root string) (*State, error) {
164164
if err := json.NewDecoder(f).Decode(&state); err != nil {
165165
return nil, err
166166
}
167+
// Cgroup v1 fs manager expect Resources to never be nil.
168+
if state.Config.Cgroups.Resources == nil {
169+
state.Config.Cgroups.Resources = &cgroups.Resources{}
170+
}
167171
return state, nil
168172
}
169173

tests/integration/cgroups.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ convert_hugetlb_size() {
328328
check_systemd_value "MemoryHigh" 5242880
329329
check_systemd_value "MemoryMax" 10485760
330330
check_systemd_value "TasksMax" 99
331-
check_cpu_quota 10000 100000 "100ms"
331+
check_cpu_quota 10000 100000
332332
check_cpu_weight 42
333333
}
334334

@@ -390,7 +390,7 @@ convert_hugetlb_size() {
390390
[ "$output" = '42' ]
391391
check_systemd_value "TasksMax" 42
392392

393-
check_cpu_quota 5000 50000 "100ms"
393+
check_cpu_quota 5000 50000
394394

395395
check_cpu_weight 42
396396
}

tests/integration/helpers.bash

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,33 @@ function check_systemd_value() {
302302
function check_cpu_quota() {
303303
local quota=$1
304304
local period=$2
305-
local sd_quota=$3
305+
local sd_quota
306+
307+
if [ -v RUNC_USE_SYSTEMD ]; then
308+
if [ "$quota" = "-1" ]; then
309+
sd_quota="infinity"
310+
else
311+
# In systemd world, quota (CPUQuotaPerSec) is measured in ms
312+
# (per second), and systemd rounds it up to 10ms. For example,
313+
# given quota=4000 and period=10000, systemd value is 400ms.
314+
#
315+
# Calculate milliseconds (quota/period * 1000).
316+
# First multiply by 1000 to get milliseconds,
317+
# then add half of period for proper rounding.
318+
local ms=$(((quota * 1000 + period / 2) / period))
319+
# Round up to nearest 10ms.
320+
ms=$(((ms + 5) / 10 * 10))
321+
sd_quota="${ms}ms"
322+
323+
# Recalculate quota based on systemd value.
324+
# Convert ms back to quota units.
325+
quota=$((ms * period / 1000))
326+
327+
fi
328+
329+
# Systemd values are the same for v1 and v2.
330+
check_systemd_value "CPUQuotaPerSecUSec" "$sd_quota"
331+
fi
306332

307333
if [ -v CGROUP_V2 ]; then
308334
if [ "$quota" = "-1" ]; then
@@ -313,8 +339,6 @@ function check_cpu_quota() {
313339
check_cgroup_value "cpu.cfs_quota_us" "$quota"
314340
check_cgroup_value "cpu.cfs_period_us" "$period"
315341
fi
316-
# systemd values are the same for v1 and v2
317-
check_systemd_value "CPUQuotaPerSecUSec" "$sd_quota"
318342

319343
# CPUQuotaPeriodUSec requires systemd >= v242
320344
[ "$(systemd_version)" -lt 242 ] && return
@@ -344,7 +368,18 @@ function check_cpu_shares() {
344368
local shares=$1
345369

346370
if [ -v CGROUP_V2 ]; then
347-
local weight=$((1 + ((shares - 2) * 9999) / 262142))
371+
# Same formula as ConvertCPUSharesToCgroupV2Value.
372+
local weight
373+
weight=$(awk -v shares="$shares" '
374+
BEGIN {
375+
if (shares == 0) { print 0; exit }
376+
if (shares <= 2) { print 1; exit }
377+
if (shares >= 262144) { print 10000; exit }
378+
l = log(shares) / log(2)
379+
exponent = (l*l + 125*l) / 612.0 - 7.0/34.0
380+
print int(exp(exponent * log(10)) + 0.99)
381+
}')
382+
348383
check_cpu_weight "$weight"
349384
else
350385
check_cgroup_value "cpu.shares" "$shares"

tests/integration/update.bats

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -263,26 +263,26 @@ EOF
263263
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
264264
[ "$status" -eq 0 ]
265265

266-
# check that initial values were properly set
267-
check_cpu_quota 500000 1000000 "500ms"
266+
# Check that initial values were properly set.
267+
check_cpu_quota 500000 1000000
268268
check_cpu_shares 100
269269

270-
# update cpu period
270+
# Update cpu period.
271271
runc update test_update --cpu-period 900000
272272
[ "$status" -eq 0 ]
273-
check_cpu_quota 500000 900000 "560ms"
273+
check_cpu_quota 500000 900000
274274

275-
# update cpu quota
275+
# Update cpu quota.
276276
runc update test_update --cpu-quota 600000
277277
[ "$status" -eq 0 ]
278-
check_cpu_quota 600000 900000 "670ms"
278+
check_cpu_quota 600000 900000
279279

280-
# remove cpu quota
280+
# Remove cpu quota.
281281
runc update test_update --cpu-quota -1
282282
[ "$status" -eq 0 ]
283-
check_cpu_quota -1 900000 "infinity"
283+
check_cpu_quota -1 900000
284284

285-
# update cpu-shares
285+
# Update cpu-shares.
286286
runc update test_update --cpu-share 200
287287
[ "$status" -eq 0 ]
288288
check_cpu_shares 200
@@ -298,21 +298,21 @@ EOF
298298
}
299299
EOF
300300
[ "$status" -eq 0 ]
301-
check_cpu_quota 500000 1000000 "500ms"
301+
check_cpu_quota 500000 1000000
302302

303-
# redo all the changes at once
303+
# Redo all the changes at once.
304304
runc update test_update \
305305
--cpu-period 900000 --cpu-quota 600000 --cpu-share 200
306306
[ "$status" -eq 0 ]
307-
check_cpu_quota 600000 900000 "670ms"
307+
check_cpu_quota 600000 900000
308308
check_cpu_shares 200
309309

310-
# remove cpu quota and reset the period
310+
# Remove cpu quota and reset the period.
311311
runc update test_update --cpu-quota -1 --cpu-period 100000
312312
[ "$status" -eq 0 ]
313-
check_cpu_quota -1 100000 "infinity"
313+
check_cpu_quota -1 100000
314314

315-
# reset to initial test value via json file
315+
# Reset to initial test values via json file.
316316
cat <<EOF >"$BATS_RUN_TMPDIR"/runc-cgroups-integration-test.json
317317
{
318318
"cpu": {
@@ -326,7 +326,7 @@ EOF
326326

327327
runc update -r "$BATS_RUN_TMPDIR"/runc-cgroups-integration-test.json test_update
328328
[ "$status" -eq 0 ]
329-
check_cpu_quota 500000 1000000 "500ms"
329+
check_cpu_quota 500000 1000000
330330
check_cpu_shares 100
331331
}
332332

@@ -363,7 +363,7 @@ EOF
363363
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
364364
[ "$status" -eq 0 ]
365365

366-
check_cpu_quota -1 1000000 "infinity"
366+
check_cpu_quota -1 1000000
367367
}
368368

369369
@test "set cpu period with no quota (invalid period)" {
@@ -382,7 +382,7 @@ EOF
382382

383383
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
384384
[ "$status" -eq 0 ]
385-
check_cpu_quota 5000 100000 "50ms"
385+
check_cpu_quota 5000 100000
386386
}
387387

388388
@test "update cpu period with no previous period/quota set" {
@@ -393,10 +393,10 @@ EOF
393393
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
394394
[ "$status" -eq 0 ]
395395

396-
# update the period alone, no old values were set
396+
# Update the period alone, no old values were set.
397397
runc update --cpu-period 50000 test_update
398398
[ "$status" -eq 0 ]
399-
check_cpu_quota -1 50000 "infinity"
399+
check_cpu_quota -1 50000
400400
}
401401

402402
@test "update cpu quota with no previous period/quota set" {
@@ -407,10 +407,10 @@ EOF
407407
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
408408
[ "$status" -eq 0 ]
409409

410-
# update the quota alone, no old values were set
410+
# Update the quota alone, no old values were set.
411411
runc update --cpu-quota 30000 test_update
412412
[ "$status" -eq 0 ]
413-
check_cpu_quota 30000 100000 "300ms"
413+
check_cpu_quota 30000 100000
414414
}
415415

416416
@test "update cpu period in a pod cgroup with pod limit set" {
@@ -445,7 +445,7 @@ EOF
445445
# Finally, the test itself: set 30% limit but with lower period.
446446
runc update --cpu-period 10000 --cpu-quota 3000 test_update
447447
[ "$status" -eq 0 ]
448-
check_cpu_quota 3000 10000 "300ms"
448+
check_cpu_quota 3000 10000
449449
}
450450

451451
@test "update cgroup cpu.idle" {
@@ -545,10 +545,9 @@ EOF
545545
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
546546
[ "$status" -eq 0 ]
547547

548-
# check that initial values were properly set
549-
check_cpu_quota 500000 1000000 "500ms"
550-
# initial cpu shares of 100 corresponds to weight of 4
551-
check_cpu_weight 4
548+
# Check that initial values (from setup) were properly set.
549+
check_cpu_quota 500000 1000000
550+
check_cpu_shares 100
552551
check_systemd_value "TasksMax" 20
553552

554553
runc update -r - test_update <<EOF
@@ -561,8 +560,8 @@ EOF
561560
}
562561
EOF
563562

564-
# check the updated systemd unit properties
565-
check_cpu_quota -1 100000 "infinity"
563+
# Check the updated systemd unit properties.
564+
check_cpu_quota -1 100000
566565
check_cpu_weight 16
567566
check_systemd_value "TasksMax" 10
568567
}

vendor/github.com/opencontainers/cgroups/.golangci-extra.yml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/opencontainers/cgroups/.golangci.yml

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/opencontainers/cgroups/RELEASES.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)