File: system_cpu.go

package info (click to toggle)
golang-procfs 0%2Bgit20181204.1dc9a6c-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports, buster-backports-sloppy
  • size: 504 kB
  • sloc: sh: 317; makefile: 20
file content (141 lines) | stat: -rw-r--r-- 3,898 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !windows

package sysfs

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/prometheus/procfs/internal/util"
)

// SystemCPUCpufreqStats contains stats from devices/system/cpu/cpu[0-9]*/cpufreq/...
type SystemCPUCpufreqStats struct {
	Name               string
	CurrentFrequency   uint64
	MinimumFrequency   uint64
	MaximumFrequency   uint64
	TransitionLatency  uint64
	AvailableGovernors string
	Driver             string
	Govenor            string
	RelatedCpus        string
	SetSpeed           string
}

// TODO: Add topology support.

// TODO: Add thermal_throttle support.

// NewSystemCpufreq returns CPU frequency metrics for all CPUs.
func NewSystemCpufreq() ([]SystemCPUCpufreqStats, error) {
	fs, err := NewFS(DefaultMountPoint)
	if err != nil {
		return []SystemCPUCpufreqStats{}, err
	}

	return fs.NewSystemCpufreq()
}

// NewSystemCpufreq returns CPU frequency metrics for all CPUs.
func (fs FS) NewSystemCpufreq() ([]SystemCPUCpufreqStats, error) {
	var cpufreq = &SystemCPUCpufreqStats{}

	cpus, err := filepath.Glob(fs.Path("devices/system/cpu/cpu[0-9]*"))
	if err != nil {
		return []SystemCPUCpufreqStats{}, err
	}

	systemCpufreq := []SystemCPUCpufreqStats{}
	for _, cpu := range cpus {
		cpuName := filepath.Base(cpu)
		cpuNum := strings.TrimPrefix(cpuName, "cpu")

		cpuCpufreqPath := filepath.Join(cpu, "cpufreq")
		if _, err := os.Stat(cpuCpufreqPath); os.IsNotExist(err) {
			continue
		}
		if err != nil {
			return []SystemCPUCpufreqStats{}, err
		}

		if _, err = os.Stat(filepath.Join(cpuCpufreqPath, "scaling_cur_freq")); err == nil {
			cpufreq, err = parseCpufreqCpuinfo("scaling", cpuCpufreqPath)
		} else if _, err = os.Stat(filepath.Join(cpuCpufreqPath, "cpuinfo_cur_freq")); err == nil {
			// Older kernels have metrics named `cpuinfo_...`.
			cpufreq, err = parseCpufreqCpuinfo("cpuinfo", cpuCpufreqPath)
		} else {
			return []SystemCPUCpufreqStats{}, fmt.Errorf("CPU %v is missing cpufreq", cpu)
		}
		if err != nil {
			return []SystemCPUCpufreqStats{}, err
		}
		cpufreq.Name = cpuNum
		systemCpufreq = append(systemCpufreq, *cpufreq)
	}

	return systemCpufreq, nil
}

func parseCpufreqCpuinfo(prefix string, cpuPath string) (*SystemCPUCpufreqStats, error) {
	uintFiles := []string{
		prefix + "_cur_freq",
		prefix + "_max_freq",
		prefix + "_min_freq",
		"cpuinfo_transition_latency",
	}
	uintOut := make([]uint64, len(uintFiles))

	for i, f := range uintFiles {
		v, err := util.ReadUintFromFile(filepath.Join(cpuPath, f))
		if err != nil {
			return &SystemCPUCpufreqStats{}, err
		}

		uintOut[i] = v
	}

	stringFiles := []string{
		"scaling_available_governors",
		"scaling_driver",
		"scaling_governor",
		"related_cpus",
		"scaling_setspeed",
	}
	stringOut := make([]string, len(stringFiles))
	var err error

	for i, f := range stringFiles {
		stringOut[i], err = util.SysReadFile(filepath.Join(cpuPath, f))
		if err != nil {
			return &SystemCPUCpufreqStats{}, err
		}
	}

	return &SystemCPUCpufreqStats{
		CurrentFrequency:   uintOut[0],
		MaximumFrequency:   uintOut[1],
		MinimumFrequency:   uintOut[2],
		TransitionLatency:  uintOut[3],
		AvailableGovernors: stringOut[0],
		Driver:             stringOut[1],
		Govenor:            stringOut[2],
		RelatedCpus:        stringOut[3],
		SetSpeed:           stringOut[4],
	}, nil
}