Ocean
PowerMonitor.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_POWER_MONITOR_H
9 #define META_OCEAN_PLATFORM_ANDROID_POWER_MONITOR_H
10 
12 
14 #include "ocean/base/Thread.h"
15 #include "ocean/base/Timestamp.h"
16 
17 namespace Ocean
18 {
19 
20 namespace Platform
21 {
22 
23 namespace Android
24 {
25 
26 class OCEAN_PLATFORM_ANDROID_EXPORT PowerMonitor final : protected Thread
27 {
28  public:
29 
30  /**
31  * Definition of a scoped subscription object.
32  */
34 
35  protected:
36 
37  /**
38  * This class holds the relevant information of one measurement.
39  */
41  {
42  public:
43 
44  /**
45  * Creates a new measurement object.
46  * @param timestamp The timestamp at which the measurement was done, must be valid
47  * @param current The current value, with range (-infinity, infinity)
48  * @param voltage The voltage value, with range [0, infinity)
49  */
50  inline Measurement(const Timestamp& timestamp, const double current, const double voltage);
51 
52  public:
53 
54  /// The measurement's timestamp.
56 
57  /// The measurement's current value.
58  double current_;
59 
60  /// The measurement's voltage value.
61  double voltage_;
62  };
63 
64  /**
65  * Definition of a vector holding measurement objects.
66  */
67  using Measurements = std::vector<Measurement>;
68 
69  public:
70 
71  /**
72  * Default constructor.
73  * @param updateFrequency The update frequency to be used, in Hz, with range (0, infinity)
74  */
75  explicit PowerMonitor(const double updateFrequency);
76 
77  /**
78  * Destructs this monitor.
79  */
80  ~PowerMonitor() override;
81 
82  /**
83  * Returns whether this monitor is valid and ready to be used.
84  * @return True, if so
85  */
86  bool isValid() const;
87 
88  /**
89  * Returns the update frequency the monitor uses.
90  * @return The monitor's update frequency, in Hz
91  */
92  double updateFrequency() const;
93 
94  /**
95  * Resets the monitor.
96  * Stop all measurements before calling this function.
97  * @return True, if succeeded
98  */
99  bool reset();
100 
101  /**
102  * Starts a new measurement.
103  * @return The resulting subscription object, the measurement is active as long as the object exists
104  */
106 
107  /**
108  * Returns the average power consumption.
109  * @param power The resulting averaged power consumption
110  * @param standardDeviation Optional resulting standard deviation of the average power, nullptr if not of interest
111  * @param numberMeasurements Optional resulting number of measurements which have been used to determine the average power, nullptr if not of interest
112  * @return True, if succeeded
113  */
114  bool averagePower(double& power, double* standardDeviation = nullptr, size_t* numberMeasurements = nullptr) const;
115 
116  /**
117  * Returns the median power consumption.
118  * @param power The resulting median power consumption
119  * @param numberMeasurements Optional resulting number of measurements which have been used to determine the average power, nullptr if not of interest
120  * @return True, if succeeded
121  */
122  bool medianPower(double& power, size_t* numberMeasurements = nullptr) const;
123 
124  /**
125  * Determines the ideal update frequency the monitor should use.
126  * @param testDuration The test duration, in seconds, with range (0, infinity)
127  * @return The resulting update frequency, in Hz, -1 in case of an error
128  */
129  static double determineIdealUpdateFrequency(const double testDuration = 5.0);
130 
131  protected:
132 
133  /**
134  * Stops a measurement.
135  */
136  void stopMeasurement(const bool&);
137 
138  /**
139  * The thread run function.
140  */
141  void threadRun() override;
142 
143  /**
144  * Determines the ideal update frequency the monitor should use for a specific monitor value.
145  * @param updateFrequency The resulting update frequency, in Hz
146  * @param testDuration The test duration, in seconds, with range (0, infinity)
147  * @return True, if succeeded
148  * @tparam tForCurrent True, to determine the update frequency for the current value; False, to determine the update frequency for the voltage value
149  */
150  template <bool tForCurrent>
151  static bool determineIdealUpdateFrequency(double& updateFrequency, const double testDuration = 5.0);
152 
153  /**
154  * Determines the ideal update frequency from a given set of timestamps.
155  * @param timestamps The timestamps for which the update frequency will be determined
156  * @param updateFrequency The resulting update frequency, in Hz
157  * @return True, if succeeded
158  */
159  static bool determineIdealUpdateFrequency(const Timestamps& timestamps, double& updateFrequency);
160 
161  protected:
162 
163  /// The update frequency to be used, in Hz.
164  double updateFrequency_ = -1.0;
165 
166  /// True, if the monitor is active.
167  bool isActive_ = false;
168 
169  /// The measurements.
171 
172  /// The monitor's lock.
173  mutable Lock lock_;
174 };
175 
176 inline PowerMonitor::Measurement::Measurement(const Timestamp& timestamp, const double current, const double voltage) :
177  timestamp_(timestamp),
178  current_(current),
179  voltage_(voltage)
180 {
181  // nothing to do here
182 }
183 
184 }
185 
186 }
187 
188 }
189 
190 #endif // META_OCEAN_PLATFORM_ANDROID_POWER_MONITOR_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class holds the relevant information of one measurement.
Definition: PowerMonitor.h:41
Measurement(const Timestamp &timestamp, const double current, const double voltage)
Creates a new measurement object.
Definition: PowerMonitor.h:176
double voltage_
The measurement's voltage value.
Definition: PowerMonitor.h:61
double current_
The measurement's current value.
Definition: PowerMonitor.h:58
Timestamp timestamp_
The measurement's timestamp.
Definition: PowerMonitor.h:55
Definition: PowerMonitor.h:27
static bool determineIdealUpdateFrequency(const Timestamps &timestamps, double &updateFrequency)
Determines the ideal update frequency from a given set of timestamps.
bool reset()
Resets the monitor.
PowerMonitor(const double updateFrequency)
Default constructor.
bool medianPower(double &power, size_t *numberMeasurements=nullptr) const
Returns the median power consumption.
Measurements measurements_
The measurements.
Definition: PowerMonitor.h:170
void stopMeasurement(const bool &)
Stops a measurement.
ScopedSubscription startMeasurement()
Starts a new measurement.
static double determineIdealUpdateFrequency(const double testDuration=5.0)
Determines the ideal update frequency the monitor should use.
static bool determineIdealUpdateFrequency(double &updateFrequency, const double testDuration=5.0)
Determines the ideal update frequency the monitor should use for a specific monitor value.
double updateFrequency() const
Returns the update frequency the monitor uses.
Lock lock_
The monitor's lock.
Definition: PowerMonitor.h:173
bool isValid() const
Returns whether this monitor is valid and ready to be used.
~PowerMonitor() override
Destructs this monitor.
bool averagePower(double &power, double *standardDeviation=nullptr, size_t *numberMeasurements=nullptr) const
Returns the average power consumption.
void threadRun() override
The thread run function.
std::vector< Measurement > Measurements
Definition of a vector holding measurement objects.
Definition: PowerMonitor.h:67
This class implements a subscription object which can be used unique subscriptions to e....
Definition: ScopedSubscription.h:28
This class implements a thread.
Definition: Thread.h:115
This class implements a timestamp.
Definition: Timestamp.h:36
std::vector< Timestamp > Timestamps
Definition of a vector holding Timestamp objects.
Definition: Timestamp.h:19
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15