• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ANDROID_SERVICE_UTILS_SESSION_STATS_BUILDER_H
18 #define ANDROID_SERVICE_UTILS_SESSION_STATS_BUILDER_H
19 
20 #include <utils/Errors.h>
21 
22 #include <array>
23 #include <map>
24 #include <mutex>
25 
26 namespace android {
27 
28 // Helper class to build stream stats
29 struct StreamStats {
30     // Fields for buffer drop
31     int64_t mRequestedFrameCount;
32     int64_t mDroppedFrameCount;
33     bool mCounterStopped;
34 
35     // Fields for stream startup latency
36     int32_t mStartLatencyMs;
37 
38     // 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 
StreamStatsStreamStats46     StreamStats() : mRequestedFrameCount(0),
47                      mDroppedFrameCount(0),
48                      mCounterStopped(false),
49                      mStartLatencyMs(0),
50                      mCaptureLatencyHistogram{}
51                   {}
52 
53     void updateLatencyHistogram(int32_t latencyMs);
54 };
55 
56 // Helper class to build session stats
57 class SessionStatsBuilder {
58 public:
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 
SessionStatsBuilder()79     SessionStatsBuilder() : mRequestCount(0), mErrorResultCount(0),
80              mCounterStopped(false), mDeviceError(false) {}
81 private:
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
94