From 3eb469b029db0bee34247b26ad568deb16efa644 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 29 Sep 2020 11:43:27 +0200 Subject: [PATCH] libcontainer: remove redundant strings.Join() Signed-off-by: Sebastiaan van Stijn --- libcontainer/cgroups/fs/hugetlb.go | 9 ++++----- libcontainer/cgroups/fs/memory.go | 15 ++++++++------- libcontainer/cgroups/fs2/hugetlb.go | 8 +++----- libcontainer/cgroups/fs2/memory.go | 7 +++---- libcontainer/nsenter/nsenter_test.go | 1 - 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/libcontainer/cgroups/fs/hugetlb.go b/libcontainer/cgroups/fs/hugetlb.go index d32d02da1..861793411 100644 --- a/libcontainer/cgroups/fs/hugetlb.go +++ b/libcontainer/cgroups/fs/hugetlb.go @@ -5,7 +5,6 @@ package fs import ( "fmt" "strconv" - "strings" "github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/cgroups/fscommon" @@ -25,7 +24,7 @@ func (s *HugetlbGroup) Apply(path string, d *cgroupData) error { func (s *HugetlbGroup) Set(path string, cgroup *configs.Cgroup) error { 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 { + if err := fscommon.WriteFile(path, "hugetlb."+hugetlb.Pagesize+".limit_in_bytes", strconv.FormatUint(hugetlb.Limit, 10)); err != nil { return err } } @@ -39,21 +38,21 @@ func (s *HugetlbGroup) GetStats(path string, stats *cgroups.Stats) error { return nil } for _, pageSize := range HugePageSizes { - usage := strings.Join([]string{"hugetlb", pageSize, "usage_in_bytes"}, ".") + usage := "hugetlb." + pageSize + ".usage_in_bytes" 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 := "hugetlb." + pageSize + ".max_usage_in_bytes" 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 := "hugetlb." + pageSize + ".failcnt" value, err = fscommon.GetCgroupParamUint(path, failcnt) if err != nil { return fmt.Errorf("failed to parse %s - %v", failcnt, err) diff --git a/libcontainer/cgroups/fs/memory.go b/libcontainer/cgroups/fs/memory.go index 41adcd38f..2a2f0dd6c 100644 --- a/libcontainer/cgroups/fs/memory.go +++ b/libcontainer/cgroups/fs/memory.go @@ -200,8 +200,7 @@ func (s *MemoryGroup) GetStats(path string, stats *cgroups.Stats) error { } stats.MemoryStats.KernelTCPUsage = kernelTCPUsage - useHierarchy := strings.Join([]string{"memory", "use_hierarchy"}, ".") - value, err := fscommon.GetCgroupParamUint(path, useHierarchy) + value, err := fscommon.GetCgroupParamUint(path, "memory.use_hierarchy") if err != nil { return err } @@ -233,12 +232,14 @@ func getMemoryData(path, name string) (cgroups.MemoryData, error) { moduleName := "memory" if name != "" { - moduleName = strings.Join([]string{"memory", name}, ".") + moduleName = "memory." + name } - usage := strings.Join([]string{moduleName, "usage_in_bytes"}, ".") - maxUsage := strings.Join([]string{moduleName, "max_usage_in_bytes"}, ".") - failcnt := strings.Join([]string{moduleName, "failcnt"}, ".") - limit := strings.Join([]string{moduleName, "limit_in_bytes"}, ".") + var ( + usage = moduleName + ".usage_in_bytes" + maxUsage = moduleName + ".max_usage_in_bytes" + failcnt = moduleName + ".failcnt" + limit = moduleName + ".limit_in_bytes" + ) value, err := fscommon.GetCgroupParamUint(path, usage) if err != nil { diff --git a/libcontainer/cgroups/fs2/hugetlb.go b/libcontainer/cgroups/fs2/hugetlb.go index b7adf8710..60ec78fda 100644 --- a/libcontainer/cgroups/fs2/hugetlb.go +++ b/libcontainer/cgroups/fs2/hugetlb.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "path/filepath" "strconv" - "strings" "github.com/pkg/errors" @@ -24,7 +23,7 @@ func setHugeTlb(dirPath string, cgroup *configs.Cgroup) error { return nil } 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 { + if err := fscommon.WriteFile(dirPath, "hugetlb."+hugetlb.Pagesize+".max", strconv.FormatUint(hugetlb.Limit, 10)); err != nil { return err } } @@ -40,14 +39,13 @@ func statHugeTlb(dirPath string, stats *cgroups.Stats) error { hugetlbStats := cgroups.HugetlbStats{} for _, pagesize := range hugePageSizes { - usage := strings.Join([]string{"hugetlb", pagesize, "current"}, ".") - value, err := fscommon.GetCgroupParamUint(dirPath, usage) + value, err := fscommon.GetCgroupParamUint(dirPath, "hugetlb."+pagesize+".current") if err != nil { return err } hugetlbStats.Usage = value - fileName := strings.Join([]string{"hugetlb", pagesize, "events"}, ".") + fileName := "hugetlb." + pagesize + ".events" filePath := filepath.Join(dirPath, fileName) contents, err := ioutil.ReadFile(filePath) if err != nil { diff --git a/libcontainer/cgroups/fs2/memory.go b/libcontainer/cgroups/fs2/memory.go index 51d12c086..68f2546c7 100644 --- a/libcontainer/cgroups/fs2/memory.go +++ b/libcontainer/cgroups/fs2/memory.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "strconv" - "strings" "github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/cgroups/fscommon" @@ -112,10 +111,10 @@ func getMemoryDataV2(path, name string) (cgroups.MemoryData, error) { moduleName := "memory" if name != "" { - moduleName = strings.Join([]string{"memory", name}, ".") + moduleName = "memory." + name } - usage := strings.Join([]string{moduleName, "current"}, ".") - limit := strings.Join([]string{moduleName, "max"}, ".") + usage := moduleName + ".current" + limit := moduleName + ".max" value, err := fscommon.GetCgroupParamUint(path, usage) if err != nil { diff --git a/libcontainer/nsenter/nsenter_test.go b/libcontainer/nsenter/nsenter_test.go index c4d3c863c..925f5ca79 100644 --- a/libcontainer/nsenter/nsenter_test.go +++ b/libcontainer/nsenter/nsenter_test.go @@ -13,7 +13,6 @@ import ( "github.com/opencontainers/runc/libcontainer" "github.com/vishvananda/netlink/nl" - "golang.org/x/sys/unix" )