Skip to content

Commit df0dab4

Browse files
committed
fix gocritic lint issues
Signed-off-by: Tariq Ibrahim <[email protected]>
1 parent 41955a0 commit df0dab4

File tree

5 files changed

+48
-32
lines changed

5 files changed

+48
-32
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ linters:
33
enable:
44
- asciicheck
55
- contextcheck
6+
- gocritic
67
- godot
78
- gofmt
89
- goimports
910
- misspell
1011
# TODO: re-enable once we have addressed the warnings
1112
disable:
1213
- unused
13-
- gocritic
1414
- stylecheck
1515
- forcetypeassert
1616

pkg/nvlib/device/device.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (d *device) GetBrandAsString() (string, error) {
125125
case nvml.BRAND_NVIDIA_VWS:
126126
return "NvidiaVWS", nil
127127
// Deprecated in favor of nvml.BRAND_NVIDIA_CLOUD_GAMING
128-
//case nvml.BRAND_NVIDIA_VGAMING:
128+
// case nvml.BRAND_NVIDIA_VGAMING:
129129
// return "VGaming", nil
130130
case nvml.BRAND_NVIDIA_CLOUD_GAMING:
131131
return "NvidiaCloudGaming", nil

pkg/nvpci/nvpci.go

+38-22
Original file line numberDiff line numberDiff line change
@@ -280,27 +280,14 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
280280
return nil, fmt.Errorf("unable to convert device string to uint16: %v", deviceStr)
281281
}
282282

283-
driver, err := filepath.EvalSymlinks(path.Join(devicePath, "driver"))
284-
if err == nil {
285-
driver = filepath.Base(driver)
286-
} else if os.IsNotExist(err) {
287-
driver = ""
288-
} else {
289-
return nil, fmt.Errorf("unable to detect driver for %s: %v", address, err)
283+
driver, err := getDriver(devicePath)
284+
if err != nil {
285+
return nil, fmt.Errorf("unable to detect driver for %s: %w", address, err)
290286
}
291287

292-
var iommuGroup int64
293-
iommu, err := filepath.EvalSymlinks(path.Join(devicePath, "iommu_group"))
294-
if err == nil {
295-
iommuGroupStr := strings.TrimSpace(filepath.Base(iommu))
296-
iommuGroup, err = strconv.ParseInt(iommuGroupStr, 0, 64)
297-
if err != nil {
298-
return nil, fmt.Errorf("unable to convert iommu_group string to int64: %v", iommuGroupStr)
299-
}
300-
} else if os.IsNotExist(err) {
301-
iommuGroup = -1
302-
} else {
303-
return nil, fmt.Errorf("unable to detect iommu_group for %s: %v", address, err)
288+
iommuGroup, err := getIOMMUGroup(devicePath)
289+
if err != nil {
290+
return nil, fmt.Errorf("unable to detect IOMMU group for %s: %w", address, err)
304291
}
305292

306293
numa, err := os.ReadFile(path.Join(devicePath, "numa_node"))
@@ -359,7 +346,8 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
359346
var sriovInfo SriovInfo
360347
// Device is a virtual function (VF) if "physfn" symlink exists.
361348
physFnAddress, err := filepath.EvalSymlinks(path.Join(devicePath, "physfn"))
362-
if err == nil {
349+
switch {
350+
case err == nil:
363351
physFn, err := p.getGPUByPciBusID(filepath.Base(physFnAddress), cache)
364352
if err != nil {
365353
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
369357
PhysicalFunction: physFn,
370358
},
371359
}
372-
} else if os.IsNotExist(err) {
360+
case os.IsNotExist(err):
373361
sriovInfo, err = p.getSriovInfoForPhysicalFunction(devicePath)
374362
if err != nil {
375363
return nil, fmt.Errorf("unable to read SRIOV physical function details for %s: %v", devicePath, err)
376364
}
377-
} else {
365+
default:
378366
return nil, fmt.Errorf("unable to read %s: %v", path.Join(devicePath, "physfn"), err)
379367
}
380368

@@ -521,3 +509,31 @@ func (p *nvpci) getSriovInfoForPhysicalFunction(devicePath string) (sriovInfo Sr
521509
}
522510
return sriovInfo, nil
523511
}
512+
513+
func getDriver(devicePath string) (string, error) {
514+
driver, err := filepath.EvalSymlinks(path.Join(devicePath, "driver"))
515+
switch {
516+
case os.IsNotExist(err):
517+
return "", nil
518+
case err == nil:
519+
return filepath.Base(driver), nil
520+
}
521+
return "", err
522+
}
523+
524+
func getIOMMUGroup(devicePath string) (int64, error) {
525+
var iommuGroup int64
526+
iommu, err := filepath.EvalSymlinks(path.Join(devicePath, "iommu_group"))
527+
switch {
528+
case err == nil:
529+
iommuGroupStr := strings.TrimSpace(filepath.Base(iommu))
530+
iommuGroup, err = strconv.ParseInt(iommuGroupStr, 0, 64)
531+
if err != nil {
532+
return 0, fmt.Errorf("unable to convert iommu_group string to int64: %v", iommuGroupStr)
533+
}
534+
return iommuGroup, nil
535+
case os.IsNotExist(err):
536+
iommuGroup = -1
537+
}
538+
return iommuGroup, err
539+
}

pkg/nvpci/resources.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (mrs MemoryResources) GetTotalAddressableMemory(roundUp bool) (uint64, uint
112112
if key >= pciIOVNumBAR || numBAR == pciIOVNumBAR {
113113
break
114114
}
115-
numBAR = numBAR + 1
115+
numBAR++
116116

117117
region := mrs[key]
118118

@@ -123,10 +123,10 @@ func (mrs MemoryResources) GetTotalAddressableMemory(roundUp bool) (uint64, uint
123123
memSize := (region.End - region.Start) + 1
124124

125125
if memType32bit {
126-
memSize32bit = memSize32bit + uint64(memSize)
126+
memSize32bit += uint64(memSize)
127127
}
128128
if memType64bit {
129-
memSize64bit = memSize64bit + uint64(memSize)
129+
memSize64bit += uint64(memSize)
130130
}
131131

132132
}

pkg/pciids/pciids.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func (p *parser) parse() Interface {
396396
hkClass = db.classes[uint32(id)]
397397

398398
hkFullID = uint32(id) << 16
399-
hkFullID = hkFullID & 0xFFFF0000
399+
hkFullID &= 0xFFFF0000
400400
hkFullName[0] = fmt.Sprintf("%s (%02x)", lit.name, id)
401401
}
402402

@@ -408,11 +408,11 @@ func (p *parser) parse() Interface {
408408
}
409409
hkSubClass = hkClass.subClasses[uint32(id)]
410410

411-
// Clear the last detected sub class.
412-
hkFullID = hkFullID & 0xFFFF0000
413-
hkFullID = hkFullID | uint32(id)<<8
411+
// Clear the last detected subclass.
412+
hkFullID &= 0xFFFF0000
413+
hkFullID |= uint32(id) << 8
414414
// Clear the last detected prog iface.
415-
hkFullID = hkFullID & 0xFFFFFF00
415+
hkFullID &= 0xFFFFFF00
416416
hkFullName[1] = fmt.Sprintf("%s (%02x)", lit.name, id)
417417

418418
db.classes[uint32(hkFullID)] = class{

0 commit comments

Comments
 (0)