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