blob: d99c5b1c3397ba227d9ca0da3d342c826a5d0b68 [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;
Girishde8eb592023-04-13 18:49:17 +000080 // concurrent HW Video codecs.
81 int mHWVideoCodecs;
82 // concurrent SW Video codecs.
83 int mSWVideoCodecs;
84 // concurrent Video codecs.
85 int mVideoCodecs;
86 // concurrent Audio codecs.
87 int mAudioCodecs;
88 // concurrent Image codecs.
89 int mImageCodecs;
Girish1f002cf2023-02-17 00:36:29 +000090};
91
92// Current and Peak pixel count for a process.
93struct PixelCount {
94 long mCurrent = 0;
95 long mPeak = 0;
96};
97
98//
99// ResourceManagerMetrics class that maintaines concurrent codec count based:
100//
101// 1. # of concurrent active codecs (initialized, but aren't released yet) of given
102// implementation (by codec name) across the system.
103//
104// 2. # of concurrent codec usage (started, but not stopped yet), which is
105// measured using codec type bucket (CodecBucket) for:
106// - each process/application.
107// - across the system.
108// Also the peak count of the same for each process/application is maintained.
109//
110// 3. # of Peak Concurrent Pixels for each process/application.
111// This should help with understanding the (video) memory usage per
112// application.
113//
114//
115class ResourceManagerMetrics {
116public:
117 ResourceManagerMetrics(const sp<ProcessInfoInterface>& processInfo);
118 ~ResourceManagerMetrics();
119
120 // To be called when a client is created.
121 void notifyClientCreated(const ClientInfoParcel& clientInfo);
122
123 // To be called when a client is released.
124 void notifyClientReleased(const ClientInfoParcel& clientInfo);
125
126 // To be called when a client is started.
127 void notifyClientStarted(const ClientConfigParcel& clientConfig);
128
129 // To be called when a client is stopped.
130 void notifyClientStopped(const ClientConfigParcel& clientConfig);
131
Girishde8eb592023-04-13 18:49:17 +0000132 // To be called when a client's configuration has changed.
133 void notifyClientConfigChanged(const ClientConfigParcel& clientConfig);
134
Girish1f002cf2023-02-17 00:36:29 +0000135 // To be called when after a reclaim event.
136 void pushReclaimAtom(const ClientInfoParcel& clientInfo,
137 const std::vector<int>& priorities,
Girish434b4d82023-07-11 23:24:54 +0000138 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients,
Girish1f002cf2023-02-17 00:36:29 +0000139 const PidUidVector& idList, bool reclaimed);
140
141 // Add this pid/uid set to monitor for the process termination state.
142 void addPid(int pid, uid_t uid = 0);
143
144 // Get the peak concurrent pixel count (associated with the video codecs) for the process.
145 long getPeakConcurrentPixelCount(int pid) const;
146 // Get the current concurrent pixel count (associated with the video codecs) for the process.
147 long getCurrentConcurrentPixelCount(int pid) const;
148
149private:
150 ResourceManagerMetrics(const ResourceManagerMetrics&) = delete;
151 ResourceManagerMetrics(ResourceManagerMetrics&&) = delete;
152 ResourceManagerMetrics& operator=(const ResourceManagerMetrics&) = delete;
153 ResourceManagerMetrics& operator=(ResourceManagerMetrics&&) = delete;
154
155 // To increase/decrease the concurrent codec usage for a given CodecBucket.
156 void increaseConcurrentCodecs(int32_t pid, CodecBucket codecBucket);
157 void decreaseConcurrentCodecs(int32_t pid, CodecBucket codecBucket);
158
Girishde8eb592023-04-13 18:49:17 +0000159 // To increase/update/decrease the concurrent pixels usage for a process.
Girish1f002cf2023-02-17 00:36:29 +0000160 void increasePixelCount(int32_t pid, long pixels);
Girishde8eb592023-04-13 18:49:17 +0000161 void updatePixelCount(int32_t pid, long newPixels, long lastPixels);
Girish1f002cf2023-02-17 00:36:29 +0000162 void decreasePixelCount(int32_t pid, long pixels);
163
164 // Issued when the process/application with given pid/uid is terminated.
165 void onProcessTerminated(int32_t pid, uid_t uid);
166
167 // To push conccuret codec usage of a process/application.
168 void pushConcurrentUsageReport(int32_t pid, uid_t uid);
169
170private:
171 std::mutex mLock;
172
173 // Map of client id and the configuration.
174 ClientConfigMap mClientConfigMap;
175
176 // Concurrent and Peak Pixel count for each process/application.
177 std::map<int32_t, PixelCount> mProcessPixelsMap;
178
179 // Map of resources (name) and number of concurrent instances
180 std::map<std::string, int> mConcurrentResourceCountMap;
181
182 // Map of concurrent codes by CodecBucket across the system.
183 ConcurrentCodecsMap mConcurrentCodecsMap;
184 // Map of concurrent and peak codes by CodecBucket for each process/application.
185 std::map<int32_t, ConcurrentCodecs> mProcessConcurrentCodecsMap;
186
187 // Uid Observer to monitor the application termination.
188 sp<UidObserver> mUidObserver;
189};
190
191} // namespace android
192
193#endif // ANDROID_MEDIA_RESOURCEMANAGERMETRICS_H_