github.com/google/cadvisor@v0.49.1/resctrl/manager.go (about)

     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.
    19  package resctrl
    20  
    21  import (
    22  	"errors"
    23  	"time"
    24  
    25  	"k8s.io/klog/v2"
    26  
    27  	"github.com/google/cadvisor/container/raw"
    28  	"github.com/google/cadvisor/stats"
    29  )
    30  
    31  type Manager interface {
    32  	Destroy()
    33  	GetCollector(containerName string, getContainerPids func() ([]string, error), numberOfNUMANodes int) (stats.Collector, error)
    34  }
    35  
    36  type manager struct {
    37  	stats.NoopDestroy
    38  	interval        time.Duration
    39  	vendorID        string
    40  	inHostNamespace bool
    41  }
    42  
    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
    48  	}
    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
    71  }
    72  
    73  type NoopManager struct {
    74  	stats.NoopDestroy
    75  }
    76  
    77  func (np *NoopManager) GetCollector(_ string, _ func() ([]string, error), _ int) (stats.Collector, error) {
    78  	return &stats.NoopCollector{}, nil
    79  }