Ocean
ProcessorMonitor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_ANDROID_PROCESSOR_MONITOR_H
9 #define META_OCEAN_PLATFORM_ANDROID_PROCESSOR_MONITOR_H
10 
13 
14 #include "ocean/base/Singleton.h"
15 #include "ocean/base/Timestamp.h"
16 #include "ocean/base/Thread.h"
17 
18 namespace Ocean
19 {
20 
21 namespace Platform
22 {
23 
24 namespace Android
25 {
26 
27 /**
28  * This class implements a processor monitor.
29  * @ingroup platformandroid
30  */
31 class OCEAN_PLATFORM_ANDROID_EXPORT ProcessorMonitor :
32  public Singleton<ProcessorMonitor>,
33  protected Thread
34 {
35  friend class Singleton<ProcessorMonitor>;
36 
37  public:
38 
39  /**
40  * Definition of a vector holding processor operation frequencies.
41  */
42  typedef std::vector<Processor::Frequency> Frequencies;
43 
44  /**
45  * Definition of a pair combining a timestamp with frequencies.
46  */
47  typedef std::pair<Timestamp, Frequencies> FrequencyPair;
48 
49  /**
50  * Definition of a vector holding frequency pairs.
51  */
52  typedef std::vector<FrequencyPair> FrequencyPairs;
53 
54  /**
55  * Definition of an ordered map mapping timestamps to frequencies.
56  */
57  typedef std::map<Timestamp, Frequencies> FrequencyMap;
58 
59  public:
60 
61  /**
62  * Returns the number of CPU cores this monitor controls.
63  * @return Number of CPU cores
64  */
65  inline unsigned int installedCores() const;
66 
67  /**
68  * Returns the update rate of this monitor in seconds.
69  * @return Update rate in seconds, 0.01 by default
70  */
71  inline double updateRate() const;
72 
73  /**
74  * Sets the update rate of this monitor in seconds.
75  * @param rate Update rate in seconds to be set, with range (0, infinity)
76  */
77  inline void setUpdateRate(const double rate);
78 
79  /**
80  * Returns the gathered monitor values between two timestamp.
81  * @param beginTimestamp Begin timestamp, -1 to return entire information database
82  * @param endTimestamp End timestamp
83  * @return Gathered information
84  */
85  FrequencyPairs monitorData(const Timestamp beginTimestamp, const Timestamp endTimestamp);
86 
87  /**
88  * Clears the monitor information.
89  */
90  void clear();
91 
92  private:
93 
94  /**
95  * Creates a new processor monitor object.
96  */
98 
99  /**
100  * Destructs a processor monitor object.
101  */
102  ~ProcessorMonitor() override;
103 
104  /**
105  * Either this function has to be overloaded in derived classes or the thread run callback functions must be set.
106  * @see Thread::threadRun().
107  */
108  void threadRun() override;
109 
110  private:
111 
112  /// Monitor CPU core number.
113  unsigned int installedCores_ = 0u;
114 
115  /// Monitor update rate in seconds.
116  double updateRate_ = 0.01;
117 
118  /// Previous information timestamp.
120 
121  /// Monitor frequency map.
123 
124  /// Monitor lock object.
126 };
127 
128 /**
129  * This class implements a processor statistic object.
130  * @ingroup platformandroid
131  */
132 class OCEAN_PLATFORM_ANDROID_EXPORT ProcessorStatistic
133 {
134  public:
135 
136  /**
137  * Creates a new statistic object.
138  */
139  ProcessorStatistic() = default;
140 
141  /**
142  * Starts the statistic.
143  */
144  void start();
145 
146  /**
147  * Stops the statistic.
148  */
149  void stop();
150 
151  /**
152  * Resets the statistic.
153  */
154  void reset();
155 
156  /**
157  * Returns the measurement duration.
158  * @return Measurement duration
159  */
160  inline double duration() const;
161 
162  /**
163  * Returns the number of measurements.
164  * @return Measurement number
165  */
166  inline unsigned int measurements() const;
167 
168  /**
169  * Returns the average number of active CPU cores.
170  * @return Average active CPU cores
171  */
172  inline double averageActiveCores() const;
173 
174  /**
175  * Returns the average (of all active cores) CPU frequencies.
176  * @return Average CPU frequency
177  */
178  inline double averageFrequency() const;
179 
180  /**
181  * Returns the ratio between average CPU frequency and maximal possible CPU frequency.
182  * @return Average CPU performance factor.
183  */
184  inline double averagePerformanceRate() const;
185 
186  /**
187  * Returns the minimal (of all active cores) CPU frequency.
188  * @return Minimal CPU frequency
189  */
190  inline double minimalFrequency() const;
191 
192  /**
193  * Returns the maximal (of all active cores) CPU frequency.
194  * @return Maximal CPU frequency
195  */
196  inline double maximalFrequency() const;
197 
198  private:
199 
200  /// Start timestamp of the statistic.
202 
203  /// Measurement duration.
204  double duration_ = 0.0;
205 
206  /// Average active CPU cores.
207  double averageActiveCores_ = -1.0;
208 
209  /// Average total CPU frequency.
210  double averageFrequency_ = -1.0;
211 
212  /// Minimal CPU frequency.
213  double minimalFrequency_ = -1.0;
214 
215  /// Maximal CPU frequency.
216  double maximalFrequency_ = -1.0;
217 
218  /// Measurement number.
219  unsigned int measurements_ = 0u;
220 
221  /// Pairs of timestamps and frequencies.
223 };
224 
225 inline unsigned int ProcessorMonitor::installedCores() const
226 {
227  ocean_assert(installedCores_ > 0u);
228  return installedCores_;
229 }
230 
231 inline double ProcessorMonitor::updateRate() const
232 {
233  return updateRate_;
234 }
235 
236 inline void ProcessorMonitor::setUpdateRate(const double rate)
237 {
238  ocean_assert(rate > 0.0);
239  updateRate_ = rate;
240 }
241 
242 inline double ProcessorStatistic::duration() const
243 {
244  return duration_;
245 }
246 
247 inline unsigned int ProcessorStatistic::measurements() const
248 {
249  return measurements_;
250 }
251 
253 {
254  return averageActiveCores_;
255 }
256 
258 {
259  return averageFrequency_;
260 }
261 
263 {
264  ocean_assert(maximalFrequency_ > 0.0);
265  if (maximalFrequency_ <= 0.0)
266  {
267  return -1.0;
268  }
269 
270  const double maxPossibleFrequency = Processor::maxFrequency();
271  if (maxPossibleFrequency <= 0.0)
272  {
273  return -1;
274  }
275 
276  ocean_assert(maxPossibleFrequency >= averageFrequency_);
277 
278  return averageFrequency_ / maxPossibleFrequency;
279 }
280 
282 {
283  return minimalFrequency_;
284 }
285 
287 {
288  return maximalFrequency_;
289 }
290 
291 }
292 
293 }
294 
295 }
296 
297 #endif // META_OCEAN_PLATFORM_ANDROID_PROCESSOR_MONITOR_H
This class implements a recursive lock object.
Definition: Lock.h:31
static Frequency maxFrequency(const unsigned int core=0u)
Returns the maximal operation frequency of a specified core.
This class implements a processor monitor.
Definition: ProcessorMonitor.h:34
std::vector< FrequencyPair > FrequencyPairs
Definition of a vector holding frequency pairs.
Definition: ProcessorMonitor.h:52
FrequencyMap frequencyMap_
Monitor frequency map.
Definition: ProcessorMonitor.h:122
~ProcessorMonitor() override
Destructs a processor monitor object.
double updateRate_
Monitor update rate in seconds.
Definition: ProcessorMonitor.h:116
ProcessorMonitor()
Creates a new processor monitor object.
std::map< Timestamp, Frequencies > FrequencyMap
Definition of an ordered map mapping timestamps to frequencies.
Definition: ProcessorMonitor.h:57
unsigned int installedCores() const
Returns the number of CPU cores this monitor controls.
Definition: ProcessorMonitor.h:225
std::pair< Timestamp, Frequencies > FrequencyPair
Definition of a pair combining a timestamp with frequencies.
Definition: ProcessorMonitor.h:47
unsigned int installedCores_
Monitor CPU core number.
Definition: ProcessorMonitor.h:113
void threadRun() override
Either this function has to be overloaded in derived classes or the thread run callback functions mus...
Timestamp previousTimestamp_
Previous information timestamp.
Definition: ProcessorMonitor.h:119
void setUpdateRate(const double rate)
Sets the update rate of this monitor in seconds.
Definition: ProcessorMonitor.h:236
double updateRate() const
Returns the update rate of this monitor in seconds.
Definition: ProcessorMonitor.h:231
std::vector< Processor::Frequency > Frequencies
Definition of a vector holding processor operation frequencies.
Definition: ProcessorMonitor.h:42
Lock lock_
Monitor lock object.
Definition: ProcessorMonitor.h:125
void clear()
Clears the monitor information.
FrequencyPairs monitorData(const Timestamp beginTimestamp, const Timestamp endTimestamp)
Returns the gathered monitor values between two timestamp.
This class implements a processor statistic object.
Definition: ProcessorMonitor.h:133
double averageFrequency() const
Returns the average (of all active cores) CPU frequencies.
Definition: ProcessorMonitor.h:257
double averagePerformanceRate() const
Returns the ratio between average CPU frequency and maximal possible CPU frequency.
Definition: ProcessorMonitor.h:262
double averageActiveCores_
Average active CPU cores.
Definition: ProcessorMonitor.h:207
ProcessorStatistic()=default
Creates a new statistic object.
double maximalFrequency_
Maximal CPU frequency.
Definition: ProcessorMonitor.h:216
double duration_
Measurement duration.
Definition: ProcessorMonitor.h:204
double minimalFrequency_
Minimal CPU frequency.
Definition: ProcessorMonitor.h:213
double averageFrequency_
Average total CPU frequency.
Definition: ProcessorMonitor.h:210
double averageActiveCores() const
Returns the average number of active CPU cores.
Definition: ProcessorMonitor.h:252
unsigned int measurements() const
Returns the number of measurements.
Definition: ProcessorMonitor.h:247
unsigned int measurements_
Measurement number.
Definition: ProcessorMonitor.h:219
Timestamp startTimestamp_
Start timestamp of the statistic.
Definition: ProcessorMonitor.h:201
double maximalFrequency() const
Returns the maximal (of all active cores) CPU frequency.
Definition: ProcessorMonitor.h:286
double duration() const
Returns the measurement duration.
Definition: ProcessorMonitor.h:242
ProcessorMonitor::FrequencyPairs frequencyPairs_
Pairs of timestamps and frequencies.
Definition: ProcessorMonitor.h:222
double minimalFrequency() const
Returns the minimal (of all active cores) CPU frequency.
Definition: ProcessorMonitor.h:281
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This class implements a thread.
Definition: Thread.h:115
This class implements a timestamp.
Definition: Timestamp.h:36
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15