master
go 102 lines 1.76 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package intelgpu
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "fmt"
10 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
11 )
12
13 //go:embed "config_schema.json"
14 var configSchema string
15
16 func init() {
17 collectorapi.Register("intelgpu", collectorapi.Creator{
18 JobConfigSchema: configSchema,
19 Create: func() collectorapi.CollectorV1 { return New() },
20 Config: func() any { return &Config{} },
21 })
22 }
23
24 func New() *Collector {
25 return &Collector{
26 ndsudoName: "ndsudo",
27 charts: charts.Copy(),
28 engines: make(map[string]bool),
29 }
30 }
31
32 type Config struct {
33 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
34 Device string `yaml:"device,omitempty" json:"device"`
35 }
36
37 type Collector struct {
38 collectorapi.Base
39 Config `yaml:",inline" json:""`
40
41 charts *collectorapi.Charts
42
43 exec intelGpuTop
44 ndsudoName string
45
46 engines map[string]bool
47 }
48
49 func (c *Collector) Configuration() any {
50 return c.Config
51 }
52
53 func (c *Collector) Init(context.Context) error {
54 topExec, err := c.initIntelGPUTopExec()
55
56 if err != nil {
57 return fmt.Errorf("init intelgpu top exec: %v", err)
58 }
59
60 c.exec = topExec
61
62 return nil
63 }
64
65 func (c *Collector) Check(context.Context) error {
66 mx, err := c.collect()
67 if err != nil {
68 return err
69 }
70
71 if len(mx) == 0 {
72 return errors.New("no metrics collected")
73 }
74
75 return nil
76 }
77
78 func (c *Collector) Charts() *collectorapi.Charts {
79 return c.charts
80 }
81
82 func (c *Collector) Collect(context.Context) map[string]int64 {
83 mx, err := c.collect()
84 if err != nil {
85 c.Error(err)
86 }
87
88 if len(mx) == 0 {
89 return nil
90 }
91
92 return mx
93 }
94
95 func (c *Collector) Cleanup(context.Context) {
96 if c.exec != nil {
97 if err := c.exec.stop(); err != nil {
98 c.Error(err)
99 }
100 c.exec = nil
101 }
102 }