Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for rsvd accounting hugetlb cgroup #2360

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions libcontainer/cgroups/fs/hugetlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,30 @@ func (s *HugetlbGroup) Apply(d *cgroupData) error {
return nil
}

// HasReservationAccountingSupport checks if reservation accounting of huge pages in the hugetlb cgroup
// is supported. This is supported from linux 5.7
// https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/hugetlb.html
func (s *HugetlbGroup) HasReservationAccountingSupport(path string) bool {
if len(HugePageSizes) == 0 {
return false
}
_, err := fscommon.ReadFile(path, strings.Join([]string{"hugetlb", HugePageSizes[0], "rsvd", "limit_in_bytes"}, "."))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use cgroups.PathExists here

return err == nil
}

func (s *HugetlbGroup) Set(path string, cgroup *configs.Cgroup) error {
supportsReservationAccounting := s.HasReservationAccountingSupport(path)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the best way to check, or should we try to "cache" the value like we do with HugePageSizes?

for _, hugetlb := range cgroup.Resources.HugetlbLimit {
if err := fscommon.WriteFile(path, strings.Join([]string{"hugetlb", hugetlb.Pagesize, "limit_in_bytes"}, "."), strconv.FormatUint(hugetlb.Limit, 10)); err != nil {
return err
}

if !supportsReservationAccounting {
continue
}
if err := fscommon.WriteFile(path, strings.Join([]string{"hugetlb", hugetlb.Pagesize, "rsvd", "limit_in_bytes"}, "."), strconv.FormatUint(hugetlb.Limit, 10)); err != nil {
return err
}
}

return nil
Expand All @@ -43,22 +62,29 @@ func (s *HugetlbGroup) Remove(d *cgroupData) error {

func (s *HugetlbGroup) GetStats(path string, stats *cgroups.Stats) error {
hugetlbStats := cgroups.HugetlbStats{}
supportsReservationAccounting := s.HasReservationAccountingSupport(path)

for _, pageSize := range HugePageSizes {
usage := strings.Join([]string{"hugetlb", pageSize, "usage_in_bytes"}, ".")
filenamePrefix := strings.Join([]string{"hugetlb", pageSize}, ".")

if supportsReservationAccounting {
filenamePrefix += ".rsvd"
}
usage := fmt.Sprintf("%s.usage_in_bytes", filenamePrefix)
value, err := fscommon.GetCgroupParamUint(path, usage)
if err != nil {
return fmt.Errorf("failed to parse %s - %v", usage, err)
}
hugetlbStats.Usage = value

maxUsage := strings.Join([]string{"hugetlb", pageSize, "max_usage_in_bytes"}, ".")
maxUsage := fmt.Sprintf("%s.max_usage_in_bytes", filenamePrefix)
value, err = fscommon.GetCgroupParamUint(path, maxUsage)
if err != nil {
return fmt.Errorf("failed to parse %s - %v", maxUsage, err)
}
hugetlbStats.MaxUsage = value

failcnt := strings.Join([]string{"hugetlb", pageSize, "failcnt"}, ".")
failcnt := fmt.Sprintf("%s.failcnt", filenamePrefix)
value, err = fscommon.GetCgroupParamUint(path, failcnt)
if err != nil {
return fmt.Errorf("failed to parse %s - %v", failcnt, err)
Expand Down
57 changes: 57 additions & 0 deletions libcontainer/cgroups/fs/hugetlb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ var (
limit = "hugetlb.%s.limit_in_bytes"
maxUsage = "hugetlb.%s.max_usage_in_bytes"
failcnt = "hugetlb.%s.failcnt"

rsvdUsage = "hugetlb.%s.rsvd.usage_in_bytes"
rsvdLimit = "hugetlb.%s.rsvd.limit_in_bytes"
rsvdMaxUsage = "hugetlb.%s.rsvd.max_usage_in_bytes"
rsvdFailcnt = "hugetlb.%s.rsvd.failcnt"
)

func TestHugetlbSetHugetlb(t *testing.T) {
Expand Down Expand Up @@ -65,6 +70,58 @@ func TestHugetlbSetHugetlb(t *testing.T) {
}
}

func TestHugetlbSetHugetlbWithReservedAccounting(t *testing.T) {
helper := NewCgroupTestUtil("hugetlb", t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this test be skipped if !HasReservationAccountingSupport()?

defer helper.cleanup()

const (
hugetlbBefore = 256
hugetlbAfter = 512
)

for _, pageSize := range HugePageSizes {
helper.writeFileContents(map[string]string{
fmt.Sprintf(limit, pageSize): strconv.Itoa(hugetlbBefore),
})
helper.writeFileContents(map[string]string{
fmt.Sprintf(rsvdLimit, pageSize): strconv.Itoa(hugetlbBefore),
})
}

for _, pageSize := range HugePageSizes {
helper.CgroupData.config.Resources.HugetlbLimit = []*configs.HugepageLimit{
{
Pagesize: pageSize,
Limit: hugetlbAfter,
},
}
hugetlb := &HugetlbGroup{}
if err := hugetlb.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
}
}

for _, pageSize := range HugePageSizes {
limitFile := fmt.Sprintf(limit, pageSize)
value, err := fscommon.GetCgroupParamUint(helper.CgroupPath, limitFile)
if err != nil {
t.Fatalf("Failed to parse %s - %s", limitFile, err)
}
if value != hugetlbAfter {
t.Fatalf("Set hugetlb.limit_in_bytes failed. Expected: %v, Got: %v", hugetlbAfter, value)
}

rsvdLimitFile := fmt.Sprintf(rsvdLimit, pageSize)
rsvdValue, err := fscommon.GetCgroupParamUint(helper.CgroupPath, rsvdLimitFile)
if err != nil {
t.Fatalf("Failed to parse %s - %s", rsvdLimitFile, err)
}
if rsvdValue != hugetlbAfter {
t.Fatalf("Set hugetlb.limit_in_bytes failed. Expected: %v, Got: %v", hugetlbAfter, rsvdValue)
}
}
}

func TestHugetlbStats(t *testing.T) {
helper := NewCgroupTestUtil("hugetlb", t)
defer helper.cleanup()
Expand Down
32 changes: 30 additions & 2 deletions libcontainer/cgroups/fs2/hugetlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package fs2

import (
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
Expand All @@ -19,14 +20,33 @@ func isHugeTlbSet(cgroup *configs.Cgroup) bool {
return len(cgroup.Resources.HugetlbLimit) > 0
}

// HasReservationAccountingSupport checks if reservation accounting of huge pages in the hugetlb cgroup
// is supported. This is supported from linux 5.7
// https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/hugetlb.html
func HasReservationAccountingSupport(dirPath string) bool {
hugePageSizes, err := cgroups.GetHugePageSize()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it makes sense to do this check once, using sync.Once.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or not... since different cgroups can have different controls I guess...

if err != nil || len(hugePageSizes) == 0 {
return false
}
_, err = fscommon.ReadFile(dirPath, strings.Join([]string{"hugetlb", hugePageSizes[0], "rsvd", "max"}, "."))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use cgroups.PathExists()

return err == nil
}

func setHugeTlb(dirPath string, cgroup *configs.Cgroup) error {
if !isHugeTlbSet(cgroup) {
return nil
}
supportsReservationAccounting := HasReservationAccountingSupport(dirPath)
for _, hugetlb := range cgroup.Resources.HugetlbLimit {
if err := fscommon.WriteFile(dirPath, strings.Join([]string{"hugetlb", hugetlb.Pagesize, "max"}, "."), strconv.FormatUint(hugetlb.Limit, 10)); err != nil {
return err
}
if !supportsReservationAccounting {
continue
}
if err := fscommon.WriteFile(dirPath, strings.Join([]string{"hugetlb", hugetlb.Pagesize, "rsvd", "max"}, "."), strconv.FormatUint(hugetlb.Limit, 10)); err != nil {
return err
}
}

return nil
Expand All @@ -39,15 +59,23 @@ func statHugeTlb(dirPath string, stats *cgroups.Stats) error {
}
hugetlbStats := cgroups.HugetlbStats{}

supportsReservationAccounting := HasReservationAccountingSupport(dirPath)

for _, pagesize := range hugePageSizes {
usage := strings.Join([]string{"hugetlb", pagesize, "current"}, ".")
filenamePrefix := strings.Join([]string{"hugetlb", pagesize}, ".")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe it would be better to have it as

filenamePrefix := "hugetlb."+pagesize

(for readability)


if supportsReservationAccounting {
filenamePrefix += ".rsvd"
}

usage := fmt.Sprintf("%s.current", filenamePrefix)
value, err := fscommon.GetCgroupParamUint(dirPath, usage)
if err != nil {
return errors.Wrapf(err, "failed to parse hugetlb.%s.current file", pagesize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message from GetCgroupParamUint already contain file name, so you can return the error as-is, no need to wrap it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also the error now returns the wrong file name in case supportsReservationAccounting is set

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing this should be done as a separate first patch I think.

}
hugetlbStats.Usage = value

fileName := strings.Join([]string{"hugetlb", pagesize, "events"}, ".")
fileName := fmt.Sprintf("%s.events", filenamePrefix)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: using fileName := filenamePrefix + ".events" would be faster

but either way is fine

filePath := filepath.Join(dirPath, fileName)
contents, err := ioutil.ReadFile(filePath)
if err != nil {
Expand Down