Skip to content

Commit

Permalink
Move linux-specific functions
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Lezar <elezar@nvidia.com>
  • Loading branch information
elezar committed Jun 7, 2024
1 parent 82131f9 commit 67c2e39
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 52 deletions.
12 changes: 0 additions & 12 deletions pkg/cgroups/cgroupid.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package cgroups

import (
"encoding/binary"
"fmt"
"os"
"path/filepath"
"sync"

"golang.org/x/sys/unix"
)

// CgroupID implements mapping kernel cgroup IDs to cgroupfs paths with transparent caching.
Expand All @@ -25,15 +22,6 @@ func NewCgroupID(root string) *CgroupID {
}
}

func getID(path string) uint64 {
h, _, err := unix.NameToHandleAt(unix.AT_FDCWD, path, 0)
if err != nil {
return 0
}

return binary.LittleEndian.Uint64(h.Bytes())
}

// Find finds the path for the given cgroup id.
func (cgid *CgroupID) Find(id uint64) (string, error) {
found := false
Expand Down
12 changes: 12 additions & 0 deletions pkg/cgroups/cgroupid_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cgroups

import "encoding/binary"

func getID(path string) uint64 {
h, _, err := unix.NameToHandleAt(unix.AT_FDCWD, path, 0)
if err != nil {
return 0
}

return binary.LittleEndian.Uint64(h.Bytes())
}
8 changes: 8 additions & 0 deletions pkg/cgroups/cgroupid_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !linux
// +build !linux

package cgroups

func getID(path string) uint64 {
panic("not implemented")
}
40 changes: 0 additions & 40 deletions pkg/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
"os"
"path/filepath"
"strings"
"syscall"

"golang.org/x/sys/unix"
)

// to mock in tests
Expand Down Expand Up @@ -235,43 +232,6 @@ func (h *Hint) String() string {
return "<hints " + cpus + nodes + sockets + " (from " + h.Provider + ")>"
}

// FindSysFsDevice for given argument returns physical device where it is linked to.
// For device nodes it will return path for device itself. For regular files or directories
// this function returns physical device where this inode resides (storage device).
// If result device is a virtual one (e.g. tmpfs), error will be returned.
// For non-existing path, no error returned and path is empty.
func FindSysFsDevice(dev string) (string, error) {
fi, err := os.Stat(dev)
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", fmt.Errorf("unable to get stat for %s: %w", dev, err)
}

devType := "block"
rdev := fi.Sys().(*syscall.Stat_t).Dev
if mode := fi.Mode(); mode&os.ModeDevice != 0 {
rdev = fi.Sys().(*syscall.Stat_t).Rdev
if mode&os.ModeCharDevice != 0 {
devType = "char"
}
}

major := int64(unix.Major(rdev))
minor := int64(unix.Minor(rdev))
if major == 0 {
return "", fmt.Errorf("%s is a virtual device node: %w", dev, err)
}

realDevPath, err := findSysFsDevice(devType, major, minor)
if err != nil {
return "", fmt.Errorf("failed to find sysfs device for %s: %w", dev, err)
}

return realDevPath, nil
}

// FindGivenSysFsDevice returns the physical device with the given device type,
// major, and minor numbers.
func FindGivenSysFsDevice(devType string, major, minor int64) (string, error) {
Expand Down
46 changes: 46 additions & 0 deletions pkg/topology/topology_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package topology

import (
"fmt"
"os"
"syscall"

"golang.org/x/sys/unix"
)

// FindSysFsDevice for given argument returns physical device where it is linked to.
// For device nodes it will return path for device itself. For regular files or directories
// this function returns physical device where this inode resides (storage device).
// If result device is a virtual one (e.g. tmpfs), error will be returned.
// For non-existing path, no error returned and path is empty.
func FindSysFsDevice(dev string) (string, error) {
fi, err := os.Stat(dev)
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", fmt.Errorf("unable to get stat for %s: %w", dev, err)
}

devType := "block"
rdev := fi.Sys().(*syscall.Stat_t).Dev
if mode := fi.Mode(); mode&os.ModeDevice != 0 {
rdev = fi.Sys().(*syscall.Stat_t).Rdev
if mode&os.ModeCharDevice != 0 {
devType = "char"
}
}

major := int64(unix.Major(rdev))
minor := int64(unix.Minor(rdev))
if major == 0 {
return "", fmt.Errorf("%s is a virtual device node: %w", dev, err)
}

realDevPath, err := findSysFsDevice(devType, major, minor)
if err != nil {
return "", fmt.Errorf("failed to find sysfs device for %s: %w", dev, err)
}

return realDevPath, nil
}
15 changes: 15 additions & 0 deletions pkg/topology/topology_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !linux
// +build !linux

package topology

import "errors"

// FindSysFsDevice for given argument returns physical device where it is linked to.
// For device nodes it will return path for device itself. For regular files or directories
// this function returns physical device where this inode resides (storage device).
// If result device is a virtual one (e.g. tmpfs), error will be returned.
// For non-existing path, no error returned and path is empty.
func FindSysFsDevice(dev string) (string, error) {
return "", errors.New("not implemented")
}

0 comments on commit 67c2e39

Please sign in to comment.