cpu_probe_linux.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SERVICES_DEVICE_COMPUTE_PRESSURE_CPU_PROBE_LINUX_H_
  5. #define SERVICES_DEVICE_COMPUTE_PRESSURE_CPU_PROBE_LINUX_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/files/file_path.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/thread_annotations.h"
  12. #include "services/device/compute_pressure/core_times.h"
  13. #include "services/device/compute_pressure/cpu_probe.h"
  14. #include "services/device/compute_pressure/pressure_sample.h"
  15. #include "services/device/compute_pressure/procfs_stat_cpu_parser.h"
  16. namespace device {
  17. class CpuProbeLinux : public CpuProbe {
  18. public:
  19. // Factory method for production instances.
  20. static std::unique_ptr<CpuProbeLinux> Create();
  21. // Factory method with dependency injection support for testing.
  22. static std::unique_ptr<CpuProbeLinux> CreateForTesting(
  23. base::FilePath procfs_stat_path);
  24. ~CpuProbeLinux() override;
  25. CpuProbeLinux(const CpuProbeLinux&) = delete;
  26. CpuProbeLinux& operator=(const CpuProbeLinux&) = delete;
  27. // CpuProbe implementation.
  28. void Update() override;
  29. PressureSample LastSample() override;
  30. private:
  31. explicit CpuProbeLinux(base::FilePath procfs_stat_path);
  32. // Called when a core is seen the first time in /proc/stat.
  33. //
  34. // For most systems, the cores listed in /proc/stat are static. However, it is
  35. // theoretically possible for cores to go online and offline.
  36. void InitializeCore(size_t core_index, const CoreTimes& initial_core_times);
  37. SEQUENCE_CHECKER(sequence_checker_);
  38. // /proc/stat parser. Used to derive CPU utilization.
  39. ProcfsStatCpuParser stat_parser_ GUARDED_BY_CONTEXT(sequence_checker_);
  40. // Most recent per-core times from /proc/stat.
  41. std::vector<CoreTimes> last_per_core_times_
  42. GUARDED_BY_CONTEXT(sequence_checker_);
  43. PressureSample last_sample_ GUARDED_BY_CONTEXT(sequence_checker_) =
  44. kUnsupportedValue;
  45. };
  46. } // namespace device
  47. #endif // SERVICES_DEVICE_COMPUTE_PRESSURE_CPU_PROBE_LINUX_H_