1 /* 2 * Copyright 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef UTILITY_MONOTONIC_COUNTER_H 18 #define UTILITY_MONOTONIC_COUNTER_H 19 20 #include <stdint.h> 21 22 /** 23 * Maintain a 64-bit monotonic counter. 24 * Can be used to track a 32-bit counter that wraps or gets reset. 25 * 26 * Note that this is not atomic and has no interior locks. 27 * A caller will need to provide their own exterior locking 28 * if they need to use it from multiple threads. 29 */ 30 class MonotonicCounter { 31 32 public: MonotonicCounter()33 MonotonicCounter() {}; ~MonotonicCounter()34 virtual ~MonotonicCounter() {}; 35 36 /** 37 * @return current value of the counter 38 */ get()39 int64_t get() const { 40 return mCounter64; 41 } 42 43 /** 44 * set the current value of the counter 45 */ set(int64_t counter)46 void set(int64_t counter) { 47 mCounter64 = counter; 48 } 49 50 /** 51 * Advance the counter if delta is positive. 52 * @return current value of the counter 53 */ increment(int64_t delta)54 int64_t increment(int64_t delta) { 55 if (delta > 0) { 56 mCounter64 += delta; 57 } 58 return mCounter64; 59 } 60 61 /** 62 * Advance the 64-bit counter if (current32 - previousCurrent32) > 0. 63 * This can be used to convert a 32-bit counter that may be wrapping into 64 * a monotonic 64-bit counter. 65 * 66 * This counter32 should NOT be allowed to advance by more than 0x7FFFFFFF between calls. 67 * Think of the wrapping counter like a sine wave. If the frequency of the signal 68 * is more than half the sampling rate (Nyquist rate) then you cannot measure it properly. 69 * If the counter wraps around every 24 hours then we should measure it with a period 70 * of less than 12 hours. 71 * 72 * @return current value of the 64-bit counter 73 */ update32(int32_t counter32)74 int64_t update32(int32_t counter32) { 75 int32_t delta = counter32 - mCounter32; 76 // protect against the mCounter64 going backwards 77 if (delta > 0) { 78 mCounter64 += delta; 79 mCounter32 = counter32; 80 } 81 return mCounter64; 82 } 83 84 /** 85 * Reset the stored value of the 32-bit counter. 86 * This is used if your counter32 has been reset to zero. 87 */ reset32()88 void reset32() { 89 mCounter32 = 0; 90 } 91 92 /** 93 * Round 64-bit counter up to a multiple of the period. 94 * 95 * @param period might be, for example, a buffer capacity 96 */ roundUp64(int32_t period)97 void roundUp64(int32_t period) { 98 if (period > 0) { 99 int64_t numPeriods = (mCounter64 + period - 1) / period; 100 mCounter64 = numPeriods * period; 101 } 102 } 103 104 private: 105 int64_t mCounter64 = 0; 106 int32_t mCounter32 = 0; 107 }; 108 109 110 #endif //UTILITY_MONOTONIC_COUNTER_H 111