Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 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 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "ResourceManagerMetrics" |
| 20 | #include <utils/Log.h> |
| 21 | #include <mediautils/ProcessInfo.h> |
| 22 | |
| 23 | #include <stats_media_metrics.h> |
| 24 | |
| 25 | #include "UidObserver.h" |
| 26 | #include "ResourceManagerMetrics.h" |
| 27 | |
| 28 | #include <cmath> |
| 29 | #include <sstream> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | using stats::media_metrics::stats_write; |
| 34 | using stats::media_metrics::MEDIA_CODEC_STARTED; |
| 35 | using stats::media_metrics::MEDIA_CODEC_STOPPED; |
| 36 | // Disabling this for now. |
| 37 | #ifdef ENABLE_MEDIA_CODEC_CONCURRENT_USAGE_REPORTED |
| 38 | using stats::media_metrics::MEDIA_CODEC_CONCURRENT_USAGE_REPORTED; |
| 39 | #endif |
| 40 | using stats::media_metrics::MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED; |
| 41 | using stats::media_metrics::MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED__RECLAIM_STATUS__RECLAIM_SUCCESS; |
| 42 | using stats::media_metrics::\ |
| 43 | MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED__RECLAIM_STATUS__RECLAIM_FAILED_NO_CLIENTS; |
| 44 | using stats::media_metrics::\ |
| 45 | MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED__RECLAIM_STATUS__RECLAIM_FAILED_RECLAIM_RESOURCES; |
| 46 | |
| 47 | inline const char* getCodecType(MediaResourceSubType codecType) { |
| 48 | switch (codecType) { |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 49 | case MediaResourceSubType::kHwAudioCodec: return "Hw Audio"; |
| 50 | case MediaResourceSubType::kSwAudioCodec: return "Sw Audio"; |
| 51 | case MediaResourceSubType::kHwVideoCodec: return "Hw Video"; |
| 52 | case MediaResourceSubType::kSwVideoCodec: return "Sw Video"; |
| 53 | case MediaResourceSubType::kHwImageCodec: return "Hw Image"; |
| 54 | case MediaResourceSubType::kSwImageCodec: return "Sw Image"; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 55 | case MediaResourceSubType::kUnspecifiedSubType: |
| 56 | default: |
| 57 | return "Unspecified"; |
| 58 | } |
| 59 | return "Unspecified"; |
| 60 | } |
| 61 | |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 62 | inline bool isHardwareCodec(MediaResourceSubType codecType) { |
| 63 | return (codecType == MediaResourceSubType::kHwAudioCodec || |
| 64 | codecType == MediaResourceSubType::kHwVideoCodec || |
| 65 | codecType == MediaResourceSubType::kHwImageCodec); |
| 66 | } |
| 67 | |
| 68 | static CodecBucket getCodecBucket(bool isEncoder, MediaResourceSubType codecType) { |
| 69 | switch (codecType) { |
| 70 | case MediaResourceSubType::kHwAudioCodec: |
| 71 | return isEncoder? HwAudioEncoder : HwAudioDecoder; |
| 72 | case MediaResourceSubType::kSwAudioCodec: |
| 73 | return isEncoder? SwAudioEncoder : SwAudioDecoder; |
| 74 | case MediaResourceSubType::kHwVideoCodec: |
| 75 | return isEncoder? HwVideoEncoder : HwVideoDecoder; |
| 76 | case MediaResourceSubType::kSwVideoCodec: |
| 77 | return isEncoder? SwVideoEncoder : SwVideoDecoder; |
| 78 | case MediaResourceSubType::kHwImageCodec: |
| 79 | return isEncoder? HwImageEncoder : HwImageDecoder; |
| 80 | case MediaResourceSubType::kSwImageCodec: |
| 81 | return isEncoder? SwImageEncoder : SwImageDecoder; |
| 82 | case MediaResourceSubType::kUnspecifiedSubType: |
| 83 | default: |
| 84 | return CodecBucketUnspecified; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return CodecBucketUnspecified; |
| 88 | } |
| 89 | |
| 90 | static bool getLogMessage(int hwCount, int swCount, std::stringstream& logMsg) { |
| 91 | bool update = false; |
| 92 | logMsg.clear(); |
| 93 | |
| 94 | if (hwCount > 0) { |
| 95 | logMsg << " HW: " << hwCount; |
| 96 | update = true; |
| 97 | } |
| 98 | if (swCount > 0) { |
| 99 | logMsg << " SW: " << swCount; |
| 100 | update = true; |
| 101 | } |
| 102 | |
| 103 | if (update) { |
| 104 | logMsg << " ] "; |
| 105 | } |
| 106 | return update; |
| 107 | } |
| 108 | |
| 109 | ResourceManagerMetrics::ResourceManagerMetrics(const sp<ProcessInfoInterface>& processInfo) { |
| 110 | // Create a process termination watcher, with 5seconds of polling frequency. |
| 111 | mUidObserver = sp<UidObserver>::make(processInfo, |
| 112 | [this] (int32_t pid, uid_t uid) { |
| 113 | onProcessTerminated(pid, uid); |
| 114 | }); |
| 115 | mUidObserver->start(); |
| 116 | } |
| 117 | |
| 118 | ResourceManagerMetrics::~ResourceManagerMetrics() { |
| 119 | mUidObserver->stop(); |
| 120 | } |
| 121 | |
| 122 | void ResourceManagerMetrics::addPid(int pid, uid_t uid) { |
| 123 | if (uid != 0) { |
| 124 | std::scoped_lock lock(mLock); |
| 125 | mUidObserver->add(pid, uid); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void ResourceManagerMetrics::notifyClientCreated(const ClientInfoParcel& clientInfo) { |
| 130 | std::scoped_lock lock(mLock); |
| 131 | // Update the resource instance count. |
| 132 | std::map<std::string, int>::iterator found = mConcurrentResourceCountMap.find(clientInfo.name); |
| 133 | if (found == mConcurrentResourceCountMap.end()) { |
| 134 | mConcurrentResourceCountMap[clientInfo.name] = 1; |
| 135 | } else { |
| 136 | found->second++; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void ResourceManagerMetrics::notifyClientReleased(const ClientInfoParcel& clientInfo) { |
| 141 | bool stopCalled = true; |
Girish | c963768 | 2023-03-23 23:33:32 +0000 | [diff] [blame] | 142 | ClientConfigParcel clientConfig; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 143 | { |
| 144 | std::scoped_lock lock(mLock); |
Girish | c963768 | 2023-03-23 23:33:32 +0000 | [diff] [blame] | 145 | ClientConfigMap::iterator found = mClientConfigMap.find(clientInfo.id); |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 146 | if (found != mClientConfigMap.end()) { |
| 147 | // Release is called without Stop! |
| 148 | stopCalled = false; |
Girish | c963768 | 2023-03-23 23:33:32 +0000 | [diff] [blame] | 149 | clientConfig = found->second; |
| 150 | // Update the timestamp for stopping the codec. |
| 151 | clientConfig.timeStamp = systemTime(SYSTEM_TIME_MONOTONIC) / 1000LL; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | if (!stopCalled) { |
| 155 | // call Stop to update the metrics. |
Girish | c963768 | 2023-03-23 23:33:32 +0000 | [diff] [blame] | 156 | notifyClientStopped(clientConfig); |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 157 | } |
| 158 | { |
| 159 | std::scoped_lock lock(mLock); |
| 160 | // Update the resource instance count also. |
| 161 | std::map<std::string, int>::iterator found = |
| 162 | mConcurrentResourceCountMap.find(clientInfo.name); |
| 163 | if (found != mConcurrentResourceCountMap.end()) { |
| 164 | if (found->second > 0) { |
| 165 | found->second--; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 171 | void ResourceManagerMetrics::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) { |
| 172 | std::scoped_lock lock(mLock); |
| 173 | ClientConfigMap::iterator entry = mClientConfigMap.find(clientConfig.clientInfo.id); |
| 174 | if (entry != mClientConfigMap.end() && |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 175 | (clientConfig.codecType == MediaResourceSubType::kHwVideoCodec || |
| 176 | clientConfig.codecType == MediaResourceSubType::kSwVideoCodec || |
| 177 | clientConfig.codecType == MediaResourceSubType::kHwImageCodec || |
| 178 | clientConfig.codecType == MediaResourceSubType::kSwImageCodec)) { |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 179 | int pid = clientConfig.clientInfo.pid; |
| 180 | // Update the pixel count for this process |
| 181 | updatePixelCount(pid, clientConfig.width * (long)clientConfig.height, |
| 182 | entry->second.width * (long)entry->second.height); |
| 183 | // Update the resolution in the record. |
| 184 | entry->second.width = clientConfig.width; |
| 185 | entry->second.height = clientConfig.height; |
| 186 | } |
| 187 | } |
| 188 | |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 189 | void ResourceManagerMetrics::notifyClientStarted(const ClientConfigParcel& clientConfig) { |
| 190 | std::scoped_lock lock(mLock); |
| 191 | int pid = clientConfig.clientInfo.pid; |
| 192 | // We need to observer this process. |
| 193 | mUidObserver->add(pid, clientConfig.clientInfo.uid); |
| 194 | |
| 195 | // Update the client config for thic client. |
| 196 | mClientConfigMap[clientConfig.clientInfo.id] = clientConfig; |
| 197 | |
| 198 | // Update the concurrent codec count for this process. |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 199 | CodecBucket codecBucket = getCodecBucket(clientConfig.isEncoder, clientConfig.codecType); |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 200 | increaseConcurrentCodecs(pid, codecBucket); |
| 201 | |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 202 | if (clientConfig.codecType == MediaResourceSubType::kHwVideoCodec || |
| 203 | clientConfig.codecType == MediaResourceSubType::kSwVideoCodec || |
| 204 | clientConfig.codecType == MediaResourceSubType::kHwImageCodec || |
| 205 | clientConfig.codecType == MediaResourceSubType::kSwImageCodec) { |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 206 | // Update the pixel count for this process |
| 207 | increasePixelCount(pid, clientConfig.width * (long)clientConfig.height); |
| 208 | } |
| 209 | |
| 210 | // System concurrent codec usage |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 211 | int systemConcurrentCodecs = mConcurrentCodecsMap[codecBucket]; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 212 | // Process/Application concurrent codec usage for this type of codec |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 213 | const ConcurrentCodecs& concurrentCodecs = mProcessConcurrentCodecsMap[pid]; |
| 214 | int appConcurrentCodecs = concurrentCodecs.mCurrent[codecBucket]; |
| 215 | int hwVideoCodecs = concurrentCodecs.mHWVideoCodecs; |
| 216 | int swVideoCodecs = concurrentCodecs.mSWVideoCodecs; |
| 217 | int videoCodecs = concurrentCodecs.mVideoCodecs; |
| 218 | int audioCodecs = concurrentCodecs.mAudioCodecs; |
| 219 | int imageCodecs = concurrentCodecs.mImageCodecs; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 220 | // Process/Application's current pixel count. |
| 221 | long pixelCount = 0; |
| 222 | std::map<int32_t, PixelCount>::iterator it = mProcessPixelsMap.find(pid); |
| 223 | if (it != mProcessPixelsMap.end()) { |
| 224 | pixelCount = it->second.mCurrent; |
| 225 | } |
| 226 | |
| 227 | int result = stats_write( |
| 228 | MEDIA_CODEC_STARTED, |
| 229 | clientConfig.clientInfo.uid, |
| 230 | clientConfig.id, |
| 231 | clientConfig.clientInfo.name.c_str(), |
| 232 | static_cast<int32_t>(clientConfig.codecType), |
| 233 | clientConfig.isEncoder, |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 234 | isHardwareCodec(clientConfig.codecType), |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 235 | clientConfig.width, clientConfig.height, |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 236 | systemConcurrentCodecs, |
| 237 | appConcurrentCodecs, |
| 238 | pixelCount, |
| 239 | hwVideoCodecs, |
| 240 | swVideoCodecs, |
| 241 | videoCodecs, |
| 242 | audioCodecs, |
| 243 | imageCodecs); |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 244 | |
| 245 | ALOGV("%s: Pushed MEDIA_CODEC_STARTED atom: " |
| 246 | "Process[pid(%d): uid(%d)] " |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 247 | "Codec: [%s: %ju] is %s %s " |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 248 | "Timestamp: %jd " |
| 249 | "Resolution: %d x %d " |
| 250 | "ConcurrentCodec[%d]={System: %d App: %d} " |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 251 | "AppConcurrentCodecs{Video: %d(HW[%d] SW[%d]) Audio: %d Image: %d} " |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 252 | "result: %d", |
| 253 | __func__, |
| 254 | pid, clientConfig.clientInfo.uid, |
| 255 | clientConfig.clientInfo.name.c_str(), |
| 256 | clientConfig.id, |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 257 | getCodecType(clientConfig.codecType), |
| 258 | clientConfig.isEncoder? "encoder" : "decoder", |
| 259 | clientConfig.timeStamp, |
| 260 | clientConfig.width, clientConfig.height, |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 261 | codecBucket, systemConcurrentCodecs, appConcurrentCodecs, |
| 262 | videoCodecs, hwVideoCodecs, swVideoCodecs, audioCodecs, imageCodecs, |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 263 | result); |
| 264 | } |
| 265 | |
| 266 | void ResourceManagerMetrics::notifyClientStopped(const ClientConfigParcel& clientConfig) { |
| 267 | std::scoped_lock lock(mLock); |
| 268 | int pid = clientConfig.clientInfo.pid; |
| 269 | // Update the concurrent codec count for this process. |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 270 | CodecBucket codecBucket = getCodecBucket(clientConfig.isEncoder, clientConfig.codecType); |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 271 | decreaseConcurrentCodecs(pid, codecBucket); |
| 272 | |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 273 | if (clientConfig.codecType == MediaResourceSubType::kHwVideoCodec || |
| 274 | clientConfig.codecType == MediaResourceSubType::kSwVideoCodec || |
| 275 | clientConfig.codecType == MediaResourceSubType::kHwImageCodec || |
| 276 | clientConfig.codecType == MediaResourceSubType::kSwImageCodec) { |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 277 | // Update the pixel count for this process |
| 278 | decreasePixelCount(pid, clientConfig.width * (long)clientConfig.height); |
| 279 | } |
| 280 | |
| 281 | // System concurrent codec usage |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 282 | int systemConcurrentCodecs = mConcurrentCodecsMap[codecBucket]; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 283 | // Process/Application concurrent codec usage for this type of codec |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 284 | int appConcurrentCodecs = 0; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 285 | std::map<int32_t, ConcurrentCodecs>::iterator found = mProcessConcurrentCodecsMap.find(pid); |
| 286 | if (found != mProcessConcurrentCodecsMap.end()) { |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 287 | appConcurrentCodecs = found->second.mCurrent[codecBucket]; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 288 | } |
| 289 | // Process/Application's current pixel count. |
| 290 | long pixelCount = 0; |
| 291 | std::map<int32_t, PixelCount>::iterator it = mProcessPixelsMap.find(pid); |
| 292 | if (it != mProcessPixelsMap.end()) { |
| 293 | pixelCount = it->second.mCurrent; |
| 294 | } |
| 295 | |
| 296 | // calculate the usageTime as: |
| 297 | // MediaCodecStopped.clientConfig.timeStamp - |
| 298 | // MediaCodecStarted.clientConfig.timeStamp |
| 299 | int64_t usageTime = 0; |
| 300 | ClientConfigMap::iterator entry = mClientConfigMap.find(clientConfig.clientInfo.id); |
| 301 | if (entry != mClientConfigMap.end()) { |
| 302 | usageTime = clientConfig.timeStamp - entry->second.timeStamp; |
| 303 | // And we can erase this config now. |
| 304 | mClientConfigMap.erase(entry); |
| 305 | } else { |
| 306 | ALOGW("%s: Start Config is missing!", __func__); |
| 307 | } |
| 308 | |
| 309 | int result = stats_write( |
| 310 | MEDIA_CODEC_STOPPED, |
| 311 | clientConfig.clientInfo.uid, |
| 312 | clientConfig.id, |
| 313 | clientConfig.clientInfo.name.c_str(), |
| 314 | static_cast<int32_t>(clientConfig.codecType), |
| 315 | clientConfig.isEncoder, |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 316 | isHardwareCodec(clientConfig.codecType), |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 317 | clientConfig.width, clientConfig.height, |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 318 | systemConcurrentCodecs, |
| 319 | appConcurrentCodecs, |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 320 | pixelCount, |
| 321 | usageTime); |
| 322 | ALOGV("%s: Pushed MEDIA_CODEC_STOPPED atom: " |
| 323 | "Process[pid(%d): uid(%d)] " |
Girish | a5a2d67 | 2023-09-20 18:40:20 +0000 | [diff] [blame^] | 324 | "Codec: [%s: %ju] is %s %s " |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 325 | "Timestamp: %jd Usage time: %jd " |
| 326 | "Resolution: %d x %d " |
| 327 | "ConcurrentCodec[%d]={System: %d App: %d} " |
| 328 | "result: %d", |
| 329 | __func__, |
| 330 | pid, clientConfig.clientInfo.uid, |
| 331 | clientConfig.clientInfo.name.c_str(), |
| 332 | clientConfig.id, |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 333 | getCodecType(clientConfig.codecType), |
| 334 | clientConfig.isEncoder? "encoder" : "decoder", |
| 335 | clientConfig.timeStamp, usageTime, |
| 336 | clientConfig.width, clientConfig.height, |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 337 | codecBucket, systemConcurrentCodecs, appConcurrentCodecs, |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 338 | result); |
| 339 | } |
| 340 | |
| 341 | void ResourceManagerMetrics::onProcessTerminated(int32_t pid, uid_t uid) { |
| 342 | std::scoped_lock lock(mLock); |
| 343 | // post MediaCodecConcurrentUsageReported for this terminated pid. |
| 344 | pushConcurrentUsageReport(pid, uid); |
| 345 | } |
| 346 | |
| 347 | void ResourceManagerMetrics::pushConcurrentUsageReport(int32_t pid, uid_t uid) { |
| 348 | // Process/Application peak concurrent codec usage |
| 349 | std::map<int32_t, ConcurrentCodecs>::iterator found = mProcessConcurrentCodecsMap.find(pid); |
| 350 | if (found == mProcessConcurrentCodecsMap.end()) { |
| 351 | ALOGI("%s: No MEDIA_CODEC_CONCURRENT_USAGE_REPORTED atom Entry for: " |
| 352 | "Application[pid(%d): uid(%d)]", __func__, pid, uid); |
| 353 | return; |
| 354 | } |
| 355 | const ConcurrentCodecsMap& codecsMap = found->second.mPeak; |
| 356 | int peakHwAudioEncoderCount = codecsMap[HwAudioEncoder]; |
| 357 | int peakHwAudioDecoderCount = codecsMap[HwAudioDecoder]; |
| 358 | int peakHwVideoEncoderCount = codecsMap[HwVideoEncoder]; |
| 359 | int peakHwVideoDecoderCount = codecsMap[HwVideoDecoder]; |
| 360 | int peakHwImageEncoderCount = codecsMap[HwImageEncoder]; |
| 361 | int peakHwImageDecoderCount = codecsMap[HwImageDecoder]; |
| 362 | int peakSwAudioEncoderCount = codecsMap[SwAudioEncoder]; |
| 363 | int peakSwAudioDecoderCount = codecsMap[SwAudioDecoder]; |
| 364 | int peakSwVideoEncoderCount = codecsMap[SwVideoEncoder]; |
| 365 | int peakSwVideoDecoderCount = codecsMap[SwVideoDecoder]; |
| 366 | int peakSwImageEncoderCount = codecsMap[SwImageEncoder]; |
| 367 | int peakSwImageDecoderCount = codecsMap[SwImageDecoder]; |
| 368 | |
| 369 | long peakPixels = 0; |
| 370 | std::map<int32_t, PixelCount>::iterator it = mProcessPixelsMap.find(pid); |
| 371 | if (it == mProcessPixelsMap.end()) { |
| 372 | ALOGI("%s: No Video Codec Entry for Application[pid(%d): uid(%d)]", |
| 373 | __func__, pid, uid); |
| 374 | } else { |
| 375 | peakPixels = it->second.mPeak; |
| 376 | } |
| 377 | std::string peakPixelsLog("Peak Pixels: " + std::to_string(peakPixels)); |
| 378 | |
| 379 | std::stringstream peakCodecLog; |
| 380 | peakCodecLog << "Peak { "; |
| 381 | std::stringstream logMsg; |
| 382 | if (getLogMessage(peakHwAudioEncoderCount, peakSwAudioEncoderCount, logMsg)) { |
| 383 | peakCodecLog << "AudioEnc[" << logMsg.str(); |
| 384 | } |
| 385 | if (getLogMessage(peakHwAudioDecoderCount, peakSwAudioDecoderCount, logMsg)) { |
| 386 | peakCodecLog << "AudioDec[" << logMsg.str(); |
| 387 | } |
| 388 | if (getLogMessage(peakHwVideoEncoderCount, peakSwVideoEncoderCount, logMsg)) { |
| 389 | peakCodecLog << "VideoEnc[" << logMsg.str(); |
| 390 | } |
| 391 | if (getLogMessage(peakHwVideoDecoderCount, peakSwVideoDecoderCount, logMsg)) { |
| 392 | peakCodecLog << "VideoDec[" << logMsg.str(); |
| 393 | } |
| 394 | if (getLogMessage(peakHwImageEncoderCount, peakSwImageEncoderCount, logMsg)) { |
| 395 | peakCodecLog << "ImageEnc[" << logMsg.str(); |
| 396 | } |
| 397 | if (getLogMessage(peakHwImageDecoderCount, peakSwImageDecoderCount, logMsg)) { |
| 398 | peakCodecLog << "ImageDec[" << logMsg.str(); |
| 399 | } |
| 400 | peakCodecLog << "}"; |
| 401 | |
| 402 | #ifdef ENABLE_MEDIA_CODEC_CONCURRENT_USAGE_REPORTED |
| 403 | int result = stats_write( |
| 404 | MEDIA_CODEC_CONCURRENT_USAGE_REPORTED, |
| 405 | uid, |
| 406 | peakHwVideoDecoderCount, |
| 407 | peakHwVideoEncoderCount, |
| 408 | peakSwVideoDecoderCount, |
| 409 | peakSwVideoEncoderCount, |
| 410 | peakHwAudioDecoderCount, |
| 411 | peakHwAudioEncoderCount, |
| 412 | peakSwAudioDecoderCount, |
| 413 | peakSwAudioEncoderCount, |
| 414 | peakHwImageDecoderCount, |
| 415 | peakHwImageEncoderCount, |
| 416 | peakSwImageDecoderCount, |
| 417 | peakSwImageEncoderCount, |
| 418 | peakPixels); |
| 419 | ALOGI("%s: Pushed MEDIA_CODEC_CONCURRENT_USAGE_REPORTED atom: " |
| 420 | "Process[pid(%d): uid(%d)] %s %s result: %d", |
| 421 | __func__, pid, uid, peakCodecLog.str().c_str(), peakPixelsLog.c_str(), result); |
| 422 | #else |
| 423 | ALOGI("%s: Concurrent Codec Usage Report for the Process[pid(%d): uid(%d)] is %s %s", |
| 424 | __func__, pid, uid, peakCodecLog.str().c_str(), peakPixelsLog.c_str()); |
| 425 | #endif |
| 426 | } |
| 427 | |
| 428 | void ResourceManagerMetrics::pushReclaimAtom(const ClientInfoParcel& clientInfo, |
| 429 | const std::vector<int>& priorities, |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 430 | const std::vector<std::shared_ptr<IResourceManagerClient>>& clients, |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 431 | const PidUidVector& idList, bool reclaimed) { |
| 432 | // Construct the metrics for codec reclaim as a pushed atom. |
| 433 | // 1. Information about the requester. |
| 434 | // - UID and the priority (oom score) |
| 435 | int32_t callingPid = clientInfo.pid; |
| 436 | int32_t requesterUid = clientInfo.uid; |
| 437 | std::string clientName = clientInfo.name; |
| 438 | int requesterPriority = priorities[0]; |
| 439 | |
| 440 | // 2. Information about the codec. |
| 441 | // - Name of the codec requested |
| 442 | // - Number of concurrent codecs running. |
| 443 | int32_t noOfConcurrentCodecs = 0; |
| 444 | std::map<std::string, int>::iterator found = mConcurrentResourceCountMap.find(clientName); |
| 445 | if (found != mConcurrentResourceCountMap.end()) { |
| 446 | noOfConcurrentCodecs = found->second; |
| 447 | } |
| 448 | |
| 449 | // 3. Information about the Reclaim: |
| 450 | // - Status of reclaim request |
| 451 | // - How many codecs are reclaimed |
| 452 | // - For each codecs reclaimed, information of the process that it belonged to: |
| 453 | // - UID and the Priority (oom score) |
| 454 | int32_t reclaimStatus = MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED__RECLAIM_STATUS__RECLAIM_SUCCESS; |
| 455 | if (!reclaimed) { |
| 456 | if (clients.size() == 0) { |
| 457 | // No clients to reclaim from |
| 458 | reclaimStatus = |
| 459 | MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED__RECLAIM_STATUS__RECLAIM_FAILED_NO_CLIENTS; |
| 460 | } else { |
| 461 | // Couldn't reclaim resources from the clients |
| 462 | reclaimStatus = |
| 463 | MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED__RECLAIM_STATUS__RECLAIM_FAILED_RECLAIM_RESOURCES; |
| 464 | } |
| 465 | } |
| 466 | int32_t noOfCodecsReclaimed = clients.size(); |
| 467 | int32_t targetIndex = 1; |
| 468 | for (PidUidVector::const_reference id : idList) { |
| 469 | int32_t targetUid = id.second; |
| 470 | int targetPriority = priorities[targetIndex]; |
| 471 | // Post the pushed atom |
| 472 | int result = stats_write( |
| 473 | MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED, |
| 474 | requesterUid, |
| 475 | requesterPriority, |
| 476 | clientName.c_str(), |
| 477 | noOfConcurrentCodecs, |
| 478 | reclaimStatus, |
| 479 | noOfCodecsReclaimed, |
| 480 | targetIndex, |
| 481 | targetUid, |
| 482 | targetPriority); |
| 483 | ALOGI("%s: Pushed MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED atom: " |
| 484 | "Requester[pid(%d): uid(%d): priority(%d)] " |
| 485 | "Codec: [%s] " |
| 486 | "No of concurrent codecs: %d " |
| 487 | "Reclaim Status: %d " |
| 488 | "No of codecs reclaimed: %d " |
| 489 | "Target[%d][pid(%d): uid(%d): priority(%d)] result: %d", |
| 490 | __func__, callingPid, requesterUid, requesterPriority, |
| 491 | clientName.c_str(), noOfConcurrentCodecs, |
| 492 | reclaimStatus, noOfCodecsReclaimed, |
| 493 | targetIndex, id.first, targetUid, targetPriority, result); |
| 494 | targetIndex++; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void ResourceManagerMetrics::increaseConcurrentCodecs(int32_t pid, |
| 499 | CodecBucket codecBucket) { |
| 500 | // Increase the codec usage across the system. |
| 501 | mConcurrentCodecsMap[codecBucket]++; |
| 502 | |
| 503 | // Now update the codec usage for this (pid) process. |
| 504 | std::map<int32_t, ConcurrentCodecs>::iterator found = mProcessConcurrentCodecsMap.find(pid); |
| 505 | if (found == mProcessConcurrentCodecsMap.end()) { |
| 506 | ConcurrentCodecs codecs; |
| 507 | codecs.mCurrent[codecBucket] = 1; |
| 508 | codecs.mPeak[codecBucket] = 1; |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 509 | auto added = mProcessConcurrentCodecsMap.emplace(pid, codecs); |
| 510 | found = added.first; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 511 | } else { |
| 512 | found->second.mCurrent[codecBucket]++; |
| 513 | // Check if it's the peak count for this slot. |
| 514 | if (found->second.mPeak[codecBucket] < found->second.mCurrent[codecBucket]) { |
| 515 | found->second.mPeak[codecBucket] = found->second.mCurrent[codecBucket]; |
| 516 | } |
| 517 | } |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 518 | |
| 519 | switch (codecBucket) { |
| 520 | case HwVideoEncoder: |
| 521 | case HwVideoDecoder: |
| 522 | case SwVideoEncoder: |
| 523 | case SwVideoDecoder: |
| 524 | if (codecBucket == HwVideoEncoder || codecBucket == HwVideoDecoder) { |
| 525 | found->second.mHWVideoCodecs++; |
| 526 | } else { |
| 527 | found->second.mSWVideoCodecs++; |
| 528 | } |
| 529 | found->second.mVideoCodecs++; |
| 530 | break; |
| 531 | case HwAudioEncoder: |
| 532 | case HwAudioDecoder: |
| 533 | case SwAudioEncoder: |
| 534 | case SwAudioDecoder: |
| 535 | found->second.mAudioCodecs++; |
| 536 | break; |
| 537 | case HwImageEncoder: |
| 538 | case HwImageDecoder: |
| 539 | case SwImageEncoder: |
| 540 | case SwImageDecoder: |
| 541 | found->second.mImageCodecs++; |
| 542 | break; |
| 543 | default: |
| 544 | break; |
| 545 | } |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void ResourceManagerMetrics::decreaseConcurrentCodecs(int32_t pid, |
| 549 | CodecBucket codecBucket) { |
| 550 | // Decrease the codec usage across the system. |
| 551 | if (mConcurrentCodecsMap[codecBucket] > 0) { |
| 552 | mConcurrentCodecsMap[codecBucket]--; |
| 553 | } |
| 554 | |
| 555 | // Now update the codec usage for this (pid) process. |
| 556 | std::map<int32_t, ConcurrentCodecs>::iterator found = mProcessConcurrentCodecsMap.find(pid); |
| 557 | if (found != mProcessConcurrentCodecsMap.end()) { |
| 558 | if (found->second.mCurrent[codecBucket] > 0) { |
| 559 | found->second.mCurrent[codecBucket]--; |
| 560 | } |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 561 | |
| 562 | switch (codecBucket) { |
| 563 | case HwVideoEncoder: |
| 564 | case HwVideoDecoder: |
| 565 | case SwVideoEncoder: |
| 566 | case SwVideoDecoder: |
| 567 | if (codecBucket == HwVideoEncoder || codecBucket == HwVideoDecoder) { |
| 568 | found->second.mHWVideoCodecs--; |
| 569 | } else { |
| 570 | found->second.mSWVideoCodecs--; |
| 571 | } |
| 572 | found->second.mVideoCodecs--; |
| 573 | break; |
| 574 | case HwAudioEncoder: |
| 575 | case HwAudioDecoder: |
| 576 | case SwAudioEncoder: |
| 577 | case SwAudioDecoder: |
| 578 | found->second.mAudioCodecs--; |
| 579 | break; |
| 580 | case HwImageEncoder: |
| 581 | case HwImageDecoder: |
| 582 | case SwImageEncoder: |
| 583 | case SwImageDecoder: |
| 584 | found->second.mImageCodecs--; |
| 585 | break; |
| 586 | default: |
| 587 | break; |
| 588 | } |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | void ResourceManagerMetrics::increasePixelCount(int32_t pid, long pixels) { |
| 593 | // Now update the current pixel usage for this (pid) process. |
| 594 | std::map<int32_t, PixelCount>::iterator found = mProcessPixelsMap.find(pid); |
| 595 | if (found == mProcessPixelsMap.end()) { |
| 596 | PixelCount pixelCount {pixels, pixels}; |
| 597 | mProcessPixelsMap.emplace(pid, pixelCount); |
| 598 | } else { |
| 599 | if (__builtin_add_overflow(found->second.mCurrent, pixels, &found->second.mCurrent)) { |
| 600 | ALOGI("Pixel Count overflow"); |
| 601 | return; |
| 602 | } |
| 603 | // Check if it's the peak count for this slot. |
| 604 | if (found->second.mPeak < found->second.mCurrent) { |
| 605 | found->second.mPeak = found->second.mCurrent; |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 610 | void ResourceManagerMetrics::updatePixelCount(int32_t pid, long newPixels, long lastPixels) { |
| 611 | // Since there is change in resolution, decrease it by last pixels and |
| 612 | // increase it by new pixels. |
| 613 | decreasePixelCount(pid, lastPixels); |
| 614 | increasePixelCount(pid, newPixels); |
| 615 | } |
| 616 | |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 617 | void ResourceManagerMetrics::decreasePixelCount(int32_t pid, long pixels) { |
| 618 | // Now update the current pixel usage for this (pid) process. |
| 619 | std::map<int32_t, PixelCount>::iterator found = mProcessPixelsMap.find(pid); |
| 620 | if (found != mProcessPixelsMap.end()) { |
| 621 | if (found->second.mCurrent < pixels) { |
| 622 | found->second.mCurrent = 0; |
| 623 | } else { |
| 624 | if (__builtin_sub_overflow(found->second.mCurrent, pixels, &found->second.mCurrent)) { |
| 625 | ALOGI("Pixel Count overflow"); |
| 626 | return; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | long ResourceManagerMetrics::getPeakConcurrentPixelCount(int pid) const { |
| 633 | std::map<int32_t, PixelCount>::const_iterator found = mProcessPixelsMap.find(pid); |
| 634 | if (found != mProcessPixelsMap.end()) { |
| 635 | return found->second.mPeak; |
| 636 | } |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | long ResourceManagerMetrics::getCurrentConcurrentPixelCount(int pid) const { |
| 642 | std::map<int32_t, PixelCount>::const_iterator found = mProcessPixelsMap.find(pid); |
| 643 | if (found != mProcessPixelsMap.end()) { |
| 644 | return found->second.mCurrent; |
| 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | } // namespace android |