Skip to content

Commit

Permalink
cpu: Add retrieveCgroupMiscCapacityValue() for legibility
Browse files Browse the repository at this point in the history
Let's refactor part of the getCgroupMiscCapacity() out to its own
retrieveCgroupMiscCapacityValue(), for the legibility sake.

Signed-off-by: Fabiano Fidêncio <fabiano.fidencio@intel.com>
  • Loading branch information
fidencio committed Jul 19, 2023
1 parent 8ed5a23 commit 7532ac3
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions source/cpu/security_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,10 @@ func sevParameterEnabled(parameter string) bool {
return false
}

func getCgroupMiscCapacity(resource string) int64 {
func retrieveCgroupMiscCapacityValue(miscCgroupPath *os.File, resource string) int64 {

Check warning on line 125 in source/cpu/security_amd64.go

View check run for this annotation

Codecov / codecov/patch

source/cpu/security_amd64.go#L125

Added line #L125 was not covered by tests
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)

Check warning on line 128 in source/cpu/security_amd64.go

View check run for this annotation

Codecov / codecov/patch

source/cpu/security_amd64.go#L128

Added line #L128 was not covered by tests
for {
line, _, err := r.ReadLine()
if err != nil {
Expand All @@ -167,3 +151,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)
}

Check warning on line 164 in source/cpu/security_amd64.go

View check run for this annotation

Codecov / codecov/patch

source/cpu/security_amd64.go#L155-L164

Added lines #L155 - L164 were not covered by tests
}

return -1

Check warning on line 167 in source/cpu/security_amd64.go

View check run for this annotation

Codecov / codecov/patch

source/cpu/security_amd64.go#L167

Added line #L167 was not covered by tests
}

0 comments on commit 7532ac3

Please sign in to comment.