| 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 |  | 
|  | 22 | #include <binder/IServiceManager.h> | 
|  | 23 | #include <dirent.h> | 
|  | 24 | #include <media/stagefright/ProcessInfo.h> | 
|  | 25 | #include <string.h> | 
|  | 26 | #include <sys/types.h> | 
|  | 27 | #include <sys/stat.h> | 
|  | 28 | #include <sys/time.h> | 
|  | 29 | #include <unistd.h> | 
|  | 30 |  | 
|  | 31 | #include "ResourceManagerService.h" | 
|  | 32 |  | 
|  | 33 | namespace android { | 
|  | 34 |  | 
|  | 35 | template <typename T> | 
|  | 36 | static String8 getString(const Vector<T> &items) { | 
|  | 37 | String8 itemsStr; | 
|  | 38 | for (size_t i = 0; i < items.size(); ++i) { | 
|  | 39 | itemsStr.appendFormat("%s ", items[i].toString().string()); | 
|  | 40 | } | 
|  | 41 | return itemsStr; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | static bool hasResourceType(String8 type, Vector<MediaResource> resources) { | 
|  | 45 | for (size_t i = 0; i < resources.size(); ++i) { | 
|  | 46 | if (resources[i].mType == type) { | 
|  | 47 | return true; | 
|  | 48 | } | 
|  | 49 | } | 
|  | 50 | return false; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | static bool hasResourceType(String8 type, ResourceInfos infos) { | 
|  | 54 | for (size_t i = 0; i < infos.size(); ++i) { | 
|  | 55 | if (hasResourceType(type, infos[i].resources)) { | 
|  | 56 | return true; | 
|  | 57 | } | 
|  | 58 | } | 
|  | 59 | return false; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | static ResourceInfos& getResourceInfosForEdit( | 
|  | 63 | int pid, | 
|  | 64 | PidResourceInfosMap& map) { | 
|  | 65 | ssize_t index = map.indexOfKey(pid); | 
|  | 66 | if (index < 0) { | 
|  | 67 | // new pid | 
|  | 68 | ResourceInfos infosForPid; | 
|  | 69 | map.add(pid, infosForPid); | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | return map.editValueFor(pid); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | static ResourceInfo& getResourceInfoForEdit( | 
|  | 76 | int64_t clientId, | 
|  | 77 | const sp<IResourceManagerClient> client, | 
|  | 78 | ResourceInfos& infos) { | 
|  | 79 | for (size_t i = 0; i < infos.size(); ++i) { | 
|  | 80 | if (infos[i].clientId == clientId) { | 
|  | 81 | return infos.editItemAt(i); | 
|  | 82 | } | 
|  | 83 | } | 
|  | 84 | ResourceInfo info; | 
|  | 85 | info.clientId = clientId; | 
|  | 86 | info.client = client; | 
|  | 87 | infos.push_back(info); | 
|  | 88 | return infos.editItemAt(infos.size() - 1); | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | ResourceManagerService::ResourceManagerService() | 
|  | 92 | : mProcessInfo(new ProcessInfo()), | 
|  | 93 | mSupportsMultipleSecureCodecs(true), | 
|  | 94 | mSupportsSecureWithNonSecureCodec(true) {} | 
|  | 95 |  | 
|  | 96 | ResourceManagerService::ResourceManagerService(sp<ProcessInfoInterface> processInfo) | 
|  | 97 | : mProcessInfo(processInfo), | 
|  | 98 | mSupportsMultipleSecureCodecs(true), | 
|  | 99 | mSupportsSecureWithNonSecureCodec(true) {} | 
|  | 100 |  | 
|  | 101 | ResourceManagerService::~ResourceManagerService() {} | 
|  | 102 |  | 
|  | 103 | void ResourceManagerService::config(const Vector<MediaResourcePolicy> &policies) { | 
|  | 104 | ALOGV("config(%s)", getString(policies).string()); | 
|  | 105 |  | 
|  | 106 | Mutex::Autolock lock(mLock); | 
|  | 107 | for (size_t i = 0; i < policies.size(); ++i) { | 
|  | 108 | String8 type = policies[i].mType; | 
|  | 109 | uint64_t value = policies[i].mValue; | 
|  | 110 | if (type == kPolicySupportsMultipleSecureCodecs) { | 
|  | 111 | mSupportsMultipleSecureCodecs = (value != 0); | 
|  | 112 | } else if (type == kPolicySupportsSecureWithNonSecureCodec) { | 
|  | 113 | mSupportsSecureWithNonSecureCodec = (value != 0); | 
|  | 114 | } | 
|  | 115 | } | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | void ResourceManagerService::addResource( | 
|  | 119 | int pid, | 
|  | 120 | int64_t clientId, | 
|  | 121 | const sp<IResourceManagerClient> client, | 
|  | 122 | const Vector<MediaResource> &resources) { | 
|  | 123 | ALOGV("addResource(pid %d, clientId %lld, resources %s)", | 
|  | 124 | pid, (long long) clientId, getString(resources).string()); | 
|  | 125 |  | 
|  | 126 | Mutex::Autolock lock(mLock); | 
|  | 127 | ResourceInfos& infos = getResourceInfosForEdit(pid, mMap); | 
|  | 128 | ResourceInfo& info = getResourceInfoForEdit(clientId, client, infos); | 
|  | 129 | info.resources.appendVector(resources); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | void ResourceManagerService::removeResource(int64_t clientId) { | 
|  | 133 | ALOGV("removeResource(%lld)", (long long) clientId); | 
|  | 134 |  | 
|  | 135 | Mutex::Autolock lock(mLock); | 
|  | 136 | bool found = false; | 
|  | 137 | for (size_t i = 0; i < mMap.size(); ++i) { | 
|  | 138 | ResourceInfos &infos = mMap.editValueAt(i); | 
|  | 139 | for (size_t j = 0; j < infos.size();) { | 
|  | 140 | if (infos[j].clientId == clientId) { | 
|  | 141 | j = infos.removeAt(j); | 
|  | 142 | found = true; | 
|  | 143 | } else { | 
|  | 144 | ++j; | 
|  | 145 | } | 
|  | 146 | } | 
|  | 147 | if (found) { | 
|  | 148 | break; | 
|  | 149 | } | 
|  | 150 | } | 
|  | 151 | if (!found) { | 
|  | 152 | ALOGV("didn't find client"); | 
|  | 153 | } | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | bool ResourceManagerService::reclaimResource( | 
|  | 157 | int callingPid, const Vector<MediaResource> &resources) { | 
|  | 158 | ALOGV("reclaimResource(callingPid %d, resources %s)", | 
|  | 159 | callingPid, getString(resources).string()); | 
|  | 160 |  | 
|  | 161 | Vector<sp<IResourceManagerClient>> clients; | 
|  | 162 | { | 
|  | 163 | Mutex::Autolock lock(mLock); | 
|  | 164 | // first pass to handle secure/non-secure codec conflict | 
|  | 165 | for (size_t i = 0; i < resources.size(); ++i) { | 
|  | 166 | String8 type = resources[i].mType; | 
|  | 167 | if (type == kResourceSecureCodec) { | 
|  | 168 | if (!mSupportsMultipleSecureCodecs) { | 
|  | 169 | if (!getAllClients_l(callingPid, String8(kResourceSecureCodec), &clients)) { | 
|  | 170 | return false; | 
|  | 171 | } | 
|  | 172 | } | 
|  | 173 | if (!mSupportsSecureWithNonSecureCodec) { | 
|  | 174 | if (!getAllClients_l(callingPid, String8(kResourceNonSecureCodec), &clients)) { | 
|  | 175 | return false; | 
|  | 176 | } | 
|  | 177 | } | 
|  | 178 | } else if (type == kResourceNonSecureCodec) { | 
|  | 179 | if (!mSupportsSecureWithNonSecureCodec) { | 
|  | 180 | if (!getAllClients_l(callingPid, String8(kResourceSecureCodec), &clients)) { | 
|  | 181 | return false; | 
|  | 182 | } | 
|  | 183 | } | 
|  | 184 | } | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | if (clients.size() == 0) { | 
|  | 188 | // if no secure/non-secure codec conflict, run second pass to handle other resources. | 
|  | 189 | for (size_t i = 0; i < resources.size(); ++i) { | 
|  | 190 | String8 type = resources[i].mType; | 
|  | 191 | if (type == kResourceGraphicMemory) { | 
|  | 192 | sp<IResourceManagerClient> client; | 
|  | 193 | if (!getLowestPriorityBiggestClient_l(callingPid, type, &client)) { | 
|  | 194 | return false; | 
|  | 195 | } | 
|  | 196 | clients.push_back(client); | 
|  | 197 | } | 
|  | 198 | } | 
|  | 199 | } | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | if (clients.size() == 0) { | 
|  | 203 | return false; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | for (size_t i = 0; i < clients.size(); ++i) { | 
|  | 207 | ALOGV("reclaimResource from client %p", clients[i].get()); | 
|  | 208 | if (!clients[i]->reclaimResource()) { | 
|  | 209 | return false; | 
|  | 210 | } | 
|  | 211 | } | 
|  | 212 | return true; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | bool ResourceManagerService::getAllClients_l( | 
|  | 216 | int callingPid, const String8 &type, Vector<sp<IResourceManagerClient>> *clients) { | 
|  | 217 | Vector<sp<IResourceManagerClient>> temp; | 
|  | 218 | for (size_t i = 0; i < mMap.size(); ++i) { | 
|  | 219 | ResourceInfos &infos = mMap.editValueAt(i); | 
|  | 220 | for (size_t j = 0; j < infos.size(); ++j) { | 
|  | 221 | if (hasResourceType(type, infos[j].resources)) { | 
|  | 222 | if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) { | 
|  | 223 | // some higher/equal priority process owns the resource, | 
|  | 224 | // this request can't be fulfilled. | 
|  | 225 | ALOGE("getAllClients_l: can't reclaim resource %s from pid %d", | 
|  | 226 | type.string(), mMap.keyAt(i)); | 
|  | 227 | return false; | 
|  | 228 | } | 
|  | 229 | temp.push_back(infos[j].client); | 
|  | 230 | } | 
|  | 231 | } | 
|  | 232 | } | 
|  | 233 | if (temp.size() == 0) { | 
|  | 234 | ALOGV("getAllClients_l: didn't find any resource %s", type.string()); | 
|  | 235 | return true; | 
|  | 236 | } | 
|  | 237 | clients->appendVector(temp); | 
|  | 238 | return true; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | bool ResourceManagerService::getLowestPriorityBiggestClient_l( | 
|  | 242 | int callingPid, const String8 &type, sp<IResourceManagerClient> *client) { | 
|  | 243 | int lowestPriorityPid; | 
|  | 244 | int lowestPriority; | 
|  | 245 | int callingPriority; | 
|  | 246 | if (!mProcessInfo->getPriority(callingPid, &callingPriority)) { | 
|  | 247 | ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d", | 
|  | 248 | callingPid); | 
|  | 249 | return false; | 
|  | 250 | } | 
|  | 251 | if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) { | 
|  | 252 | return false; | 
|  | 253 | } | 
|  | 254 | if (lowestPriority <= callingPriority) { | 
|  | 255 | ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d", | 
|  | 256 | lowestPriority, callingPriority); | 
|  | 257 | return false; | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | if (!getBiggestClient_l(lowestPriorityPid, type, client)) { | 
|  | 261 | return false; | 
|  | 262 | } | 
|  | 263 | return true; | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | bool ResourceManagerService::getLowestPriorityPid_l( | 
|  | 267 | const String8 &type, int *lowestPriorityPid, int *lowestPriority) { | 
|  | 268 | int pid = -1; | 
|  | 269 | int priority = -1; | 
|  | 270 | for (size_t i = 0; i < mMap.size(); ++i) { | 
|  | 271 | if (mMap.valueAt(i).size() == 0) { | 
|  | 272 | // no client on this process. | 
|  | 273 | continue; | 
|  | 274 | } | 
|  | 275 | if (!hasResourceType(type, mMap.valueAt(i))) { | 
|  | 276 | // doesn't have the requested resource type | 
|  | 277 | continue; | 
|  | 278 | } | 
|  | 279 | int tempPid = mMap.keyAt(i); | 
|  | 280 | int tempPriority; | 
|  | 281 | if (!mProcessInfo->getPriority(tempPid, &tempPriority)) { | 
|  | 282 | ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid); | 
|  | 283 | // TODO: remove this pid from mMap? | 
|  | 284 | continue; | 
|  | 285 | } | 
|  | 286 | if (pid == -1 || tempPriority > priority) { | 
|  | 287 | // initial the value | 
|  | 288 | pid = tempPid; | 
|  | 289 | priority = tempPriority; | 
|  | 290 | } | 
|  | 291 | } | 
|  | 292 | if (pid != -1) { | 
|  | 293 | *lowestPriorityPid = pid; | 
|  | 294 | *lowestPriority = priority; | 
|  | 295 | } | 
|  | 296 | return (pid != -1); | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) { | 
|  | 300 | int callingPidPriority; | 
|  | 301 | if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) { | 
|  | 302 | return false; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | int priority; | 
|  | 306 | if (!mProcessInfo->getPriority(pid, &priority)) { | 
|  | 307 | return false; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | return (callingPidPriority < priority); | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | bool ResourceManagerService::getBiggestClient_l( | 
|  | 314 | int pid, const String8 &type, sp<IResourceManagerClient> *client) { | 
|  | 315 | ssize_t index = mMap.indexOfKey(pid); | 
|  | 316 | if (index < 0) { | 
|  | 317 | ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid); | 
|  | 318 | return false; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | sp<IResourceManagerClient> clientTemp; | 
|  | 322 | uint64_t largestValue = 0; | 
|  | 323 | const ResourceInfos &infos = mMap.valueAt(index); | 
|  | 324 | for (size_t i = 0; i < infos.size(); ++i) { | 
|  | 325 | Vector<MediaResource> resources = infos[i].resources; | 
|  | 326 | for (size_t j = 0; j < resources.size(); ++j) { | 
|  | 327 | if (resources[j].mType == type) { | 
|  | 328 | if (resources[j].mValue > largestValue) { | 
|  | 329 | largestValue = resources[j].mValue; | 
|  | 330 | clientTemp = infos[i].client; | 
|  | 331 | } | 
|  | 332 | } | 
|  | 333 | } | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | if (clientTemp == NULL) { | 
|  | 337 | ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", type.string(), pid); | 
|  | 338 | return false; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | *client = clientTemp; | 
|  | 342 | return true; | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | } // namespace android |