manager_libpfm.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //go:build libpfm && cgo
  2. // +build libpfm,cgo
  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. // Manager of perf events for containers.
  17. package perf
  18. import (
  19. "fmt"
  20. "os"
  21. info "github.com/google/cadvisor/info/v1"
  22. "github.com/google/cadvisor/stats"
  23. "github.com/google/cadvisor/utils/sysinfo"
  24. )
  25. type manager struct {
  26. events PerfEvents
  27. onlineCPUs []int
  28. cpuToSocket map[int]int
  29. stats.NoopDestroy
  30. }
  31. func NewManager(configFile string, topology []info.Node) (stats.Manager, error) {
  32. if configFile == "" {
  33. return &stats.NoopManager{}, nil
  34. }
  35. file, err := os.Open(configFile)
  36. if err != nil {
  37. return nil, fmt.Errorf("unable to read configuration file %q: %w", configFile, err)
  38. }
  39. config, err := parseConfig(file)
  40. if err != nil {
  41. return nil, fmt.Errorf("unable to parse configuration file %q: %w", configFile, err)
  42. }
  43. if len(config.Core.Events) == 0 && len(config.Uncore.Events) == 0 {
  44. return nil, fmt.Errorf("there is no events in config file %q", configFile)
  45. }
  46. onlineCPUs := sysinfo.GetOnlineCPUs(topology)
  47. cpuToSocket := make(map[int]int)
  48. for _, cpu := range onlineCPUs {
  49. cpuToSocket[cpu] = sysinfo.GetSocketFromCPU(topology, cpu)
  50. }
  51. return &manager{events: config, onlineCPUs: onlineCPUs, cpuToSocket: cpuToSocket}, nil
  52. }
  53. func (m *manager) GetCollector(cgroupPath string) (stats.Collector, error) {
  54. collector := newCollector(cgroupPath, m.events, m.onlineCPUs, m.cpuToSocket)
  55. err := collector.setup()
  56. if err != nil {
  57. collector.Destroy()
  58. return &stats.NoopCollector{}, err
  59. }
  60. return collector, nil
  61. }