Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct one typo and two nitpicks. #740

Merged
merged 1 commit into from Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -116,7 +116,7 @@ func (p *none) DescribeMetrics() []*prometheus.Desc {
return nil
}

// CollectMetrics generates prometheus metrics from cached/polled policys-specific metrics data.
// CollectMetrics generates prometheus metrics from cached/polled policy-specific metrics data.
func (p *none) CollectMetrics(policy.Metrics) ([]prometheus.Metric, error) {
return nil, nil
}
Expand Down
Expand Up @@ -130,7 +130,7 @@ func (p *podpools) PollMetrics() policy.Metrics {
return policyMetrics
}

// CollectMetrics generates prometheus metrics from cached/polled policys-specific metrics data.
// CollectMetrics generates prometheus metrics from cached/polled policy-specific metrics data.
func (p *podpools) CollectMetrics(m policy.Metrics) ([]prometheus.Metric, error) {
metrics, ok := m.(*Metrics)
if !ok {
Expand Down
Expand Up @@ -225,7 +225,7 @@ func (p *staticplus) PollMetrics() policy.Metrics {
return nil
}

// CollectMetrics generates prometheus metrics from cached/polled policys-specific metrics data.
// CollectMetrics generates prometheus metrics from cached/polled policy-specific metrics data.
func (p *staticplus) CollectMetrics(policy.Metrics) ([]prometheus.Metric, error) {
return nil, nil
}
Expand Down
Expand Up @@ -262,7 +262,7 @@ func (p *stp) PollMetrics() policy.Metrics {
return nil
}

// CollectMetrics generates prometheus metrics from cached/polled policys-specific metrics data.
// CollectMetrics generates prometheus metrics from cached/polled policy-specific metrics data.
func (p *stp) CollectMetrics(policy.Metrics) ([]prometheus.Metric, error) {
return nil, nil
}
Expand Down
Expand Up @@ -214,7 +214,7 @@ func (p *static) PollMetrics() policy.Metrics {
return nil
}

// CollectMetrics generates prometheus metrics from cached/polled policys-specific metrics data.
// CollectMetrics generates prometheus metrics from cached/polled policy-specific metrics data.
func (p *static) CollectMetrics(policy.Metrics) ([]prometheus.Metric, error) {
return nil, nil
}
Expand Down
Expand Up @@ -298,7 +298,7 @@ func (p *policy) PollMetrics() policyapi.Metrics {
return nil
}

// CollectMetrics generates prometheus metrics from cached/polled policys-specific metrics data.
// CollectMetrics generates prometheus metrics from cached/polled policy-specific metrics data.
func (p *policy) CollectMetrics(policyapi.Metrics) ([]prometheus.Metric, error) {
return nil, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cri/resource-manager/policy/policy.go
Expand Up @@ -133,7 +133,7 @@ type Backend interface {
DescribeMetrics() []*prometheus.Desc
// PollMetrics provides policy metrics for monitoring.
PollMetrics() Metrics
// CollectMetrics generates prometheus metrics from cached/polled policys-specific metrics data.
// CollectMetrics generates prometheus metrics from cached/polled policy-specific metrics data.
CollectMetrics(Metrics) ([]prometheus.Metric, error)
}

Expand Down Expand Up @@ -432,7 +432,7 @@ func (p *policy) DescribeMetrics() []*prometheus.Desc {
return nil
}

// CollectMetrics generates prometheus metrics from cached/polled policys-specific metrics data.
// CollectMetrics generates prometheus metrics from cached/polled policy-specific metrics data.
func (p *policy) CollectMetrics(m Metrics) ([]prometheus.Metric, error) {
if !p.Bypassed() {
return p.active.CollectMetrics(m)
Expand Down
30 changes: 7 additions & 23 deletions pkg/procstats/procstats.go
Expand Up @@ -16,7 +16,6 @@ package procstats

import (
"io/ioutil"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -46,29 +45,30 @@ var (

// GetCPUTimeStat calculates CPU usage by using the CPU time statistics from /proc/stat
func (t *CPUTimeStat) GetCPUTimeStat() error {
lines, err := readLines(procStat)
if err != nil {
return err
}
// /proc/stat looks like this:
// cpuid: user, nice, system, idle, iowait, irq, softirq
// cpu 130216 19944 162525 1491240 3784 24749 17773 0 0 0
// cpu0 40321 11452 49784 403099 2615 6076 6748 0 0 0
// cpu1 26585 2425 36639 151166 404 2533 3541 0 0 0
// ...
stats, err := ioutil.ReadFile(procStat)
if err != nil {
return err
}
t.Lock()
defer t.Unlock()
sys, err := sysfs.DiscoverSystem()
if err != nil {
return err
}
cpuCount := len(sys.CPUIDs())
for index, line := range lines {
for index, line := range strings.Split(string(stats), "\n") {
if index > cpuCount {
break
}
split := strings.Split(line, " ")
if matched, _ := regexp.MatchString("cpu+\\d", split[0]); matched {

if strings.HasPrefix(split[0], "cpu") && split[0] != "cpu" {
i, err := strconv.Atoi(split[0][3:])
if err != nil {
log.Error("Fail to get CPU index.")
Expand Down Expand Up @@ -109,19 +109,3 @@ func (t *CPUTimeStat) GetCPUTimeStat() error {
t.IsGetCPUUsageBegin = true
return nil
}

func readLines(filePath string) ([]string, error) {
f, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
data := string(f)
rawLines := strings.Split(data, "\n")
lines := make([]string, 0)
for _, rawLine := range rawLines {
if len(strings.TrimSpace(rawLine)) > 0 {
lines = append(lines, rawLine)
}
}
return lines, nil
}