1// +build linux
2
3// Copyright 2020 Google Inc. All Rights Reserved.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9//     http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17// Collector of resctrl for a container.
18package resctrl
19
20import (
21	info "github.com/google/cadvisor/info/v1"
22	"github.com/google/cadvisor/stats"
23
24	"github.com/opencontainers/runc/libcontainer/configs"
25	"github.com/opencontainers/runc/libcontainer/intelrdt"
26)
27
28type collector struct {
29	resctrl intelrdt.Manager
30	stats.NoopDestroy
31}
32
33func newCollector(id string, resctrlPath string) *collector {
34	collector := &collector{
35		resctrl: intelrdt.NewManager(
36			&configs.Config{
37				IntelRdt: &configs.IntelRdt{},
38			},
39			id,
40			resctrlPath,
41		),
42	}
43
44	return collector
45}
46
47func (c *collector) UpdateStats(stats *info.ContainerStats) error {
48	stats.Resctrl = info.ResctrlStats{}
49
50	resctrlStats, err := c.resctrl.GetStats()
51	if err != nil {
52		return err
53	}
54
55	numberOfNUMANodes := len(*resctrlStats.MBMStats)
56
57	stats.Resctrl.MemoryBandwidth = make([]info.MemoryBandwidthStats, 0, numberOfNUMANodes)
58	stats.Resctrl.Cache = make([]info.CacheStats, 0, numberOfNUMANodes)
59
60	for _, numaNodeStats := range *resctrlStats.MBMStats {
61		stats.Resctrl.MemoryBandwidth = append(stats.Resctrl.MemoryBandwidth,
62			info.MemoryBandwidthStats{
63				TotalBytes: numaNodeStats.MBMTotalBytes,
64				LocalBytes: numaNodeStats.MBMLocalBytes,
65			})
66	}
67
68	for _, numaNodeStats := range *resctrlStats.CMTStats {
69		stats.Resctrl.Cache = append(stats.Resctrl.Cache,
70			info.CacheStats{LLCOccupancy: numaNodeStats.LLCOccupancy})
71	}
72
73	return nil
74}
75