1 /* 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 23 namespace android { 24 25 using ::aidl::android::media::ClientInfoParcel; 26 using ::aidl::android::media::ClientConfigParcel; 27 using ::aidl::android::media::IResourceManagerClient; 28 29 struct ProcessInfoInterface; 30 31 class 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 // 39 enum 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. 57 typedef std::map<int64_t, ClientConfigParcel> ClientConfigMap; 58 59 // Map of pid and the uid. 60 typedef std::map<int32_t, uid_t> PidUidMap; 61 62 // Map of concurrent codes by Codec type bucket. 63 struct 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 72 private: 73 int mCodec[CodecBucketMaxSize] = {0}; 74 }; 75 76 // Current and Peak ConcurrentCodecMap for a process. 77 struct ConcurrentCodecs { 78 ConcurrentCodecsMap mCurrent; 79 ConcurrentCodecsMap mPeak; 80 // 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; 90 }; 91 92 // Current and Peak pixel count for a process. 93 struct 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 // 115 class ResourceManagerMetrics { 116 public: 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 132 // To be called when a client's configuration has changed. 133 void notifyClientConfigChanged(const ClientConfigParcel& clientConfig); 134 135 // To be called when after a reclaim event. 136 void pushReclaimAtom(const ClientInfoParcel& clientInfo, 137 const std::vector<int>& priorities, 138 const Vector<std::shared_ptr<IResourceManagerClient>>& clients, 139 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 149 private: 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 159 // To increase/update/decrease the concurrent pixels usage for a process. 160 void increasePixelCount(int32_t pid, long pixels); 161 void updatePixelCount(int32_t pid, long newPixels, long lastPixels); 162 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 170 private: 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_ 194