Skip to content

Commit

Permalink
Fix missing GPM metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Lezar <elezar@nvidia.com>
  • Loading branch information
elezar committed May 23, 2024
1 parent a94486b commit 89a4a32
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/nvml/gpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,31 @@ func (g *nvmlGpmMetricsGetType) convert() *GpmMetricsGetType {

// nvml.GpmMetricsGet()
type GpmMetricsGetVType struct {
metricsGet *nvmlGpmMetricsGetType
metricsGet *GpmMetricsGetType
}

func (l *library) GpmMetricsGetV(metricsGet *GpmMetricsGetType) GpmMetricsGetVType {
return GpmMetricsGetVType{metricsGet.convert()}
return GpmMetricsGetVType{metricsGet}
}

// nvmlGpmMetricsGetStub is a stub function that can be overridden for testing.
var nvmlGpmMetricsGetStub = nvmlGpmMetricsGet

func (metricsGetV GpmMetricsGetVType) V1() Return {
metricsGetV.metricsGet.Version = 1
return nvmlGpmMetricsGet(metricsGetV.metricsGet)
return gpmMetricsGet(metricsGetV.metricsGet)
}

func (l *library) GpmMetricsGet(metricsGet *GpmMetricsGetType) Return {
metricsGet.Version = GPM_METRICS_GET_VERSION
return nvmlGpmMetricsGet(metricsGet.convert())
return gpmMetricsGet(metricsGet)
}

func gpmMetricsGet(metricsGet *GpmMetricsGetType) Return {
nvmlMetricsGet := metricsGet.convert()
ret := nvmlGpmMetricsGetStub(nvmlMetricsGet)
*metricsGet = *nvmlMetricsGet.convert()
return ret
}

// nvml.GpmSampleFree()
Expand Down
79 changes: 79 additions & 0 deletions pkg/nvml/gpm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
# Copyright 2024 NVIDIA CORPORATION
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package nvml

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestGpmMetricsGet(t *testing.T) {
overrideMetrics := [98]GpmMetric{
{
Value: 99,
},
}
defer setNvmlGpmMetricsGetStubForTest(func(metricsGet *nvmlGpmMetricsGetType) Return {
metricsGet.Metrics = overrideMetrics
return SUCCESS
})()

metrics := GpmMetricsGetType{
Sample1: nvmlGpmSample{},
Sample2: nvmlGpmSample{},
}
ret := GpmMetricsGet(&metrics)

require.Equal(t, SUCCESS, ret)
require.EqualValues(t, GPM_METRICS_GET_VERSION, metrics.Version)

require.EqualValues(t, overrideMetrics, metrics.Metrics)
}

func TestGpmMetricsGetV(t *testing.T) {
overrideMetrics := [98]GpmMetric{
{
Value: 99,
},
}
defer setNvmlGpmMetricsGetStubForTest(func(metricsGet *nvmlGpmMetricsGetType) Return {
metricsGet.Metrics = overrideMetrics
return SUCCESS
})()

metrics := GpmMetricsGetType{
Sample1: nvmlGpmSample{},
Sample2: nvmlGpmSample{},
}

ret := GpmMetricsGetV(&metrics).V1()

require.Equal(t, SUCCESS, ret)
require.EqualValues(t, GPM_METRICS_GET_VERSION, metrics.Version)

require.EqualValues(t, overrideMetrics, metrics.Metrics)
}

func setNvmlGpmMetricsGetStubForTest(mock func(metricsGet *nvmlGpmMetricsGetType) Return) func() {
original := nvmlGpmMetricsGetStub

nvmlGpmMetricsGetStub = mock
return func() {
nvmlGpmMetricsGetStub = original
}
}

0 comments on commit 89a4a32

Please sign in to comment.