Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2015, 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 "ResourceManagerService" |
| 20 | #include <utils/Log.h> |
| 21 | |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 22 | #include <binder/IMediaResourceMonitor.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 24 | #include <cutils/sched_policy.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 25 | #include <dirent.h> |
| 26 | #include <media/stagefright/ProcessInfo.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 27 | #include <mediautils/BatteryNotifier.h> |
| 28 | #include <mediautils/SchedulingPolicyService.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/time.h> |
| 33 | #include <unistd.h> |
| 34 | |
| 35 | #include "ResourceManagerService.h" |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 36 | #include "ServiceLog.h" |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 37 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 38 | namespace android { |
| 39 | |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | class DeathNotifier : public IBinder::DeathRecipient { |
| 43 | public: |
| 44 | DeathNotifier(const wp<ResourceManagerService> &service, int pid, int64_t clientId) |
| 45 | : mService(service), mPid(pid), mClientId(clientId) {} |
| 46 | |
| 47 | virtual void binderDied(const wp<IBinder> & /* who */) override { |
| 48 | // Don't check for pid validity since we know it's already dead. |
| 49 | sp<ResourceManagerService> service = mService.promote(); |
| 50 | if (service == nullptr) { |
| 51 | ALOGW("ResourceManagerService is dead as well."); |
| 52 | return; |
| 53 | } |
| 54 | service->removeResource(mPid, mClientId, false); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | wp<ResourceManagerService> mService; |
| 59 | int mPid; |
| 60 | int64_t mClientId; |
| 61 | }; |
| 62 | |
| 63 | } // namespace |
| 64 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 65 | template <typename T> |
| 66 | static String8 getString(const Vector<T> &items) { |
| 67 | String8 itemsStr; |
| 68 | for (size_t i = 0; i < items.size(); ++i) { |
| 69 | itemsStr.appendFormat("%s ", items[i].toString().string()); |
| 70 | } |
| 71 | return itemsStr; |
| 72 | } |
| 73 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 74 | static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) { |
| 75 | for (auto it = resources.begin(); it != resources.end(); it++) { |
| 76 | if (it->second.mType == type) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
Chih-Hung Hsieh | 51873d8 | 2016-08-09 14:18:51 -0700 | [diff] [blame] | 83 | static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 84 | for (size_t i = 0; i < infos.size(); ++i) { |
| 85 | if (hasResourceType(type, infos[i].resources)) { |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | static ResourceInfos& getResourceInfosForEdit( |
| 93 | int pid, |
| 94 | PidResourceInfosMap& map) { |
| 95 | ssize_t index = map.indexOfKey(pid); |
| 96 | if (index < 0) { |
| 97 | // new pid |
| 98 | ResourceInfos infosForPid; |
| 99 | map.add(pid, infosForPid); |
| 100 | } |
| 101 | |
| 102 | return map.editValueFor(pid); |
| 103 | } |
| 104 | |
| 105 | static ResourceInfo& getResourceInfoForEdit( |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 106 | uid_t uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 107 | int64_t clientId, |
Chih-Hung Hsieh | 51873d8 | 2016-08-09 14:18:51 -0700 | [diff] [blame] | 108 | const sp<IResourceManagerClient>& client, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 109 | ResourceInfos& infos) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 110 | ssize_t index = infos.indexOfKey(clientId); |
| 111 | |
| 112 | if (index < 0) { |
| 113 | ResourceInfo info; |
| 114 | info.uid = uid; |
| 115 | info.clientId = clientId; |
| 116 | info.client = client; |
| 117 | |
| 118 | index = infos.add(clientId, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 119 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 120 | |
| 121 | return infos.editValueAt(index); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 124 | static void notifyResourceGranted(int pid, const Vector<MediaResource> &resources) { |
| 125 | static const char* const kServiceName = "media_resource_monitor"; |
Dongwon Kang | 2642c84 | 2016-03-23 18:07:29 -0700 | [diff] [blame] | 126 | sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName)); |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 127 | if (binder != NULL) { |
| 128 | sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder); |
| 129 | for (size_t i = 0; i < resources.size(); ++i) { |
Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 130 | if (resources[i].mSubType == MediaResource::kAudioCodec) { |
| 131 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC); |
| 132 | } else if (resources[i].mSubType == MediaResource::kVideoCodec) { |
| 133 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC); |
| 134 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 139 | status_t ResourceManagerService::dump(int fd, const Vector<String16>& /* args */) { |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 140 | String8 result; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 141 | |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 142 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 143 | result.format("Permission Denial: " |
| 144 | "can't dump ResourceManagerService from pid=%d, uid=%d\n", |
| 145 | IPCThreadState::self()->getCallingPid(), |
| 146 | IPCThreadState::self()->getCallingUid()); |
| 147 | write(fd, result.string(), result.size()); |
| 148 | return PERMISSION_DENIED; |
| 149 | } |
| 150 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 151 | PidResourceInfosMap mapCopy; |
| 152 | bool supportsMultipleSecureCodecs; |
| 153 | bool supportsSecureWithNonSecureCodec; |
| 154 | String8 serviceLog; |
| 155 | { |
| 156 | Mutex::Autolock lock(mLock); |
| 157 | mapCopy = mMap; // Shadow copy, real copy will happen on write. |
| 158 | supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs; |
| 159 | supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec; |
| 160 | serviceLog = mServiceLog->toString(" " /* linePrefix */); |
| 161 | } |
| 162 | |
| 163 | const size_t SIZE = 256; |
| 164 | char buffer[SIZE]; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 165 | snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this); |
| 166 | result.append(buffer); |
| 167 | result.append(" Policies:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 168 | snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 169 | result.append(buffer); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 170 | snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n", |
| 171 | supportsSecureWithNonSecureCodec); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 172 | result.append(buffer); |
| 173 | |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 174 | result.append(" Processes:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 175 | for (size_t i = 0; i < mapCopy.size(); ++i) { |
| 176 | snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i)); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 177 | result.append(buffer); |
| 178 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 179 | const ResourceInfos &infos = mapCopy.valueAt(i); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 180 | for (size_t j = 0; j < infos.size(); ++j) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 181 | result.append(" Client:\n"); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 182 | snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId); |
| 183 | result.append(buffer); |
| 184 | |
| 185 | snprintf(buffer, SIZE, " Name: %s\n", infos[j].client->getName().string()); |
| 186 | result.append(buffer); |
| 187 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 188 | const ResourceList &resources = infos[j].resources; |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 189 | result.append(" Resources:\n"); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 190 | for (auto it = resources.begin(); it != resources.end(); it++) { |
| 191 | snprintf(buffer, SIZE, " %s\n", it->second.toString().string()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 192 | result.append(buffer); |
| 193 | } |
| 194 | } |
| 195 | } |
Ronghua Wu | 022ed72 | 2015-05-11 15:15:09 -0700 | [diff] [blame] | 196 | result.append(" Events logs (most recent at top):\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 197 | result.append(serviceLog); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 198 | |
| 199 | write(fd, result.string(), result.size()); |
| 200 | return OK; |
| 201 | } |
| 202 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 203 | ResourceManagerService::ResourceManagerService() |
Dongwon Kang | 2642c84 | 2016-03-23 18:07:29 -0700 | [diff] [blame] | 204 | : ResourceManagerService(new ProcessInfo()) {} |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 205 | |
| 206 | ResourceManagerService::ResourceManagerService(sp<ProcessInfoInterface> processInfo) |
| 207 | : mProcessInfo(processInfo), |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 208 | mServiceLog(new ServiceLog()), |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 209 | mSupportsMultipleSecureCodecs(true), |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 210 | mSupportsSecureWithNonSecureCodec(true), |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 211 | mCpuBoostCount(0) { |
| 212 | BatteryNotifier::getInstance().noteResetVideo(); |
| 213 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 214 | |
| 215 | ResourceManagerService::~ResourceManagerService() {} |
| 216 | |
| 217 | void ResourceManagerService::config(const Vector<MediaResourcePolicy> &policies) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 218 | String8 log = String8::format("config(%s)", getString(policies).string()); |
| 219 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 220 | |
| 221 | Mutex::Autolock lock(mLock); |
| 222 | for (size_t i = 0; i < policies.size(); ++i) { |
| 223 | String8 type = policies[i].mType; |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 224 | String8 value = policies[i].mValue; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 225 | if (type == kPolicySupportsMultipleSecureCodecs) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 226 | mSupportsMultipleSecureCodecs = (value == "true"); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 227 | } else if (type == kPolicySupportsSecureWithNonSecureCodec) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 228 | mSupportsSecureWithNonSecureCodec = (value == "true"); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 233 | void ResourceManagerService::onFirstAdded( |
| 234 | const MediaResource& resource, const ResourceInfo& clientInfo) { |
| 235 | // first time added |
| 236 | if (resource.mType == MediaResource::kCpuBoost |
| 237 | && resource.mSubType == MediaResource::kUnspecifiedSubType) { |
| 238 | // Request it on every new instance of kCpuBoost, as the media.codec |
| 239 | // could have died, if we only do it the first time subsequent instances |
| 240 | // never gets the boost. |
| 241 | if (requestCpusetBoost(true, this) != OK) { |
| 242 | ALOGW("couldn't request cpuset boost"); |
| 243 | } |
| 244 | mCpuBoostCount++; |
| 245 | } else if (resource.mType == MediaResource::kBattery |
| 246 | && resource.mSubType == MediaResource::kVideoCodec) { |
| 247 | BatteryNotifier::getInstance().noteStartVideo(clientInfo.uid); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void ResourceManagerService::onLastRemoved( |
| 252 | const MediaResource& resource, const ResourceInfo& clientInfo) { |
| 253 | if (resource.mType == MediaResource::kCpuBoost |
| 254 | && resource.mSubType == MediaResource::kUnspecifiedSubType |
| 255 | && mCpuBoostCount > 0) { |
| 256 | if (--mCpuBoostCount == 0) { |
| 257 | requestCpusetBoost(false, this); |
| 258 | } |
| 259 | } else if (resource.mType == MediaResource::kBattery |
| 260 | && resource.mSubType == MediaResource::kVideoCodec) { |
| 261 | BatteryNotifier::getInstance().noteStopVideo(clientInfo.uid); |
| 262 | } |
| 263 | } |
| 264 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 265 | void ResourceManagerService::addResource( |
| 266 | int pid, |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 267 | int uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 268 | int64_t clientId, |
| 269 | const sp<IResourceManagerClient> client, |
| 270 | const Vector<MediaResource> &resources) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 271 | String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)", |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 272 | pid, (long long) clientId, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 273 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 274 | |
| 275 | Mutex::Autolock lock(mLock); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 276 | if (!mProcessInfo->isValidPid(pid)) { |
| 277 | ALOGE("Rejected addResource call with invalid pid."); |
| 278 | return; |
| 279 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 280 | ResourceInfos& infos = getResourceInfosForEdit(pid, mMap); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 281 | ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos); |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 282 | |
| 283 | for (size_t i = 0; i < resources.size(); ++i) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 284 | const auto resType = std::make_pair(resources[i].mType, resources[i].mSubType); |
| 285 | if (info.resources.find(resType) == info.resources.end()) { |
| 286 | onFirstAdded(resources[i], info); |
| 287 | info.resources[resType] = resources[i]; |
| 288 | } else { |
| 289 | info.resources[resType].mValue += resources[i].mValue; |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 292 | if (info.deathNotifier == nullptr) { |
| 293 | info.deathNotifier = new DeathNotifier(this, pid, clientId); |
| 294 | IInterface::asBinder(client)->linkToDeath(info.deathNotifier); |
| 295 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 296 | notifyResourceGranted(pid, resources); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 299 | void ResourceManagerService::removeResource(int pid, int64_t clientId, |
| 300 | const Vector<MediaResource> &resources) { |
| 301 | String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)", |
| 302 | pid, (long long) clientId, getString(resources).string()); |
| 303 | mServiceLog->add(log); |
| 304 | |
| 305 | Mutex::Autolock lock(mLock); |
| 306 | if (!mProcessInfo->isValidPid(pid)) { |
| 307 | ALOGE("Rejected removeResource call with invalid pid."); |
| 308 | return; |
| 309 | } |
| 310 | ssize_t index = mMap.indexOfKey(pid); |
| 311 | if (index < 0) { |
| 312 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
| 313 | return; |
| 314 | } |
| 315 | ResourceInfos &infos = mMap.editValueAt(index); |
| 316 | |
| 317 | index = infos.indexOfKey(clientId); |
| 318 | if (index < 0) { |
| 319 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | ResourceInfo &info = infos.editValueAt(index); |
| 324 | |
| 325 | for (size_t i = 0; i < resources.size(); ++i) { |
| 326 | const auto resType = std::make_pair(resources[i].mType, resources[i].mSubType); |
| 327 | // ignore if we don't have it |
| 328 | if (info.resources.find(resType) != info.resources.end()) { |
| 329 | MediaResource &resource = info.resources[resType]; |
| 330 | if (resource.mValue > resources[i].mValue) { |
| 331 | resource.mValue -= resources[i].mValue; |
| 332 | } else { |
| 333 | onLastRemoved(resources[i], info); |
| 334 | info.resources.erase(resType); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void ResourceManagerService::removeClient(int pid, int64_t clientId) { |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 341 | removeResource(pid, clientId, true); |
| 342 | } |
| 343 | |
| 344 | void ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) { |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 345 | String8 log = String8::format( |
| 346 | "removeResource(pid %d, clientId %lld)", |
| 347 | pid, (long long) clientId); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 348 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 349 | |
| 350 | Mutex::Autolock lock(mLock); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 351 | if (checkValid && !mProcessInfo->isValidPid(pid)) { |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 352 | ALOGE("Rejected removeResource call with invalid pid."); |
| 353 | return; |
| 354 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 355 | ssize_t index = mMap.indexOfKey(pid); |
| 356 | if (index < 0) { |
| 357 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
| 358 | return; |
| 359 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 360 | ResourceInfos &infos = mMap.editValueAt(index); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 361 | |
| 362 | index = infos.indexOfKey(clientId); |
| 363 | if (index < 0) { |
| 364 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
| 365 | return; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 366 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 367 | |
| 368 | const ResourceInfo &info = infos[index]; |
| 369 | for (auto it = info.resources.begin(); it != info.resources.end(); it++) { |
| 370 | onLastRemoved(it->second, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 371 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 372 | |
| 373 | IInterface::asBinder(info.client)->unlinkToDeath(info.deathNotifier); |
| 374 | |
| 375 | infos.removeItemsAt(index); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 378 | void ResourceManagerService::getClientForResource_l( |
| 379 | int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients) { |
| 380 | if (res == NULL) { |
| 381 | return; |
| 382 | } |
| 383 | sp<IResourceManagerClient> client; |
| 384 | if (getLowestPriorityBiggestClient_l(callingPid, res->mType, &client)) { |
| 385 | clients->push_back(client); |
| 386 | } |
| 387 | } |
| 388 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 389 | bool ResourceManagerService::reclaimResource( |
| 390 | int callingPid, const Vector<MediaResource> &resources) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 391 | String8 log = String8::format("reclaimResource(callingPid %d, resources %s)", |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 392 | callingPid, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 393 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 394 | |
| 395 | Vector<sp<IResourceManagerClient>> clients; |
| 396 | { |
| 397 | Mutex::Autolock lock(mLock); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 398 | if (!mProcessInfo->isValidPid(callingPid)) { |
| 399 | ALOGE("Rejected reclaimResource call with invalid callingPid."); |
| 400 | return false; |
| 401 | } |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 402 | const MediaResource *secureCodec = NULL; |
| 403 | const MediaResource *nonSecureCodec = NULL; |
| 404 | const MediaResource *graphicMemory = NULL; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 405 | for (size_t i = 0; i < resources.size(); ++i) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 406 | MediaResource::Type type = resources[i].mType; |
| 407 | if (resources[i].mType == MediaResource::kSecureCodec) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 408 | secureCodec = &resources[i]; |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 409 | } else if (type == MediaResource::kNonSecureCodec) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 410 | nonSecureCodec = &resources[i]; |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 411 | } else if (type == MediaResource::kGraphicMemory) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 412 | graphicMemory = &resources[i]; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | // first pass to handle secure/non-secure codec conflict |
| 417 | if (secureCodec != NULL) { |
| 418 | if (!mSupportsMultipleSecureCodecs) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 419 | if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 420 | return false; |
| 421 | } |
| 422 | } |
| 423 | if (!mSupportsSecureWithNonSecureCodec) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 424 | if (!getAllClients_l(callingPid, MediaResource::kNonSecureCodec, &clients)) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 425 | return false; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | if (nonSecureCodec != NULL) { |
| 430 | if (!mSupportsSecureWithNonSecureCodec) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 431 | if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 432 | return false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (clients.size() == 0) { |
| 438 | // if no secure/non-secure codec conflict, run second pass to handle other resources. |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 439 | getClientForResource_l(callingPid, graphicMemory, &clients); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 440 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 441 | |
| 442 | if (clients.size() == 0) { |
| 443 | // if we are here, run the third pass to free one codec with the same type. |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 444 | getClientForResource_l(callingPid, secureCodec, &clients); |
| 445 | getClientForResource_l(callingPid, nonSecureCodec, &clients); |
| 446 | } |
| 447 | |
| 448 | if (clients.size() == 0) { |
| 449 | // if we are here, run the fourth pass to free one codec with the different type. |
| 450 | if (secureCodec != NULL) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 451 | MediaResource temp(MediaResource::kNonSecureCodec, 1); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 452 | getClientForResource_l(callingPid, &temp, &clients); |
| 453 | } |
| 454 | if (nonSecureCodec != NULL) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 455 | MediaResource temp(MediaResource::kSecureCodec, 1); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 456 | getClientForResource_l(callingPid, &temp, &clients); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | if (clients.size() == 0) { |
| 462 | return false; |
| 463 | } |
| 464 | |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 465 | sp<IResourceManagerClient> failedClient; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 466 | for (size_t i = 0; i < clients.size(); ++i) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 467 | log = String8::format("reclaimResource from client %p", clients[i].get()); |
| 468 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 469 | if (!clients[i]->reclaimResource()) { |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 470 | failedClient = clients[i]; |
| 471 | break; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 474 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 475 | if (failedClient == NULL) { |
| 476 | return true; |
| 477 | } |
| 478 | |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 479 | { |
| 480 | Mutex::Autolock lock(mLock); |
| 481 | bool found = false; |
| 482 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 483 | ResourceInfos &infos = mMap.editValueAt(i); |
| 484 | for (size_t j = 0; j < infos.size();) { |
| 485 | if (infos[j].client == failedClient) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 486 | j = infos.removeItemsAt(j); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 487 | found = true; |
| 488 | } else { |
| 489 | ++j; |
| 490 | } |
| 491 | } |
| 492 | if (found) { |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | if (!found) { |
| 497 | ALOGV("didn't find failed client"); |
| 498 | } |
| 499 | } |
| 500 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 501 | return false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | bool ResourceManagerService::getAllClients_l( |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 505 | int callingPid, MediaResource::Type type, Vector<sp<IResourceManagerClient>> *clients) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 506 | Vector<sp<IResourceManagerClient>> temp; |
| 507 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 508 | ResourceInfos &infos = mMap.editValueAt(i); |
| 509 | for (size_t j = 0; j < infos.size(); ++j) { |
| 510 | if (hasResourceType(type, infos[j].resources)) { |
| 511 | if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) { |
| 512 | // some higher/equal priority process owns the resource, |
| 513 | // this request can't be fulfilled. |
| 514 | ALOGE("getAllClients_l: can't reclaim resource %s from pid %d", |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 515 | asString(type), mMap.keyAt(i)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | temp.push_back(infos[j].client); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | if (temp.size() == 0) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 523 | ALOGV("getAllClients_l: didn't find any resource %s", asString(type)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 524 | return true; |
| 525 | } |
| 526 | clients->appendVector(temp); |
| 527 | return true; |
| 528 | } |
| 529 | |
| 530 | bool ResourceManagerService::getLowestPriorityBiggestClient_l( |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 531 | int callingPid, MediaResource::Type type, sp<IResourceManagerClient> *client) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 532 | int lowestPriorityPid; |
| 533 | int lowestPriority; |
| 534 | int callingPriority; |
| 535 | if (!mProcessInfo->getPriority(callingPid, &callingPriority)) { |
| 536 | ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d", |
| 537 | callingPid); |
| 538 | return false; |
| 539 | } |
| 540 | if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) { |
| 541 | return false; |
| 542 | } |
| 543 | if (lowestPriority <= callingPriority) { |
| 544 | ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d", |
| 545 | lowestPriority, callingPriority); |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | if (!getBiggestClient_l(lowestPriorityPid, type, client)) { |
| 550 | return false; |
| 551 | } |
| 552 | return true; |
| 553 | } |
| 554 | |
| 555 | bool ResourceManagerService::getLowestPriorityPid_l( |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 556 | MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 557 | int pid = -1; |
| 558 | int priority = -1; |
| 559 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 560 | if (mMap.valueAt(i).size() == 0) { |
| 561 | // no client on this process. |
| 562 | continue; |
| 563 | } |
| 564 | if (!hasResourceType(type, mMap.valueAt(i))) { |
| 565 | // doesn't have the requested resource type |
| 566 | continue; |
| 567 | } |
| 568 | int tempPid = mMap.keyAt(i); |
| 569 | int tempPriority; |
| 570 | if (!mProcessInfo->getPriority(tempPid, &tempPriority)) { |
| 571 | ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid); |
| 572 | // TODO: remove this pid from mMap? |
| 573 | continue; |
| 574 | } |
| 575 | if (pid == -1 || tempPriority > priority) { |
| 576 | // initial the value |
| 577 | pid = tempPid; |
| 578 | priority = tempPriority; |
| 579 | } |
| 580 | } |
| 581 | if (pid != -1) { |
| 582 | *lowestPriorityPid = pid; |
| 583 | *lowestPriority = priority; |
| 584 | } |
| 585 | return (pid != -1); |
| 586 | } |
| 587 | |
| 588 | bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) { |
| 589 | int callingPidPriority; |
| 590 | if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) { |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | int priority; |
| 595 | if (!mProcessInfo->getPriority(pid, &priority)) { |
| 596 | return false; |
| 597 | } |
| 598 | |
| 599 | return (callingPidPriority < priority); |
| 600 | } |
| 601 | |
| 602 | bool ResourceManagerService::getBiggestClient_l( |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 603 | int pid, MediaResource::Type type, sp<IResourceManagerClient> *client) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 604 | ssize_t index = mMap.indexOfKey(pid); |
| 605 | if (index < 0) { |
| 606 | ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid); |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | sp<IResourceManagerClient> clientTemp; |
| 611 | uint64_t largestValue = 0; |
| 612 | const ResourceInfos &infos = mMap.valueAt(index); |
| 613 | for (size_t i = 0; i < infos.size(); ++i) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 614 | const ResourceList &resources = infos[i].resources; |
| 615 | for (auto it = resources.begin(); it != resources.end(); it++) { |
| 616 | const MediaResource &resource = it->second; |
| 617 | if (resource.mType == type) { |
| 618 | if (resource.mValue > largestValue) { |
| 619 | largestValue = resource.mValue; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 620 | clientTemp = infos[i].client; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if (clientTemp == NULL) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 627 | ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 628 | return false; |
| 629 | } |
| 630 | |
| 631 | *client = clientTemp; |
| 632 | return true; |
| 633 | } |
| 634 | |
| 635 | } // namespace android |