Skip to content

Commit

Permalink
Merge pull request #207 from klihub/fixes/sysfs-discovery
Browse files Browse the repository at this point in the history
fixes: don't use deprecated sysfs entry.
  • Loading branch information
fmuyassarov authored Dec 11, 2023
2 parents 6f2781b + c3b7a5b commit b313128
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
24 changes: 10 additions & 14 deletions pkg/sysfs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package sysfs

import (
"errors"
"fmt"
"io/ioutil"
"io/fs"
"path/filepath"
"sort"
"strconv"
Expand Down Expand Up @@ -587,8 +588,14 @@ func (sys *system) discoverCPU(path string) error {
if _, err := readSysfsEntry(path, "topology/core_id", &cpu.core); err != nil {
return err
}
if _, err := readSysfsEntry(path, "topology/thread_siblings_list", &cpu.threads, ","); err != nil {
return err

if _, err := readSysfsEntry(path, "topology/core_cpus_list", &cpu.threads, ","); err != nil {
if errors.Is(err, fs.ErrNotExist) {
_, err = readSysfsEntry(path, "topology/thread_siblings_list", &cpu.threads, ",")
}
if err != nil {
return err
}
}
} else {
sys.offline.Add(cpu.id)
Expand Down Expand Up @@ -725,17 +732,6 @@ func (c *cpu) SetFrequencyLimits(min, max uint64) error {
return nil
}

func readCPUsetFile(base, entry string) (cpuset.CPUSet, error) {
path := filepath.Join(base, entry)

blob, err := ioutil.ReadFile(path)
if err != nil {
return cpuset.New(), sysfsError(path, "failed to read sysfs entry: %v", err)
}

return cpuset.Parse(strings.Trim(string(blob), "\n"))
}

// Discover NUMA nodes present in the system.
func (sys *system) discoverNodes() error {
if sys.nodes != nil {
Expand Down
32 changes: 16 additions & 16 deletions pkg/sysfs/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func readSysfsEntry(base, entry string, ptr interface{}, args ...interface{}) (s

blob, err := ioutil.ReadFile(path)
if err != nil {
return "", sysfsError(path, "failed to read sysfs entry: %v", err)
return "", sysfsError(path, "failed to read sysfs entry: %w", err)
}
buf = strings.Trim(string(blob), "\n")

Expand All @@ -68,18 +68,18 @@ func readSysfsEntry(base, entry string, ptr interface{}, args ...interface{}) (s
case *string, *int, *uint, *int8, *uint8, *int16, *uint16, *int32, *uint32, *int64, *uint64:
err := parseValue(buf, ptr)
if err != nil {
return "", sysfsError(path, "%v", err)
return "", sysfsError(path, "%w", err)
}
return buf, nil

case *idset.IDSet, *[]int, *[]uint, *[]int8, *[]uint8, *[]int16, *[]uint16, *[]int32, *[]uint32, *[]int64, *[]uint64:
sep, err := getSeparator(" ", args)
if err != nil {
return "", sysfsError(path, "%v", err)
return "", sysfsError(path, "%w", err)
}
err = parseValueList(buf, sep, ptr)
if err != nil {
return "", sysfsError(path, "%v", err)
return "", sysfsError(path, "%w", err)
}
return buf, nil
case *EPP:
Expand Down Expand Up @@ -107,17 +107,17 @@ func writeSysfsEntry(base, entry string, val, oldp interface{}, args ...interfac
case string, int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64:
buf, err = formatValue(val)
if err != nil {
return "", sysfsError(path, "%v", err)
return "", sysfsError(path, "%w", err)
}

case idset.IDSet, []int, []uint, []int8, []uint8, []int16, []uint16, []int32, []uint32, []int64, []uint64:
sep, err := getSeparator(" ", args)
if err != nil {
return "", sysfsError(path, "%v", err)
return "", sysfsError(path, "%w", err)
}
buf, err = formatValueList(sep, val)
if err != nil {
return "", sysfsError(path, "%v", err)
return "", sysfsError(path, "%w", err)
}

default:
Expand All @@ -126,12 +126,12 @@ func writeSysfsEntry(base, entry string, val, oldp interface{}, args ...interfac

f, err := os.OpenFile(path, os.O_WRONLY, 0)
if err != nil {
return "", sysfsError(path, "cannot open: %v", err)
return "", sysfsError(path, "cannot open: %w", err)
}
defer f.Close()

if _, err = f.Write([]byte(buf + "\n")); err != nil {
return "", sysfsError(path, "cannot write: %v", err)
return "", sysfsError(path, "cannot write: %w", err)
}

return old, nil
Expand All @@ -158,7 +158,7 @@ func parseValue(str string, value interface{}) error {
case *int, *int8, *int16, *int32, *int64:
v, err := strconv.ParseInt(str, 0, 0)
if err != nil {
return fmt.Errorf("invalid entry '%s': %v", str, err)
return fmt.Errorf("invalid entry '%s': %w", str, err)
}

switch value.(type) {
Expand All @@ -177,7 +177,7 @@ func parseValue(str string, value interface{}) error {
case *uint, *uint8, *uint16, *uint32, *uint64:
v, err := strconv.ParseUint(str, 0, 0)
if err != nil {
return fmt.Errorf("invalid entry: '%s': %v", str, err)
return fmt.Errorf("invalid entry: '%s': %w", str, err)
}

switch value.(type) {
Expand Down Expand Up @@ -237,17 +237,17 @@ func parseValueList(str, sep string, valuep interface{}) error {
if rng := strings.Split(s, "-"); len(rng) == 1 {
id, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("invalid entry '%s': %v", s, err)
return fmt.Errorf("invalid entry '%s': %w", s, err)
}
value.(idset.IDSet).Add(idset.ID(id))
} else {
beg, err := strconv.Atoi(rng[0])
if err != nil {
return fmt.Errorf("invalid entry '%s': %v", s, err)
return fmt.Errorf("invalid entry '%s': %w", s, err)
}
end, err := strconv.Atoi(rng[1])
if err != nil {
return fmt.Errorf("invalid entry '%s': %v", s, err)
return fmt.Errorf("invalid entry '%s': %w", s, err)
}
for id := beg; id <= end; id++ {
value.(idset.IDSet).Add(idset.ID(id))
Expand All @@ -257,7 +257,7 @@ func parseValueList(str, sep string, valuep interface{}) error {
case []int, []int8, []int16, []int32, []int64:
v, err := strconv.ParseInt(s, 0, 0)
if err != nil {
return fmt.Errorf("invalid entry '%s': %v", s, err)
return fmt.Errorf("invalid entry '%s': %w", s, err)
}
switch value.(type) {
case []int:
Expand All @@ -275,7 +275,7 @@ func parseValueList(str, sep string, valuep interface{}) error {
case []uint, []uint8, []uint16, []uint32, []uint64:
v, err := strconv.ParseUint(s, 0, 0)
if err != nil {
return fmt.Errorf("invalid entry '%s': %v", s, err)
return fmt.Errorf("invalid entry '%s': %w", s, err)
}
switch value.(type) {
case []uint:
Expand Down

0 comments on commit b313128

Please sign in to comment.