Skip to content

Commit

Permalink
Merge branch 'reformat-go' into 'master'
Browse files Browse the repository at this point in the history
Reformat go

See merge request nvidia/container-toolkit/gpu-monitoring-tools!12
  • Loading branch information
RenaudWasTaken committed May 5, 2020
2 parents a29767f + 2f67009 commit 936349a
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 96 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ GOLANG_VERSION := 1.14.2
VERSION := 2.0.0-rc.7
FULL_VERSION := $(DCGM_VERSION)-$(VERSION)

.PHONY: all binary install
.PHONY: all binary install check-format
all: ubuntu18.04 ubi8

binary:
Expand All @@ -32,6 +32,9 @@ install: binary
install -m 557 -D ./etc/dcgm-exporter/default-counters.csv /etc/dcgm-exporter/default-counters.csv
install -m 557 -D ./etc/dcgm-exporter/dcp-metrics-included.csv /etc/dcgm-exporter/dcp-metrics-included.csv

check-format:
test $$(gofmt -l pkg bindings | tee /dev/stderr | wc -l) -eq 0

push:
$(DOCKER) push "$(REGISTRY)/dcgm-exporter:$(FULL_VERSION)-ubuntu18.04"
$(DOCKER) push "$(REGISTRY)/dcgm-exporter:$(FULL_VERSION)-ubi8"
Expand Down
4 changes: 2 additions & 2 deletions bindings/go/dcgm/device_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

type PCIInfo struct {
BusID string
BAR1 uint // MB
FBTotal uint // MB
BAR1 uint // MB
FBTotal uint // MB
Bandwidth int64 // MB/s
}

