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 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 22 | #include <android/binder_manager.h> |
| 23 | #include <android/binder_process.h> |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 24 | #include <binder/IMediaResourceMonitor.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 26 | #include <cutils/sched_policy.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 27 | #include <dirent.h> |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 28 | #include <media/MediaResourcePolicy.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 29 | #include <media/stagefright/ProcessInfo.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 30 | #include <mediautils/BatteryNotifier.h> |
| 31 | #include <mediautils/SchedulingPolicyService.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/time.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | #include "ResourceManagerService.h" |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 39 | #include "ServiceLog.h" |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 40 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 43 | DeathNotifier::DeathNotifier(const std::shared_ptr<ResourceManagerService> &service, |
| 44 | int pid, int64_t clientId) |
| 45 | : mService(service), mPid(pid), mClientId(clientId) {} |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 46 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 47 | //static |
| 48 | void DeathNotifier::BinderDiedCallback(void* cookie) { |
| 49 | auto thiz = static_cast<DeathNotifier*>(cookie); |
| 50 | thiz->binderDied(); |
| 51 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 52 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 53 | void DeathNotifier::binderDied() { |
| 54 | // Don't check for pid validity since we know it's already dead. |
| 55 | std::shared_ptr<ResourceManagerService> service = mService.lock(); |
| 56 | if (service == nullptr) { |
| 57 | ALOGW("ResourceManagerService is dead as well."); |
| 58 | return; |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 59 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 60 | |
| 61 | service->overridePid(mPid, -1); |
Henry Fang | bf0d7ca | 2020-06-29 13:11:39 -0700 | [diff] [blame] | 62 | // thiz is freed in the call below, so it must be last call referring thiz |
| 63 | service->removeResource(mPid, mClientId, false); |
| 64 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 65 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 66 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 67 | template <typename T> |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 68 | static String8 getString(const std::vector<T> &items) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 69 | String8 itemsStr; |
| 70 | for (size_t i = 0; i < items.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 71 | itemsStr.appendFormat("%s ", toString(items[i]).string()); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 72 | } |
| 73 | return itemsStr; |
| 74 | } |
| 75 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 76 | static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) { |
| 77 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 78 | if (it->second.type == type) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
Chih-Hung Hsieh | 51873d8 | 2016-08-09 14:18:51 -0700 | [diff] [blame] | 85 | static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 86 | for (size_t i = 0; i < infos.size(); ++i) { |
| 87 | if (hasResourceType(type, infos[i].resources)) { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | static ResourceInfos& getResourceInfosForEdit( |
| 95 | int pid, |
| 96 | PidResourceInfosMap& map) { |
| 97 | ssize_t index = map.indexOfKey(pid); |
| 98 | if (index < 0) { |
| 99 | // new pid |
| 100 | ResourceInfos infosForPid; |
| 101 | map.add(pid, infosForPid); |
| 102 | } |
| 103 | |
| 104 | return map.editValueFor(pid); |
| 105 | } |
| 106 | |
| 107 | static ResourceInfo& getResourceInfoForEdit( |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 108 | uid_t uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 109 | int64_t clientId, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 110 | const std::shared_ptr<IResourceManagerClient>& client, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 111 | ResourceInfos& infos) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 112 | ssize_t index = infos.indexOfKey(clientId); |
| 113 | |
| 114 | if (index < 0) { |
| 115 | ResourceInfo info; |
| 116 | info.uid = uid; |
| 117 | info.clientId = clientId; |
| 118 | info.client = client; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 119 | info.pendingRemoval = false; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 120 | |
| 121 | index = infos.add(clientId, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 122 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 123 | |
| 124 | return infos.editValueAt(index); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 127 | static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) { |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 128 | static const char* const kServiceName = "media_resource_monitor"; |
Dongwon Kang | 2642c84 | 2016-03-23 18:07:29 -0700 | [diff] [blame] | 129 | sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName)); |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 130 | if (binder != NULL) { |
| 131 | sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder); |
| 132 | for (size_t i = 0; i < resources.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 133 | if (resources[i].subType == MediaResource::SubType::kAudioCodec) { |
Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 134 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 135 | } else if (resources[i].subType == MediaResource::SubType::kVideoCodec) { |
Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 136 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC); |
| 137 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 142 | binder_status_t ResourceManagerService::dump( |
| 143 | int fd, const char** /*args*/, uint32_t /*numArgs*/) { |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 144 | String8 result; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 145 | |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 146 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 147 | result.format("Permission Denial: " |
| 148 | "can't dump ResourceManagerService from pid=%d, uid=%d\n", |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 149 | AIBinder_getCallingPid(), |
| 150 | AIBinder_getCallingUid()); |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 151 | write(fd, result.string(), result.size()); |
| 152 | return PERMISSION_DENIED; |
| 153 | } |
| 154 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 155 | PidResourceInfosMap mapCopy; |
| 156 | bool supportsMultipleSecureCodecs; |
| 157 | bool supportsSecureWithNonSecureCodec; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 158 | std::map<int, int> overridePidMapCopy; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 159 | String8 serviceLog; |
| 160 | { |
| 161 | Mutex::Autolock lock(mLock); |
| 162 | mapCopy = mMap; // Shadow copy, real copy will happen on write. |
| 163 | supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs; |
| 164 | supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec; |
| 165 | serviceLog = mServiceLog->toString(" " /* linePrefix */); |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 166 | overridePidMapCopy = mOverridePidMap; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | const size_t SIZE = 256; |
| 170 | char buffer[SIZE]; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 171 | snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this); |
| 172 | result.append(buffer); |
| 173 | result.append(" Policies:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 174 | snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 175 | result.append(buffer); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 176 | snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n", |
| 177 | supportsSecureWithNonSecureCodec); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 178 | result.append(buffer); |
| 179 | |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 180 | result.append(" Processes:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 181 | for (size_t i = 0; i < mapCopy.size(); ++i) { |
| 182 | snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i)); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 183 | result.append(buffer); |
| 184 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 185 | const ResourceInfos &infos = mapCopy.valueAt(i); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 186 | for (size_t j = 0; j < infos.size(); ++j) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 187 | result.append(" Client:\n"); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 188 | snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId); |
| 189 | result.append(buffer); |
| 190 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 191 | std::string clientName; |
| 192 | Status status = infos[j].client->getName(&clientName); |
| 193 | if (!status.isOk()) { |
| 194 | clientName = "<unknown client>"; |
| 195 | } |
| 196 | snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 197 | result.append(buffer); |
| 198 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 199 | const ResourceList &resources = infos[j].resources; |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 200 | result.append(" Resources:\n"); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 201 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 202 | snprintf(buffer, SIZE, " %s\n", toString(it->second).string()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 203 | result.append(buffer); |
| 204 | } |
| 205 | } |
| 206 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 207 | result.append(" Process Pid override:\n"); |
| 208 | for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) { |
| 209 | snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n", |
| 210 | it->first, it->second); |
| 211 | result.append(buffer); |
| 212 | } |
Ronghua Wu | 022ed72 | 2015-05-11 15:15:09 -0700 | [diff] [blame] | 213 | result.append(" Events logs (most recent at top):\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 214 | result.append(serviceLog); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 215 | |
| 216 | write(fd, result.string(), result.size()); |
| 217 | return OK; |
| 218 | } |
| 219 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 220 | struct SystemCallbackImpl : |
| 221 | public ResourceManagerService::SystemCallbackInterface { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 222 | SystemCallbackImpl() : mClientToken(new BBinder()) {} |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 223 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 224 | virtual void noteStartVideo(int uid) override { |
| 225 | BatteryNotifier::getInstance().noteStartVideo(uid); |
| 226 | } |
| 227 | virtual void noteStopVideo(int uid) override { |
| 228 | BatteryNotifier::getInstance().noteStopVideo(uid); |
| 229 | } |
| 230 | virtual void noteResetVideo() override { |
| 231 | BatteryNotifier::getInstance().noteResetVideo(); |
| 232 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 233 | virtual bool requestCpusetBoost(bool enable) override { |
| 234 | return android::requestCpusetBoost(enable, mClientToken); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | protected: |
| 238 | virtual ~SystemCallbackImpl() {} |
| 239 | |
| 240 | private: |
| 241 | DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl); |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 242 | sp<IBinder> mClientToken; |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | ResourceManagerService::ResourceManagerService() |
| 246 | : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {} |
| 247 | |
| 248 | ResourceManagerService::ResourceManagerService( |
| 249 | const sp<ProcessInfoInterface> &processInfo, |
| 250 | const sp<SystemCallbackInterface> &systemResource) |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 251 | : mProcessInfo(processInfo), |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 252 | mSystemCB(systemResource), |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 253 | mServiceLog(new ServiceLog()), |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 254 | mSupportsMultipleSecureCodecs(true), |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 255 | mSupportsSecureWithNonSecureCodec(true), |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 256 | mCpuBoostCount(0), |
| 257 | mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 258 | mSystemCB->noteResetVideo(); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 259 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 260 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 261 | //static |
| 262 | void ResourceManagerService::instantiate() { |
| 263 | std::shared_ptr<ResourceManagerService> service = |
| 264 | ::ndk::SharedRefBase::make<ResourceManagerService>(); |
| 265 | binder_status_t status = |
| 266 | AServiceManager_addService(service->asBinder().get(), getServiceName()); |
| 267 | if (status != STATUS_OK) { |
| 268 | return; |
| 269 | } |
| 270 | // TODO: mediaserver main() is already starting the thread pool, |
| 271 | // move this to mediaserver main() when other services in mediaserver |
| 272 | // are converted to ndk-platform aidl. |
| 273 | //ABinderProcess_startThreadPool(); |
| 274 | } |
| 275 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 276 | ResourceManagerService::~ResourceManagerService() {} |
| 277 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 278 | Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 279 | String8 log = String8::format("config(%s)", getString(policies).string()); |
| 280 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 281 | |
| 282 | Mutex::Autolock lock(mLock); |
| 283 | for (size_t i = 0; i < policies.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 284 | const std::string &type = policies[i].type; |
| 285 | const std::string &value = policies[i].value; |
| 286 | if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 287 | mSupportsMultipleSecureCodecs = (value == "true"); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 288 | } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 289 | mSupportsSecureWithNonSecureCodec = (value == "true"); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 292 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 295 | void ResourceManagerService::onFirstAdded( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 296 | const MediaResourceParcel& resource, const ResourceInfo& clientInfo) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 297 | // first time added |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 298 | if (resource.type == MediaResource::Type::kCpuBoost |
| 299 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 300 | // Request it on every new instance of kCpuBoost, as the media.codec |
| 301 | // could have died, if we only do it the first time subsequent instances |
| 302 | // never gets the boost. |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 303 | if (mSystemCB->requestCpusetBoost(true) != OK) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 304 | ALOGW("couldn't request cpuset boost"); |
| 305 | } |
| 306 | mCpuBoostCount++; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 307 | } else if (resource.type == MediaResource::Type::kBattery |
| 308 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 309 | mSystemCB->noteStartVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | void ResourceManagerService::onLastRemoved( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 314 | const MediaResourceParcel& resource, const ResourceInfo& clientInfo) { |
| 315 | if (resource.type == MediaResource::Type::kCpuBoost |
| 316 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 317 | && mCpuBoostCount > 0) { |
| 318 | if (--mCpuBoostCount == 0) { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 319 | mSystemCB->requestCpusetBoost(false); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 320 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 321 | } else if (resource.type == MediaResource::Type::kBattery |
| 322 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 323 | mSystemCB->noteStopVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 327 | void ResourceManagerService::mergeResources( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 328 | MediaResourceParcel& r1, const MediaResourceParcel& r2) { |
| 329 | // The resource entry on record is maintained to be in [0,INT64_MAX]. |
| 330 | // Clamp if merging in the new resource value causes it to go out of bound. |
| 331 | // Note that the new resource value could be negative, eg.DrmSession, the |
| 332 | // value goes lower when the session is used more often. During reclaim |
| 333 | // the session with the highest value (lowest usage) would be closed. |
| 334 | if (r2.value < INT64_MAX - r1.value) { |
| 335 | r1.value += r2.value; |
| 336 | if (r1.value < 0) { |
| 337 | r1.value = 0; |
| 338 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 339 | } else { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 340 | r1.value = INT64_MAX; |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 344 | Status ResourceManagerService::addResource( |
| 345 | int32_t pid, |
| 346 | int32_t uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 347 | int64_t clientId, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 348 | const std::shared_ptr<IResourceManagerClient>& client, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 349 | const std::vector<MediaResourceParcel>& resources) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 350 | String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)", |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 351 | pid, (long long) clientId, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 352 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 353 | |
| 354 | Mutex::Autolock lock(mLock); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 355 | if (!mProcessInfo->isValidPid(pid)) { |
| 356 | ALOGE("Rejected addResource call with invalid pid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 357 | return Status::fromServiceSpecificError(BAD_VALUE); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 358 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 359 | ResourceInfos& infos = getResourceInfosForEdit(pid, mMap); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 360 | ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos); |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 361 | |
| 362 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 363 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 364 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 365 | |
| 366 | if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) { |
| 367 | ALOGW("Ignoring request to remove negative value of non-drm resource"); |
| 368 | continue; |
| 369 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 370 | if (info.resources.find(resType) == info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 371 | if (res.value <= 0) { |
| 372 | // We can't init a new entry with negative value, although it's allowed |
| 373 | // to merge in negative values after the initial add. |
| 374 | ALOGW("Ignoring request to add new resource entry with value <= 0"); |
| 375 | continue; |
| 376 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 377 | onFirstAdded(res, info); |
| 378 | info.resources[resType] = res; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 379 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 380 | mergeResources(info.resources[resType], res); |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 383 | if (info.deathNotifier == nullptr && client != nullptr) { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 384 | info.deathNotifier = new DeathNotifier(ref<ResourceManagerService>(), pid, clientId); |
| 385 | AIBinder_linkToDeath(client->asBinder().get(), |
| 386 | mDeathRecipient.get(), info.deathNotifier.get()); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 387 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 388 | notifyResourceGranted(pid, resources); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 389 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 392 | Status ResourceManagerService::removeResource( |
| 393 | int32_t pid, int64_t clientId, |
| 394 | const std::vector<MediaResourceParcel>& resources) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 395 | String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)", |
| 396 | pid, (long long) clientId, getString(resources).string()); |
| 397 | mServiceLog->add(log); |
| 398 | |
| 399 | Mutex::Autolock lock(mLock); |
| 400 | if (!mProcessInfo->isValidPid(pid)) { |
| 401 | ALOGE("Rejected removeResource call with invalid pid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 402 | return Status::fromServiceSpecificError(BAD_VALUE); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 403 | } |
| 404 | ssize_t index = mMap.indexOfKey(pid); |
| 405 | if (index < 0) { |
| 406 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 407 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 408 | } |
| 409 | ResourceInfos &infos = mMap.editValueAt(index); |
| 410 | |
| 411 | index = infos.indexOfKey(clientId); |
| 412 | if (index < 0) { |
| 413 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 414 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | ResourceInfo &info = infos.editValueAt(index); |
| 418 | |
| 419 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 420 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 421 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 422 | |
| 423 | if (res.value < 0) { |
| 424 | ALOGW("Ignoring request to remove negative value of resource"); |
| 425 | continue; |
| 426 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 427 | // ignore if we don't have it |
| 428 | if (info.resources.find(resType) != info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 429 | MediaResourceParcel &resource = info.resources[resType]; |
| 430 | if (resource.value > res.value) { |
| 431 | resource.value -= res.value; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 432 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 433 | onLastRemoved(res, info); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 434 | info.resources.erase(resType); |
| 435 | } |
| 436 | } |
| 437 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 438 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 441 | Status ResourceManagerService::removeClient(int32_t pid, int64_t clientId) { |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 442 | removeResource(pid, clientId, true); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 443 | return Status::ok(); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 444 | } |
| 445 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 446 | Status ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) { |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 447 | String8 log = String8::format( |
| 448 | "removeResource(pid %d, clientId %lld)", |
| 449 | pid, (long long) clientId); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 450 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 451 | |
| 452 | Mutex::Autolock lock(mLock); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 453 | if (checkValid && !mProcessInfo->isValidPid(pid)) { |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 454 | ALOGE("Rejected removeResource call with invalid pid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 455 | return Status::fromServiceSpecificError(BAD_VALUE); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 456 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 457 | ssize_t index = mMap.indexOfKey(pid); |
| 458 | if (index < 0) { |
| 459 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 460 | return Status::ok(); |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 461 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 462 | ResourceInfos &infos = mMap.editValueAt(index); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 463 | |
| 464 | index = infos.indexOfKey(clientId); |
| 465 | if (index < 0) { |
| 466 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 467 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 468 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 469 | |
| 470 | const ResourceInfo &info = infos[index]; |
| 471 | for (auto it = info.resources.begin(); it != info.resources.end(); it++) { |
| 472 | onLastRemoved(it->second, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 473 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 474 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 475 | AIBinder_unlinkToDeath(info.client->asBinder().get(), |
| 476 | mDeathRecipient.get(), info.deathNotifier.get()); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 477 | |
| 478 | infos.removeItemsAt(index); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 479 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 482 | void ResourceManagerService::getClientForResource_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 483 | int callingPid, const MediaResourceParcel *res, |
| 484 | Vector<std::shared_ptr<IResourceManagerClient>> *clients) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 485 | if (res == NULL) { |
| 486 | return; |
| 487 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 488 | std::shared_ptr<IResourceManagerClient> client; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 489 | if (getLowestPriorityBiggestClient_l(callingPid, res->type, &client)) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 490 | clients->push_back(client); |
| 491 | } |
| 492 | } |
| 493 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 494 | Status ResourceManagerService::reclaimResource( |
| 495 | int32_t callingPid, |
| 496 | const std::vector<MediaResourceParcel>& resources, |
| 497 | bool* _aidl_return) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 498 | String8 log = String8::format("reclaimResource(callingPid %d, resources %s)", |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 499 | callingPid, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 500 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 501 | *_aidl_return = false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 502 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 503 | Vector<std::shared_ptr<IResourceManagerClient>> clients; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 504 | { |
| 505 | Mutex::Autolock lock(mLock); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 506 | if (!mProcessInfo->isValidPid(callingPid)) { |
| 507 | ALOGE("Rejected reclaimResource call with invalid callingPid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 508 | return Status::fromServiceSpecificError(BAD_VALUE); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 509 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 510 | const MediaResourceParcel *secureCodec = NULL; |
| 511 | const MediaResourceParcel *nonSecureCodec = NULL; |
| 512 | const MediaResourceParcel *graphicMemory = NULL; |
| 513 | const MediaResourceParcel *drmSession = NULL; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 514 | for (size_t i = 0; i < resources.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 515 | MediaResource::Type type = resources[i].type; |
| 516 | if (resources[i].type == MediaResource::Type::kSecureCodec) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 517 | secureCodec = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 518 | } else if (type == MediaResource::Type::kNonSecureCodec) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 519 | nonSecureCodec = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 520 | } else if (type == MediaResource::Type::kGraphicMemory) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 521 | graphicMemory = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 522 | } else if (type == MediaResource::Type::kDrmSession) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 523 | drmSession = &resources[i]; |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | // first pass to handle secure/non-secure codec conflict |
| 528 | if (secureCodec != NULL) { |
| 529 | if (!mSupportsMultipleSecureCodecs) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 530 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) { |
| 531 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | if (!mSupportsSecureWithNonSecureCodec) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 535 | if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, &clients)) { |
| 536 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
| 540 | if (nonSecureCodec != NULL) { |
| 541 | if (!mSupportsSecureWithNonSecureCodec) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 542 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) { |
| 543 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 547 | if (drmSession != NULL) { |
| 548 | getClientForResource_l(callingPid, drmSession, &clients); |
| 549 | if (clients.size() == 0) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 550 | return Status::ok(); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 551 | } |
| 552 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 553 | |
| 554 | if (clients.size() == 0) { |
| 555 | // 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] | 556 | getClientForResource_l(callingPid, graphicMemory, &clients); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 557 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 558 | |
| 559 | if (clients.size() == 0) { |
| 560 | // 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] | 561 | getClientForResource_l(callingPid, secureCodec, &clients); |
| 562 | getClientForResource_l(callingPid, nonSecureCodec, &clients); |
| 563 | } |
| 564 | |
| 565 | if (clients.size() == 0) { |
| 566 | // if we are here, run the fourth pass to free one codec with the different type. |
| 567 | if (secureCodec != NULL) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 568 | MediaResource temp(MediaResource::Type::kNonSecureCodec, 1); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 569 | getClientForResource_l(callingPid, &temp, &clients); |
| 570 | } |
| 571 | if (nonSecureCodec != NULL) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 572 | MediaResource temp(MediaResource::Type::kSecureCodec, 1); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 573 | getClientForResource_l(callingPid, &temp, &clients); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 578 | *_aidl_return = reclaimInternal(clients); |
| 579 | return Status::ok(); |
| 580 | } |
| 581 | |
| 582 | bool ResourceManagerService::reclaimInternal( |
| 583 | const Vector<std::shared_ptr<IResourceManagerClient>> &clients) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 584 | if (clients.size() == 0) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 585 | return false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 588 | std::shared_ptr<IResourceManagerClient> failedClient; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 589 | for (size_t i = 0; i < clients.size(); ++i) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 590 | String8 log = String8::format("reclaimResource from client %p", clients[i].get()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 591 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 592 | bool success; |
| 593 | Status status = clients[i]->reclaimResource(&success); |
| 594 | if (!status.isOk() || !success) { |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 595 | failedClient = clients[i]; |
| 596 | break; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 599 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 600 | if (failedClient == NULL) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 601 | return true; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 604 | { |
| 605 | Mutex::Autolock lock(mLock); |
| 606 | bool found = false; |
| 607 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 608 | ResourceInfos &infos = mMap.editValueAt(i); |
| 609 | for (size_t j = 0; j < infos.size();) { |
| 610 | if (infos[j].client == failedClient) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 611 | j = infos.removeItemsAt(j); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 612 | found = true; |
| 613 | } else { |
| 614 | ++j; |
| 615 | } |
| 616 | } |
| 617 | if (found) { |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | if (!found) { |
| 622 | ALOGV("didn't find failed client"); |
| 623 | } |
| 624 | } |
| 625 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 626 | return false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 629 | Status ResourceManagerService::overridePid( |
| 630 | int originalPid, |
| 631 | int newPid) { |
| 632 | String8 log = String8::format("overridePid(originalPid %d, newPid %d)", |
| 633 | originalPid, newPid); |
| 634 | mServiceLog->add(log); |
| 635 | |
| 636 | // allow if this is called from the same process or the process has |
| 637 | // permission. |
| 638 | if ((AIBinder_getCallingPid() != getpid()) && |
| 639 | (checkCallingPermission(String16( |
| 640 | "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) { |
| 641 | ALOGE( |
| 642 | "Permission Denial: can't access overridePid method from pid=%d, " |
| 643 | "self pid=%d\n", |
| 644 | AIBinder_getCallingPid(), getpid()); |
| 645 | return Status::fromServiceSpecificError(PERMISSION_DENIED); |
| 646 | } |
| 647 | |
| 648 | { |
| 649 | Mutex::Autolock lock(mLock); |
| 650 | mOverridePidMap.erase(originalPid); |
| 651 | if (newPid != -1) { |
| 652 | mOverridePidMap.emplace(originalPid, newPid); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | return Status::ok(); |
| 657 | } |
| 658 | |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 659 | Status ResourceManagerService::markClientForPendingRemoval(int32_t pid, int64_t clientId) { |
| 660 | String8 log = String8::format( |
| 661 | "markClientForPendingRemoval(pid %d, clientId %lld)", |
| 662 | pid, (long long) clientId); |
| 663 | mServiceLog->add(log); |
| 664 | |
| 665 | Mutex::Autolock lock(mLock); |
| 666 | if (!mProcessInfo->isValidPid(pid)) { |
| 667 | ALOGE("Rejected markClientForPendingRemoval call with invalid pid."); |
| 668 | return Status::fromServiceSpecificError(BAD_VALUE); |
| 669 | } |
| 670 | ssize_t index = mMap.indexOfKey(pid); |
| 671 | if (index < 0) { |
| 672 | ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld", |
| 673 | pid, (long long)clientId); |
| 674 | return Status::ok(); |
| 675 | } |
| 676 | ResourceInfos &infos = mMap.editValueAt(index); |
| 677 | |
| 678 | index = infos.indexOfKey(clientId); |
| 679 | if (index < 0) { |
| 680 | ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId); |
| 681 | return Status::ok(); |
| 682 | } |
| 683 | |
| 684 | ResourceInfo &info = infos.editValueAt(index); |
| 685 | info.pendingRemoval = true; |
| 686 | return Status::ok(); |
| 687 | } |
| 688 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 689 | Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) { |
| 690 | String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid); |
| 691 | mServiceLog->add(log); |
| 692 | |
| 693 | Vector<std::shared_ptr<IResourceManagerClient>> clients; |
| 694 | { |
| 695 | Mutex::Autolock lock(mLock); |
| 696 | if (!mProcessInfo->isValidPid(pid)) { |
| 697 | ALOGE("Rejected reclaimResourcesFromClientsPendingRemoval call with invalid pid."); |
| 698 | return Status::fromServiceSpecificError(BAD_VALUE); |
| 699 | } |
| 700 | |
| 701 | for (MediaResource::Type type : {MediaResource::Type::kSecureCodec, |
| 702 | MediaResource::Type::kNonSecureCodec, |
| 703 | MediaResource::Type::kGraphicMemory, |
| 704 | MediaResource::Type::kDrmSession}) { |
| 705 | std::shared_ptr<IResourceManagerClient> client; |
| 706 | if (getBiggestClient_l(pid, type, &client, true /* pendingRemovalOnly */)) { |
| 707 | clients.add(client); |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | if (!clients.empty()) { |
| 714 | reclaimInternal(clients); |
| 715 | } |
| 716 | return Status::ok(); |
| 717 | } |
| 718 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 719 | bool ResourceManagerService::getPriority_l(int pid, int* priority) { |
| 720 | int newPid = pid; |
| 721 | |
| 722 | if (mOverridePidMap.find(pid) != mOverridePidMap.end()) { |
| 723 | newPid = mOverridePidMap[pid]; |
| 724 | ALOGD("getPriority_l: use override pid %d instead original pid %d", |
| 725 | newPid, pid); |
| 726 | } |
| 727 | |
| 728 | return mProcessInfo->getPriority(newPid, priority); |
| 729 | } |
| 730 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 731 | bool ResourceManagerService::getAllClients_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 732 | int callingPid, MediaResource::Type type, |
| 733 | Vector<std::shared_ptr<IResourceManagerClient>> *clients) { |
| 734 | Vector<std::shared_ptr<IResourceManagerClient>> temp; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 735 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 736 | ResourceInfos &infos = mMap.editValueAt(i); |
| 737 | for (size_t j = 0; j < infos.size(); ++j) { |
| 738 | if (hasResourceType(type, infos[j].resources)) { |
| 739 | if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) { |
| 740 | // some higher/equal priority process owns the resource, |
| 741 | // this request can't be fulfilled. |
| 742 | ALOGE("getAllClients_l: can't reclaim resource %s from pid %d", |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 743 | asString(type), mMap.keyAt(i)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 744 | return false; |
| 745 | } |
| 746 | temp.push_back(infos[j].client); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | if (temp.size() == 0) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 751 | ALOGV("getAllClients_l: didn't find any resource %s", asString(type)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 752 | return true; |
| 753 | } |
| 754 | clients->appendVector(temp); |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | bool ResourceManagerService::getLowestPriorityBiggestClient_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 759 | int callingPid, MediaResource::Type type, |
| 760 | std::shared_ptr<IResourceManagerClient> *client) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 761 | int lowestPriorityPid; |
| 762 | int lowestPriority; |
| 763 | int callingPriority; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 764 | |
| 765 | // Before looking into other processes, check if we have clients marked for |
| 766 | // pending removal in the same process. |
| 767 | if (getBiggestClient_l(callingPid, type, client, true /* pendingRemovalOnly */)) { |
| 768 | return true; |
| 769 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 770 | if (!getPriority_l(callingPid, &callingPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 771 | ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d", |
| 772 | callingPid); |
| 773 | return false; |
| 774 | } |
| 775 | if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) { |
| 776 | return false; |
| 777 | } |
| 778 | if (lowestPriority <= callingPriority) { |
| 779 | ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d", |
| 780 | lowestPriority, callingPriority); |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | if (!getBiggestClient_l(lowestPriorityPid, type, client)) { |
| 785 | return false; |
| 786 | } |
| 787 | return true; |
| 788 | } |
| 789 | |
| 790 | bool ResourceManagerService::getLowestPriorityPid_l( |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 791 | MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 792 | int pid = -1; |
| 793 | int priority = -1; |
| 794 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 795 | if (mMap.valueAt(i).size() == 0) { |
| 796 | // no client on this process. |
| 797 | continue; |
| 798 | } |
| 799 | if (!hasResourceType(type, mMap.valueAt(i))) { |
| 800 | // doesn't have the requested resource type |
| 801 | continue; |
| 802 | } |
| 803 | int tempPid = mMap.keyAt(i); |
| 804 | int tempPriority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 805 | if (!getPriority_l(tempPid, &tempPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 806 | ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid); |
| 807 | // TODO: remove this pid from mMap? |
| 808 | continue; |
| 809 | } |
| 810 | if (pid == -1 || tempPriority > priority) { |
| 811 | // initial the value |
| 812 | pid = tempPid; |
| 813 | priority = tempPriority; |
| 814 | } |
| 815 | } |
| 816 | if (pid != -1) { |
| 817 | *lowestPriorityPid = pid; |
| 818 | *lowestPriority = priority; |
| 819 | } |
| 820 | return (pid != -1); |
| 821 | } |
| 822 | |
| 823 | bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) { |
| 824 | int callingPidPriority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 825 | if (!getPriority_l(callingPid, &callingPidPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 826 | return false; |
| 827 | } |
| 828 | |
| 829 | int priority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 830 | if (!getPriority_l(pid, &priority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 831 | return false; |
| 832 | } |
| 833 | |
| 834 | return (callingPidPriority < priority); |
| 835 | } |
| 836 | |
| 837 | bool ResourceManagerService::getBiggestClient_l( |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 838 | int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client, |
| 839 | bool pendingRemovalOnly) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 840 | ssize_t index = mMap.indexOfKey(pid); |
| 841 | if (index < 0) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 842 | ALOGE_IF(!pendingRemovalOnly, |
| 843 | "getBiggestClient_l: can't find resource info for pid %d", pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 844 | return false; |
| 845 | } |
| 846 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 847 | std::shared_ptr<IResourceManagerClient> clientTemp; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 848 | uint64_t largestValue = 0; |
| 849 | const ResourceInfos &infos = mMap.valueAt(index); |
| 850 | for (size_t i = 0; i < infos.size(); ++i) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 851 | const ResourceList &resources = infos[i].resources; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 852 | if (pendingRemovalOnly && !infos[i].pendingRemoval) { |
| 853 | continue; |
| 854 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 855 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 856 | const MediaResourceParcel &resource = it->second; |
| 857 | if (resource.type == type) { |
| 858 | if (resource.value > largestValue) { |
| 859 | largestValue = resource.value; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 860 | clientTemp = infos[i].client; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | if (clientTemp == NULL) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 867 | ALOGE_IF(!pendingRemovalOnly, |
| 868 | "getBiggestClient_l: can't find resource type %s for pid %d", |
| 869 | asString(type), pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 870 | return false; |
| 871 | } |
| 872 | |
| 873 | *client = clientTemp; |
| 874 | return true; |
| 875 | } |
| 876 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 877 | } // namespace android |