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