summaryrefslogtreecommitdiff
path: root/camera/include/HdrPlusProfiler.h
blob: cc277f87975da9bf91ff37ca2309fb1f66fc68d4 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright 2017 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.
 */
#ifndef HDR_PLUS_PROFILER_H
#define HDR_PLUS_PROFILER_H

#ifndef ENABLE_HDRPLUS_PROFILER
#define ENABLE_HDRPLUS_PROFILER 0
#endif

#if !ENABLE_HDRPLUS_PROFILER

// If profiler is not enabled, make every macro a noop
#define DECLARE_PROFILER_TIMER(_var, _description)
#define START_PROFILER_TIMER(_var) do {} while(0)
#define END_PROFILER_TIMER(_var) do {} while(0)
#define SCOPE_PROFILER_TIMER(_description) do {} while(0)

#else

#include <string>

/*
 * Declare a profiler timer.
 *
 * _var is the variable that will be declared as a timer.
 * _description is the description for this timer. It will be used when logging the timer duration.
 */
#define DECLARE_PROFILER_TIMER(_var, _description) pbcamera::TimerLogger _var = {_description}

/*
 * Start a timer.
 *
 * _var is a timer declared with DECALRE_PROFILER_TIMER.
 */
#define START_PROFILER_TIMER(_var) ((_var).start())

/*
 * End a timer and log the duration since last start.
 *
 * _var is a timer declared with DECALRE_PROFILER_TIMER.
 */
#define END_PROFILER_TIMER(_var) ((_var).end())

/*
 * Declare a scope timer that starts now and ends when it goes out of scope.
 *
 * __description is the description for this timer. It will be used when logging the timer duration.
 */
#define SCOPE_PROFILER_TIMER(_description) pbcamera::ScopeTimerLogger _timer(_description)

namespace pbcamera {

#define TIMER_TAG "[PROFILE_TIMER]"

/**
 * TimerLogger provides a timer to log the duration between start() and end().
 */
class TimerLogger {
public:
    TimerLogger(const char *name) : mName(name), mInvalid(true) {};

    // Start the timer.
    void start() {
        mInvalid = (clock_gettime(kClockId, &mStartTime) != 0);
    }

    // End the timer and log the duration since last start.
    void end() {
        if (mInvalid) {
            ALOGE("%s <%s> start time is invalid.", TIMER_TAG, mName.c_str());
            return;
        }

        struct timespec endTime;
        mInvalid = (clock_gettime(kClockId, &endTime) != 0);
        if (mInvalid) {
            ALOGE("%s <%s> end time is invalid.", TIMER_TAG, mName.c_str());
            return;
        }

        int64_t startNs = static_cast<int64_t>(mStartTime.tv_sec) * kNsPerSec + mStartTime.tv_nsec;
        int64_t endNs = static_cast<int64_t>(endTime.tv_sec) * kNsPerSec + endTime.tv_nsec;
        ALOGI("%s <%s> took %f ms.", TIMER_TAG, mName.c_str(),
            static_cast<float>(endNs - startNs) / kNsPerMs);
    }

private:
    const static int64_t kNsPerSec = 1000000000;
    const static int64_t kNsPerMs = 1000000;
    const static clockid_t kClockId = CLOCK_BOOTTIME;

    std::string mName;
    struct timespec mStartTime;
    bool mInvalid;

};

/**
 * ScopeTimerLogger provides a timer to log the duration of the instance lifetime.
 */
class ScopeTimerLogger {
public:
    ScopeTimerLogger(const char *name) : mTimerLogger(name) { mTimerLogger.start(); };
    virtual ~ScopeTimerLogger() { mTimerLogger.end(); };
private:
    TimerLogger mTimerLogger;
};

} // namespace pbcamera

#endif // !ENABLE_HDRPLUS_PROFILER

#endif // HDR_PLUS_PROFILER_H