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      * advance the current value to match the counter
45      */
catchUpTo(int64_t counter)46     void catchUpTo(int64_t counter) {
47         if ((counter - mCounter64) > 0) {
48             mCounter64 = counter;
49         }
50     }
51 
52     /**
53      * Advance the counter if delta is positive.
54      * @return current value of the counter
55      */
increment(int64_t delta)56     int64_t increment(int64_t delta) {
57         if (delta > 0) {
58             mCounter64 += delta;
59         }
60         return mCounter64;
61     }
62 
63     /**
64      * Advance the 64-bit counter if (current32 - previousCurrent32) > 0.
65      * This can be used to convert a 32-bit counter that may be wrapping into
66      * a monotonic 64-bit counter.
67      *
68      * This counter32 should NOT be allowed to advance by more than 0x7FFFFFFF between calls.
69      * Think of the wrapping counter like a sine wave. If the frequency of the signal
70      * is more than half the sampling rate (Nyquist rate) then you cannot measure it properly.
71      * If the counter wraps around every 24 hours then we should measure it with a period
72      * of less than 12 hours.
73      *
74      * @return current value of the 64-bit counter
75      */
update32(int32_t counter32)76     int64_t update32(int32_t counter32) {
77         int32_t delta = counter32 - mCounter32;
78         // protect against the mCounter64 going backwards
79         if (delta > 0) {
80             mCounter64 += delta;
81             mCounter32 = counter32;
82         }
83         return mCounter64;
84     }
85 
86     /**
87      * Reset the stored value of the 32-bit counter.
88      * This is used if your counter32 has been reset to zero.
89      */
reset32()90     void reset32() {
91         mCounter32 = 0;
92     }
93 
94     /**
95      * Round 64-bit counter up to a multiple of the period.
96      *
97      * @param period might be, for example, a buffer capacity
98      */
roundUp64(int32_t period)99     void roundUp64(int32_t period) {
100         if (period > 0) {
101             int64_t numPeriods = (mCounter64 + period - 1) / period;
102             mCounter64 = numPeriods * period;
103         }
104     }
105 
106 private:
107     int64_t mCounter64 = 0;
108     int32_t mCounter32 = 0;
109 };
110 
111 
112 #endif //UTILITY_MONOTONIC_COUNTER_H
113