-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"}, ".")) | ||
return err == nil | ||
} | ||
|
||
func (s *HugetlbGroup) Set(path string, cgroup *configs.Cgroup) error { | ||
supportsReservationAccounting := s.HasReservationAccountingSupport(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 | ||
|
@@ -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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -65,6 +70,58 @@ func TestHugetlbSetHugetlb(t *testing.T) { | |
} | ||
} | ||
|
||
func TestHugetlbSetHugetlbWithReservedAccounting(t *testing.T) { | ||
helper := NewCgroupTestUtil("hugetlb", t) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this test be skipped |
||
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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
package fs2 | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strconv" | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think it makes sense to do this check once, using There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"}, ".")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
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 | ||
|
@@ -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}, ".") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also the error now returns the wrong file name in case There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: using but either way is fine |
||
filePath := filepath.Join(dirPath, fileName) | ||
contents, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
cgroups.PathExists
here