Home | History | Annotate | Download | only in include
      1 /*
      2  * Copyright 2017 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 #ifndef HDR_PLUS_PROFILER_H
     17 #define HDR_PLUS_PROFILER_H
     18 
     19 #ifndef ENABLE_HDRPLUS_PROFILER
     20 #define ENABLE_HDRPLUS_PROFILER 0
     21 #endif
     22 
     23 #if !ENABLE_HDRPLUS_PROFILER
     24 
     25 // If profiler is not enabled, make every macro a noop
     26 #define DECLARE_PROFILER_TIMER(_var, _description)
     27 #define START_PROFILER_TIMER(_var) do {} while(0)
     28 #define END_PROFILER_TIMER(_var) do {} while(0)
     29 #define SCOPE_PROFILER_TIMER(_description) do {} while(0)
     30 
     31 #else
     32 
     33 #include <string>
     34 
     35 /*
     36  * Declare a profiler timer.
     37  *
     38  * _var is the variable that will be declared as a timer.
     39  * _description is the description for this timer. It will be used when logging the timer duration.
     40  */
     41 #define DECLARE_PROFILER_TIMER(_var, _description) pbcamera::TimerLogger _var = {_description}
     42 
     43 /*
     44  * Start a timer.
     45  *
     46  * _var is a timer declared with DECALRE_PROFILER_TIMER.
     47  */
     48 #define START_PROFILER_TIMER(_var) ((_var).start())
     49 
     50 /*
     51  * End a timer and log the duration since last start.
     52  *
     53  * _var is a timer declared with DECALRE_PROFILER_TIMER.
     54  */
     55 #define END_PROFILER_TIMER(_var) ((_var).end())
     56 
     57 /*
     58  * Declare a scope timer that starts now and ends when it goes out of scope.
     59  *
     60  * __description is the description for this timer. It will be used when logging the timer duration.
     61  */
     62 #define SCOPE_PROFILER_TIMER(_description) pbcamera::ScopeTimerLogger _timer(_description)
     63 
     64 namespace pbcamera {
     65 
     66 #define TIMER_TAG "[PROFILE_TIMER]"
     67 
     68 /**
     69  * TimerLogger provides a timer to log the duration between start() and end().
     70  */
     71 class TimerLogger {
     72 public:
     73     TimerLogger(const char *name) : mName(name), mInvalid(true) {};
     74 
     75     // Start the timer.
     76     void start() {
     77         mInvalid = (clock_gettime(kClockId, &mStartTime) != 0);
     78     }
     79 
     80     // End the timer and log the duration since last start.
     81     void end() {
     82         if (mInvalid) {
     83             ALOGE("%s <%s> start time is invalid.", TIMER_TAG, mName.c_str());
     84             return;
     85         }
     86 
     87         struct timespec endTime;
     88         mInvalid = (clock_gettime(kClockId, &endTime) != 0);
     89         if (mInvalid) {
     90             ALOGE("%s <%s> end time is invalid.", TIMER_TAG, mName.c_str());
     91             return;
     92         }
     93 
     94         int64_t startNs = static_cast<int64_t>(mStartTime.tv_sec) * kNsPerSec + mStartTime.tv_nsec;
     95         int64_t endNs = static_cast<int64_t>(endTime.tv_sec) * kNsPerSec + endTime.tv_nsec;
     96         ALOGI("%s <%s> took %f ms.", TIMER_TAG, mName.c_str(),
     97             static_cast<float>(endNs - startNs) / kNsPerMs);
     98     }
     99 
    100 private:
    101     const static int64_t kNsPerSec = 1000000000;
    102     const static int64_t kNsPerMs = 1000000;
    103     const static clockid_t kClockId = CLOCK_BOOTTIME;
    104 
    105     std::string mName;
    106     struct timespec mStartTime;
    107     bool mInvalid;
    108 
    109 };
    110 
    111 /**
    112  * ScopeTimerLogger provides a timer to log the duration of the instance lifetime.
    113  */
    114 class ScopeTimerLogger {
    115 public:
    116     ScopeTimerLogger(const char *name) : mTimerLogger(name) { mTimerLogger.start(); };
    117     virtual ~ScopeTimerLogger() { mTimerLogger.end(); };
    118 private:
    119     TimerLogger mTimerLogger;
    120 };
    121 
    122 } // namespace pbcamera
    123 
    124 #endif // !ENABLE_HDRPLUS_PROFILER
    125 
    126 #endif // HDR_PLUS_PROFILER_H
    127