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