github.com/opencontainers/runtime-tools@v0.9.0/validation/linux_cgroups_relative_hugetlb/linux_cgroups_relative_hugetlb.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/mndrix/tap-go"
     5  	rspec "github.com/opencontainers/runtime-spec/specs-go"
     6  	"github.com/opencontainers/runtime-tools/cgroups"
     7  	"github.com/opencontainers/runtime-tools/validation/util"
     8  )
     9  
    10  func main() {
    11  	page := "1GB"
    12  	var limit uint64 = 56892210544640
    13  
    14  	t := tap.New()
    15  	t.Header(0)
    16  	defer t.AutoPlan()
    17  
    18  	g, err := util.GetDefaultGenerator()
    19  	if err != nil {
    20  		util.Fatal(err)
    21  	}
    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  		}
    31  
    32  		lhd, err := cg.GetHugepageLimitData(state.Pid, config.Linux.CgroupsPath)
    33  		t.Ok((err == nil), "get hugetlb cgroup data")
    34  		if err != nil {
    35  			t.Diagnostic(err.Error())
    36  			return nil
    37  		}
    38  
    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)
    45  			}
    46  		}
    47  		t.Ok(found, "hugepage limit found")
    48  
    49  		return nil
    50  	})
    51  
    52  	if err != nil {
    53  		t.Fail(err.Error())
    54  	}
    55  }