-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cpu_ctx_switches_linux.go
55 lines (48 loc) · 1.42 KB
/
cpu_ctx_switches_linux.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
53
54
55
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
package cpu
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/config"
)
func readCtxSwitches(procStatPath string) (ctxSwitches int64, err error) {
file, err := os.Open(procStatPath)
if err != nil {
return 0, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for i := 0; scanner.Scan(); i++ {
txt := scanner.Text()
if strings.HasPrefix(txt, "ctxt") {
elemts := strings.Split(txt, " ")
ctxSwitches, err = strconv.ParseInt(elemts[1], 10, 64)
if err != nil {
return 0, fmt.Errorf("%s in '%s' at line %d", err, procStatPath, i)
}
return ctxSwitches, nil
}
}
return 0, fmt.Errorf("could not find the context switches in stat file")
}
func (c *Check) collectCtxSwitches(sender sender.Sender) error {
procfsPath := "/proc"
if config.Datadog.IsSet("procfs_path") {
procfsPath = config.Datadog.GetString("procfs_path")
}
ctxSwitches, err := readCtxSwitches(filepath.Join(procfsPath, "/stat"))
if err != nil {
return err
}
sender.MonotonicCount("system.cpu.context_switches", float64(ctxSwitches), "", nil)
return nil
}