Expand Down
3 changes: 1 addition & 2 deletions bindings/go/dcgm/device_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type PCIStatusInfo struct {

type DeviceStatus struct {
Power float64 // W
Temperature int64 // °C
Temperature int64 // °C
Utilization UtilizationInfo
Memory MemoryInfo
Clocks ClockInfo
Expand Down Expand Up @@ -111,7 +111,6 @@ func latestValuesForDevice(gpuId uint) (status DeviceStatus, err error) {
deviceFields[pstate] = C.DCGM_FI_DEV_PSTATE
deviceFields[fanSpeed] = C.DCGM_FI_DEV_FAN_SPEED


fieldsName := fmt.Sprintf("devStatusFields%d", rand.Uint64())
fieldsId, err := FieldGroupCreate(fieldsName, deviceFields)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions bindings/go/dcgm/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func toFieldValue(cfields []C.dcgmFieldValue_v1) []FieldValue_v1 {
fields := make([]FieldValue_v1, len(cfields))
for i, f := range cfields {
fields[i] = FieldValue_v1{
Version: uint(f.version),
FieldId: uint(f.fieldId),
Version: uint(f.version),
FieldId: uint(f.fieldId),
FieldType: uint(f.fieldType),
Status: int(f.status),
Ts: int64(f.ts),
Value: f.value,
Status: int(f.status),
Ts: int64(f.ts),
Value: f.value,
}
}

Expand All @@ -125,6 +125,6 @@ func (fv FieldValue_v1) String() string {
return *(*string)(unsafe.Pointer(&fv.Value[0]))
}

func (fv FieldValue_v1) Blob() [4096]byte{
func (fv FieldValue_v1) Blob() [4096]byte {
return fv.Value
}
2 changes: 1 addition & 1 deletion docker/Dockerfile.ubi8
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WORKDIR /go/src/github.com/NVIDIA/gpu-monitoring-tools

COPY . .

RUN make binary
RUN make binary check-format

FROM registry.access.redhat.com/ubi8:latest
LABEL io.k8s.display-name="NVIDIA DCGM Exporter"
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.ubuntu18.04
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WORKDIR /go/src/github.com/NVIDIA/gpu-monitoring-tools

COPY . .

RUN make binary
RUN make binary check-format

FROM ubuntu:18.04
LABEL io.k8s.display-name="NVIDIA DCGM Exporter"
Expand Down
5 changes: 2 additions & 3 deletions pkg/dcgm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
func NewGroup() (dcgm.GroupHandle, func(), error) {
group, err := dcgm.NewDefaultGroup(fmt.Sprintf("gpu-collector-group-%d", rand.Uint64()))
if err != nil {
return dcgm.GroupHandle{}, func(){}, err
return dcgm.GroupHandle{}, func() {}, err
}

return group, func() { dcgm.DestroyGroup(group) }, nil
Expand All @@ -45,7 +45,7 @@ func NewFieldGroup(deviceFields []dcgm.Short) (dcgm.FieldHandle, func(), error)
name := fmt.Sprintf("gpu-collector-fieldgroup-%d", rand.Uint64())
fieldGroup, err := dcgm.FieldGroupCreate(name, deviceFields)
if err != nil {
return dcgm.FieldHandle{}, func(){}, err
return dcgm.FieldHandle{}, func() {}, err
}

return fieldGroup, func() { dcgm.FieldGroupDestroy(fieldGroup) }, nil
Expand Down Expand Up @@ -95,4 +95,3 @@ fail:

return nil, err
}

9 changes: 4 additions & 5 deletions pkg/gpu_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (

func NewDCGMCollector(c []Counter) (*DCGMCollector, func(), error) {
collector := &DCGMCollector{
Counters: c,
Counters: c,
DeviceFields: NewDeviceFields(c),
}

cleanups, err := SetupDcgmFieldsWatch(collector.DeviceFields)
if err != nil {
return nil, func(){}, err
return nil, func() {}, err
}

collector.Cleanups = cleanups
Expand Down Expand Up @@ -73,10 +73,10 @@ func ToMetric(values []dcgm.FieldValue_v1, c []Counter, d dcgm.Device) []Metric

for i, val := range values {
metrics[i] = Metric{
Name: c[i].FieldName,
Name: c[i].FieldName,
Value: ToString(val),

GPU: fmt.Sprintf("%d", d.GPU),
GPU: fmt.Sprintf("%d", d.GPU),
GPUUUID: d.UUID,

Attributes: map[string]string{},
Expand All @@ -101,4 +101,3 @@ func ToString(value dcgm.FieldValue_v1) string {

return FailedToConvert
}

3 changes: 1 addition & 2 deletions pkg/gpu_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ var sampleCounters = []Counter{
{dcgm.DCGM_FI_DEV_TOTAL_ENERGY_CONSUMPTION, "DCGM_FI_DEV_TOTAL_ENERGY_CONSUMPTION", "gauge", "Energy help info"},
{dcgm.DCGM_FI_DEV_POWER_USAGE, "DCGM_FI_DEV_POWER_USAGE", "gauge", "Power help info"},
}


func TestDCGMCollector(t *testing.T) {
cleanup, err := dcgm.Init(dcgm.Embedded);
cleanup, err := dcgm.Init(dcgm.Embedded)
require.NoError(t, err)
defer cleanup()

Expand Down
7 changes: 3 additions & 4 deletions pkg/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ import (
"context"
"fmt"
"net"
"time"
"os"
"time"

"google.golang.org/grpc"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
podresourcesapi "k8s.io/kubernetes/pkg/kubelet/apis/podresources/v1alpha1"
)

var (
socketDir = "/var/lib/kubelet/pod-resources"
socketPath = socketDir + "/kubelet.sock"

connectionTimeout = 10 * time.Second
connectionTimeout = 10 * time.Second
)

func NewPodMapper(c *Config) *PodMapper {
Expand All @@ -52,7 +52,6 @@ func (p *PodMapper) Process(metrics [][]Metric) error {
return nil
}


// TODO: This needs to be moved out of the critical path.
c, cleanup, err := connectToServer(socketPath)
if err != nil {
Expand Down
33 changes: 16 additions & 17 deletions pkg/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
package main

import (
"fmt"
"testing"
"context"
"time"
"fmt"
"io/ioutil"
"os"
"testing"
"time"

"github.com/NVIDIA/gpu-monitoring-tools/bindings/go/dcgm"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
podresourcesapi "k8s.io/kubernetes/pkg/kubelet/apis/podresources/v1alpha1"
"github.com/NVIDIA/gpu-monitoring-tools/bindings/go/dcgm"
"k8s.io/kubernetes/pkg/kubelet/util"
"google.golang.org/grpc"

)

var tmpDir string
Expand All @@ -38,7 +37,7 @@ func TestProcessPodMapper(t *testing.T) {
cleanup := CreateTmpDir(t)
defer cleanup()

cleanup, err := dcgm.Init(dcgm.Embedded);
cleanup, err := dcgm.Init(dcgm.Embedded)
require.NoError(t, err)
defer cleanup()

Expand Down Expand Up @@ -77,9 +76,9 @@ func TestProcessPodMapper(t *testing.T) {
}
}

func GetGPUUUIDs(metrics [][]Metric) []string{
func GetGPUUUIDs(metrics [][]Metric) []string {
gpus := make([]string, len(metrics))
for i, dev := range metrics{
for i, dev := range metrics {
gpus[i] = dev[0].GPUUUID
}

Expand All @@ -100,10 +99,10 @@ func StartMockServer(t *testing.T, server *grpc.Server, socket string) func() {
return func() {
server.Stop()
select {
case <-stopped:
return
case <-time.After(1 * time.Second):
t.Fatal("Failed waiting for gRPC server to stop")
case <-stopped:
return
case <-time.After(1 * time.Second):
t.Fatal("Failed waiting for gRPC server to stop")
}
}
}
Expand Down Expand Up @@ -135,15 +134,15 @@ func (s *PodResourcesMockServer) List(ctx context.Context, req *podresourcesapi.

for i, gpu := range s.gpus {
podResources[i] = &podresourcesapi.PodResources{
Name: fmt.Sprintf("gpu-pod-%d", i),
Namespace: "default",
Name: fmt.Sprintf("gpu-pod-%d", i),
Namespace: "default",
Containers: []*podresourcesapi.ContainerResources{
&podresourcesapi.ContainerResources{
Name: "default",
Name: "default",
Devices: []*podresourcesapi.ContainerDevices{
&podresourcesapi.ContainerDevices{
ResourceName: nvidiaResourceName,
DeviceIds: []string{gpu},
DeviceIds: []string{gpu},
},
},
},
Expand Down
22 changes: 11 additions & 11 deletions pkg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ package main

import (
"os"
"syscall"
"sync"
"syscall"
"time"

"github.com/NVIDIA/gpu-monitoring-tools/bindings/go/dcgm"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/NVIDIA/gpu-monitoring-tools/bindings/go/dcgm"
)

var (
BuildVersion = "Filled by the build system"

CLIFieldsFile = "collectors"
CLIPort = "port"
CLIFieldsFile = "collectors"
CLIPort = "port"
CLICollectInterval = "collect-interval"
CLIKubernetes = "kubernetes"
CLIKubernetes = "kubernetes"
)

func main() {
Expand Down Expand Up @@ -83,7 +83,7 @@ func main() {
}

func Run(c *cli.Context) error {
restart:
restart:

logrus.Info("Starting dcgm-exporter")
config := contextToConfig(c)
Expand Down Expand Up @@ -122,7 +122,7 @@ func Run(c *cli.Context) error {
select {
case sig := <-sigs:
close(stop)
err := WaitWithTimeout(&wg, time.Second * 2)
err := WaitWithTimeout(&wg, time.Second*2)
if err != nil {
logrus.Fatal(err)
}
Expand All @@ -139,10 +139,10 @@ func Run(c *cli.Context) error {
}

func contextToConfig(c *cli.Context) *Config {
return &Config {
CollectorsFile: c.String(CLIFieldsFile),
Port: c.Int(CLIPort),
return &Config{
CollectorsFile: c.String(CLIFieldsFile),
Port: c.Int(CLIPort),
CollectInterval: c.Int(CLICollectInterval),
Kubernetes: c.Bool(CLIKubernetes),
Kubernetes: c.Bool(CLIKubernetes),
}
}
5 changes: 2 additions & 3 deletions pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
package main

import (
"encoding/csv"
"fmt"
"os"
"strings"
"encoding/csv"

"github.com/sirupsen/logrus"
"github.com/NVIDIA/gpu-monitoring-tools/bindings/go/dcgm"
"github.com/sirupsen/logrus"
)


func ExtractCounters(filename string) ([]Counter, error) {
records, err := ReadCSVFile(filename)
if err != nil {
Expand Down
Loading

0 comments on commit 936349a

Please sign in to comment.