| 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 |  | 
| Dmitri Plotnikov | 8ca08ab | 2022-04-26 21:10:34 -0700 | [diff] [blame] | 142 | if (isEnabled) { | 
| Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 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); | 
| Dmitri Plotnikov | 8ca08ab | 2022-04-26 21:10:34 -0700 | [diff] [blame] | 146 | isEnabled = false; | 
|  | 147 | } else { | 
|  | 148 | // If the counter is being enabled with an out-of-order timestamp, just push back | 
|  | 149 | // the timestamp to avoid having the situation where | 
|  | 150 | // timeInStateSinceUpdate > timeSinceUpdate | 
|  | 151 | if (timestamp < lastUpdateTimestamp) { | 
|  | 152 | timestamp = lastUpdateTimestamp; | 
|  | 153 | } | 
| Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 154 |  | 
| Dmitri Plotnikov | 8ca08ab | 2022-04-26 21:10:34 -0700 | [diff] [blame] | 155 | if (lastStateChangeTimestamp >= 0) { | 
|  | 156 | lastStateChangeTimestamp = timestamp; | 
|  | 157 | } | 
|  | 158 | isEnabled = true; | 
| Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 159 | } | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | template <class T> | 
|  | 163 | void MultiStateCounter<T>::setState(state_t state, time_t timestamp) { | 
| Dmitri Plotnikov | 8ca08ab | 2022-04-26 21:10:34 -0700 | [diff] [blame] | 164 | if (isEnabled && lastStateChangeTimestamp >= 0 && lastUpdateTimestamp >= 0) { | 
|  | 165 | // If the update arrived out-of-order, just push back the timestamp to | 
|  | 166 | // avoid having the situation where timeInStateSinceUpdate > timeSinceUpdate | 
|  | 167 | if (timestamp < lastUpdateTimestamp) { | 
|  | 168 | timestamp = lastUpdateTimestamp; | 
|  | 169 | } | 
|  | 170 |  | 
| Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 171 | if (timestamp >= lastStateChangeTimestamp) { | 
|  | 172 | states[currentState].timeInStateSinceUpdate += timestamp - lastStateChangeTimestamp; | 
|  | 173 | } else { | 
|  | 174 | ALOGE("setState is called with an earlier timestamp: %lu, previous timestamp: %lu\n", | 
|  | 175 | (unsigned long)timestamp, (unsigned long)lastStateChangeTimestamp); | 
|  | 176 | // The accumulated durations have become unreliable. For example, if the timestamp | 
|  | 177 | // sequence was 1000, 2000, 1000, 3000, if we accumulated the positive deltas, | 
|  | 178 | // we would get 4000, which is greater than (last - first). This could lead to | 
|  | 179 | // counts exceeding 100%. | 
|  | 180 | for (int i = 0; i < stateCount; i++) { | 
|  | 181 | states[i].timeInStateSinceUpdate = 0; | 
|  | 182 | } | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 183 | } | 
|  | 184 | } | 
|  | 185 | currentState = state; | 
|  | 186 | lastStateChangeTimestamp = timestamp; | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | template <class T> | 
| Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 190 | void MultiStateCounter<T>::setValue(state_t state, const T& value) { | 
|  | 191 | states[state].counter = value; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | template <class T> | 
| Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 195 | const T& MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) { | 
| Dmitri Plotnikov | 41f5663 | 2021-09-28 17:21:54 -0700 | [diff] [blame] | 196 | T* returnValue = &emptyValue; | 
|  | 197 |  | 
| Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 198 | // If the counter is disabled, we ignore the update, except when the counter got disabled after | 
|  | 199 | // the previous update, in which case we still need to pick up the residual delta. | 
|  | 200 | if (isEnabled || lastUpdateTimestamp < lastStateChangeTimestamp) { | 
| Dmitri Plotnikov | 8ca08ab | 2022-04-26 21:10:34 -0700 | [diff] [blame] | 201 | // If the update arrived out of order, just push back the timestamp to | 
|  | 202 | // avoid having the situation where timeInStateSinceUpdate > timeSinceUpdate | 
|  | 203 | if (timestamp < lastStateChangeTimestamp) { | 
|  | 204 | timestamp = lastStateChangeTimestamp; | 
|  | 205 | } | 
|  | 206 |  | 
| Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 207 | // Confirm the current state for the side-effect of updating the time-in-state | 
|  | 208 | // counter for the current state. | 
|  | 209 | setState(currentState, timestamp); | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 210 |  | 
| Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 211 | if (lastUpdateTimestamp >= 0) { | 
|  | 212 | if (timestamp > lastUpdateTimestamp) { | 
|  | 213 | if (delta(lastValue, value, &deltaValue)) { | 
| Dmitri Plotnikov | 41f5663 | 2021-09-28 17:21:54 -0700 | [diff] [blame] | 214 | returnValue = &deltaValue; | 
| Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 215 | time_t timeSinceUpdate = timestamp - lastUpdateTimestamp; | 
|  | 216 | for (int i = 0; i < stateCount; i++) { | 
|  | 217 | time_t timeInState = states[i].timeInStateSinceUpdate; | 
|  | 218 | if (timeInState) { | 
|  | 219 | add(&states[i].counter, deltaValue, timeInState, timeSinceUpdate); | 
|  | 220 | states[i].timeInStateSinceUpdate = 0; | 
|  | 221 | } | 
| Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 222 | } | 
| Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 223 | } else { | 
|  | 224 | std::stringstream str; | 
|  | 225 | str << "updateValue is called with a value " << valueToString(value) | 
|  | 226 | << ", which is lower than the previous value " << valueToString(lastValue) | 
|  | 227 | << "\n"; | 
|  | 228 | ALOGE("%s", str.str().c_str()); | 
| Dmitri Plotnikov | 4b238fb | 2021-11-09 11:52:08 -0800 | [diff] [blame] | 229 |  | 
|  | 230 | for (int i = 0; i < stateCount; i++) { | 
|  | 231 | states[i].timeInStateSinceUpdate = 0; | 
|  | 232 | } | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 233 | } | 
| Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 234 | } else if (timestamp < lastUpdateTimestamp) { | 
|  | 235 | ALOGE("updateValue is called with an earlier timestamp: %lu, previous: %lu\n", | 
|  | 236 | (unsigned long)timestamp, (unsigned long)lastUpdateTimestamp); | 
| Dmitri Plotnikov | 4b238fb | 2021-11-09 11:52:08 -0800 | [diff] [blame] | 237 |  | 
|  | 238 | for (int i = 0; i < stateCount; i++) { | 
|  | 239 | states[i].timeInStateSinceUpdate = 0; | 
|  | 240 | } | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 241 | } | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 242 | } | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 243 | } | 
|  | 244 | lastValue = value; | 
|  | 245 | lastUpdateTimestamp = timestamp; | 
| Dmitri Plotnikov | 41f5663 | 2021-09-28 17:21:54 -0700 | [diff] [blame] | 246 | return *returnValue; | 
| Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
|  | 249 | template <class T> | 
| Dmitri Plotnikov | 40dcce2 | 2021-12-10 15:38:17 -0800 | [diff] [blame] | 250 | void MultiStateCounter<T>::incrementValue(const T& increment, time_t timestamp) { | 
|  | 251 | T newValue = lastValue; | 
|  | 252 | add(&newValue, increment, 1 /* numerator */, 1 /* denominator */); | 
|  | 253 | updateValue(newValue, timestamp); | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | template <class T> | 
| Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 257 | void MultiStateCounter<T>::addValue(const T& value) { | 
|  | 258 | if (!isEnabled) { | 
|  | 259 | return; | 
|  | 260 | } | 
| Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 261 | add(&states[currentState].counter, value, 1 /* numerator */, 1 /* denominator */); | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
|  | 264 | template <class T> | 
| Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 265 | void MultiStateCounter<T>::reset() { | 
|  | 266 | lastStateChangeTimestamp = -1; | 
|  | 267 | lastUpdateTimestamp = -1; | 
|  | 268 | for (int i = 0; i < stateCount; i++) { | 
|  | 269 | states[i].timeInStateSinceUpdate = 0; | 
|  | 270 | states[i].counter = emptyValue; | 
|  | 271 | } | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | template <class T> | 
| Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 275 | uint16_t MultiStateCounter<T>::getStateCount() { | 
|  | 276 | return stateCount; | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | template <class T> | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 280 | const T& MultiStateCounter<T>::getCount(state_t state) { | 
|  | 281 | return states[state].counter; | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | template <class T> | 
|  | 285 | std::string MultiStateCounter<T>::toString() { | 
|  | 286 | std::stringstream str; | 
| Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 287 | str << "["; | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 288 | for (int i = 0; i < stateCount; i++) { | 
|  | 289 | if (i != 0) { | 
|  | 290 | str << ", "; | 
|  | 291 | } | 
| Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 292 | str << i << ": " << valueToString(states[i].counter); | 
|  | 293 | if (states[i].timeInStateSinceUpdate > 0) { | 
|  | 294 | str << " timeInStateSinceUpdate: " << states[i].timeInStateSinceUpdate; | 
|  | 295 | } | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 296 | } | 
|  | 297 | str << "]"; | 
| Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 298 | if (lastUpdateTimestamp >= 0) { | 
|  | 299 | str << " updated: " << lastUpdateTimestamp; | 
|  | 300 | } | 
|  | 301 | if (lastStateChangeTimestamp >= 0) { | 
|  | 302 | str << " currentState: " << currentState; | 
|  | 303 | if (lastStateChangeTimestamp > lastUpdateTimestamp) { | 
|  | 304 | str << " stateChanged: " << lastStateChangeTimestamp; | 
|  | 305 | } | 
|  | 306 | } else { | 
|  | 307 | str << " currentState: none"; | 
|  | 308 | } | 
| Dmitri Plotnikov | 8940e5d | 2021-09-23 14:56:30 -0700 | [diff] [blame] | 309 | if (!isEnabled) { | 
|  | 310 | str << " disabled"; | 
|  | 311 | } | 
| Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 312 | return str.str(); | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | } // namespace battery | 
|  | 316 | } // namespace android |