Skip to content

Commit 0ab761a

Browse files
committed
Update hugetlb tests to be more portable
Not all arches/setups supports all page sizes. Should only use the ones supported on the current platform Signed-off-by: Odin Ugedal <odin@ugedal.com>
1 parent 9bd7cf3 commit 0ab761a

File tree

2 files changed

+68
-47
lines changed

2 files changed

+68
-47
lines changed

validation/linux_cgroups_hugetlb/linux_cgroups_hugetlb.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,35 @@ import (
1010
"github.com/opencontainers/runtime-tools/validation/util"
1111
)
1212

13+
const (
14+
B = 1 << (10 * iota)
15+
KiB
16+
MiB
17+
GiB
18+
)
19+
1320
func testHugetlbCgroups() error {
1421
t := tap.New()
1522
t.Header(0)
1623
defer t.AutoPlan()
1724

18-
// limit =~ 100 * page size
19-
// NOTE: on some systems, pagesize "1GB" doesn't seem to work.
20-
// Ideally we should auto-detect the value.
21-
cases := []struct {
22-
page string
23-
limit uint64
24-
}{
25-
{"2MB", 100 * 2 * 1024 * 1024},
26-
{"1GB", 100 * 1024 * 1024 * 1024},
27-
{"2MB", 100 * 2 * 1024 * 1024},
28-
{"1GB", 100 * 1024 * 1024 * 1024},
25+
pageSizes, err := cgroups.GetHugePageSize()
26+
27+
if err != nil {
28+
t.Fail(fmt.Sprintf("error when getting hugepage sizes: %+v", err))
2929
}
3030

31-
for _, c := range cases {
31+
// When setting the limit just for checking if writing works, the amount of memory
32+
// requested does not matter, as all insigned integers will be accepted.
33+
const limit = 2 * GiB
34+
35+
for _, pageSize := range pageSizes {
3236
g, err := util.GetDefaultGenerator()
3337
if err != nil {
3438
return err
3539
}
3640
g.SetLinuxCgroupsPath(cgroups.AbsCgroupPath)
37-
g.AddLinuxResourcesHugepageLimit(c.page, c.limit)
41+
g.AddLinuxResourcesHugepageLimit(pageSize, limit)
3842
err = util.RuntimeOutsideValidate(g, t, func(config *rspec.Spec, t *tap.T, state *rspec.State) error {
3943
cg, err := cgroups.FindCgroup()
4044
if err != nil {
@@ -45,11 +49,11 @@ func testHugetlbCgroups() error {
4549
return err
4650
}
4751
for _, lhl := range lhd {
48-
if lhl.Pagesize != c.page {
52+
if lhl.Pagesize != pageSize {
4953
continue
5054
}
51-
t.Ok(lhl.Limit == c.limit, "hugepage limit is set correctly")
52-
t.Diagnosticf("expect: %d, actual: %d", c.limit, lhl.Limit)
55+
t.Ok(lhl.Limit == limit, fmt.Sprintf("hugepage limit is set correctly for size: %s", pageSize))
56+
t.Diagnosticf("expect: %d, actual: %d", limit, lhl.Limit)
5357
}
5458
return nil
5559
})
@@ -63,7 +67,8 @@ func testHugetlbCgroups() error {
6367

6468
func testWrongHugetlb() error {
6569
// We deliberately set the page size to a wrong value, "3MB", to see
66-
// if the container really returns an error.
70+
// if the container really returns an error. Page sizes will always be a
71+
// on the format 2^(integer)
6772
page := "3MB"
6873
var limit uint64 = 100 * 3 * 1024 * 1024
6974

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,71 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/mndrix/tap-go"
56
rspec "github.com/opencontainers/runtime-spec/specs-go"
67
"github.com/opencontainers/runtime-tools/cgroups"
78
"github.com/opencontainers/runtime-tools/validation/util"
89
)
910

10-
func main() {
11-
page := "1GB"
12-
var limit uint64 = 56892210544640
11+
const (
12+
B = 1 << (10 * iota)
13+
KiB
14+
MiB
15+
GiB
16+
)
1317

18+
func main() {
1419
t := tap.New()
1520
t.Header(0)
1621
defer t.AutoPlan()
1722

18-
g, err := util.GetDefaultGenerator()
23+
pageSizes, err := cgroups.GetHugePageSize()
24+
1925
if err != nil {
20-
util.Fatal(err)
26+
t.Fail(fmt.Sprintf("error when getting hugepage sizes: %+v", err))
2127
}
22-
g.SetLinuxCgroupsPath(cgroups.RelCgroupPath)
23-
g.AddLinuxResourcesHugepageLimit(page, limit)
24-
err = util.RuntimeOutsideValidate(g, t, func(config *rspec.Spec, t *tap.T, state *rspec.State) error {
25-
cg, err := cgroups.FindCgroup()
26-
t.Ok((err == nil), "find hugetlb cgroup")
27-
if err != nil {
28-
t.Diagnostic(err.Error())
29-
return nil
30-
}
28+
// When setting the limit just for checking if writing works, the amount of memory
29+
// requested does not matter, as all insigned integers will be accepted.
30+
const limit = 2 * GiB
3131

32-
lhd, err := cg.GetHugepageLimitData(state.Pid, config.Linux.CgroupsPath)
33-
t.Ok((err == nil), "get hugetlb cgroup data")
32+
for _, pageSize := range pageSizes {
33+
g, err := util.GetDefaultGenerator()
3434
if err != nil {
35-
t.Diagnostic(err.Error())
36-
return nil
35+
util.Fatal(err)
3736
}
37+
g.SetLinuxCgroupsPath(cgroups.RelCgroupPath)
38+
g.AddLinuxResourcesHugepageLimit(pageSize, limit)
39+
err = util.RuntimeOutsideValidate(g, t, func(config *rspec.Spec, t *tap.T, state *rspec.State) error {
40+
cg, err := cgroups.FindCgroup()
41+
t.Ok((err == nil), "find hugetlb cgroup")
42+
if err != nil {
43+
t.Diagnostic(err.Error())
44+
return nil
45+
}
3846

39-
found := false
40-
for _, lhl := range lhd {
41-
if lhl.Pagesize == page {
42-
found = true
43-
t.Ok(lhl.Limit == limit, "hugepage limit is set correctly")
44-
t.Diagnosticf("expect: %d, actual: %d", limit, lhl.Limit)
47+
lhd, err := cg.GetHugepageLimitData(state.Pid, config.Linux.CgroupsPath)
48+
t.Ok((err == nil), "get hugetlb cgroup data")
49+
if err != nil {
50+
t.Diagnostic(err.Error())
51+
return nil
4552
}
46-
}
47-
t.Ok(found, "hugepage limit found")
4853

49-
return nil
50-
})
54+
found := false
55+
for _, lhl := range lhd {
56+
if lhl.Pagesize == pageSize {
57+
found = true
58+
t.Ok(lhl.Limit == limit, fmt.Sprintf("hugepage limit is set correctly for size: %s", pageSize))
59+
t.Diagnosticf("expect: %d, actual: %d", limit, lhl.Limit)
60+
}
61+
}
62+
t.Ok(found, "hugepage limit found")
5163

52-
if err != nil {
53-
t.Fail(err.Error())
64+
return nil
65+
})
66+
67+
if err != nil {
68+
t.Fail(err.Error())
69+
}
5470
}
5571
}

0 commit comments

Comments
 (0)