blob: c23abb6067450838cfb4e64fe7bcae398140235e [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#ifndef ANDROID_SERVICE_UTILS_SESSION_STATS_BUILDER_H
18#define ANDROID_SERVICE_UTILS_SESSION_STATS_BUILDER_H
19
20#include <utils/Errors.h>
21
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070022#include <array>
Shuzhen Wang316781a2020-08-18 18:11:01 -070023#include <map>
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070024#include <mutex>
Shuzhen Wang316781a2020-08-18 18:11:01 -070025
26namespace android {
27
28// Helper class to build stream stats
29struct StreamStats {
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070030 // Fields for buffer drop
Shuzhen Wang316781a2020-08-18 18:11:01 -070031 int64_t mRequestedFrameCount;
32 int64_t mDroppedFrameCount;
33 bool mCounterStopped;
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070034
35 // Fields for stream startup latency
Shuzhen Wang316781a2020-08-18 18:11:01 -070036 int32_t mStartLatencyMs;
37
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070038 // Fields for capture latency measurement
39 const static int LATENCY_BIN_COUNT = 10;
40 // Boundary values separating between adjacent bins, excluding 0 and
41 // infinity.
42 const static std::array<int32_t, LATENCY_BIN_COUNT-1> mCaptureLatencyBins;
43 // Counter values for all histogram bins. One more entry than mCaptureLatencyBins.
44 std::array<int64_t, LATENCY_BIN_COUNT> mCaptureLatencyHistogram;
45
Shuzhen Wang316781a2020-08-18 18:11:01 -070046 StreamStats() : mRequestedFrameCount(0),
47 mDroppedFrameCount(0),
48 mCounterStopped(false),
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070049 mStartLatencyMs(0),
50 mCaptureLatencyHistogram{}
51 {}
52
53 void updateLatencyHistogram(int32_t latencyMs);
Shuzhen Wang316781a2020-08-18 18:11:01 -070054};
55
56// Helper class to build session stats
57class SessionStatsBuilder {
58public:
59
60 status_t addStream(int streamId);
61 status_t removeStream(int streamId);
62
63 // Return the session statistics and reset the internal states.
64 void buildAndReset(/*out*/int64_t* requestCount,
65 /*out*/int64_t* errorResultCount,
66 /*out*/bool* deviceError,
67 /*out*/std::map<int, StreamStats> *statsMap);
68
69 // Stream specific counter
70 void startCounter(int streamId);
71 void stopCounter(int streamId);
72 void incCounter(int streamId, bool dropped, int32_t captureLatencyMs);
73
74 // Session specific counter
75 void stopCounter();
76 void incResultCounter(bool dropped);
77 void onDeviceError();
78
79 SessionStatsBuilder() : mRequestCount(0), mErrorResultCount(0),
80 mCounterStopped(false), mDeviceError(false) {}
81private:
82 std::mutex mLock;
83 int64_t mRequestCount;
84 int64_t mErrorResultCount;
85 bool mCounterStopped;
86 bool mDeviceError;
87 // Map from stream id to stream statistics
88 std::map<int, StreamStats> mStatsMap;
89};
90
91}; // namespace android
92
93#endif // ANDROID_SERVICE_UTILS_SESSION_STATS_BUILDER_H