manager.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //go:build linux
  2. // +build linux
  3. // Copyright 2021 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. // Manager of resctrl for containers.
  17. package resctrl
  18. import (
  19. "errors"
  20. "time"
  21. "k8s.io/klog/v2"
  22. "github.com/google/cadvisor/container/raw"
  23. "github.com/google/cadvisor/stats"
  24. )
  25. type Manager interface {
  26. Destroy()
  27. GetCollector(containerName string, getContainerPids func() ([]string, error), numberOfNUMANodes int) (stats.Collector, error)
  28. }
  29. type manager struct {
  30. stats.NoopDestroy
  31. interval time.Duration
  32. vendorID string
  33. inHostNamespace bool
  34. }
  35. func (m *manager) GetCollector(containerName string, getContainerPids func() ([]string, error), numberOfNUMANodes int) (stats.Collector, error) {
  36. collector := newCollector(containerName, getContainerPids, m.interval, numberOfNUMANodes, m.vendorID, m.inHostNamespace)
  37. err := collector.setup()
  38. if err != nil {
  39. return &stats.NoopCollector{}, err
  40. }
  41. return collector, nil
  42. }
  43. func NewManager(interval time.Duration, setup func() error, vendorID string, inHostNamespace bool) (Manager, error) {
  44. err := setup()
  45. if err != nil {
  46. return &NoopManager{}, err
  47. }
  48. if !isResctrlInitialized {
  49. return &NoopManager{}, errors.New("the resctrl isn't initialized")
  50. }
  51. if !(enabledCMT || enabledMBM) {
  52. return &NoopManager{}, errors.New("there are no monitoring features available")
  53. }
  54. if !*raw.DockerOnly {
  55. klog.Warning("--docker_only should be set when collecting Resctrl metrics! See the runtime docs.")
  56. }
  57. return &manager{interval: interval, vendorID: vendorID, inHostNamespace: inHostNamespace}, nil
  58. }
  59. type NoopManager struct {
  60. stats.NoopDestroy
  61. }
  62. func (np *NoopManager) GetCollector(_ string, _ func() ([]string, error), _ int) (stats.Collector, error) {
  63. return &stats.NoopCollector{}, nil
  64. }