Skip to content

Commit

Permalink
Ensure that exported structs reference interface types not private types
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Klues <kklues@nvidia.com>
  • Loading branch information
klueska committed Apr 12, 2024
1 parent f01937e commit b34e706
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 20 deletions.
5 changes: 4 additions & 1 deletion gen/nvml/nvml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ TRANSLATOR:
- {action: replace, from: "^Device$", to: "nvmlDevice"}
- {action: replace, from: "^Unit$", to: "nvmlUnit"}
- {action: replace, from: "^EventSet$", to: "nvmlEventSet"}
- {action: replace, from: "^EventData$", to: "nvmlEventData"}
- {action: replace, from: "^GpmSample$", to: "nvmlGpmSample"}
- {action: replace, from: "^ComputeInstance$", to: "nvmlComputeInstance"}
- {action: replace, from: "^ComputeInstanceInfo$", to: "nvmlComputeInstanceInfo"}
- {action: replace, from: "^GpuInstance$", to: "nvmlGpuInstance"}
- {action: replace, from: "^GpuInstanceInfo$", to: "nvmlGpuInstanceInfo"}
- {action: replace, from: "^VgpuInstance$", to: "nvmlVgpuInstance"}
- {action: replace, from: "^VgpuTypeId$", to: "nvmlVgpuTypeId"}
- {action: replace, from: "^VgpuMetadata", to: "nvmlVgpuMetadata"}
- {action: replace, from: "^VgpuPgpuMetadata", to: "nvmlVgpuPgpuMetadata"}
- {action: replace, from: "^GpmMetricsGet", to: "GpmMetricsGetType"}
- {action: replace, from: "^GpmMetricsGet", to: "nvmlGpmMetricsGetType"}
function:
- {action: accept, from: "^nvml"}
- {action: replace, from: "^nvmlInit$", to: "nvmlInit_v1"}
Expand Down
67 changes: 63 additions & 4 deletions pkg/nvml/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,65 @@ import (
// EccBitType
type EccBitType = MemoryErrorType

// GpuInstanceInfo includes an interface type for Device instead of nvmlDevice
type GpuInstanceInfo struct {
Device Device
Id uint32
ProfileId uint32
Placement GpuInstancePlacement
}

func (g GpuInstanceInfo) convert() nvmlGpuInstanceInfo {
out := nvmlGpuInstanceInfo{
Device: g.Device.(nvmlDevice),
Id: g.Id,
ProfileId: g.ProfileId,
Placement: g.Placement,
}
return out
}

func (g nvmlGpuInstanceInfo) convert() GpuInstanceInfo {
out := GpuInstanceInfo{
Device: g.Device,
Id: g.Id,
ProfileId: g.ProfileId,
Placement: g.Placement,
}
return out
}

// ComputeInstanceInfo includes an interface type for Device instead of nvmlDevice
type ComputeInstanceInfo struct {
Device Device
GpuInstance GpuInstance
Id uint32
ProfileId uint32
Placement ComputeInstancePlacement
}

func (c ComputeInstanceInfo) convert() nvmlComputeInstanceInfo {
out := nvmlComputeInstanceInfo{
Device: c.Device.(nvmlDevice),
GpuInstance: c.GpuInstance.(nvmlGpuInstance),
Id: c.Id,
ProfileId: c.ProfileId,
Placement: c.Placement,
}
return out
}

func (c nvmlComputeInstanceInfo) convert() ComputeInstanceInfo {
out := ComputeInstanceInfo{
Device: c.Device,
GpuInstance: c.GpuInstance,
Id: c.Id,
ProfileId: c.ProfileId,
Placement: c.Placement,
}
return out
}

// nvml.DeviceGetCount()
func (l *library) DeviceGetCount() (int, Return) {
var deviceCount uint32
Expand Down Expand Up @@ -2113,9 +2172,9 @@ func (l *library) GpuInstanceGetInfo(gpuInstance GpuInstance) (GpuInstanceInfo,
}

func (gpuInstance nvmlGpuInstance) GetInfo() (GpuInstanceInfo, Return) {
var info GpuInstanceInfo
var info nvmlGpuInstanceInfo
ret := nvmlGpuInstanceGetInfo(gpuInstance, &info)
return info, ret
return info.convert(), ret
}

// nvml.GpuInstanceGetComputeInstanceProfileInfo()
Expand Down Expand Up @@ -2224,9 +2283,9 @@ func (l *library) ComputeInstanceGetInfo(computeInstance ComputeInstance) (Compu
}

func (computeInstance nvmlComputeInstance) GetInfo() (ComputeInstanceInfo, Return) {
var info ComputeInstanceInfo
var info nvmlComputeInstanceInfo
ret := nvmlComputeInstanceGetInfo(computeInstance, &info)
return info, ret
return info.convert(), ret
}

// nvml.DeviceIsMigDeviceHandle()
Expand Down
35 changes: 33 additions & 2 deletions pkg/nvml/event_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@

package nvml

// EventData includes an interface type for Device instead of nvmlDevice
type EventData struct {
Device Device
EventType uint64
EventData uint64
GpuInstanceId uint32
ComputeInstanceId uint32
}

func (e EventData) convert() nvmlEventData {
out := nvmlEventData{
Device: e.Device.(nvmlDevice),
EventType: e.EventType,
EventData: e.EventData,
GpuInstanceId: e.GpuInstanceId,
ComputeInstanceId: e.ComputeInstanceId,
}
return out
}

func (e nvmlEventData) convert() EventData {
out := EventData{
Device: e.Device,
EventType: e.EventType,
EventData: e.EventData,
GpuInstanceId: e.GpuInstanceId,
ComputeInstanceId: e.ComputeInstanceId,
}
return out
}

// nvml.EventSetCreate()
func (l *library) EventSetCreate() (EventSet, Return) {
var Set nvmlEventSet
Expand All @@ -27,9 +58,9 @@ func (l *library) EventSetWait(set EventSet, timeoutms uint32) (EventData, Retur
}

func (set nvmlEventSet) Wait(timeoutms uint32) (EventData, Return) {
var data EventData
var data nvmlEventData
ret := nvmlEventSetWait(set, &data, timeoutms)
return data, ret
return data.convert(), ret
}

// nvml.EventSetFree()
Expand Down
41 changes: 38 additions & 3 deletions pkg/nvml/gpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,48 @@

package nvml

// GpmMetricsGetType includes interface types for GpmSample instead of nvmlGpmSample
type GpmMetricsGetType struct {
Version uint32
NumMetrics uint32
Sample1 GpmSample
Sample2 GpmSample
Metrics [98]GpmMetric
}

func (g *GpmMetricsGetType) convert() *nvmlGpmMetricsGetType {
out := &nvmlGpmMetricsGetType{
Version: g.Version,
NumMetrics: g.NumMetrics,
Sample1: g.Sample1.(nvmlGpmSample),
Sample2: g.Sample2.(nvmlGpmSample),
}
for i := range g.Metrics {
out.Metrics[i] = g.Metrics[i]
}
return out
}

func (g *nvmlGpmMetricsGetType) convert() *GpmMetricsGetType {
out := &GpmMetricsGetType{
Version: g.Version,
NumMetrics: g.NumMetrics,
Sample1: g.Sample1,
Sample2: g.Sample2,
}
for i := range g.Metrics {
out.Metrics[i] = g.Metrics[i]
}
return out
}

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

func (l *library) GpmMetricsGetV(metricsGet *GpmMetricsGetType) GpmMetricsGetVType {
return GpmMetricsGetVType{metricsGet}
return GpmMetricsGetVType{metricsGet.convert()}
}
func (metricsGetV GpmMetricsGetVType) V1() Return {
metricsGetV.metricsGet.Version = 1
Expand All @@ -29,7 +64,7 @@ func (metricsGetV GpmMetricsGetVType) V1() Return {

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

// nvml.GpmSampleFree()
Expand Down
12 changes: 6 additions & 6 deletions pkg/nvml/nvml.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/nvml/types_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b34e706

Please sign in to comment.