1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
6 #define CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/chromeos/power/cpu_data_collector.h"
14 #include "chromeos/chromeos_export.h"
15 #include "chromeos/dbus/power_manager_client.h"
17 namespace power_manager
{
18 class PowerSupplyProperties
;
23 // A class which starts collecting power metrics, like the battery charge, as
24 // soon as it is initialized via Initialize().
26 // This class is implemented as a global singleton, initialized after
27 // DBusThreadManager which it depends on.
28 class CHROMEOS_EXPORT PowerDataCollector
: public PowerManagerClient::Observer
{
30 struct PowerSupplySample
{
33 // Time when the sample was captured. We use base::Time instead of
34 // base::TimeTicks because the latter does not advance when the system is
38 // True if connected to external power at the time of the sample.
41 // The battery charge as a percentage of full charge in range [0.0, 100.00].
42 double battery_percent
;
44 // The battery discharge rate in W. Positive if the battery is being
45 // discharged and negative if it's being charged.
46 double battery_discharge_rate
;
49 struct SystemResumedSample
{
50 SystemResumedSample();
52 // Time when the system resumed.
55 // The duration for which the system was in sleep/suspend state.
56 base::TimeDelta sleep_duration
;
59 const std::deque
<PowerSupplySample
>& power_supply_data() const {
60 return power_supply_data_
;
63 const std::deque
<SystemResumedSample
>& system_resumed_data() const {
64 return system_resumed_data_
;
67 const CpuDataCollector
& cpu_data_collector() const {
68 return cpu_data_collector_
;
71 // Can be called only after DBusThreadManager is initialized.
72 static void Initialize();
74 // Same as Initialize, but does not start the CpuDataCollector.
75 static void InitializeForTesting();
77 // Can be called only if initialized via Initialize, and before
78 // DBusThreadManager is destroyed.
79 static void Shutdown();
81 // Returns the global instance of PowerDataCollector.
82 static PowerDataCollector
* Get();
84 // PowerManagerClient::Observer implementation:
85 void PowerChanged(const power_manager::PowerSupplyProperties
& prop
) override
;
86 void SuspendDone(const base::TimeDelta
& sleep_duration
) override
;
88 // Only those power data samples which fall within the last
89 // |kSampleTimeLimitSec| are stored in memory.
90 static const int kSampleTimeLimitSec
;
93 explicit PowerDataCollector(const bool start_cpu_data_collector
);
95 ~PowerDataCollector() override
;
97 std::deque
<PowerSupplySample
> power_supply_data_
;
98 std::deque
<SystemResumedSample
> system_resumed_data_
;
99 CpuDataCollector cpu_data_collector_
;
101 DISALLOW_COPY_AND_ASSIGN(PowerDataCollector
);
104 // Adds |sample| to |sample_deque|.
105 // It dumps samples |PowerDataCollector::kSampleTimeLimitSec| or more older than
107 template <typename SampleType
>
108 void AddSample(std::deque
<SampleType
>* sample_queue
, const SampleType
& sample
) {
109 while (!sample_queue
->empty()) {
110 const SampleType
& first
= sample_queue
->front();
111 if (sample
.time
- first
.time
>
112 base::TimeDelta::FromSeconds(PowerDataCollector::kSampleTimeLimitSec
)) {
113 sample_queue
->pop_front();
118 sample_queue
->push_back(sample
);
121 } // namespace chromeos
123 #endif // CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_