speed_limit_observer_win.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2020 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 BASE_POWER_MONITOR_SPEED_LIMIT_OBSERVER_WIN_H_
  5. #define BASE_POWER_MONITOR_SPEED_LIMIT_OBSERVER_WIN_H_
  6. #include "base/base_export.h"
  7. #include "base/callback.h"
  8. #include "base/power_monitor/moving_average.h"
  9. #include "base/power_monitor/power_observer.h"
  10. #include "base/time/time.h"
  11. #include "base/timer/timer.h"
  12. namespace base {
  13. // This class is used to listen for speed-limit changes and route new values to
  14. // PowerMonitorSource when they are changed. The speed-limit value represents
  15. // how well the CPU is running, where 100 means that it is running at normal
  16. // speed (not throttled) and 0 means that it is so severely throttled (thermal
  17. // throttling, power throttling, or other) that it is not running at all.
  18. // A value under 70 indicates noticeable throttling, and a value under 40
  19. // indicates severe throttling. Well designed systems with sufficient power
  20. // and cooling should be able to run with no throttling, but some systems
  21. // (laptops in particular) may be throttled, especially in hot environments or
  22. // when running on battery. On a well designed computer this metric should stay
  23. // at 100, only going lower if there is insufficient cooling or power.
  24. class BASE_EXPORT SpeedLimitObserverWin final {
  25. public:
  26. typedef base::RepeatingCallback<void(int)> SpeedLimitUpdateCallback;
  27. explicit SpeedLimitObserverWin(
  28. SpeedLimitUpdateCallback speed_limit_update_callback);
  29. ~SpeedLimitObserverWin();
  30. private:
  31. int GetCurrentSpeedLimit();
  32. void OnTimerTick();
  33. float EstimateThrottlingLevel();
  34. size_t num_cpus() const { return num_cpus_; }
  35. const SpeedLimitUpdateCallback callback_;
  36. // Periodically calls OnTimerTick() where a new speed-limit metric is
  37. // calculated. The timer is cancelled once this object is destroyed.
  38. base::RepeatingTimer timer_;
  39. // Number of logical cores in the existing physical processor.
  40. // Example: a processor with 6 cores which supports hyperthreading has 12
  41. // logical cores, hence `num_cpus_` equals 12 in this case.
  42. const size_t num_cpus_;
  43. // A simple MA filter of size 10 is used to smooth out the speed-limit
  44. // value and to remove noise from short spikes in CPU load. The existing
  45. // sample rate is one sample per seconds but the existing choice is rather
  46. // ad-hoc and not based on any deeper analysis into exact frequency
  47. // characteristics of the underlying process.
  48. MovingAverage moving_average_;
  49. // Max speed-limit value is 100 (%) and it is also used in cases where the
  50. // native Windows API(s) fail.
  51. int speed_limit_ = PowerThermalObserver::kSpeedLimitMax;
  52. };
  53. } // namespace base
  54. #endif // BASE_POWER_MONITOR_SPEED_LIMIT_OBSERVER_WIN_H_