Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/monitor/nvidia/v0/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s Spec) DeviceMax() int {
}

func (s Spec) DeviceNum() int {
return int(s.sr.num)
return int(min(s.sr.num, uint64(maxDevices)))
}

// activeProcs returns the process slots currently in use. procnum is read from
Expand Down
2 changes: 2 additions & 0 deletions pkg/monitor/nvidia/v0/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func TestSpec_DeviceNum(t *testing.T) {
tests := []specTest{
{name: "device num is 4", spec: &Spec{sr: &sharedRegionT{num: 4}}, expected: 4},
{name: "device num is 8", spec: &Spec{sr: &sharedRegionT{num: 8}}, expected: 8},
{name: "num larger than maxDevices is clamped", spec: &Spec{sr: &sharedRegionT{num: 9999}}, expected: maxDevices},
{name: "high-bit uint64 num is clamped not negative", spec: &Spec{sr: &sharedRegionT{num: 0x8000000000000001}}, expected: maxDevices},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/monitor/nvidia/v1/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (s Spec) DeviceMax() int {
}

func (s Spec) DeviceNum() int {
return int(s.sr.num)
return int(min(s.sr.num, uint64(maxDevices)))
}

// activeProcs returns the process slots currently in use. procnum is read from
Expand Down
18 changes: 18 additions & 0 deletions pkg/monitor/nvidia/v1/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@ func Test_DeviceNum(t *testing.T) {
},
want: int(4),
},
{
name: "num larger than maxDevices is clamped",
args: &Spec{
sr: &sharedRegionT{
num: 9999,
},
},
want: maxDevices,
},
{
name: "high-bit uint64 num is clamped not negative",
args: &Spec{
sr: &sharedRegionT{
num: 0x8000000000000001,
},
},
want: maxDevices,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
Loading