platform_collector.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_PLATFORM_COLLECTOR_H_
  5. #define SERVICES_DEVICE_COMPUTE_PRESSURE_PLATFORM_COLLECTOR_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/callback_forward.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/task/sequenced_task_runner.h"
  12. #include "base/thread_annotations.h"
  13. #include "base/timer/timer.h"
  14. #include "services/device/compute_pressure/pressure_sample.h"
  15. namespace device {
  16. class CpuProbe;
  17. // Drives the process that measures the compute pressure state.
  18. //
  19. // Responsible for invoking the platform-specific measurement code in a CpuProbe
  20. // implementation at regular intervals, and for straddling between sequences to
  21. // meet the CpuProbe requirements.
  22. //
  23. // Instances are not thread-safe. They must be used on the same sequence.
  24. //
  25. // The instance is owned by a PressureManagerImpl.
  26. class PlatformCollector {
  27. public:
  28. // The caller must ensure that `cpu_probe` outlives this instance. Production
  29. // code should pass CpuProbe::Create().
  30. //
  31. // `sampling_interval` is exposed to avoid idling in tests. Production code
  32. // should pass `kDefaultSamplingInterval`.
  33. //
  34. // `sampling_callback` is called regularly every `sampling_interval` while
  35. // the collector is started.
  36. PlatformCollector(
  37. std::unique_ptr<CpuProbe> cpu_probe,
  38. base::TimeDelta sampling_interval,
  39. base::RepeatingCallback<void(PressureSample)> sampling_callback);
  40. ~PlatformCollector();
  41. bool has_probe() const {
  42. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  43. return probe_ != nullptr;
  44. }
  45. // Idempotent. Must only be called if has_probe() returns true.
  46. //
  47. // After this method is called, the sampling callback passsed to the
  48. // constructor will be called regularly.
  49. void EnsureStarted();
  50. // Idempotent.
  51. void Stop();
  52. // Used by tests that pass in a FakeCpuProbe that they need to direct.
  53. CpuProbe* cpu_probe_for_testing() {
  54. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  55. return probe_.get();
  56. }
  57. private:
  58. // Called periodically while the collector is running.
  59. void UpdateProbe();
  60. // Called after the CpuProbe is updated.
  61. void DidUpdateProbe(PressureSample sample);
  62. SEQUENCE_CHECKER(sequence_checker_);
  63. // A sequence that can execute methods on the CpuProbe instance.
  64. const scoped_refptr<base::SequencedTaskRunner> probe_task_runner_;
  65. // Methods on the underlying probe must be executed on `probe_task_runner_`.
  66. //
  67. // Constant between the collector's construction and destruction.
  68. std::unique_ptr<CpuProbe> probe_ GUARDED_BY_CONTEXT(sequence_checker_);
  69. // Drives repeated sampling.
  70. base::RepeatingTimer timer_ GUARDED_BY_CONTEXT(sequence_checker_);
  71. const base::TimeDelta sampling_interval_
  72. GUARDED_BY_CONTEXT(sequence_checker_);
  73. // Called with each sample reading.
  74. base::RepeatingCallback<void(PressureSample)> sampling_callback_
  75. GUARDED_BY_CONTEXT(sequence_checker_);
  76. // True if the CpuProbe state will be reported after the next update.
  77. //
  78. // The PressureSample reported by many CpuProbe implementations relies
  79. // on the differences observed between two Update() calls. For this reason,
  80. // the PressureSample reported after a first Update() call is not
  81. // reported via `sampling_callback_`.
  82. bool got_probe_baseline_ GUARDED_BY_CONTEXT(sequence_checker_) = false;
  83. base::WeakPtrFactory<PlatformCollector> weak_factory_
  84. GUARDED_BY_CONTEXT(sequence_checker_){this};
  85. };
  86. } // namespace device
  87. #endif // SERVICES_DEVICE_COMPUTE_PRESSURE_PLATFORM_COLLECTOR_H_