summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/windows/collect_cpu.go
blob: fc1412e3d69cac454c180b2c49b17fa4f8176c8a (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// SPDX-License-Identifier: GPL-3.0-or-later

package windows

import (
	"github.com/netdata/netdata/go/go.d.plugin/pkg/prometheus"
)

const (
	metricCPUTimeTotal       = "windows_cpu_time_total"
	metricCPUInterruptsTotal = "windows_cpu_interrupts_total"
	metricCPUDPCsTotal       = "windows_cpu_dpcs_total"
	metricCPUCStateTotal     = "windows_cpu_cstate_seconds_total"
)

func (w *Windows) collectCPU(mx map[string]int64, pms prometheus.Series) {
	if !w.cache.collection[collectorCPU] {
		w.cache.collection[collectorCPU] = true
		w.addCPUCharts()
	}

	seen := make(map[string]bool)
	for _, pm := range pms.FindByName(metricCPUTimeTotal) {
		core := pm.Labels.Get("core")
		mode := pm.Labels.Get("mode")
		if core == "" || mode == "" {
			continue
		}

		seen[core] = true
		mx["cpu_"+mode+"_time"] += int64(pm.Value * precision)
		mx["cpu_core_"+core+"_"+mode+"_time"] += int64(pm.Value * precision)
	}

	for _, pm := range pms.FindByName(metricCPUInterruptsTotal) {
		core := pm.Labels.Get("core")
		if core == "" {
			continue
		}

		seen[core] = true
		mx["cpu_core_"+core+"_interrupts"] += int64(pm.Value)
	}

	for _, pm := range pms.FindByName(metricCPUDPCsTotal) {
		core := pm.Labels.Get("core")
		if core == "" {
			continue
		}

		seen[core] = true
		mx["cpu_core_"+core+"_dpcs"] += int64(pm.Value)
	}

	for _, pm := range pms.FindByName(metricCPUCStateTotal) {
		core := pm.Labels.Get("core")
		state := pm.Labels.Get("state")
		if core == "" || state == "" {
			continue
		}

		seen[core] = true
		mx["cpu_core_"+core+"_cstate_"+state] += int64(pm.Value * precision)
	}

	for core := range seen {
		if !w.cache.cores[core] {
			w.cache.cores[core] = true
			w.addCPUCoreCharts(core)
		}
	}
	for core := range w.cache.cores {
		if !seen[core] {
			delete(w.cache.cores, core)
			w.removeCPUCoreCharts(core)
		}
	}
}