1 /*
2 * Copyright (C) 2020 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 "CameraSessionStatsBuilder"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20
21 #include <numeric>
22
23 #include <inttypes.h>
24 #include <utils/Log.h>
25
26 #include "SessionStatsBuilder.h"
27
28 namespace android {
29
30 // Bins for capture latency: [0, 100], [100, 200], [200, 300], ...
31 // [1300, 2100], [2100, inf].
32 // Capture latency is in the unit of millisecond.
33 const std::array<int32_t, StreamStats::LATENCY_BIN_COUNT-1> StreamStats::mCaptureLatencyBins {
34 { 100, 200, 300, 400, 500, 700, 900, 1300, 2100 } };
35
addStream(int id)36 status_t SessionStatsBuilder::addStream(int id) {
37 std::lock_guard<std::mutex> l(mLock);
38 StreamStats stats;
39 mStatsMap.emplace(id, stats);
40 return OK;
41 }
42
removeStream(int id)43 status_t SessionStatsBuilder::removeStream(int id) {
44 std::lock_guard<std::mutex> l(mLock);
45 mStatsMap.erase(id);
46 return OK;
47 }
48
buildAndReset(int64_t * requestCount,int64_t * errorResultCount,bool * deviceError,std::pair<int32_t,int32_t> * mostRequestedFpsRange,std::map<int,StreamStats> * statsMap)49 void SessionStatsBuilder::buildAndReset(int64_t* requestCount,
50 int64_t* errorResultCount, bool* deviceError,
51 std::pair<int32_t, int32_t>* mostRequestedFpsRange,
52 std::map<int, StreamStats>* statsMap) {
53 std::lock_guard<std::mutex> l(mLock);
54 *requestCount = mRequestCount;
55 *errorResultCount = mErrorResultCount;
56 *deviceError = mDeviceError;
57 *statsMap = mStatsMap;
58
59 int32_t minFps = 0, maxFps = 0;
60 if (mRequestedFpsRangeHistogram.size() > 0) {
61 auto mostCommonIt = mRequestedFpsRangeHistogram.begin();
62 for (auto it = mostCommonIt; it != mRequestedFpsRangeHistogram.end(); it++) {
63 if (it->second.first > mostCommonIt->second.first) {
64 mostCommonIt = it;
65 }
66 }
67 minFps = mostCommonIt->first >> 32;
68 maxFps = mostCommonIt->first & 0xFFFF'FFFFU;
69 }
70 *mostRequestedFpsRange = std::make_pair(minFps, maxFps);
71
72 // Reset internal states
73 mRequestCount = 0;
74 mErrorResultCount = 0;
75 mCounterStopped = false;
76 mDeviceError = false;
77 mUserTag.clear();
78 mRequestedFpsRangeHistogram.clear();
79
80 for (auto& streamStats : mStatsMap) {
81 StreamStats& streamStat = streamStats.second;
82 streamStat.mRequestedFrameCount = 0;
83 streamStat.mDroppedFrameCount = 0;
84 streamStat.mCounterStopped = false;
85 streamStat.mStartLatencyMs = 0;
86
87 std::fill(streamStat.mCaptureLatencyHistogram.begin(),
88 streamStat.mCaptureLatencyHistogram.end(), 0);
89 }
90 }
91
startCounter(int id)92 void SessionStatsBuilder::startCounter(int id) {
93 std::lock_guard<std::mutex> l(mLock);
94 mStatsMap[id].mCounterStopped = false;
95 }
96
stopCounter(int id)97 void SessionStatsBuilder::stopCounter(int id) {
98 std::lock_guard<std::mutex> l(mLock);
99 StreamStats& streamStat = mStatsMap[id];
100 streamStat.mCounterStopped = true;
101 }
102
incCounter(int id,bool dropped,int32_t captureLatencyMs)103 void SessionStatsBuilder::incCounter(int id, bool dropped, int32_t captureLatencyMs) {
104 std::lock_guard<std::mutex> l(mLock);
105
106 auto it = mStatsMap.find(id);
107 if (it == mStatsMap.end()) return;
108
109 StreamStats& streamStat = it->second;
110 if (streamStat.mCounterStopped) return;
111
112 streamStat.mRequestedFrameCount++;
113 if (dropped) {
114 streamStat.mDroppedFrameCount++;
115 } else if (streamStat.mRequestedFrameCount - streamStat.mDroppedFrameCount == 1) {
116 // The capture latency for the first request.
117 streamStat.mStartLatencyMs = captureLatencyMs;
118 }
119
120 streamStat.updateLatencyHistogram(captureLatencyMs);
121 }
122
stopCounter()123 void SessionStatsBuilder::stopCounter() {
124 std::lock_guard<std::mutex> l(mLock);
125 mCounterStopped = true;
126 for (auto& streamStats : mStatsMap) {
127 streamStats.second.mCounterStopped = true;
128 }
129 }
130
incResultCounter(bool dropped)131 void SessionStatsBuilder::incResultCounter(bool dropped) {
132 std::lock_guard<std::mutex> l(mLock);
133 if (mCounterStopped) return;
134
135 mRequestCount++;
136 if (dropped) mErrorResultCount++;
137 }
138
onDeviceError()139 void SessionStatsBuilder::onDeviceError() {
140 std::lock_guard<std::mutex> l(mLock);
141 mDeviceError = true;
142 }
143
incFpsRequestedCount(int32_t minFps,int32_t maxFps,int64_t frameNumber)144 void SessionStatsBuilder::incFpsRequestedCount(int32_t minFps, int32_t maxFps,
145 int64_t frameNumber) {
146 std::lock_guard<std::mutex> l(mLock);
147
148 // Stuff range into a 64-bit value to make hashing simple
149 uint64_t currentFpsTarget = minFps;
150 currentFpsTarget = currentFpsTarget << 32 | maxFps;
151
152 auto &stats = mRequestedFpsRangeHistogram[currentFpsTarget];
153 stats.first++;
154 stats.second = frameNumber;
155
156 // Ensure weird app input of target FPS ranges doesn't cause unbounded memory growth
157 if (mRequestedFpsRangeHistogram.size() > FPS_HISTOGRAM_MAX_SIZE) {
158 // Find oldest used fps to drop by last seen frame number
159 auto deleteIt = mRequestedFpsRangeHistogram.begin();
160 for (auto it = deleteIt; it != mRequestedFpsRangeHistogram.end(); it++) {
161 if (it->second.second < deleteIt->second.second) {
162 deleteIt = it;
163 }
164 }
165 mRequestedFpsRangeHistogram.erase(deleteIt);
166 }
167 }
168
updateLatencyHistogram(int32_t latencyMs)169 void StreamStats::updateLatencyHistogram(int32_t latencyMs) {
170 size_t i;
171 for (i = 0; i < mCaptureLatencyBins.size(); i++) {
172 if (latencyMs < mCaptureLatencyBins[i]) {
173 mCaptureLatencyHistogram[i] ++;
174 break;
175 }
176 }
177
178 if (i == mCaptureLatencyBins.size()) {
179 mCaptureLatencyHistogram[i]++;
180 }
181 }
182
183 }; // namespace android
184