blob: c3aac7276a9bdc994dd174edae7c25c65daf50af [file] [log] [blame]
Shuzhen Wang316781a2020-08-18 18:11:01 -07001/*
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
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070021#include <numeric>
22
23#include <inttypes.h>
Shuzhen Wang316781a2020-08-18 18:11:01 -070024#include <utils/Log.h>
25
26#include "SessionStatsBuilder.h"
27
28namespace android {
29
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070030// Bins for capture latency: [0, 100], [100, 200], [200, 300], ...
31// [1300, 2100], [2100, inf].
32// Capture latency is in the unit of millisecond.
33const std::array<int32_t, StreamStats::LATENCY_BIN_COUNT-1> StreamStats::mCaptureLatencyBins {
34 { 100, 200, 300, 400, 500, 700, 900, 1300, 2100 } };
35
Shuzhen Wang316781a2020-08-18 18:11:01 -070036status_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
43status_t SessionStatsBuilder::removeStream(int id) {
44 std::lock_guard<std::mutex> l(mLock);
45 mStatsMap.erase(id);
46 return OK;
47}
48
49void SessionStatsBuilder::buildAndReset(int64_t* requestCount,
50 int64_t* errorResultCount, bool* deviceError,
51 std::map<int, StreamStats> *statsMap) {
52 std::lock_guard<std::mutex> l(mLock);
53 *requestCount = mRequestCount;
54 *errorResultCount = mErrorResultCount;
55 *deviceError = mDeviceError;
56 *statsMap = mStatsMap;
57
58 // Reset internal states
59 mRequestCount = 0;
60 mErrorResultCount = 0;
61 mCounterStopped = false;
62 mDeviceError = false;
Shuzhen Wangd26b1862022-03-07 12:05:05 -080063 mUserTag.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -070064 for (auto& streamStats : mStatsMap) {
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070065 StreamStats& streamStat = streamStats.second;
66 streamStat.mRequestedFrameCount = 0;
67 streamStat.mDroppedFrameCount = 0;
68 streamStat.mCounterStopped = false;
69 streamStat.mStartLatencyMs = 0;
70
71 std::fill(streamStat.mCaptureLatencyHistogram.begin(),
72 streamStat.mCaptureLatencyHistogram.end(), 0);
Shuzhen Wang316781a2020-08-18 18:11:01 -070073 }
74}
75
76void SessionStatsBuilder::startCounter(int id) {
77 std::lock_guard<std::mutex> l(mLock);
78 mStatsMap[id].mCounterStopped = false;
79}
80
81void SessionStatsBuilder::stopCounter(int id) {
82 std::lock_guard<std::mutex> l(mLock);
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070083 StreamStats& streamStat = mStatsMap[id];
84 streamStat.mCounterStopped = true;
Shuzhen Wang316781a2020-08-18 18:11:01 -070085}
86
87void SessionStatsBuilder::incCounter(int id, bool dropped, int32_t captureLatencyMs) {
88 std::lock_guard<std::mutex> l(mLock);
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070089
Shuzhen Wang316781a2020-08-18 18:11:01 -070090 auto it = mStatsMap.find(id);
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070091 if (it == mStatsMap.end()) return;
92
93 StreamStats& streamStat = it->second;
94 if (streamStat.mCounterStopped) return;
95
96 streamStat.mRequestedFrameCount++;
97 if (dropped) {
98 streamStat.mDroppedFrameCount++;
99 } else if (streamStat.mRequestedFrameCount - streamStat.mDroppedFrameCount == 1) {
100 // The capture latency for the first request.
101 streamStat.mStartLatencyMs = captureLatencyMs;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700102 }
Shuzhen Wangdb8f2782020-10-30 08:43:37 -0700103
104 streamStat.updateLatencyHistogram(captureLatencyMs);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700105}
106
107void SessionStatsBuilder::stopCounter() {
108 std::lock_guard<std::mutex> l(mLock);
109 mCounterStopped = true;
110 for (auto& streamStats : mStatsMap) {
111 streamStats.second.mCounterStopped = true;
112 }
113}
114
115void SessionStatsBuilder::incResultCounter(bool dropped) {
116 std::lock_guard<std::mutex> l(mLock);
Shuzhen Wangdb8f2782020-10-30 08:43:37 -0700117 if (mCounterStopped) return;
118
119 mRequestCount++;
120 if (dropped) mErrorResultCount++;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700121}
122
123void SessionStatsBuilder::onDeviceError() {
124 std::lock_guard<std::mutex> l(mLock);
125 mDeviceError = true;
126}
127
Shuzhen Wangdb8f2782020-10-30 08:43:37 -0700128void StreamStats::updateLatencyHistogram(int32_t latencyMs) {
129 size_t i;
130 for (i = 0; i < mCaptureLatencyBins.size(); i++) {
131 if (latencyMs < mCaptureLatencyBins[i]) {
132 mCaptureLatencyHistogram[i] ++;
133 break;
134 }
135 }
136
137 if (i == mCaptureLatencyBins.size()) {
138 mCaptureLatencyHistogram[i]++;
139 }
140}
141
Shuzhen Wang316781a2020-08-18 18:11:01 -0700142}; // namespace android