blob: 914c09ec564d3a57c46f12951df6e7b03d19aac9 [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>
Eino-Ville Talvalaffc186b2024-04-09 18:10:47 -070025#include <unordered_map>
26#include <utility>
Shuzhen Wang316781a2020-08-18 18:11:01 -070027
28namespace android {
29
30// Helper class to build stream stats
31struct StreamStats {
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070032 // Fields for buffer drop
Shuzhen Wang316781a2020-08-18 18:11:01 -070033 int64_t mRequestedFrameCount;
34 int64_t mDroppedFrameCount;
35 bool mCounterStopped;
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070036
37 // Fields for stream startup latency
Shuzhen Wang316781a2020-08-18 18:11:01 -070038 int32_t mStartLatencyMs;
39
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070040 // Fields for capture latency measurement
41 const static int LATENCY_BIN_COUNT = 10;
42 // Boundary values separating between adjacent bins, excluding 0 and
43 // infinity.
44 const static std::array<int32_t, LATENCY_BIN_COUNT-1> mCaptureLatencyBins;
45 // Counter values for all histogram bins. One more entry than mCaptureLatencyBins.
46 std::array<int64_t, LATENCY_BIN_COUNT> mCaptureLatencyHistogram;
47
Shuzhen Wang316781a2020-08-18 18:11:01 -070048 StreamStats() : mRequestedFrameCount(0),
49 mDroppedFrameCount(0),
50 mCounterStopped(false),
Shuzhen Wangdb8f2782020-10-30 08:43:37 -070051 mStartLatencyMs(0),
52 mCaptureLatencyHistogram{}
53 {}
54
55 void updateLatencyHistogram(int32_t latencyMs);
Shuzhen Wang316781a2020-08-18 18:11:01 -070056};
57
58// Helper class to build session stats
59class SessionStatsBuilder {
60public:
61
62 status_t addStream(int streamId);
63 status_t removeStream(int streamId);
64
65 // Return the session statistics and reset the internal states.
66 void buildAndReset(/*out*/int64_t* requestCount,
67 /*out*/int64_t* errorResultCount,
68 /*out*/bool* deviceError,
Eino-Ville Talvalaffc186b2024-04-09 18:10:47 -070069 /*out*/std::pair<int32_t, int32_t>* mostRequestedFpsRange,
70 /*out*/std::map<int, StreamStats>* statsMap);
Shuzhen Wang316781a2020-08-18 18:11:01 -070071
72 // Stream specific counter
73 void startCounter(int streamId);
74 void stopCounter(int streamId);
75 void incCounter(int streamId, bool dropped, int32_t captureLatencyMs);
76
77 // Session specific counter
78 void stopCounter();
79 void incResultCounter(bool dropped);
80 void onDeviceError();
81
Eino-Ville Talvalaffc186b2024-04-09 18:10:47 -070082 // Session specific statistics
83
84 // Limit on size of FPS range histogram
85 static const size_t FPS_HISTOGRAM_MAX_SIZE = 10;
86
87 void incFpsRequestedCount(int32_t minFps, int32_t maxFps, int64_t frameNumber);
88
Shuzhen Wang316781a2020-08-18 18:11:01 -070089 SessionStatsBuilder() : mRequestCount(0), mErrorResultCount(0),
90 mCounterStopped(false), mDeviceError(false) {}
91private:
92 std::mutex mLock;
93 int64_t mRequestCount;
94 int64_t mErrorResultCount;
95 bool mCounterStopped;
96 bool mDeviceError;
Shuzhen Wangd26b1862022-03-07 12:05:05 -080097 std::string mUserTag;
Eino-Ville Talvalaffc186b2024-04-09 18:10:47 -070098
99 // Histogram of frame counts of requested target FPS ranges
100 // (min_fps << 32 | max_fps) -> (# of frames with this fps, last seen framenumber)
101 std::unordered_map<uint64_t, std::pair<int64_t, int64_t>> mRequestedFpsRangeHistogram;
102
Shuzhen Wang316781a2020-08-18 18:11:01 -0700103 // Map from stream id to stream statistics
104 std::map<int, StreamStats> mStatsMap;
105};
106
107}; // namespace android
108
109#endif // ANDROID_SERVICE_UTILS_SESSION_STATS_BUILDER_H