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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
*.swo
tags
.idea/

# Ignore all dot files and directories
.*

# But keep these
!.gitignore
!.gitattributes
!.github/
!.gitlab/
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ variables:
CUDA_VERSION: "12.5.1"
DISTRO_FLAVOR: "ubuntu24.04"
GO_VERSION: "1.24.4"
DCGM_VERSION: "4.2.3-2"
DCGM_VERSION: "4.5.0-1"
# Image names
BUILD_IMAGE: "$CI_REGISTRY_IMAGE/build:$CI_COMMIT_SHA"
TEST_IMAGE: "$CI_REGISTRY_IMAGE/test:$CI_COMMIT_SHA"
Expand Down
51 changes: 51 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,57 @@
Want to hack on the NVIDIA DCGM Golang Bindings Project? Awesome!
We only require you to sign your work, the below section describes this!

## Updating DCGM Fields

When new fields are added to DCGM, you need to update the Go bindings. Follow these steps:

### 1. Update the dcgm_fields.h header file

Copy the latest `dcgm_fields.h` from the DCGM source repository:

```bash
# From the DCGM repository
cp /path/to/dcgm/dcgmlib/dcgm_fields.h pkg/dcgm/dcgm_fields.h
```

### 2. Generate Go constants

Run the code generator to update the Go field constants:

```bash
make generate
```

This will:
- Parse `pkg/dcgm/dcgm_fields.h`
- Generate `pkg/dcgm/const_fields.go` with all DCGM field constants and helper functions

### 3. Verify the generated code

Check that the generated code is correct:

```bash
make check-generate
```

This ensures the generated code is in sync with the header file.

### 4. Review the changes

Check what fields were added, removed, or modified:

```bash
git diff pkg/dcgm/const_fields.go
```

### 5. Test the changes

Run tests to ensure the bindings work correctly:

```bash
make test-main
```

## Validate your work

All changes need to be able to pass all linting and pre-commit checks. All tests
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ARG DISTRO_FLAVOR=ubuntu24.04

# Use build arguments to select our base image or just stick with the defaults above.
FROM nvidia/cuda:$CUDA_VERSION-base-$DISTRO_FLAVOR AS base
ARG DCGM_VERSION=4.4.2-1
ARG DCGM_VERSION=4.5.0-1
ARG GO_VERSION=1.25.5
ENV DEBIAN_FRONTEND=noninteractive

Expand Down
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@

GOLANGCILINT_TIMEOUT ?= 10m

.PHONY: all binary check-format install install-pre-commit
.PHONY: all binary check-format install install-pre-commit generate check-generate
all: binary test-main check-format

install-pre-commit:
@echo "Installing pre-commit hooks..."
pre-commit install --config .pre-commit-config.yaml
@echo "Pre-commit hooks installed."

binary:
generate:
@echo "Generating Go code from headers..."
go generate ./...

check-generate: generate
@echo "Checking if generated code is up to date..."
@git diff --exit-code pkg/dcgm/const_fields.go || \
(echo "Error: const_fields.go is out of sync. Run 'make generate'" && exit 1)

binary: generate
go build ./pkg/dcgm
cd samples/deviceInfo; go build
cd samples/dmon; go build
Expand All @@ -37,7 +46,7 @@ binary:
docker:
docker buildx bake default --load

test-main:
test-main: generate
go test -race -v ./tests
go test -v ./tests

Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ Golang bindings are provided for [NVIDIA Data Center GPU Manager (DCGM)](https:/

You will also find samples for these bindings in this repository.

## Development

### Generating Field Constants

The DCGM field constants in `pkg/dcgm/const_fields.go` are automatically generated from `pkg/dcgm/dcgm_fields.h`. To regenerate these constants after updating the header file:

```bash
make generate
```

To verify that the generated code is up to date:

```bash
make check-generate
```

See [CONTRIBUTING.md](CONTRIBUTING.md#updating-dcgm-fields) for detailed instructions on updating DCGM fields.

## Issues and Contributing

[Checkout the Contributing document!](CONTRIBUTING.md)
Expand Down
87 changes: 87 additions & 0 deletions cmd/gen-fields/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# DCGM Fields Generator

This tool generates Go constants from the DCGM C header file `dcgm_fields.h`.

## Overview

The generator parses `dcgm_fields.h` and extracts all DCGM field definitions (`DCGM_FI_*` constants), then generates a Go file with:

- Typed constants for each DCGM field
- Field name mappings for lookup by string name
- Helper functions (`GetFieldID`, `GetFieldIDOrPanic`, etc.)
- Legacy field mappings for backward compatibility

## Usage

The generator is typically invoked via `go generate` or `make generate`:

```bash
# Via Make
make generate

# Via go generate
go generate ./...
```

### Direct Usage

You can also run the generator directly:

```bash
go run cmd/gen-fields/main.go cmd/gen-fields/template.go \
pkg/dcgm/dcgm_fields.h \
pkg/dcgm/const_fields.go
```

Arguments:
1. Path to `dcgm_fields.h` (input)
2. Path to `const_fields.go` (output)

## How It Works

1. **Parse Header File**: Reads `dcgm_fields.h` and extracts all `#define DCGM_FI_*` definitions
2. **Extract Field Information**:
- Field name (e.g., `DCGM_FI_DEV_GPU_TEMP`)
- Field ID (numeric value)
- Field comment/description
3. **Generate Go Code**: Uses Go templates to create:
- Constant definitions: `DCGM_FI_DEV_GPU_TEMP Short = 150`
- Field name maps for string-based lookup
- Helper functions for field ID resolution

## Output

The generated `const_fields.go` file contains:

```go
const (
DCGM_FI_DEV_GPU_TEMP Short = 150
DCGM_FI_DEV_POWER_USAGE Short = 155
// ... etc
)

var dcgmFields = map[string]Short{
"dcgm_gpu_temp": 150,
"dcgm_power_usage": 155,
// ... etc
}

func GetFieldID(fieldName string) (Short, bool) { ... }
func GetFieldIDOrPanic(fieldName string) Short { ... }
```

## Template

The code generation template is defined in `template.go` and includes the full structure of the output Go file.

## Updating Fields

When DCGM adds new fields:

1. Update `pkg/dcgm/dcgm_fields.h` with the latest version from DCGM
2. Run `make generate`
3. Review the diff in `pkg/dcgm/const_fields.go`
4. Commit both the header and generated file

See [CONTRIBUTING.md](../../CONTRIBUTING.md#updating-dcgm-fields) for detailed instructions.

Loading