nvmltest

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package nvmltest scripts and records the small NVML query interface used by telemetry consumers. Replay makes no NVML calls. Recorded opaque handles are evidence values, not live handles usable with the driver.

Recordings distinguish measured hardware responses from synthetic fixtures. The package does not simulate GPU execution or qualify another GPU model. JSON field names are exact and may not be duplicated or aliased. Arguments include every parameter, preserving a nil ID list separately from an empty list. The 16 MiB limit includes provenance, framing and all calls. Error records exceeding 32 nested causes or 4096 cause nodes are refused.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func main() {
	script := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: "GpmSupported", Args: json.RawMessage(`{}`), Result: json.RawMessage(`true`)}}}
	device, err := nvmltest.NewReplay(script)
	if err != nil {
		panic(err)
	}
	supported, err := device.GpmSupported()
	fmt.Println(supported, err, device.Done())
}
Output:
true <nil> true
Example (RecordedReplay)
package main

import (
	"bytes"
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func main() {
	synthetic := nvmltest.Recording{
		Version:    nvmltest.Version,
		Provenance: nvmltest.Provenance{Source: "synthetic mock"},
		Calls: []nvmltest.Call{
			{Method: "GpmSupported", Args: json.RawMessage(`{}`), Result: json.RawMessage(`true`)},
		},
	}
	device, err := nvmltest.NewReplay(synthetic)
	if err != nil {
		panic(err)
	}

	recorder, err := nvmltest.NewRecorder(device, nvmltest.Provenance{Source: "recorded session"})
	if err != nil {
		panic(err)
	}
	supported, err := recorder.GpmSupported()
	if err != nil {
		panic(err)
	}

	rec, err := recorder.Recording()
	if err != nil {
		panic(err)
	}
	var buf bytes.Buffer
	if err := nvmltest.Write(&buf, rec); err != nil {
		panic(err)
	}

	loaded, err := nvmltest.Read(&buf)
	if err != nil {
		panic(err)
	}
	replay, err := nvmltest.NewReplay(loaded)
	if err != nil {
		panic(err)
	}
	replayedSupported, err := replay.GpmSupported()
	fmt.Printf("recorded=%v replayed=%v done=%v\n", supported, replayedSupported, replay.Done())
}
Output:
recorded=true replayed=true done=true

Index

Examples

Constants

View Source
const Version = 1

Version is the recording schema version.

Variables

This section is empty.

Functions

func Write

func Write(w io.Writer, rec Recording) error

Write validates and writes one recording. It does not close w.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func main() {
	var output bytes.Buffer
	err := nvmltest.Write(&output, nvmltest.Recording{Version: nvmltest.Version})
	rec, readErr := nvmltest.Read(&output)
	fmt.Println(rec.Version, err, readErr)
}
Output:
1 <nil> <nil>

Types

type Call

type Call struct {
	Method string          `json:"method"`
	Args   json.RawMessage `json:"args"`
	Result json.RawMessage `json:"result"`
	Error  *Failure        `json:"error,omitempty"`
}

Call is one ordered invocation. Args and Result are typed JSON values for Method. Per-entry status fields remain inside Result.

type Failure

type Failure struct {
	Kind    string      `json:"kind"`
	Message string      `json:"message"`
	Code    nvml.Result `json:"code,omitempty"`
	Symbol  string      `json:"symbol,omitempty"`
	Causes  []*Failure  `json:"causes,omitempty"`
}

Failure preserves NVML error classification and wrapped/joined causes. Kind is result, symbol, wrap, join or other. Other errors preserve text.

func (*Failure) Error

func (f *Failure) Error() string
Example
package main

import (
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func main() {
	f := &nvmltest.Failure{Kind: "other", Message: "query failed"}
	fmt.Println(f.Error())
}
Output:
query failed

func (*Failure) Unwrap

func (f *Failure) Unwrap() []error

Unwrap reconstructs the underlying classified errors.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml"
	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func main() {
	f := &nvmltest.Failure{Kind: "result", Message: "not supported", Code: nvml.ErrNotSupported}
	fmt.Println(errors.Is(f, nvml.ErrNotSupported))
}
Output:
true

type Provenance

type Provenance struct {
	GPU      string `json:"gpu"`
	Driver   string `json:"driver"`
	MIG      string `json:"mig"`
	Source   string `json:"source"`
	Measured bool   `json:"measured"`
}

Provenance identifies the source of responses. Measured recordings require GPU, driver and source identities; MIG records the observed mode, including unavailable. Synthetic scripts must set Measured to false.

type Queries

type Queries interface {
	GpmSupported() (bool, error)
	ProcessesUtilization(uint64) ([]nvml.ProcessUtilizationInfoV1, error)
	FabricInfo() (nvml.GpuFabricInfoV, error)
	FieldValuesFor(...uint32) ([]nvml.FieldValue, error)
	GpuInstanceList(uint32) ([]nvml.GpuInstance, error)
}

Queries is the selected consumer interface. The compile-time assertion against nvml.Device detects signature drift without mirroring every raw call.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml"
	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

// inspectDevice summarizes telemetry from any Queries implementation.
func inspectDevice(q nvmltest.Queries) (bool, int, nvml.Result, error) {
	supported, err := q.GpmSupported()
	if err != nil && !nvml.IsUnsupported(err) {
		return false, 0, 0, err
	}
	procs, err := q.ProcessesUtilization(0)
	if err != nil && !nvml.IsUnsupported(err) {
		return false, 0, 0, err
	}
	fabric, err := q.FabricInfo()
	if err != nil && !nvml.IsUnsupported(err) {
		return false, 0, 0, err
	}
	return supported, len(procs), fabric.Status, nil
}

func main() {
	rec := nvmltest.Recording{
		Version:    nvmltest.Version,
		Provenance: nvmltest.Provenance{Source: "synthetic example"},
		Calls: []nvmltest.Call{
			{Method: "GpmSupported", Args: json.RawMessage(`{}`), Result: json.RawMessage(`true`)},
			{Method: "ProcessesUtilization", Args: json.RawMessage(`{"LastSeen":0}`), Result: json.RawMessage(`[{"Pid":101,"SmUtil":12},{"Pid":102,"SmUtil":45}]`)},
			{Method: "FabricInfo", Args: json.RawMessage(`{}`), Result: json.RawMessage(`{"CliqueId":1,"Status":0}`)},
		},
	}
	device, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}

	supported, activePids, status, err := inspectDevice(device)
	if err != nil {
		panic(err)
	}
	fmt.Printf("gpm=%v procs=%d fabric=%d done=%v\n", supported, activePids, int32(status), device.Done())
}
Output:
gpm=true procs=2 fabric=0 done=true

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

Recorder serializes queries and retains their responses in call order. It does not initialize or shut down the caller's NVML instance. The zero value is not usable; call NewRecorder.

func NewRecorder

func NewRecorder(device Queries, p Provenance) (*Recorder, error)

NewRecorder records device queries with the given provenance.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func main() {
	q := exampleQuery("GpmSupported", `{}`, `true`)
	r, err := nvmltest.NewRecorder(q, nvmltest.Provenance{Source: "synthetic example"})
	if err != nil {
		panic(err)
	}
	supported, err := r.GpmSupported()
	fmt.Println(supported, err)
}
Output:
true <nil>

func (*Recorder) FabricInfo

func (r *Recorder) FabricInfo() (v nvml.GpuFabricInfoV, err error)

FabricInfo records the underlying fabric query.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func exampleRecorder(method, args, result string) *nvmltest.Recorder {
	r, err := nvmltest.NewRecorder(exampleQuery(method, args, result), nvmltest.Provenance{Source: "synthetic example"})
	if err != nil {
		panic(err)
	}
	return r
}

func main() {
	q := exampleRecorder("FabricInfo", `{}`, `{"CliqueId":11}`)
	v, err := q.FabricInfo()
	fmt.Println(v.CliqueId, err)
}
Output:
11 <nil>

func (*Recorder) FieldValuesFor

func (r *Recorder) FieldValuesFor(ids ...uint32) (v []nvml.FieldValue, err error)

FieldValuesFor records the query and ordered field IDs.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func exampleRecorder(method, args, result string) *nvmltest.Recorder {
	r, err := nvmltest.NewRecorder(exampleQuery(method, args, result), nvmltest.Provenance{Source: "synthetic example"})
	if err != nil {
		panic(err)
	}
	return r
}

func main() {
	q := exampleRecorder("FieldValuesFor", `{"IDs":[71]}`, `[{"FieldId":71,"NvmlReturn":4}]`)
	v, err := q.FieldValuesFor(71)
	fmt.Println(v[0].FieldId, int32(v[0].NvmlReturn), err)
}
Output:
71 4 <nil>

func (*Recorder) GpmSupported

func (r *Recorder) GpmSupported() (v bool, err error)

GpmSupported records the underlying support query.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func exampleRecorder(method, args, result string) *nvmltest.Recorder {
	r, err := nvmltest.NewRecorder(exampleQuery(method, args, result), nvmltest.Provenance{Source: "synthetic example"})
	if err != nil {
		panic(err)
	}
	return r
}

func main() {
	q := exampleRecorder("GpmSupported", `{}`, `true`)
	v, err := q.GpmSupported()
	fmt.Println(v, err)
}
Output:
true <nil>

func (*Recorder) GpuInstanceList

func (r *Recorder) GpuInstanceList(profileID uint32) (v []nvml.GpuInstance, err error)

GpuInstanceList records the profile query.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func exampleRecorder(method, args, result string) *nvmltest.Recorder {
	r, err := nvmltest.NewRecorder(exampleQuery(method, args, result), nvmltest.Provenance{Source: "synthetic example"})
	if err != nil {
		panic(err)
	}
	return r
}

func main() {
	q := exampleRecorder("GpuInstanceList", `{"ProfileID":3}`, `[37]`)
	v, err := q.GpuInstanceList(3)
	fmt.Println(uintptr(v[0]), err)
}
Output:
37 <nil>

func (*Recorder) ProcessesUtilization

func (r *Recorder) ProcessesUtilization(lastSeen uint64) (v []nvml.ProcessUtilizationInfoV1, err error)

ProcessesUtilization records the query and timestamp cursor.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func exampleRecorder(method, args, result string) *nvmltest.Recorder {
	r, err := nvmltest.NewRecorder(exampleQuery(method, args, result), nvmltest.Provenance{Source: "synthetic example"})
	if err != nil {
		panic(err)
	}
	return r
}

func main() {
	q := exampleRecorder("ProcessesUtilization", `{"LastSeen":7}`, `[{"Pid":42,"SmUtil":19}]`)
	v, err := q.ProcessesUtilization(7)
	fmt.Println(v[0].Pid, v[0].SmUtil, err)
}
Output:
42 19 <nil>

func (*Recorder) Recording

func (r *Recorder) Recording() (Recording, error)

Recording returns a deep copy of the calls recorded so far.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func exampleRecorder(method, args, result string) *nvmltest.Recorder {
	r, err := nvmltest.NewRecorder(exampleQuery(method, args, result), nvmltest.Provenance{Source: "synthetic example"})
	if err != nil {
		panic(err)
	}
	return r
}

func main() {
	r := exampleRecorder("GpmSupported", `{}`, `true`)
	r.GpmSupported()
	rec, err := r.Recording()
	fmt.Println(rec.Version, len(rec.Calls), rec.Provenance.Measured, err)
}
Output:
1 1 false <nil>

type Recording

type Recording struct {
	Version    int        `json:"version"`
	Provenance Provenance `json:"provenance"`
	Calls      []Call     `json:"calls"`
}

Recording is a versioned ordered script with source provenance.

func Read

func Read(r io.Reader) (Recording, error)

Read reads one recording, rejecting unknown schema fields, trailing input and recordings larger than 16 MiB or 4096 calls.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func main() {
	rec, err := nvmltest.Read(strings.NewReader(`{"version":1,"provenance":{},"calls":[]}`))
	fmt.Println(rec.Version, len(rec.Calls), err)
}
Output:
1 0 <nil>

type Replay

type Replay struct {
	// contains filtered or unexported fields
}

Replay is a race-safe script. Calls must arrive in recording order with matching arguments. A mismatch does not consume the next scripted call.

func NewReplay

func NewReplay(rec Recording) (*Replay, error)

NewReplay validates and deep-copies a recording.

Example
package main

import (
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func main() {
	q, err := nvmltest.NewReplay(nvmltest.Recording{Version: nvmltest.Version})
	fmt.Println(q.Done(), err)
}
Output:
true <nil>

func (*Replay) Done

func (r *Replay) Done() bool

Done reports whether all scripted calls were consumed.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func main() {
	q := exampleQuery("GpmSupported", `{}`, `true`)
	fmt.Println(q.Done())
	q.GpmSupported()
	fmt.Println(q.Done())
}
Output:
false
true

func (*Replay) FabricInfo

func (r *Replay) FabricInfo() (v nvml.GpuFabricInfoV, err error)

FabricInfo replays the next fabric query.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func main() {
	q := exampleQuery("FabricInfo", `{}`, `{"CliqueId":11}`)
	v, err := q.FabricInfo()
	fmt.Println(v.CliqueId, err)
}
Output:
11 <nil>

func (*Replay) FieldValuesFor

func (r *Replay) FieldValuesFor(ids ...uint32) (v []nvml.FieldValue, err error)

FieldValuesFor replays a query with fields in the requested order.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func main() {
	q := exampleQuery("FieldValuesFor", `{"IDs":[71]}`, `[{"FieldId":71,"NvmlReturn":4}]`)
	v, err := q.FieldValuesFor(71)
	fmt.Println(v[0].FieldId, int32(v[0].NvmlReturn), err)
}
Output:
71 4 <nil>

func (*Replay) GpmSupported

func (r *Replay) GpmSupported() (v bool, err error)

GpmSupported replays the next support query.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func main() {
	q := exampleQuery("GpmSupported", `{}`, `true`)
	v, err := q.GpmSupported()
	fmt.Println(v, err)
}
Output:
true <nil>

func (*Replay) GpuInstanceList

func (r *Replay) GpuInstanceList(profileID uint32) (v []nvml.GpuInstance, err error)

GpuInstanceList replays a query for profileID.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func main() {
	q := exampleQuery("GpuInstanceList", `{"ProfileID":3}`, `[37]`)
	v, err := q.GpuInstanceList(3)
	fmt.Println(uintptr(v[0]), err)
}
Output:
37 <nil>

func (*Replay) ProcessesUtilization

func (r *Replay) ProcessesUtilization(lastSeen uint64) (v []nvml.ProcessUtilizationInfoV1, err error)

ProcessesUtilization replays a query with the exact timestamp cursor.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/tmc/lib/nvidia/nvml/nvmltest"
)

func exampleQuery(method, args, result string) *nvmltest.Replay {
	rec := nvmltest.Recording{Version: nvmltest.Version, Provenance: nvmltest.Provenance{Source: "synthetic example"}, Calls: []nvmltest.Call{{Method: method, Args: json.RawMessage(args), Result: json.RawMessage(result)}}}
	q, err := nvmltest.NewReplay(rec)
	if err != nil {
		panic(err)
	}
	return q
}

func main() {
	q := exampleQuery("ProcessesUtilization", `{"LastSeen":7}`, `[{"Pid":42,"SmUtil":19}]`)
	v, err := q.ProcessesUtilization(7)
	fmt.Println(v[0].Pid, v[0].SmUtil, err)
}
Output:
42 19 <nil>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL