1 /*
2  * Copyright (C) 2019 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 __STATS_H__
18 #define __STATS_H__
19 
20 #include <android/log.h>
21 #include <inttypes.h>
22 
23 #ifndef ALOG
24 #define ALOG(priority, tag, ...) ((void)__android_log_print(ANDROID_##priority, tag, __VA_ARGS__))
25 
26 #define ALOGI(...) ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)
27 #define ALOGE(...) ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)
28 #define ALOGD(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
29 #define ALOGW(...) ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)
30 
31 #ifndef LOG_NDEBUG
32 #define LOG_NDEBUG 1
33 #endif
34 
35 #if LOG_NDEBUG
36 #define ALOGV(cond, ...)   ((void)0)
37 #else
38 #define ALOGV(...) ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
39 #endif
40 #endif  // ALOG
41 
42 #include <sys/time.h>
43 #include <algorithm>
44 #include <numeric>
45 #include <vector>
46 
47 // Include local copy of Timers taken from system/core/libutils
48 #include "utils/Timers.h"
49 
50 using namespace std;
51 
52 class Stats {
53   public:
Stats()54     Stats() {
55         mInitTimeNs = 0;
56         mDeInitTimeNs = 0;
57     }
58 
~Stats()59     ~Stats() {
60         reset();
61     }
62 
63   private:
64     nsecs_t mInitTimeNs;
65     nsecs_t mDeInitTimeNs;
66     nsecs_t mStartTimeNs;
67     std::vector<int32_t> mFrameSizes;
68     std::vector<nsecs_t> mInputTimer;
69     std::vector<nsecs_t> mOutputTimer;
70 
71   public:
getCurTime()72     nsecs_t getCurTime() { return systemTime(CLOCK_MONOTONIC); }
73 
setInitTime(nsecs_t initTime)74     void setInitTime(nsecs_t initTime) { mInitTimeNs = initTime; }
75 
setDeInitTime(nsecs_t deInitTime)76     void setDeInitTime(nsecs_t deInitTime) { mDeInitTimeNs = deInitTime; }
77 
setStartTime()78     void setStartTime() { mStartTimeNs = systemTime(CLOCK_MONOTONIC); }
79 
addFrameSize(int32_t size)80     void addFrameSize(int32_t size) { mFrameSizes.push_back(size); }
81 
addInputTime()82     void addInputTime() { mInputTimer.push_back(systemTime(CLOCK_MONOTONIC)); }
83 
addOutputTime()84     void addOutputTime() { mOutputTimer.push_back(systemTime(CLOCK_MONOTONIC)); }
85 
reset()86     void reset() {
87         if (!mFrameSizes.empty()) mFrameSizes.clear();
88         if (!mInputTimer.empty()) mInputTimer.clear();
89         if (!mOutputTimer.empty()) mOutputTimer.clear();
90     }
91 
getOutputTimer()92     std::vector<nsecs_t> getOutputTimer() { return mOutputTimer; }
93 
getInitTime()94     nsecs_t getInitTime() { return mInitTimeNs; }
95 
getDeInitTime()96     nsecs_t getDeInitTime() { return mDeInitTimeNs; }
97 
getTimeDiff(nsecs_t sTime,nsecs_t eTime)98     nsecs_t getTimeDiff(nsecs_t sTime, nsecs_t eTime) { return (eTime - sTime); }
99 
getTotalTime()100     nsecs_t getTotalTime() {
101         if (mOutputTimer.empty()) return -1;
102         return (*(mOutputTimer.end() - 1) - mStartTimeNs);
103     }
104 
105     void dumpStatistics(string operation, string inputReference, int64_t duarationUs,
106                         string codecName = "", string mode = "", string statsFile = "");
107 };
108 
109 #endif  // __STATS_H__
110