From 45bb2a23453eae6384de2eba57f2ca6893764aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Date: Wed, 19 Jul 2023 10:29:24 +0200 Subject: [PATCH 1/2] cpu: Take cgroupsv1 into account when reading misc.capacity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We've been only considering cgroupsv2 when trying to read misc.capacity. However, there are still a bunch of systems out there relying on cgroupsv1. Signed-off-by: Fabiano FidĂȘncio --- source/cpu/security_amd64.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/source/cpu/security_amd64.go b/source/cpu/security_amd64.go index 0017407a26..04a05a8803 100644 --- a/source/cpu/security_amd64.go +++ b/source/cpu/security_amd64.go @@ -116,13 +116,22 @@ func sevParameterEnabled(parameter string) bool { func getCgroupMiscCapacity(resource string) int64 { var totalResources int64 = -1 + var err error = nil + var f *os.File = nil + + miscCgroupsPaths := []string{"fs/cgroup/misc.capacity", "fs/cgroup/misc/misc.capacity"} + for _, miscCgroupsPath := range miscCgroupsPaths { + miscCgroups := hostpath.SysfsDir.Path(miscCgroupsPath) + f, err = os.Open(miscCgroups) + if err == nil { + defer f.Close() + break + } + } - miscCgroups := hostpath.SysfsDir.Path("fs/cgroup/misc.capacity") - f, err := os.Open(miscCgroups) if err != nil { return totalResources } - defer f.Close() r := bufio.NewReader(f) for { From 9b8e7d7424f8fc19cc0be612befeaef2b6275aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Date: Wed, 19 Jul 2023 11:17:37 +0200 Subject: [PATCH 2/2] cpu: Add retrieveCgroupMiscCapacityValue() for legibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let's refactor part of the getCgroupMiscCapacity() out to its own retrieveCgroupMiscCapacityValue(), for the legibility sake. Signed-off-by: Fabiano FidĂȘncio --- source/cpu/security_amd64.go | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/source/cpu/security_amd64.go b/source/cpu/security_amd64.go index 04a05a8803..58a41adad7 100644 --- a/source/cpu/security_amd64.go +++ b/source/cpu/security_amd64.go @@ -114,26 +114,10 @@ func sevParameterEnabled(parameter string) bool { return false } -func getCgroupMiscCapacity(resource string) int64 { +func retrieveCgroupMiscCapacityValue(miscCgroupPath *os.File, resource string) int64 { var totalResources int64 = -1 - var err error = nil - var f *os.File = nil - - miscCgroupsPaths := []string{"fs/cgroup/misc.capacity", "fs/cgroup/misc/misc.capacity"} - for _, miscCgroupsPath := range miscCgroupsPaths { - miscCgroups := hostpath.SysfsDir.Path(miscCgroupsPath) - f, err = os.Open(miscCgroups) - if err == nil { - defer f.Close() - break - } - } - if err != nil { - return totalResources - } - - r := bufio.NewReader(f) + r := bufio.NewReader(miscCgroupPath) for { line, _, err := r.ReadLine() if err != nil { @@ -159,3 +143,18 @@ func getCgroupMiscCapacity(resource string) int64 { return totalResources } + +func getCgroupMiscCapacity(resource string) int64 { + miscCgroupsPaths := []string{"fs/cgroup/misc.capacity", "fs/cgroup/misc/misc.capacity"} + for _, miscCgroupsPath := range miscCgroupsPaths { + miscCgroups := hostpath.SysfsDir.Path(miscCgroupsPath) + f, err := os.Open(miscCgroups) + if err == nil { + defer f.Close() + + return retrieveCgroupMiscCapacityValue(f, resource) + } + } + + return -1 +}