• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 
17 #define LOG_TAG "CameraLatencyHistogram"
18 #include <inttypes.h>
19 #include <android-base/stringprintf.h>
20 #include <utils/Log.h>
21 #include <camera/StringUtils.h>
22 
23 #include "LatencyHistogram.h"
24 
25 namespace android {
26 
CameraLatencyHistogram(int32_t binSizeMs,int32_t binCount)27 CameraLatencyHistogram::CameraLatencyHistogram(int32_t binSizeMs, int32_t binCount) :
28         mBinSizeMs(binSizeMs),
29         mBinCount(binCount),
30         mBins(binCount),
31         mTotalCount(0) {
32 }
33 
add(nsecs_t start,nsecs_t end)34 void CameraLatencyHistogram::add(nsecs_t start, nsecs_t end) {
35     nsecs_t duration = end - start;
36     int32_t durationMs = static_cast<int32_t>(duration / 1000000LL);
37     int32_t binIndex = durationMs / mBinSizeMs;
38 
39     if (binIndex < 0) {
40         binIndex = 0;
41     } else if (binIndex >= mBinCount) {
42         binIndex = mBinCount-1;
43     }
44 
45     mBins[binIndex]++;
46     mTotalCount++;
47 }
48 
reset()49 void CameraLatencyHistogram::reset() {
50     memset(mBins.data(), 0, mBins.size() * sizeof(int64_t));
51     mTotalCount = 0;
52 }
53 
dump(int fd,const char * name) const54 void CameraLatencyHistogram::dump(int fd, const char* name) const {
55     if (mTotalCount == 0) {
56         return;
57     }
58 
59     std::string lines;
60     lines += fmt::sprintf("%s (%" PRId64 ") samples\n", name, mTotalCount);
61 
62     std::string lineBins, lineBinCounts;
63     formatHistogramText(lineBins, lineBinCounts);
64 
65     lineBins += ("\n");
66     lineBinCounts += ("\n");
67     lines += lineBins;
68     lines += lineBinCounts;
69 
70     write(fd, lines.c_str(), lines.size());
71 }
72 
log(const char * fmt,...)73 void CameraLatencyHistogram::log(const char* fmt, ...) {
74     if (mTotalCount == 0) {
75         return;
76     }
77 
78     va_list args;
79     va_start(args, fmt);
80     std::string histogramName;
81     base::StringAppendV(&histogramName, fmt, args);
82     ALOGI("%s (%" PRId64 ") samples:", histogramName.c_str(), mTotalCount);
83     va_end(args);
84 
85     std::string lineBins, lineBinCounts;
86     formatHistogramText(lineBins, lineBinCounts);
87 
88     ALOGI("%s", lineBins.c_str());
89     ALOGI("%s", lineBinCounts.c_str());
90 }
91 
formatHistogramText(std::string & lineBins,std::string & lineBinCounts) const92 void CameraLatencyHistogram::formatHistogramText(
93         std::string& lineBins, std::string& lineBinCounts) const {
94     lineBins = "  ";
95     lineBinCounts = "  ";
96 
97     for (int32_t i = 0; i < mBinCount; i++) {
98         if (i == mBinCount - 1) {
99             lineBins += "    inf (max ms)";
100         } else {
101             lineBins += fmt::sprintf("%7d", mBinSizeMs*(i+1));
102         }
103         lineBinCounts += fmt::sprintf("   %02.2f", 100.0*mBins[i]/mTotalCount);
104     }
105     lineBinCounts += " (%)";
106 }
107 
108 }; //namespace android
109