forked from letmutx/nomad-nvidia-vgpu-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
52 lines (41 loc) · 1.1 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package device
import (
"context"
"fmt"
"github.com/hashicorp/nomad/plugins/device"
)
// doStats is the long running goroutine that streams device statistics
func (d *NvidiaVgpuPlugin) doStats(ctx context.Context, nvStats <-chan *device.StatsResponse, stats chan<- *device.StatsResponse) {
defer close(stats)
for {
select {
case <-ctx.Done():
return
case nvStat := <-nvStats:
stats <- d.nvStatsToVirtstats(nvStat)
}
}
}
func (d *NvidiaVgpuPlugin) nvStatsToVirtstats(nvStats *device.StatsResponse) *device.StatsResponse {
if nvStats.Error != nil {
return nvStats
}
var virtStats device.StatsResponse
for _, nvStatGroup := range nvStats.Groups {
group := &device.DeviceGroupStats{
Name: nvStatGroup.Name,
Type: nvStatGroup.Type,
Vendor: vendor,
}
instanceStats := map[string]*device.DeviceStats{}
for dev, stats := range group.InstanceStats {
for i := 0; i < d.vgpus; i++ {
dev := fmt.Sprintf("%s-%d", dev, i)
instanceStats[dev] = stats
}
}
group.InstanceStats = instanceStats
virtStats.Groups = append(virtStats.Groups, group)
}
return &virtStats
}