blob: b7810e55ee42454b56d6f7702b0d69b7383a8344 [file] [log] [blame]
Girish1f002cf2023-02-17 00:36:29 +00001/*
2**
3** Copyright 2023, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef ANDROID_MEDIA_RESOURCEMANAGERMETRICS_H_
19#define ANDROID_MEDIA_RESOURCEMANAGERMETRICS_H_
20
21#include "ResourceManagerService.h"
22
23namespace android {
24
25using ::aidl::android::media::ClientInfoParcel;
26using ::aidl::android::media::ClientConfigParcel;
27using ::aidl::android::media::IResourceManagerClient;
28
29struct ProcessInfoInterface;
30
31class UidObserver;
32
33//
34// Enumeration for Codec bucket based on:
35// - Encoder or Decoder
36// - hardware implementation or not
37// - Audio/Video/Image codec
38//
39enum CodecBucket {
40 CodecBucketUnspecified = 0,
41 HwAudioEncoder = 1,
42 HwAudioDecoder = 2,
43 HwVideoEncoder = 3,
44 HwVideoDecoder = 4,
45 HwImageEncoder = 5,
46 HwImageDecoder = 6,
47 SwAudioEncoder = 7,
48 SwAudioDecoder = 8,
49 SwVideoEncoder = 9,
50 SwVideoDecoder = 10,
51 SwImageEncoder = 11,
52 SwImageDecoder = 12,
53 CodecBucketMaxSize = 13,
54};
55
56// Map of client id and client configuration, when it was started last.
57typedef std::map<int64_t, ClientConfigParcel> ClientConfigMap;
58
59// Map of pid and the uid.
60typedef std::map<int32_t, uid_t> PidUidMap;
61
62// Map of concurrent codes by Codec type bucket.
63struct ConcurrentCodecsMap {
64 int& operator[](CodecBucket index) {
65 return mCodec[index];
66 }
67
68 const int& operator[](CodecBucket index) const {
69 return mCodec[index];
70 }
71
72private:
73 int mCodec[CodecBucketMaxSize] = {0};
74};
75
76// Current and Peak ConcurrentCodecMap for a process.
77struct ConcurrentCodecs {
78 ConcurrentCodecsMap mCurrent;
79 ConcurrentCodecsMap mPeak;
80};
81
82// Current and Peak pixel count for a process.
83struct PixelCount {
84 long mCurrent = 0;
85 long mPeak = 0;
86};
87
88//
89// ResourceManagerMetrics class that maintaines concurrent codec count based:
90//
91// 1. # of concurrent active codecs (initialized, but aren't released yet) of given
92// implementation (by codec name) across the system.
93//
94// 2. # of concurrent codec usage (started, but not stopped yet), which is
95// measured using codec type bucket (CodecBucket) for:
96// - each process/application.
97// - across the system.
98// Also the peak count of the same for each process/application is maintained.
99//
100// 3. # of Peak Concurrent Pixels for each process/application.
101// This should help with understanding the (video) memory usage per
102// application.
103//
104//
105class ResourceManagerMetrics {
106public:
107 ResourceManagerMetrics(const sp<ProcessInfoInterface>& processInfo);
108 ~ResourceManagerMetrics();
109
110 // To be called when a client is created.
111 void notifyClientCreated(const ClientInfoParcel& clientInfo);
112
113 // To be called when a client is released.
114 void notifyClientReleased(const ClientInfoParcel& clientInfo);
115
116 // To be called when a client is started.
117 void notifyClientStarted(const ClientConfigParcel& clientConfig);
118
119 // To be called when a client is stopped.
120 void notifyClientStopped(const ClientConfigParcel& clientConfig);
121
122 // To be called when after a reclaim event.
123 void pushReclaimAtom(const ClientInfoParcel& clientInfo,
124 const std::vector<int>& priorities,
125 const Vector<std::shared_ptr<IResourceManagerClient>>& clients,
126 const PidUidVector& idList, bool reclaimed);
127
128 // Add this pid/uid set to monitor for the process termination state.
129 void addPid(int pid, uid_t uid = 0);
130
131 // Get the peak concurrent pixel count (associated with the video codecs) for the process.
132 long getPeakConcurrentPixelCount(int pid) const;
133 // Get the current concurrent pixel count (associated with the video codecs) for the process.
134 long getCurrentConcurrentPixelCount(int pid) const;
135
136private:
137 ResourceManagerMetrics(const ResourceManagerMetrics&) = delete;
138 ResourceManagerMetrics(ResourceManagerMetrics&&) = delete;
139 ResourceManagerMetrics& operator=(const ResourceManagerMetrics&) = delete;
140 ResourceManagerMetrics& operator=(ResourceManagerMetrics&&) = delete;
141
142 // To increase/decrease the concurrent codec usage for a given CodecBucket.
143 void increaseConcurrentCodecs(int32_t pid, CodecBucket codecBucket);
144 void decreaseConcurrentCodecs(int32_t pid, CodecBucket codecBucket);
145
146 // To increase/decrease the concurrent pixels usage for a process.
147 void increasePixelCount(int32_t pid, long pixels);
148 void decreasePixelCount(int32_t pid, long pixels);
149
150 // Issued when the process/application with given pid/uid is terminated.
151 void onProcessTerminated(int32_t pid, uid_t uid);
152
153 // To push conccuret codec usage of a process/application.
154 void pushConcurrentUsageReport(int32_t pid, uid_t uid);
155
156private:
157 std::mutex mLock;
158
159 // Map of client id and the configuration.
160 ClientConfigMap mClientConfigMap;
161
162 // Concurrent and Peak Pixel count for each process/application.
163 std::map<int32_t, PixelCount> mProcessPixelsMap;
164
165 // Map of resources (name) and number of concurrent instances
166 std::map<std::string, int> mConcurrentResourceCountMap;
167
168 // Map of concurrent codes by CodecBucket across the system.
169 ConcurrentCodecsMap mConcurrentCodecsMap;
170 // Map of concurrent and peak codes by CodecBucket for each process/application.
171 std::map<int32_t, ConcurrentCodecs> mProcessConcurrentCodecsMap;
172
173 // Uid Observer to monitor the application termination.
174 sp<UidObserver> mUidObserver;
175};
176
177} // namespace android
178
179#endif // ANDROID_MEDIA_RESOURCEMANAGERMETRICS_H_