File: ConditionTimer.h

package info (click to toggle)
android-platform-frameworks-base 1%3A10.0.0%2Br36-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 322,016 kB
  • sloc: java: 962,234; cpp: 274,298; xml: 242,770; python: 5,060; sh: 1,432; ansic: 494; makefile: 54; sed: 19
file content (83 lines) | stat: -rw-r--r-- 2,751 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <gtest/gtest_prod.h>
#include <stdint.h>

namespace android {
namespace os {
namespace statsd {

/**
 * A simple stopwatch to time the duration of condition being true.
 *
 * The owner of the stopwatch (MetricProducer) is responsible to notify the stopwatch when condition
 * changes (start/pause), and when to start a new bucket (a new lap basically). All timestamps
 * should be elapsedRealTime in nano seconds.
 *
 * Keep the timer simple and inline everything. This class is *NOT* thread safe. Caller is
 * responsible for thread safety.
 */
class ConditionTimer {
public:
    explicit ConditionTimer(bool initCondition, int64_t bucketStartNs) : mCondition(initCondition) {
        if (initCondition) {
            mLastConditionTrueTimestampNs = bucketStartNs;
        }
    };

    // Tracks how long the condition has been stayed true in the *current* bucket.
    // When a new bucket is created, this value will be reset to 0.
    int64_t mTimerNs = 0;

    // Last elapsed real timestamp when condition turned to true
    // When a new bucket is created and the condition is true, then the timestamp is set
    // to be the bucket start timestamp.
    int64_t mLastConditionTrueTimestampNs = 0;

    bool mCondition = false;

    int64_t newBucketStart(int64_t nextBucketStartNs) {
        if (mCondition) {
            mTimerNs += (nextBucketStartNs - mLastConditionTrueTimestampNs);
            mLastConditionTrueTimestampNs = nextBucketStartNs;
        }

        int64_t temp = mTimerNs;
        mTimerNs = 0;
        return temp;
    }

    void onConditionChanged(bool newCondition, int64_t timestampNs) {
        if (newCondition == mCondition) {
            return;
        }
        mCondition = newCondition;
        if (newCondition) {
            mLastConditionTrueTimestampNs = timestampNs;
        } else {
            mTimerNs += (timestampNs - mLastConditionTrueTimestampNs);
        }
    }

    FRIEND_TEST(ConditionTimerTest, TestTimer_Inital_False);
    FRIEND_TEST(ConditionTimerTest, TestTimer_Inital_True);
};

}  // namespace statsd
}  // namespace os
}  // namespace android