Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * Android BPF library - public API |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #pragma once |
| 19 | |
| 20 | #include <inttypes.h> |
| 21 | #include <log/log.h> |
| 22 | #include <time.h> |
| 23 | #include <sstream> |
| 24 | #include <string> |
| 25 | |
| 26 | /** |
| 27 | * An object that can track changes of some value over time, taking into account an additional |
| 28 | * dimension: the object's state. As the tracked value changes, the deltas are distributed |
| 29 | * among the object states in accordance with the time spent in those states. |
| 30 | */ |
| 31 | namespace android { |
| 32 | namespace battery { |
| 33 | |
| 34 | typedef uint16_t state_t; |
| 35 | |
| 36 | template <class T> |
| 37 | class MultiStateCounter { |
| 38 | uint16_t stateCount; |
| 39 | state_t currentState; |
| 40 | time_t lastStateChangeTimestamp; |
| 41 | T emptyValue; |
| 42 | T lastValue; |
| 43 | time_t lastUpdateTimestamp; |
| 44 | T deltaValue; |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 45 | bool isEnabled; |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 46 | |
| 47 | struct State { |
| 48 | time_t timeInStateSinceUpdate; |
| 49 | T counter; |
| 50 | }; |
| 51 | |
| 52 | State* states; |
| 53 | |
| 54 | public: |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 55 | MultiStateCounter(uint16_t stateCount, const T& emptyValue); |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 56 | |
| 57 | virtual ~MultiStateCounter(); |
| 58 | |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 59 | void setEnabled(bool enabled, time_t timestamp); |
| 60 | |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 61 | void setState(state_t state, time_t timestamp); |
| 62 | |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 63 | void setValue(state_t state, const T& value); |
| 64 | |
Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 65 | /** |
Dmitri Plotnikov | 40dcce2 | 2021-12-10 15:38:17 -0800 | [diff] [blame^] | 66 | * Updates the value by distributing the delta from the previously set value |
| 67 | * among states according to their respective time-in-state. |
| 68 | * Returns the delta from the previously set value. |
Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 69 | */ |
| 70 | const T& updateValue(const T& value, time_t timestamp); |
| 71 | |
Dmitri Plotnikov | 40dcce2 | 2021-12-10 15:38:17 -0800 | [diff] [blame^] | 72 | /** |
| 73 | * Updates the value by distributing the specified increment among states according |
| 74 | * to their respective time-in-state. |
| 75 | */ |
| 76 | void incrementValue(const T& increment, time_t timestamp); |
| 77 | |
| 78 | /** |
| 79 | * Adds the specified increment to the value for the current state, without affecting |
| 80 | * the last updated value or timestamp. Ignores partial time-in-state: the entirety of |
| 81 | * the increment is given to the current state. |
| 82 | */ |
| 83 | void addValue(const T& increment); |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 84 | |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 85 | void reset(); |
| 86 | |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 87 | uint16_t getStateCount(); |
| 88 | |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 89 | const T& getCount(state_t state); |
| 90 | |
| 91 | std::string toString(); |
| 92 | |
| 93 | private: |
| 94 | /** |
| 95 | * Subtracts previousValue from newValue and returns the result in outValue. |
| 96 | * Returns true iff the combination of previousValue and newValue is valid |
| 97 | * (newValue >= prevValue) |
| 98 | */ |
| 99 | bool delta(const T& previousValue, const T& newValue, T* outValue) const; |
| 100 | |
| 101 | /** |
| 102 | * Adds value2 to value1 and stores the result in value1. Denominator is |
| 103 | * guaranteed to be non-zero. |
| 104 | */ |
| 105 | void add(T* value1, const T& value2, const uint64_t numerator, |
| 106 | const uint64_t denominator) const; |
| 107 | |
| 108 | std::string valueToString(const T& value) const; |
| 109 | }; |
| 110 | |
| 111 | // ---------------------- MultiStateCounter Implementation ------------------------- |
| 112 | // Since MultiStateCounter is a template, the implementation must be inlined. |
| 113 | |
| 114 | template <class T> |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 115 | MultiStateCounter<T>::MultiStateCounter(uint16_t stateCount, const T& emptyValue) |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 116 | : stateCount(stateCount), |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 117 | currentState(0), |
| 118 | lastStateChangeTimestamp(-1), |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 119 | emptyValue(emptyValue), |
| 120 | lastValue(emptyValue), |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 121 | lastUpdateTimestamp(-1), |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 122 | deltaValue(emptyValue), |
| 123 | isEnabled(true) { |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 124 | states = new State[stateCount]; |
| 125 | for (int i = 0; i < stateCount; i++) { |
| 126 | states[i].timeInStateSinceUpdate = 0; |
| 127 | states[i].counter = emptyValue; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | template <class T> |
| 132 | MultiStateCounter<T>::~MultiStateCounter() { |
| 133 | delete[] states; |
| 134 | }; |
| 135 | |
| 136 | template <class T> |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 137 | void MultiStateCounter<T>::setEnabled(bool enabled, time_t timestamp) { |
| 138 | if (enabled == isEnabled) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | if (!enabled) { |
| 143 | // Confirm the current state for the side-effect of updating the time-in-state |
| 144 | // counter for the current state. |
| 145 | setState(currentState, timestamp); |
| 146 | } |
| 147 | |
| 148 | isEnabled = enabled; |
| 149 | |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 150 | if (lastStateChangeTimestamp >= 0) { |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 151 | lastStateChangeTimestamp = timestamp; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | template <class T> |
| 156 | void MultiStateCounter<T>::setState(state_t state, time_t timestamp) { |
| 157 | if (isEnabled && lastStateChangeTimestamp >= 0) { |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 158 | if (timestamp >= lastStateChangeTimestamp) { |
| 159 | states[currentState].timeInStateSinceUpdate += timestamp - lastStateChangeTimestamp; |
| 160 | } else { |
| 161 | ALOGE("setState is called with an earlier timestamp: %lu, previous timestamp: %lu\n", |
| 162 | (unsigned long)timestamp, (unsigned long)lastStateChangeTimestamp); |
| 163 | // The accumulated durations have become unreliable. For example, if the timestamp |
| 164 | // sequence was 1000, 2000, 1000, 3000, if we accumulated the positive deltas, |
| 165 | // we would get 4000, which is greater than (last - first). This could lead to |
| 166 | // counts exceeding 100%. |
| 167 | for (int i = 0; i < stateCount; i++) { |
| 168 | states[i].timeInStateSinceUpdate = 0; |
| 169 | } |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | currentState = state; |
| 173 | lastStateChangeTimestamp = timestamp; |
| 174 | } |
| 175 | |
| 176 | template <class T> |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 177 | void MultiStateCounter<T>::setValue(state_t state, const T& value) { |
| 178 | states[state].counter = value; |
| 179 | } |
| 180 | |
| 181 | template <class T> |
Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 182 | const T& MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) { |
Dmitri Plotnikov | 41f5663 | 2021-09-28 17:21:54 -0700 | [diff] [blame] | 183 | T* returnValue = &emptyValue; |
| 184 | |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 185 | // If the counter is disabled, we ignore the update, except when the counter got disabled after |
| 186 | // the previous update, in which case we still need to pick up the residual delta. |
| 187 | if (isEnabled || lastUpdateTimestamp < lastStateChangeTimestamp) { |
| 188 | // Confirm the current state for the side-effect of updating the time-in-state |
| 189 | // counter for the current state. |
| 190 | setState(currentState, timestamp); |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 191 | |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 192 | if (lastUpdateTimestamp >= 0) { |
| 193 | if (timestamp > lastUpdateTimestamp) { |
| 194 | if (delta(lastValue, value, &deltaValue)) { |
Dmitri Plotnikov | 41f5663 | 2021-09-28 17:21:54 -0700 | [diff] [blame] | 195 | returnValue = &deltaValue; |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 196 | time_t timeSinceUpdate = timestamp - lastUpdateTimestamp; |
| 197 | for (int i = 0; i < stateCount; i++) { |
| 198 | time_t timeInState = states[i].timeInStateSinceUpdate; |
| 199 | if (timeInState) { |
| 200 | add(&states[i].counter, deltaValue, timeInState, timeSinceUpdate); |
| 201 | states[i].timeInStateSinceUpdate = 0; |
| 202 | } |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 203 | } |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 204 | } else { |
| 205 | std::stringstream str; |
| 206 | str << "updateValue is called with a value " << valueToString(value) |
| 207 | << ", which is lower than the previous value " << valueToString(lastValue) |
| 208 | << "\n"; |
| 209 | ALOGE("%s", str.str().c_str()); |
Dmitri Plotnikov | 4b238fb | 2021-11-09 11:52:08 -0800 | [diff] [blame] | 210 | |
| 211 | for (int i = 0; i < stateCount; i++) { |
| 212 | states[i].timeInStateSinceUpdate = 0; |
| 213 | } |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 214 | } |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 215 | } else if (timestamp < lastUpdateTimestamp) { |
| 216 | ALOGE("updateValue is called with an earlier timestamp: %lu, previous: %lu\n", |
| 217 | (unsigned long)timestamp, (unsigned long)lastUpdateTimestamp); |
Dmitri Plotnikov | 4b238fb | 2021-11-09 11:52:08 -0800 | [diff] [blame] | 218 | |
| 219 | for (int i = 0; i < stateCount; i++) { |
| 220 | states[i].timeInStateSinceUpdate = 0; |
| 221 | } |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 222 | } |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 223 | } |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 224 | } |
| 225 | lastValue = value; |
| 226 | lastUpdateTimestamp = timestamp; |
Dmitri Plotnikov | 41f5663 | 2021-09-28 17:21:54 -0700 | [diff] [blame] | 227 | return *returnValue; |
Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | template <class T> |
Dmitri Plotnikov | 40dcce2 | 2021-12-10 15:38:17 -0800 | [diff] [blame^] | 231 | void MultiStateCounter<T>::incrementValue(const T& increment, time_t timestamp) { |
| 232 | T newValue = lastValue; |
| 233 | add(&newValue, increment, 1 /* numerator */, 1 /* denominator */); |
| 234 | updateValue(newValue, timestamp); |
| 235 | } |
| 236 | |
| 237 | template <class T> |
Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 238 | void MultiStateCounter<T>::addValue(const T& value) { |
| 239 | if (!isEnabled) { |
| 240 | return; |
| 241 | } |
Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 242 | add(&states[currentState].counter, value, 1 /* numerator */, 1 /* denominator */); |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | template <class T> |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 246 | void MultiStateCounter<T>::reset() { |
| 247 | lastStateChangeTimestamp = -1; |
| 248 | lastUpdateTimestamp = -1; |
| 249 | for (int i = 0; i < stateCount; i++) { |
| 250 | states[i].timeInStateSinceUpdate = 0; |
| 251 | states[i].counter = emptyValue; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | template <class T> |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 256 | uint16_t MultiStateCounter<T>::getStateCount() { |
| 257 | return stateCount; |
| 258 | } |
| 259 | |
| 260 | template <class T> |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 261 | const T& MultiStateCounter<T>::getCount(state_t state) { |
| 262 | return states[state].counter; |
| 263 | } |
| 264 | |
| 265 | template <class T> |
| 266 | std::string MultiStateCounter<T>::toString() { |
| 267 | std::stringstream str; |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 268 | str << "["; |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 269 | for (int i = 0; i < stateCount; i++) { |
| 270 | if (i != 0) { |
| 271 | str << ", "; |
| 272 | } |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 273 | str << i << ": " << valueToString(states[i].counter); |
| 274 | if (states[i].timeInStateSinceUpdate > 0) { |
| 275 | str << " timeInStateSinceUpdate: " << states[i].timeInStateSinceUpdate; |
| 276 | } |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 277 | } |
| 278 | str << "]"; |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 279 | if (lastUpdateTimestamp >= 0) { |
| 280 | str << " updated: " << lastUpdateTimestamp; |
| 281 | } |
| 282 | if (lastStateChangeTimestamp >= 0) { |
| 283 | str << " currentState: " << currentState; |
| 284 | if (lastStateChangeTimestamp > lastUpdateTimestamp) { |
| 285 | str << " stateChanged: " << lastStateChangeTimestamp; |
| 286 | } |
| 287 | } else { |
| 288 | str << " currentState: none"; |
| 289 | } |
Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 290 | if (!isEnabled) { |
| 291 | str << " disabled"; |
| 292 | } |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 293 | return str.str(); |
| 294 | } |
| 295 | |
| 296 | } // namespace battery |
| 297 | } // namespace android |