2015-10-24 04:41:10 +08:00
|
|
|
// +build linux
|
|
|
|
|
2015-04-27 16:34:36 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2015-09-30 08:08:41 +08:00
|
|
|
"fmt"
|
2015-04-27 16:34:36 +08:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2015-04-27 16:34:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
hugetlbUsageContents = "128\n"
|
|
|
|
hugetlbMaxUsageContents = "256\n"
|
|
|
|
hugetlbFailcnt = "100\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-30 08:08:41 +08:00
|
|
|
usage = "hugetlb.%s.usage_in_bytes"
|
|
|
|
limit = "hugetlb.%s.limit_in_bytes"
|
|
|
|
maxUsage = "hugetlb.%s.max_usage_in_bytes"
|
|
|
|
failcnt = "hugetlb.%s.failcnt"
|
2015-04-27 16:34:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHugetlbSetHugetlb(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("hugetlb", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
|
|
|
|
const (
|
|
|
|
hugetlbBefore = 256
|
|
|
|
hugetlbAfter = 512
|
|
|
|
)
|
|
|
|
|
2015-09-30 08:08:41 +08:00
|
|
|
for _, pageSize := range HugePageSizes {
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
fmt.Sprintf(limit, pageSize): strconv.Itoa(hugetlbBefore),
|
|
|
|
})
|
2015-04-27 16:34:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-30 08:08:41 +08:00
|
|
|
for _, pageSize := range HugePageSizes {
|
|
|
|
helper.CgroupData.c.HugetlbLimit = []*configs.HugepageLimit{
|
|
|
|
{
|
|
|
|
Pagesize: pageSize,
|
|
|
|
Limit: hugetlbAfter,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
hugetlb := &HugetlbGroup{}
|
|
|
|
if err := hugetlb.Set(helper.CgroupPath, helper.CgroupData.c); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-27 16:34:36 +08:00
|
|
|
}
|
2015-09-30 08:08:41 +08:00
|
|
|
|
|
|
|
for _, pageSize := range HugePageSizes {
|
|
|
|
limit := fmt.Sprintf(limit, pageSize)
|
|
|
|
value, err := getCgroupParamUint(helper.CgroupPath, limit)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse %s - %s", limit, err)
|
|
|
|
}
|
|
|
|
if value != hugetlbAfter {
|
|
|
|
t.Fatalf("Set hugetlb.limit_in_bytes failed. Expected: %v, Got: %v", hugetlbAfter, value)
|
|
|
|
}
|
2015-04-27 16:34:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHugetlbStats(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("hugetlb", t)
|
|
|
|
defer helper.cleanup()
|
2015-09-30 08:08:41 +08:00
|
|
|
for _, pageSize := range HugePageSizes {
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
fmt.Sprintf(usage, pageSize): hugetlbUsageContents,
|
|
|
|
fmt.Sprintf(maxUsage, pageSize): hugetlbMaxUsageContents,
|
|
|
|
fmt.Sprintf(failcnt, pageSize): hugetlbFailcnt,
|
|
|
|
})
|
|
|
|
}
|
2015-04-27 16:34:36 +08:00
|
|
|
|
|
|
|
hugetlb := &HugetlbGroup{}
|
|
|
|
actualStats := *cgroups.NewStats()
|
|
|
|
err := hugetlb.GetStats(helper.CgroupPath, &actualStats)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expectedStats := cgroups.HugetlbStats{Usage: 128, MaxUsage: 256, Failcnt: 100}
|
2015-09-30 08:08:41 +08:00
|
|
|
for _, pageSize := range HugePageSizes {
|
|
|
|
expectHugetlbStatEquals(t, expectedStats, actualStats.HugetlbStats[pageSize])
|
|
|
|
}
|
2015-04-27 16:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHugetlbStatsNoUsageFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("hugetlb", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
maxUsage: hugetlbMaxUsageContents,
|
|
|
|
})
|
|
|
|
|
|
|
|
hugetlb := &HugetlbGroup{}
|
|
|
|
actualStats := *cgroups.NewStats()
|
|
|
|
err := hugetlb.GetStats(helper.CgroupPath, &actualStats)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHugetlbStatsNoMaxUsageFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("hugetlb", t)
|
|
|
|
defer helper.cleanup()
|
2015-09-30 08:08:41 +08:00
|
|
|
for _, pageSize := range HugePageSizes {
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
fmt.Sprintf(usage, pageSize): hugetlbUsageContents,
|
|
|
|
})
|
|
|
|
}
|
2015-04-27 16:34:36 +08:00
|
|
|
|
|
|
|
hugetlb := &HugetlbGroup{}
|
|
|
|
actualStats := *cgroups.NewStats()
|
|
|
|
err := hugetlb.GetStats(helper.CgroupPath, &actualStats)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHugetlbStatsBadUsageFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("hugetlb", t)
|
|
|
|
defer helper.cleanup()
|
2015-09-30 08:08:41 +08:00
|
|
|
for _, pageSize := range HugePageSizes {
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
fmt.Sprintf(usage, pageSize): "bad",
|
|
|
|
maxUsage: hugetlbMaxUsageContents,
|
|
|
|
})
|
|
|
|
}
|
2015-04-27 16:34:36 +08:00
|
|
|
|
|
|
|
hugetlb := &HugetlbGroup{}
|
|
|
|
actualStats := *cgroups.NewStats()
|
|
|
|
err := hugetlb.GetStats(helper.CgroupPath, &actualStats)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHugetlbStatsBadMaxUsageFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("hugetlb", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
usage: hugetlbUsageContents,
|
|
|
|
maxUsage: "bad",
|
|
|
|
})
|
|
|
|
|
|
|
|
hugetlb := &HugetlbGroup{}
|
|
|
|
actualStats := *cgroups.NewStats()
|
|
|
|
err := hugetlb.GetStats(helper.CgroupPath, &actualStats)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure")
|
|
|
|
}
|
|
|
|
}
|