File: linux_cgroups_relative_hugetlb.go

package info (click to toggle)
golang-github-opencontainers-runtime-tools 0.9.0.109.ge931285-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,212 kB
  • sloc: sh: 559; makefile: 90
file content (65 lines) | stat: -rw-r--r-- 1,698 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main

import (
	"fmt"
	"github.com/mndrix/tap-go"
	rspec "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/opencontainers/runtime-tools/cgroups"
	"github.com/opencontainers/runtime-tools/validation/util"
)

func main() {
	t := tap.New()
	t.Header(0)
	defer t.AutoPlan()

	pageSizes, err := cgroups.GetHugePageSize()

	if err != nil {
		t.Fail(fmt.Sprintf("error when getting hugepage sizes: %+v", err))
	}
	// When setting the limit just for checking if writing works, the amount of memory
	// requested does not matter, as all insigned integers will be accepted.
	// Use 2GiB as an example
	var limit uint64 = 2 * (1 << 30)

	for _, pageSize := range pageSizes {
		g, err := util.GetDefaultGenerator()
		if err != nil {
			util.Fatal(err)
		}
		g.SetLinuxCgroupsPath(cgroups.RelCgroupPath)
		g.AddLinuxResourcesHugepageLimit(pageSize, limit)
		err = util.RuntimeOutsideValidate(g, t, func(config *rspec.Spec, t *tap.T, state *rspec.State) error {
			cg, err := cgroups.FindCgroup()
			t.Ok((err == nil), "find hugetlb cgroup")
			if err != nil {
				t.Diagnostic(err.Error())
				return nil
			}

			lhd, err := cg.GetHugepageLimitData(state.Pid, config.Linux.CgroupsPath)
			t.Ok((err == nil), "get hugetlb cgroup data")
			if err != nil {
				t.Diagnostic(err.Error())
				return nil
			}

			found := false
			for _, lhl := range lhd {
				if lhl.Pagesize == pageSize {
					found = true
					t.Ok(lhl.Limit == limit, fmt.Sprintf("hugepage limit is set correctly for size: %s", pageSize))
					t.Diagnosticf("expect: %d, actual: %d", limit, lhl.Limit)
				}
			}
			t.Ok(found, "hugepage limit found")

			return nil
		})

		if err != nil {
			t.Fail(err.Error())
		}
	}
}