master
go 96 lines 1.78 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package dcgm
4
5 import "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
6
7 const (
8 maxNotSeenCharts = 10
9 maxNotSeenDims = 10
10 )
11
12 type (
13 cache struct {
14 charts map[string]*cacheChart
15 }
16
17 cacheChart struct {
18 chart *collectorapi.Chart
19 seen bool
20 notSeenTimes int
21 dims map[string]*cacheDim
22 }
23
24 cacheDim struct {
25 seen bool
26 notSeenTimes int
27 }
28 )
29
30 func newCache() *cache {
31 return &cache{charts: make(map[string]*cacheChart)}
32 }
33
34 func (c *cache) reset() {
35 for _, ch := range c.charts {
36 ch.seen = false
37 for _, d := range ch.dims {
38 d.seen = false
39 }
40 }
41 }
42
43 func (c *cache) getChart(key string) (*cacheChart, bool) {
44 v, ok := c.charts[key]
45 if !ok {
46 return nil, false
47 }
48 v.seen = true
49 v.notSeenTimes = 0
50 return v, true
51 }
52
53 func (c *cache) putChart(key string, chart *collectorapi.Chart) *cacheChart {
54 v := &cacheChart{chart: chart, seen: true, dims: make(map[string]*cacheDim)}
55 c.charts[key] = v
56 return v
57 }
58
59 func (ch *cacheChart) touchDim(dimID string) (exists bool) {
60 if d, ok := ch.dims[dimID]; ok {
61 d.seen = true
62 d.notSeenTimes = 0
63 return true
64 }
65 ch.dims[dimID] = &cacheDim{seen: true}
66 return false
67 }
68
69 func (c *Collector) removeStaleChartsAndDims() {
70 for key, ch := range c.cache.charts {
71 if !ch.seen {
72 ch.notSeenTimes++
73 if ch.notSeenTimes >= maxNotSeenCharts {
74 ch.chart.MarkRemove()
75 ch.chart.MarkNotCreated()
76 delete(c.cache.charts, key)
77 }
78 continue
79 }
80
81 for dimID, d := range ch.dims {
82 if d.seen {
83 d.notSeenTimes = 0
84 continue
85 }
86 d.notSeenTimes++
87 if d.notSeenTimes >= maxNotSeenDims {
88 if err := ch.chart.MarkDimRemove(dimID, false); err != nil {
89 c.Warning(err)
90 }
91 ch.chart.MarkNotCreated()
92 delete(ch.dims, dimID)
93 }
94 }
95 }
96 }