• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
6 #define CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
7 
8 #include <deque>
9 
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"
16 
17 namespace power_manager {
18 class PowerSupplyProperties;
19 }
20 
21 namespace chromeos {
22 
23 // A class which starts collecting power metrics, like the battery charge, as
24 // soon as it is initialized via Initialize().
25 //
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 {
29  public:
30   struct PowerSupplySample {
31     PowerSupplySample();
32 
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
35     // suspended.
36     base::Time time;
37 
38     // True if connected to external power at the time of the sample.
39     bool external_power;
40 
41     // The battery charge as a percentage of full charge in range [0.0, 100.00].
42     double battery_percent;
43 
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;
47   };
48 
49   struct SystemResumedSample {
50     SystemResumedSample();
51 
52     // Time when the system resumed.
53     base::Time time;
54 
55     // The duration for which the system was in sleep/suspend state.
56     base::TimeDelta sleep_duration;
57   };
58 
power_supply_data()59   const std::deque<PowerSupplySample>& power_supply_data() const {
60     return power_supply_data_;
61   }
62 
system_resumed_data()63   const std::deque<SystemResumedSample>& system_resumed_data() const {
64     return system_resumed_data_;
65   }
66 
cpu_data_collector()67   const CpuDataCollector& cpu_data_collector() const {
68     return cpu_data_collector_;
69   }
70 
71   // Can be called only after DBusThreadManager is initialized.
72   static void Initialize();
73 
74   // Same as Initialize, but does not start the CpuDataCollector.
75   static void InitializeForTesting();
76 
77   // Can be called only if initialized via Initialize, and before
78   // DBusThreadManager is destroyed.
79   static void Shutdown();
80 
81   // Returns the global instance of PowerDataCollector.
82   static PowerDataCollector* Get();
83 
84   // PowerManagerClient::Observer implementation:
85   virtual void PowerChanged(
86       const power_manager::PowerSupplyProperties& prop) OVERRIDE;
87   virtual void SuspendDone(const base::TimeDelta& sleep_duration) OVERRIDE;
88 
89   // Only those power data samples which fall within the last
90   // |kSampleTimeLimitSec| are stored in memory.
91   static const int kSampleTimeLimitSec;
92 
93  private:
94   explicit PowerDataCollector(const bool start_cpu_data_collector);
95 
96   virtual ~PowerDataCollector();
97 
98   std::deque<PowerSupplySample> power_supply_data_;
99   std::deque<SystemResumedSample> system_resumed_data_;
100   CpuDataCollector cpu_data_collector_;
101 
102   DISALLOW_COPY_AND_ASSIGN(PowerDataCollector);
103 };
104 
105 // Adds |sample| to |sample_deque|.
106 // It dumps samples |PowerDataCollector::kSampleTimeLimitSec| or more older than
107 // |sample|.
108 template <typename SampleType>
AddSample(std::deque<SampleType> * sample_queue,const SampleType & sample)109 void AddSample(std::deque<SampleType>* sample_queue, const SampleType& sample) {
110   while (!sample_queue->empty()) {
111     const SampleType& first = sample_queue->front();
112     if (sample.time - first.time >
113         base::TimeDelta::FromSeconds(PowerDataCollector::kSampleTimeLimitSec)) {
114       sample_queue->pop_front();
115     } else {
116       break;
117     }
118   }
119   sample_queue->push_back(sample);
120 }
121 
122 }  // namespace chromeos
123 
124 #endif  // CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
125