blob: 51eb69b00f501c1213a290f36bc00a6d391b8647 [file] [log] [blame]
Phil Burke1ce4912016-11-21 10:40:25 -08001/*
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
Phil Burkdec33ab2017-01-17 14:48:16 -080017#ifndef UTILITY_MONOTONIC_COUNTER_H
18#define UTILITY_MONOTONIC_COUNTER_H
Phil Burke1ce4912016-11-21 10:40:25 -080019
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 */
30class MonotonicCounter {
31
32public:
jiabind5bd06a2021-04-27 22:04:08 +000033 MonotonicCounter() = default;
34 virtual ~MonotonicCounter() = default;
Phil Burke1ce4912016-11-21 10:40:25 -080035
36 /**
37 * @return current value of the counter
38 */
39 int64_t get() const {
40 return mCounter64;
41 }
42
43 /**
Phil Burk58c1e6d2022-01-17 17:28:28 +000044 * Advance the current value to match the counter.
45 *
46 * Note that it will take several million years for the 64-bit
47 * counters to wrap around.
48 * So we do not use __builtin_sub_overflow.
49 * We want to know if overflow happens because of a bug.
Phil Burk7328a802017-08-30 09:29:48 -070050 */
Phil Burk18c84762018-12-18 12:15:35 -080051 void catchUpTo(int64_t counter) {
52 if ((counter - mCounter64) > 0) {
53 mCounter64 = counter;
54 }
Phil Burk7328a802017-08-30 09:29:48 -070055 }
56
57 /**
Phil Burke1ce4912016-11-21 10:40:25 -080058 * Advance the counter if delta is positive.
59 * @return current value of the counter
60 */
61 int64_t increment(int64_t delta) {
62 if (delta > 0) {
63 mCounter64 += delta;
64 }
65 return mCounter64;
66 }
67
68 /**
69 * Advance the 64-bit counter if (current32 - previousCurrent32) > 0.
70 * This can be used to convert a 32-bit counter that may be wrapping into
71 * a monotonic 64-bit counter.
72 *
73 * This counter32 should NOT be allowed to advance by more than 0x7FFFFFFF between calls.
74 * Think of the wrapping counter like a sine wave. If the frequency of the signal
75 * is more than half the sampling rate (Nyquist rate) then you cannot measure it properly.
76 * If the counter wraps around every 24 hours then we should measure it with a period
77 * of less than 12 hours.
78 *
79 * @return current value of the 64-bit counter
80 */
81 int64_t update32(int32_t counter32) {
Phil Burk58c1e6d2022-01-17 17:28:28 +000082 int32_t delta;
83 __builtin_sub_overflow(counter32, mCounter32, &delta);
Phil Burke1ce4912016-11-21 10:40:25 -080084 // protect against the mCounter64 going backwards
85 if (delta > 0) {
86 mCounter64 += delta;
87 mCounter32 = counter32;
88 }
89 return mCounter64;
90 }
91
92 /**
93 * Reset the stored value of the 32-bit counter.
94 * This is used if your counter32 has been reset to zero.
95 */
96 void reset32() {
97 mCounter32 = 0;
98 }
99
Phil Burk73af62a2017-10-26 12:11:47 -0700100 /**
101 * Round 64-bit counter up to a multiple of the period.
102 *
103 * @param period might be, for example, a buffer capacity
104 */
105 void roundUp64(int32_t period) {
106 if (period > 0) {
107 int64_t numPeriods = (mCounter64 + period - 1) / period;
108 mCounter64 = numPeriods * period;
109 }
110 }
111
Phil Burke1ce4912016-11-21 10:40:25 -0800112private:
113 int64_t mCounter64 = 0;
114 int32_t mCounter32 = 0;
115};
116
Phil Burkdec33ab2017-01-17 14:48:16 -0800117#endif //UTILITY_MONOTONIC_COUNTER_H