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