Skip to content

Commit 616ec6e

Browse files
committed
make intel resource control collector optional
Signed-off-by: Davanum Srinivas <[email protected]>
1 parent d448bc1 commit 616ec6e

13 files changed

+142
-71
lines changed

manager/manager.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, HousekeepingConfi
221221
return nil, err
222222
}
223223

224-
newManager.resctrlManager, err = resctrl.NewManager(resctrlInterval, resctrl.Setup, machineInfo.CPUVendorID, inHostNamespace)
224+
newManager.resctrlManager, err = resctrl.NewManager(resctrlInterval, machineInfo.CPUVendorID, inHostNamespace)
225225
if err != nil {
226226
klog.V(4).Infof("Cannot gather resctrl metrics: %v", err)
227227
}
@@ -264,8 +264,8 @@ type manager struct {
264264
containerWatchers []watcher.ContainerWatcher
265265
eventsChannel chan watcher.ContainerEvent
266266
collectorHTTPClient *http.Client
267-
perfManager stats.Manager
268-
resctrlManager resctrl.Manager
267+
perfManager stats.Manager
268+
resctrlManager resctrl.Manager
269269
// List of raw container cgroup path prefix whitelist.
270270
rawContainerCgroupPathPrefixWhiteList []string
271271
// List of container env prefix whitelist, the matched container envs would be collected into metrics as extra labels.

resctrl/collector.go renamed to resctrl/intel/collector.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// limitations under the License.
1717

1818
// Collector of resctrl for a container.
19-
package resctrl
19+
package intel
2020

2121
import (
2222
"fmt"

resctrl/collector_test.go renamed to resctrl/intel/collector_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// limitations under the License.
1717

1818
// Collector tests.
19-
package resctrl
19+
package intel
2020

2121
import (
2222
"fmt"

resctrl/intel/install/install.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2025 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package install
16+
17+
import (
18+
"github.com/google/cadvisor/resctrl"
19+
"github.com/google/cadvisor/resctrl/intel"
20+
"k8s.io/klog/v2"
21+
)
22+
23+
func init() {
24+
err := resctrl.RegisterManager("intel", &intel.IntelManager{})
25+
if err != nil {
26+
klog.Fatalf("Failed to register resource control plugin: %v", err)
27+
}
28+
}

resctrl/intel/manager.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//go:build linux
2+
// +build linux
3+
4+
// Copyright 2021 Google Inc. All Rights Reserved.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
package intel
19+
20+
import (
21+
"errors"
22+
"time"
23+
24+
"k8s.io/klog/v2"
25+
26+
"github.com/google/cadvisor/container/raw"
27+
"github.com/google/cadvisor/stats"
28+
)
29+
30+
type IntelManager struct {
31+
stats.NoopDestroy
32+
interval time.Duration
33+
vendorID string
34+
inHostNamespace bool
35+
}
36+
37+
func (m *IntelManager) GetCollector(containerName string, getContainerPids func() ([]string, error), numberOfNUMANodes int) (stats.Collector, error) {
38+
collector := newCollector(containerName, getContainerPids, m.interval, numberOfNUMANodes, m.vendorID, m.inHostNamespace)
39+
err := collector.setup()
40+
if err != nil {
41+
return &stats.NoopCollector{}, err
42+
}
43+
44+
return collector, nil
45+
}
46+
47+
func (m *IntelManager) Init(interval time.Duration, vendorID string, inHostNamespace bool) error {
48+
err := Setup()
49+
if err != nil {
50+
return err
51+
}
52+
53+
if !isResctrlInitialized {
54+
return errors.New("the resctrl isn't initialized")
55+
}
56+
if !(enabledCMT || enabledMBM) {
57+
return errors.New("there are no monitoring features available")
58+
}
59+
60+
if !*raw.DockerOnly {
61+
klog.Warning("--docker_only should be set when collecting Resctrl metrics! See the runtime docs.")
62+
}
63+
64+
m.interval = interval
65+
m.vendorID = vendorID
66+
m.inHostNamespace = inHostNamespace
67+
return nil
68+
}

resctrl/manager_test.go renamed to resctrl/intel/manager_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
// limitations under the License.
1717

1818
// Manager tests.
19-
package resctrl
19+
package intel
2020

2121
import (
2222
"os"
2323
"testing"
2424

25+
"github.com/google/cadvisor/resctrl"
2526
"github.com/stretchr/testify/assert"
2627
)
2728

@@ -32,7 +33,7 @@ func TestNewManager(t *testing.T) {
3233
enabledMBM bool
3334
inHostNamespace bool
3435
err string
35-
expected Manager
36+
expected resctrl.Manager
3637
}{
3738
{
3839
true,
@@ -64,39 +65,39 @@ func TestNewManager(t *testing.T) {
6465
false,
6566
false,
6667
"the resctrl isn't initialized",
67-
&NoopManager{},
68+
&resctrl.NoopManager{},
6869
},
6970
{
7071
false,
7172
false,
7273
true,
7374
false,
7475
"the resctrl isn't initialized",
75-
&NoopManager{},
76+
&resctrl.NoopManager{},
7677
},
7778
{
7879
false,
7980
true,
8081
true,
8182
true,
8283
"the resctrl isn't initialized",
83-
&NoopManager{},
84+
&resctrl.NoopManager{},
8485
},
8586
{
8687
false,
8788
false,
8889
false,
8990
true,
9091
"the resctrl isn't initialized",
91-
&NoopManager{},
92+
&resctrl.NoopManager{},
9293
},
9394
{
9495
true,
9596
false,
9697
false,
9798
true,
9899
"there are no monitoring features available",
99-
&NoopManager{},
100+
&resctrl.NoopManager{},
100101
},
101102
}
102103

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

resctrl/utils.go renamed to resctrl/intel/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// limitations under the License.
1717

1818
// Utilities.
19-
package resctrl
19+
package intel
2020

2121
import (
2222
"bufio"

resctrl/utils_test.go renamed to resctrl/intel/utils_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// Mocked environment:
2121
// - "container" first container with {1, 2, 3} processes.
2222
// - "another" second container with {5, 6} processes.
23-
package resctrl
23+
package intel
2424

2525
import (
2626
"fmt"

resctrl/manager.go

+31-57
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,53 @@
1-
//go:build linux
2-
// +build linux
3-
4-
// Copyright 2021 Google Inc. All Rights Reserved.
5-
//
6-
// Licensed under the Apache License, Version 2.0 (the "License");
7-
// you may not use this file except in compliance with the License.
8-
// You may obtain a copy of the License at
9-
//
10-
// http://www.apache.org/licenses/LICENSE-2.0
11-
//
12-
// Unless required by applicable law or agreed to in writing, software
13-
// distributed under the License is distributed on an "AS IS" BASIS,
14-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
// See the License for the specific language governing permissions and
16-
// limitations under the License.
17-
18-
// Manager of resctrl for containers.
191
package resctrl
202

213
import (
22-
"errors"
23-
"time"
24-
25-
"k8s.io/klog/v2"
26-
27-
"github.com/google/cadvisor/container/raw"
4+
"fmt"
285
"github.com/google/cadvisor/stats"
6+
"k8s.io/klog/v2"
7+
"sync"
8+
"time"
299
)
3010

11+
// Manager of resctrl for containers.
3112
type Manager interface {
3213
Destroy()
3314
GetCollector(containerName string, getContainerPids func() ([]string, error), numberOfNUMANodes int) (stats.Collector, error)
15+
Init(interval time.Duration, id string, namespace bool) error
3416
}
3517

36-
type manager struct {
37-
stats.NoopDestroy
38-
interval time.Duration
39-
vendorID string
40-
inHostNamespace bool
41-
}
18+
// All registered resource control plugins.
19+
var managersLock sync.Mutex
20+
var managers = make(map[string]Manager)
4221

43-
func (m *manager) GetCollector(containerName string, getContainerPids func() ([]string, error), numberOfNUMANodes int) (stats.Collector, error) {
44-
collector := newCollector(containerName, getContainerPids, m.interval, numberOfNUMANodes, m.vendorID, m.inHostNamespace)
45-
err := collector.setup()
46-
if err != nil {
47-
return &stats.NoopCollector{}, err
22+
func RegisterManager(name string, manager Manager) error {
23+
managersLock.Lock()
24+
defer managersLock.Unlock()
25+
if _, found := managers[name]; found {
26+
return fmt.Errorf("Plugin %q was registered twice", name)
4827
}
49-
50-
return collector, nil
51-
}
52-
53-
func NewManager(interval time.Duration, setup func() error, vendorID string, inHostNamespace bool) (Manager, error) {
54-
err := setup()
55-
if err != nil {
56-
return &NoopManager{}, err
57-
}
58-
59-
if !isResctrlInitialized {
60-
return &NoopManager{}, errors.New("the resctrl isn't initialized")
61-
}
62-
if !(enabledCMT || enabledMBM) {
63-
return &NoopManager{}, errors.New("there are no monitoring features available")
64-
}
65-
66-
if !*raw.DockerOnly {
67-
klog.Warning("--docker_only should be set when collecting Resctrl metrics! See the runtime docs.")
68-
}
69-
70-
return &manager{interval: interval, vendorID: vendorID, inHostNamespace: inHostNamespace}, nil
28+
klog.V(4).Infof("Registered Plugin %q", name)
29+
managers[name] = manager
30+
return nil
7131
}
7232

7333
type NoopManager struct {
7434
stats.NoopDestroy
7535
}
7636

37+
func (np *NoopManager) Init(_ time.Duration, _ string, _ bool) error {
38+
return nil
39+
}
40+
7741
func (np *NoopManager) GetCollector(_ string, _ func() ([]string, error), _ int) (stats.Collector, error) {
7842
return &stats.NoopCollector{}, nil
7943
}
44+
45+
func NewManager(interval time.Duration, vendorID string, inHostNamespace bool) (Manager, error) {
46+
if len(managers) == 0 {
47+
return &NoopManager{}, nil
48+
}
49+
// Currently only intel manager is supported
50+
manager := managers["intel"]
51+
manager.Init(interval, vendorID, inHostNamespace)
52+
return manager, nil
53+
}

0 commit comments

Comments
 (0)