fix gocritic lint issues

Signed-off-by: Tariq Ibrahim <tibrahim@nvidia.com>
This commit is contained in:
Tariq Ibrahim 2024-10-10 15:47:37 -07:00
parent 41955a0842
commit 30b1cf06e8
No known key found for this signature in database
GPG Key ID: 8367AA3C6B8DF06D
5 changed files with 48 additions and 32 deletions

View File

@ -3,6 +3,7 @@ linters:
enable: enable:
- asciicheck - asciicheck
- contextcheck - contextcheck
- gocritic
- godot - godot
- gofmt - gofmt
- goimports - goimports
@ -10,7 +11,6 @@ linters:
# TODO: re-enable once we have addressed the warnings # TODO: re-enable once we have addressed the warnings
disable: disable:
- unused - unused
- gocritic
- stylecheck - stylecheck
- forcetypeassert - forcetypeassert

View File

@ -280,27 +280,14 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
return nil, fmt.Errorf("unable to convert device string to uint16: %v", deviceStr) return nil, fmt.Errorf("unable to convert device string to uint16: %v", deviceStr)
} }
driver, err := filepath.EvalSymlinks(path.Join(devicePath, "driver")) driver, err := getDriver(devicePath)
if err == nil { if err != nil {
driver = filepath.Base(driver) return nil, fmt.Errorf("unable to detect driver for %s: %w", address, err)
} else if os.IsNotExist(err) {
driver = ""
} else {
return nil, fmt.Errorf("unable to detect driver for %s: %v", address, err)
} }
var iommuGroup int64 iommuGroup, err := getIOMMUGroup(devicePath)
iommu, err := filepath.EvalSymlinks(path.Join(devicePath, "iommu_group"))
if err == nil {
iommuGroupStr := strings.TrimSpace(filepath.Base(iommu))
iommuGroup, err = strconv.ParseInt(iommuGroupStr, 0, 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to convert iommu_group string to int64: %v", iommuGroupStr) return nil, fmt.Errorf("unable to detect IOMMU group for %s: %w", address, err)
}
} else if os.IsNotExist(err) {
iommuGroup = -1
} else {
return nil, fmt.Errorf("unable to detect iommu_group for %s: %v", address, err)
} }
numa, err := os.ReadFile(path.Join(devicePath, "numa_node")) numa, err := os.ReadFile(path.Join(devicePath, "numa_node"))
@ -359,7 +346,8 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
var sriovInfo SriovInfo var sriovInfo SriovInfo
// Device is a virtual function (VF) if "physfn" symlink exists. // Device is a virtual function (VF) if "physfn" symlink exists.
physFnAddress, err := filepath.EvalSymlinks(path.Join(devicePath, "physfn")) physFnAddress, err := filepath.EvalSymlinks(path.Join(devicePath, "physfn"))
if err == nil { switch {
case err == nil:
physFn, err := p.getGPUByPciBusID(filepath.Base(physFnAddress), cache) physFn, err := p.getGPUByPciBusID(filepath.Base(physFnAddress), cache)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to detect physfn for %s: %v", address, err) return nil, fmt.Errorf("unable to detect physfn for %s: %v", address, err)
@ -369,12 +357,12 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
PhysicalFunction: physFn, PhysicalFunction: physFn,
}, },
} }
} else if os.IsNotExist(err) { case os.IsNotExist(err):
sriovInfo, err = p.getSriovInfoForPhysicalFunction(devicePath) sriovInfo, err = p.getSriovInfoForPhysicalFunction(devicePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to read SRIOV physical function details for %s: %v", devicePath, err) return nil, fmt.Errorf("unable to read SRIOV physical function details for %s: %v", devicePath, err)
} }
} else { default:
return nil, fmt.Errorf("unable to read %s: %v", path.Join(devicePath, "physfn"), err) return nil, fmt.Errorf("unable to read %s: %v", path.Join(devicePath, "physfn"), err)
} }
@ -521,3 +509,31 @@ func (p *nvpci) getSriovInfoForPhysicalFunction(devicePath string) (sriovInfo Sr
} }
return sriovInfo, nil return sriovInfo, nil
} }
func getDriver(devicePath string) (string, error) {
driver, err := filepath.EvalSymlinks(path.Join(devicePath, "driver"))
switch {
case os.IsNotExist(err):
return "", nil
case err == nil:
return filepath.Base(driver), nil
}
return "", err
}
func getIOMMUGroup(devicePath string) (int64, error) {
var iommuGroup int64
iommu, err := filepath.EvalSymlinks(path.Join(devicePath, "iommu_group"))
switch {
case os.IsNotExist(err):
return -1, nil
case err == nil:
iommuGroupStr := strings.TrimSpace(filepath.Base(iommu))
iommuGroup, err = strconv.ParseInt(iommuGroupStr, 0, 64)
if err != nil {
return 0, fmt.Errorf("unable to convert iommu_group string to int64: %v", iommuGroupStr)
}
return iommuGroup, nil
}
return 0, err
}

View File

@ -112,7 +112,7 @@ func (mrs MemoryResources) GetTotalAddressableMemory(roundUp bool) (uint64, uint
if key >= pciIOVNumBAR || numBAR == pciIOVNumBAR { if key >= pciIOVNumBAR || numBAR == pciIOVNumBAR {
break break
} }
numBAR = numBAR + 1 numBAR++
region := mrs[key] region := mrs[key]
@ -123,10 +123,10 @@ func (mrs MemoryResources) GetTotalAddressableMemory(roundUp bool) (uint64, uint
memSize := (region.End - region.Start) + 1 memSize := (region.End - region.Start) + 1
if memType32bit { if memType32bit {
memSize32bit = memSize32bit + uint64(memSize) memSize32bit += uint64(memSize)
} }
if memType64bit { if memType64bit {
memSize64bit = memSize64bit + uint64(memSize) memSize64bit += uint64(memSize)
} }
} }

View File

@ -396,7 +396,7 @@ func (p *parser) parse() Interface {
hkClass = db.classes[uint32(id)] hkClass = db.classes[uint32(id)]
hkFullID = uint32(id) << 16 hkFullID = uint32(id) << 16
hkFullID = hkFullID & 0xFFFF0000 hkFullID &= 0xFFFF0000
hkFullName[0] = fmt.Sprintf("%s (%02x)", lit.name, id) hkFullName[0] = fmt.Sprintf("%s (%02x)", lit.name, id)
} }
@ -409,10 +409,10 @@ func (p *parser) parse() Interface {
hkSubClass = hkClass.subClasses[uint32(id)] hkSubClass = hkClass.subClasses[uint32(id)]
// Clear the last detected subclass. // Clear the last detected subclass.
hkFullID = hkFullID & 0xFFFF0000 hkFullID &= 0xFFFF0000
hkFullID = hkFullID | uint32(id)<<8 hkFullID |= uint32(id) << 8
// Clear the last detected prog iface. // Clear the last detected prog iface.
hkFullID = hkFullID & 0xFFFFFF00 hkFullID &= 0xFFFFFF00
hkFullName[1] = fmt.Sprintf("%s (%02x)", lit.name, id) hkFullName[1] = fmt.Sprintf("%s (%02x)", lit.name, id)
db.classes[uint32(hkFullID)] = class{ db.classes[uint32(hkFullID)] = class{