Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: chrome/browser/chromeos/power/cpu_data_collector.h

Issue 2853863002: Add parser for new cpu freq file (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_CPU_DATA_COLLECTOR_H_ 5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_CPU_DATA_COLLECTOR_H_
6 #define CHROME_BROWSER_CHROMEOS_POWER_CPU_DATA_COLLECTOR_H_ 6 #define CHROME_BROWSER_CHROMEOS_POWER_CPU_DATA_COLLECTOR_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <deque> 10 #include <deque>
11 #include <map> 11 #include <map>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/files/file_util.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h" 17 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h" 18 #include "base/time/time.h"
18 #include "base/timer/timer.h" 19 #include "base/timer/timer.h"
19 20
20 namespace chromeos { 21 namespace chromeos {
21 22
22 // A class to sample CPU idle state occupancy and freq state occupancy. 23 // A class to sample CPU idle state occupancy and freq state occupancy.
23 // Collects raw data from sysfs and does not convert it to percentage 24 // Collects raw data from sysfs and does not convert it to percentage
24 // occupancy. As CPUs can be offline at times, or the system can be suspended at 25 // occupancy. As CPUs can be offline at times, or the system can be suspended at
25 // other times, it is best for the consumer of this data to calculate percentage 26 // other times, it is best for the consumer of this data to calculate percentage
26 // occupancy information using suspend time data from 27 // occupancy information using suspend time data from
27 // PowerDataCollector::system_resumed_data. 28 // PowerDataCollector::system_resumed_data.
28 class CpuDataCollector { 29 class CpuDataCollector {
29 public: 30 public:
30 struct StateOccupancySample { 31 struct StateOccupancySample {
31 StateOccupancySample(); 32 StateOccupancySample();
32 StateOccupancySample(const StateOccupancySample& other); 33 StateOccupancySample(const StateOccupancySample& other);
33 ~StateOccupancySample(); 34 ~StateOccupancySample();
34 35
35 // The time when the data was sampled. 36 // The time when the data was sampled.
36 base::Time time; 37 base::Time time;
37 38
38 // Indicates whether the CPU is online. 39 // Indicates whether the CPU is online.
39 bool cpu_online; 40 bool cpu_online;
40 41
41 // A mapping from a CPU state to time spent in that state in milliseconds. 42 // A mapping from a CPU state to time spent in that state in milliseconds.
Daniel Erat 2017/05/03 23:39:35 nit: remove "in milliseconds" from this comment no
weidongg 2017/05/03 23:53:26 Done.
42 // For idle state samples, the name of the state at index i in this vector 43 // For idle state samples, the name of the state at index i in this vector
43 // is the name at index i in the vector returned by cpu_idle_state_names(). 44 // is the name at index i in the vector returned by cpu_idle_state_names().
44 // Similarly, for freq state occupancy, similar information is in the vector 45 // Similarly, for freq state occupancy, similar information is in the vector
45 // returned by cpu_freq_state_names(). 46 // returned by cpu_freq_state_names().
46 std::vector<int64_t> time_in_state; 47 std::vector<base::TimeDelta> time_in_state;
47 }; 48 };
48 49
49 typedef std::deque<StateOccupancySample> StateOccupancySampleDeque; 50 typedef std::deque<StateOccupancySample> StateOccupancySampleDeque;
50 51
51 const std::vector<std::string>& cpu_idle_state_names() const { 52 const std::vector<std::string>& cpu_idle_state_names() const {
52 return cpu_idle_state_names_; 53 return cpu_idle_state_names_;
53 } 54 }
54 55
55 const std::vector<StateOccupancySampleDeque>& cpu_idle_state_data() const { 56 const std::vector<StateOccupancySampleDeque>& cpu_idle_state_data() const {
56 return cpu_idle_state_data_; 57 return cpu_idle_state_data_;
57 } 58 }
58 59
59 const std::vector<std::string>& cpu_freq_state_names() const { 60 const std::vector<std::string>& cpu_freq_state_names() const {
60 return cpu_freq_state_names_; 61 return cpu_freq_state_names_;
61 } 62 }
62 63
63 const std::vector<StateOccupancySampleDeque>& cpu_freq_state_data() const { 64 const std::vector<StateOccupancySampleDeque>& cpu_freq_state_data() const {
64 return cpu_freq_state_data_; 65 return cpu_freq_state_data_;
65 } 66 }
66 67
67 CpuDataCollector(); 68 CpuDataCollector();
68 ~CpuDataCollector(); 69 ~CpuDataCollector();
69 70
70 // Starts a repeating timer which periodically runs a callback to collect 71 // Starts a repeating timer which periodically runs a callback to collect
71 // CPU state occupancy samples. 72 // CPU state occupancy samples.
72 void Start(); 73 void Start();
73 74
75 // Read CPU frequency data of a specific CPU from the file in |path| and fill
76 // |cpu_freq_state_names| and |freq_sample| with the data.
77 // Sample file looks like:
78 // 126000 223344
79 // 216000 93495
80 // ...
81 // 1800000 321907
82 static bool ReadCpuFreqTimeInState(
83 const base::FilePath& path,
84 std::vector<std::string>* cpu_freq_state_names,
85 CpuDataCollector::StateOccupancySample* freq_sample);
86
87 // Read CPU frequency data of all CPUs from the file in |path| and fill
88 // |cpu_freq_state_names| and |freq_samples| with the data. |cpu_count| is the
89 // number of possible CPUs on the system. Note that |freq_samples| is not
90 // empty and sample at index i in |freq_samples| corresponds to the freq state
91 // information of the i-th CPU. |cpu_online| of each sample must be set before
92 // calling this function.
93 // Sample file looks like:
94 // freq cpu0 cpu1
95 // 126000 223344 223311
96 // 216000 93495 93450
97 // ...
98 // 1800000 321907 331897
99 static bool ReadCpuFreqAllTimeInState(
100 int cpu_count,
101 const base::FilePath& path,
102 std::vector<std::string>* cpu_freq_state_names,
103 std::vector<CpuDataCollector::StateOccupancySample>* freq_samples);
104
74 private: 105 private:
75 // Posts callbacks to the blocking pool which collect CPU state occupancy 106 // Posts callbacks to the blocking pool which collect CPU state occupancy
76 // samples from the sysfs. 107 // samples from the sysfs.
77 void PostSampleCpuState(); 108 void PostSampleCpuState();
78 109
79 // This function commits the CPU count and samples read by 110 // This function commits the CPU count and samples read by
80 // SampleCpuStateAsync to |cpu_idle_state_data_| and 111 // SampleCpuStateAsync to |cpu_idle_state_data_| and
81 // |cpu_freq_state_data_|. Since UI is the consumer of CPU idle and freq data, 112 // |cpu_freq_state_data_|. Since UI is the consumer of CPU idle and freq data,
82 // this function should run on the UI thread. 113 // this function should run on the UI thread.
83 void SaveCpuStateSamplesOnUIThread( 114 void SaveCpuStateSamplesOnUIThread(
(...skipping 23 matching lines...) Expand all
107 // hence should be read/written to only from the blocking pool. 138 // hence should be read/written to only from the blocking pool.
108 int cpu_count_; 139 int cpu_count_;
109 140
110 base::WeakPtrFactory<CpuDataCollector> weak_ptr_factory_; 141 base::WeakPtrFactory<CpuDataCollector> weak_ptr_factory_;
111 DISALLOW_COPY_AND_ASSIGN(CpuDataCollector); 142 DISALLOW_COPY_AND_ASSIGN(CpuDataCollector);
112 }; 143 };
113 144
114 } // namespace chromeos 145 } // namespace chromeos
115 146
116 #endif // CHROME_BROWSER_CHROMEOS_POWER_CPU_DATA_COLLECTOR_H_ 147 #endif // CHROME_BROWSER_CHROMEOS_POWER_CPU_DATA_COLLECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698