OLD | NEW |
| (Empty) |
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_CPU_DATA_COLLECTOR_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_POWER_CPU_DATA_COLLECTOR_H_ | |
7 | |
8 #include <deque> | |
9 #include <map> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/basictypes.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/time/time.h" | |
16 #include "base/timer/timer.h" | |
17 | |
18 namespace chromeos { | |
19 | |
20 // A class to sample CPU idle state occupancy and freq state occupancy. | |
21 // Collects raw data from sysfs and does not convert it to percentage | |
22 // occupancy. As CPUs can be offline at times, or the system can be suspended at | |
23 // other times, it is best for the consumer of this data to calculate percentage | |
24 // occupancy information using suspend time data from | |
25 // PowerDataCollector::system_resumed_data. | |
26 class CpuDataCollector { | |
27 public: | |
28 struct StateOccupancySample { | |
29 StateOccupancySample(); | |
30 ~StateOccupancySample(); | |
31 | |
32 // The time when the data was sampled. | |
33 base::Time time; | |
34 | |
35 // Indicates whether the CPU is online. | |
36 bool cpu_online; | |
37 | |
38 // A mapping from a CPU state to time spent in that state in milliseconds. | |
39 // For idle state samples, the name of the state at index i in this vector | |
40 // is the name at index i in the vector returned by cpu_idle_state_names(). | |
41 // Similarly, for freq state occupancy, similar information is in the vector | |
42 // returned by cpu_freq_state_names(). | |
43 std::vector<int64> time_in_state; | |
44 }; | |
45 | |
46 typedef std::deque<StateOccupancySample> StateOccupancySampleDeque; | |
47 | |
48 const std::vector<std::string>& cpu_idle_state_names() const { | |
49 return cpu_idle_state_names_; | |
50 } | |
51 | |
52 const std::vector<StateOccupancySampleDeque>& cpu_idle_state_data() const { | |
53 return cpu_idle_state_data_; | |
54 } | |
55 | |
56 const std::vector<std::string>& cpu_freq_state_names() const { | |
57 return cpu_freq_state_names_; | |
58 } | |
59 | |
60 const std::vector<StateOccupancySampleDeque>& cpu_freq_state_data() const { | |
61 return cpu_freq_state_data_; | |
62 } | |
63 | |
64 CpuDataCollector(); | |
65 ~CpuDataCollector(); | |
66 | |
67 // Starts a repeating timer which periodically runs a callback to collect | |
68 // CPU state occupancy samples. | |
69 void Start(); | |
70 | |
71 private: | |
72 // Posts callbacks to the blocking pool which collect CPU state occupancy | |
73 // samples from the sysfs. | |
74 void PostSampleCpuState(); | |
75 | |
76 // Samples CPU idle and CPU freq data from sysfs. This function should run on | |
77 // the blocking pool as reading from sysfs is a blocking task. Elements at | |
78 // index i in |idle_samples| and |freq_samples| correspond to the idle and | |
79 // freq samples of CPU i. | |
80 void SampleCpuStateOnBlockingPool( | |
81 std::vector<std::string>* cpu_idle_state_names, | |
82 std::vector<StateOccupancySample>* idle_samples, | |
83 std::vector<std::string>* cpu_freq_state_names, | |
84 std::vector<StateOccupancySample>* freq_samples); | |
85 | |
86 // This function commits the samples read by SampleCpuStateOnBlockingPool to | |
87 // |cpu_idle_state_data_| and |cpu_freq_state_data_|. Since UI is the consumer | |
88 // of CPU idle and freq data, this function should run on the UI thread. | |
89 void SaveCpuStateSamplesOnUIThread( | |
90 const std::vector<std::string>* cpu_idle_state_names, | |
91 const std::vector<StateOccupancySample>* idle_samples, | |
92 const std::vector<std::string>* cpu_freq_state_names, | |
93 const std::vector<StateOccupancySample>* freq_samples); | |
94 | |
95 base::RepeatingTimer<CpuDataCollector> timer_; | |
96 | |
97 // Names of the idle states. | |
98 std::vector<std::string> cpu_idle_state_names_; | |
99 | |
100 // The deque at index <i> in the vector corresponds to the idle state | |
101 // occupancy data of CPU<i>. | |
102 std::vector<StateOccupancySampleDeque> cpu_idle_state_data_; | |
103 | |
104 // Names of the freq states. | |
105 std::vector<std::string> cpu_freq_state_names_; | |
106 | |
107 // The deque at index <i> in the vector corresponds to the frequency state | |
108 // occupancy data of CPU<i>. | |
109 std::vector<StateOccupancySampleDeque> cpu_freq_state_data_; | |
110 | |
111 // The number of CPUs on the system. This value is read from the sysfs, and | |
112 // hence should be read/written to only from the blocking pool. | |
113 int cpu_count_; | |
114 | |
115 base::WeakPtrFactory<CpuDataCollector> weak_ptr_factory_; | |
116 DISALLOW_COPY_AND_ASSIGN(CpuDataCollector); | |
117 }; | |
118 | |
119 } // namespace chromeos | |
120 | |
121 #endif // CHROME_BROWSER_CHROMEOS_POWER_CPU_DATA_COLLECTOR_H_ | |
OLD | NEW |