-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WiP: policy: implement policy system metrics.
Implement collection of policy 'system' prometheus metrics. We collect per each memory node - memory capcity - memory usage - number of containers sharing the node We collect per each CPU core - allocation from that core - number of containers sharing the core Signed-off-by: Krisztian Litkey <[email protected]>
- Loading branch information
Showing
3 changed files
with
291 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
// Copyright The NRI Plugins Authors. All Rights Reserved. | ||
// | ||
// 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. | ||
|
||
package policy | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/containers/nri-plugins/pkg/metrics" | ||
"github.com/containers/nri-plugins/pkg/resmgr/cache" | ||
system "github.com/containers/nri-plugins/pkg/sysfs" | ||
"github.com/containers/nri-plugins/pkg/utils/cpuset" | ||
"github.com/prometheus/client_golang/prometheus" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
nodeCapacity = iota | ||
nodeUsage | ||
nodeContainers | ||
cpuAllocation | ||
cpuContainers | ||
metricsCount | ||
) | ||
|
||
type ( | ||
SystemMetrics struct { | ||
cache cache.Cache | ||
system system.System | ||
Nodes map[int]*NodeMetric | ||
Cpus map[int]*CpuMetric | ||
Metrics []*prometheus.GaugeVec | ||
} | ||
NodeMetric struct { | ||
Id int | ||
IdLabel string | ||
Type string | ||
Capacity int64 | ||
Usage int64 | ||
ContainerCount int | ||
} | ||
CpuMetric struct { | ||
Id int | ||
IdLabel string | ||
Allocation int | ||
ContainerCount int | ||
} | ||
) | ||
|
||
func (p *policy) NewSystemMetrics() *SystemMetrics { | ||
s := &SystemMetrics{ | ||
cache: p.cache, | ||
system: p.system, | ||
Nodes: map[int]*NodeMetric{}, | ||
Cpus: map[int]*CpuMetric{}, | ||
Metrics: make([]*prometheus.GaugeVec, metricsCount, metricsCount), | ||
} | ||
|
||
s.Metrics[nodeCapacity] = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: "mem_node_capacity", | ||
Help: "Capacity of the memory node.", | ||
}, | ||
[]string{ | ||
"node_id", | ||
}, | ||
) | ||
s.Metrics[nodeUsage] = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: "mem_node_usage", | ||
Help: "Usage of the memory node", | ||
}, | ||
[]string{ | ||
"node_id", | ||
}, | ||
) | ||
s.Metrics[nodeContainers] = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: "mem_node_container_count", | ||
Help: "Number of containers assigned to the memory node.", | ||
}, | ||
[]string{ | ||
"node_id", | ||
}, | ||
) | ||
s.Metrics[cpuAllocation] = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: "cpu_allocation", | ||
Help: "Total allocation of the CPU.", | ||
}, | ||
[]string{ | ||
"cpu_id", | ||
}, | ||
) | ||
s.Metrics[cpuContainers] = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: "cpu_container_count", | ||
Help: "Number of containers assigned to the CPU.", | ||
}, | ||
[]string{ | ||
"cpu_id", | ||
}, | ||
) | ||
|
||
for _, id := range s.system.NodeIDs() { | ||
var ( | ||
sys = s.system.Node(id) | ||
capa, used = s.getMemInfo(sys) | ||
node = &NodeMetric{ | ||
Id: sys.ID(), | ||
IdLabel: strconv.Itoa(sys.ID()), | ||
Type: sys.GetMemoryType().String(), | ||
Capacity: capa, | ||
Usage: used, | ||
} | ||
) | ||
s.Nodes[id] = node | ||
} | ||
|
||
for _, id := range s.system.CPUIDs() { | ||
cpu := &CpuMetric{ | ||
Id: id, | ||
IdLabel: strconv.Itoa(id), | ||
} | ||
s.Cpus[id] = cpu | ||
} | ||
|
||
return s | ||
} | ||
|
||
func (s *SystemMetrics) Describe(ch chan<- *prometheus.Desc) { | ||
s.Metrics[nodeCapacity].Describe(ch) | ||
s.Metrics[nodeUsage].Describe(ch) | ||
s.Metrics[nodeContainers].Describe(ch) | ||
s.Metrics[cpuAllocation].Describe(ch) | ||
s.Metrics[cpuContainers].Describe(ch) | ||
} | ||
|
||
func (s *SystemMetrics) Collect(ch chan<- prometheus.Metric) { | ||
s.Update() | ||
s.Metrics[nodeCapacity].Collect(ch) | ||
s.Metrics[nodeUsage].Collect(ch) | ||
s.Metrics[nodeContainers].Collect(ch) | ||
s.Metrics[cpuAllocation].Collect(ch) | ||
s.Metrics[cpuContainers].Collect(ch) | ||
} | ||
|
||
func (s *SystemMetrics) Register() error { | ||
return metrics.Register("system", s, metrics.WithGroup("policy")) | ||
} | ||
|
||
func (s *SystemMetrics) Update() { | ||
for _, n := range s.Nodes { | ||
sys := s.system.Node(n.Id) | ||
capa, used := s.getMemInfo(sys) | ||
|
||
if n.Capacity == 0 { | ||
n.Capacity = capa | ||
s.Metrics[nodeCapacity].WithLabelValues(n.IdLabel).Set(float64(n.Capacity)) | ||
} | ||
|
||
n.Usage = used | ||
n.ContainerCount = 0 | ||
} | ||
|
||
for _, c := range s.Cpus { | ||
c.ContainerCount = 0 | ||
} | ||
|
||
for _, ctr := range s.cache.GetContainers() { | ||
switch ctr.GetState() { | ||
case cache.ContainerStateCreated: | ||
case cache.ContainerStateRunning: | ||
default: | ||
continue | ||
} | ||
|
||
var ( | ||
cpu, mem = s.getCpuAndMemset(ctr) | ||
req, _ = s.getCpuResources(ctr) | ||
) | ||
|
||
for _, id := range mem.List() { | ||
if n, ok := s.Nodes[id]; ok { | ||
n.ContainerCount++ | ||
} | ||
} | ||
|
||
for _, id := range cpu.List() { | ||
if c, ok := s.Cpus[id]; ok { | ||
c.ContainerCount++ | ||
if cpu.Size() > 0 { | ||
c.Allocation += req / cpu.Size() | ||
} | ||
} | ||
} | ||
} | ||
|
||
for _, n := range s.Nodes { | ||
s.Metrics[nodeUsage].WithLabelValues(n.IdLabel).Set(float64(n.Usage)) | ||
} | ||
for _, c := range s.Cpus { | ||
s.Metrics[cpuAllocation].WithLabelValues(c.IdLabel).Set(float64(c.Allocation)) | ||
s.Metrics[cpuContainers].WithLabelValues(c.IdLabel).Set(float64(c.ContainerCount)) | ||
} | ||
} | ||
|
||
func (s *SystemMetrics) getMemInfo(n system.Node) (capacity, used int64) { | ||
if n != nil { | ||
if i, _ := n.MemoryInfo(); i != nil { | ||
return int64(i.MemTotal), int64(i.MemUsed) | ||
} | ||
} | ||
return 0, 0 | ||
} | ||
|
||
func (s *SystemMetrics) getCpuAndMemset(ctr cache.Container) (cpu, mem cpuset.CPUSet) { | ||
cset, _ := cpuset.Parse(ctr.GetCpusetCpus()) | ||
mset, _ := cpuset.Parse(ctr.GetCpusetMems()) | ||
return cset, mset | ||
} | ||
|
||
func (s *SystemMetrics) getCpuResources(ctr cache.Container) (request, limit int) { | ||
res := ctr.GetResourceRequirements() | ||
if qty, ok := res.Requests[v1.ResourceCPU]; ok { | ||
request = int(qty.MilliValue()) | ||
} | ||
if qty, ok := res.Limits[v1.ResourceCPU]; ok { | ||
limit = int(qty.MilliValue()) | ||
} | ||
|
||
return request, limit | ||
} |