| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_ | 6 #define CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chrome/browser/chromeos/power/cpu_data_collector.h" | |
| 14 #include "chromeos/chromeos_export.h" | 13 #include "chromeos/chromeos_export.h" |
| 15 #include "chromeos/dbus/power_manager_client.h" | 14 #include "chromeos/dbus/power_manager_client.h" |
| 16 | 15 |
| 17 namespace power_manager { | 16 namespace power_manager { |
| 18 class PowerSupplyProperties; | 17 class PowerSupplyProperties; |
| 19 } | 18 } |
| 20 | 19 |
| 21 namespace chromeos { | 20 namespace chromeos { |
| 22 | 21 |
| 23 // A class which starts collecting power metrics, like the battery charge, as | 22 // A class which starts collecting power metrics, like the battery charge, as |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 }; | 56 }; |
| 58 | 57 |
| 59 const std::deque<PowerSupplySample>& power_supply_data() const { | 58 const std::deque<PowerSupplySample>& power_supply_data() const { |
| 60 return power_supply_data_; | 59 return power_supply_data_; |
| 61 } | 60 } |
| 62 | 61 |
| 63 const std::deque<SystemResumedSample>& system_resumed_data() const { | 62 const std::deque<SystemResumedSample>& system_resumed_data() const { |
| 64 return system_resumed_data_; | 63 return system_resumed_data_; |
| 65 } | 64 } |
| 66 | 65 |
| 67 const CpuDataCollector& cpu_data_collector() const { | |
| 68 return cpu_data_collector_; | |
| 69 } | |
| 70 | |
| 71 // Can be called only after DBusThreadManager is initialized. | 66 // Can be called only after DBusThreadManager is initialized. |
| 72 static void Initialize(); | 67 static void Initialize(); |
| 73 | 68 |
| 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 | 69 // Can be called only if initialized via Initialize, and before |
| 78 // DBusThreadManager is destroyed. | 70 // DBusThreadManager is destroyed. |
| 79 static void Shutdown(); | 71 static void Shutdown(); |
| 80 | 72 |
| 81 // Returns the global instance of PowerDataCollector. | 73 // Returns the global instance of PowerDataCollector. |
| 82 static PowerDataCollector* Get(); | 74 static PowerDataCollector* Get(); |
| 83 | 75 |
| 84 // PowerManagerClient::Observer implementation: | 76 // PowerManagerClient::Observer implementation: |
| 85 virtual void PowerChanged( | 77 virtual void PowerChanged( |
| 86 const power_manager::PowerSupplyProperties& prop) OVERRIDE; | 78 const power_manager::PowerSupplyProperties& prop) OVERRIDE; |
| 87 virtual void SystemResumed(const base::TimeDelta& sleep_duration) OVERRIDE; | 79 virtual void SystemResumed(const base::TimeDelta& sleep_duration) OVERRIDE; |
| 88 | 80 |
| 89 // Only those power data samples which fall within the last | 81 // Only those power data samples which fall within the last |
| 90 // |kSampleTimeLimitSec| are stored in memory. | 82 // |kSampleTimeLimitSec| are stored in memory. |
| 91 static const int kSampleTimeLimitSec; | 83 static const int kSampleTimeLimitSec; |
| 92 | 84 |
| 93 private: | 85 private: |
| 94 explicit PowerDataCollector(const bool start_cpu_data_collector); | 86 PowerDataCollector(); |
| 95 | 87 |
| 96 virtual ~PowerDataCollector(); | 88 virtual ~PowerDataCollector(); |
| 97 | 89 |
| 98 std::deque<PowerSupplySample> power_supply_data_; | 90 std::deque<PowerSupplySample> power_supply_data_; |
| 99 std::deque<SystemResumedSample> system_resumed_data_; | 91 std::deque<SystemResumedSample> system_resumed_data_; |
| 100 CpuDataCollector cpu_data_collector_; | |
| 101 | 92 |
| 102 DISALLOW_COPY_AND_ASSIGN(PowerDataCollector); | 93 DISALLOW_COPY_AND_ASSIGN(PowerDataCollector); |
| 103 }; | 94 }; |
| 104 | 95 |
| 105 // Adds |sample| to |sample_deque|. | 96 // Adds |sample| to |sample_deque|. |
| 106 // It dumps samples |PowerDataCollector::kSampleTimeLimitSec| or more older than | 97 // It dumps samples |PowerDataCollector::kSampleTimeLimitSec| or more older than |
| 107 // |sample|. | 98 // |sample|. |
| 108 template <typename SampleType> | 99 template <typename SampleType> |
| 109 void AddSample(std::deque<SampleType>* sample_queue, const SampleType& sample) { | 100 void AddSample(std::deque<SampleType>* sample_queue, const SampleType& sample) { |
| 110 while (!sample_queue->empty()) { | 101 while (!sample_queue->empty()) { |
| 111 const SampleType& first = sample_queue->front(); | 102 const SampleType& first = sample_queue->front(); |
| 112 if (sample.time - first.time > | 103 if (sample.time - first.time > |
| 113 base::TimeDelta::FromSeconds(PowerDataCollector::kSampleTimeLimitSec)) { | 104 base::TimeDelta::FromSeconds(PowerDataCollector::kSampleTimeLimitSec)) { |
| 114 sample_queue->pop_front(); | 105 sample_queue->pop_front(); |
| 115 } else { | 106 } else { |
| 116 break; | 107 break; |
| 117 } | 108 } |
| 118 } | 109 } |
| 119 sample_queue->push_back(sample); | 110 sample_queue->push_back(sample); |
| 120 } | 111 } |
| 121 | 112 |
| 122 } // namespace chromeos | 113 } // namespace chromeos |
| 123 | 114 |
| 124 #endif // CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_ | 115 #endif // CHROME_BROWSER_CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_ |
| OLD | NEW |