Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1 | /* |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 2 | * Copyright 2015 The Android Open Source Project |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "ResourceManagerService_test" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | #include "ResourceManagerService.h" |
| 24 | #include <media/IResourceManagerService.h> |
| 25 | #include <media/MediaResource.h> |
| 26 | #include <media/MediaResourcePolicy.h> |
| 27 | #include <media/stagefright/foundation/ADebug.h> |
| 28 | #include <media/stagefright/ProcessInfoInterface.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
Chih-Hung Hsieh | 51873d8 | 2016-08-09 14:18:51 -0700 | [diff] [blame] | 32 | static int64_t getId(const sp<IResourceManagerClient>& client) { |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 33 | return (int64_t) client.get(); |
| 34 | } |
| 35 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 36 | struct TestProcessInfo : public ProcessInfoInterface { |
| 37 | TestProcessInfo() {} |
| 38 | virtual ~TestProcessInfo() {} |
| 39 | |
| 40 | virtual bool getPriority(int pid, int *priority) { |
| 41 | // For testing, use pid as priority. |
| 42 | // Lower the value higher the priority. |
| 43 | *priority = pid; |
| 44 | return true; |
| 45 | } |
| 46 | |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 47 | virtual bool isValidPid(int /* pid */) { |
| 48 | return true; |
| 49 | } |
| 50 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 51 | private: |
| 52 | DISALLOW_EVIL_CONSTRUCTORS(TestProcessInfo); |
| 53 | }; |
| 54 | |
| 55 | struct TestClient : public BnResourceManagerClient { |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 56 | TestClient(int pid, sp<ResourceManagerService> service) |
| 57 | : mReclaimed(false), mPid(pid), mService(service) {} |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 58 | |
| 59 | virtual bool reclaimResource() { |
| 60 | sp<IResourceManagerClient> client(this); |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 61 | mService->removeResource(mPid, (int64_t) client.get()); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 62 | mReclaimed = true; |
| 63 | return true; |
| 64 | } |
| 65 | |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 66 | virtual String8 getName() { |
| 67 | return String8("test_client"); |
| 68 | } |
| 69 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 70 | bool reclaimed() const { |
| 71 | return mReclaimed; |
| 72 | } |
| 73 | |
| 74 | void reset() { |
| 75 | mReclaimed = false; |
| 76 | } |
| 77 | |
| 78 | protected: |
| 79 | virtual ~TestClient() {} |
| 80 | |
| 81 | private: |
| 82 | bool mReclaimed; |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 83 | int mPid; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 84 | sp<ResourceManagerService> mService; |
| 85 | DISALLOW_EVIL_CONSTRUCTORS(TestClient); |
| 86 | }; |
| 87 | |
| 88 | static const int kTestPid1 = 30; |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 89 | static const int kTestUid1 = 1010; |
| 90 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 91 | static const int kTestPid2 = 20; |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 92 | static const int kTestUid2 = 1011; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 93 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 94 | static const int kLowPriorityPid = 40; |
| 95 | static const int kMidPriorityPid = 25; |
| 96 | static const int kHighPriorityPid = 10; |
| 97 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 98 | class ResourceManagerServiceTest : public ::testing::Test { |
| 99 | public: |
| 100 | ResourceManagerServiceTest() |
| 101 | : mService(new ResourceManagerService(new TestProcessInfo)), |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 102 | mTestClient1(new TestClient(kTestPid1, mService)), |
| 103 | mTestClient2(new TestClient(kTestPid2, mService)), |
| 104 | mTestClient3(new TestClient(kTestPid2, mService)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | protected: |
| 108 | static bool isEqualResources(const Vector<MediaResource> &resources1, |
| 109 | const Vector<MediaResource> &resources2) { |
| 110 | if (resources1.size() != resources2.size()) { |
| 111 | return false; |
| 112 | } |
| 113 | for (size_t i = 0; i < resources1.size(); ++i) { |
| 114 | if (resources1[i] != resources2[i]) { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 121 | static void expectEqResourceInfo(const ResourceInfo &info, |
| 122 | int uid, |
| 123 | sp<IResourceManagerClient> client, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 124 | const Vector<MediaResource> &resources) { |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 125 | EXPECT_EQ(uid, info.uid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 126 | EXPECT_EQ(client, info.client); |
| 127 | EXPECT_TRUE(isEqualResources(resources, info.resources)); |
| 128 | } |
| 129 | |
| 130 | void verifyClients(bool c1, bool c2, bool c3) { |
| 131 | TestClient *client1 = static_cast<TestClient*>(mTestClient1.get()); |
| 132 | TestClient *client2 = static_cast<TestClient*>(mTestClient2.get()); |
| 133 | TestClient *client3 = static_cast<TestClient*>(mTestClient3.get()); |
| 134 | |
| 135 | EXPECT_EQ(c1, client1->reclaimed()); |
| 136 | EXPECT_EQ(c2, client2->reclaimed()); |
| 137 | EXPECT_EQ(c3, client3->reclaimed()); |
| 138 | |
| 139 | client1->reset(); |
| 140 | client2->reset(); |
| 141 | client3->reset(); |
| 142 | } |
| 143 | |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 144 | // test set up |
| 145 | // --------------------------------------------------------------------------------- |
| 146 | // pid priority client type number |
| 147 | // --------------------------------------------------------------------------------- |
| 148 | // kTestPid1(30) 30 mTestClient1 secure codec 1 |
| 149 | // graphic memory 200 |
| 150 | // graphic memory 200 |
| 151 | // --------------------------------------------------------------------------------- |
| 152 | // kTestPid2(20) 20 mTestClient2 non-secure codec 1 |
| 153 | // graphic memory 300 |
| 154 | // ------------------------------------------- |
| 155 | // mTestClient3 secure codec 1 |
| 156 | // graphic memory 100 |
| 157 | // --------------------------------------------------------------------------------- |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 158 | void addResource() { |
| 159 | // kTestPid1 mTestClient1 |
| 160 | Vector<MediaResource> resources1; |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 161 | resources1.push_back(MediaResource(MediaResource::kSecureCodec, 1)); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 162 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 163 | resources1.push_back(MediaResource(MediaResource::kGraphicMemory, 200)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 164 | Vector<MediaResource> resources11; |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 165 | resources11.push_back(MediaResource(MediaResource::kGraphicMemory, 200)); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 166 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 167 | |
| 168 | // kTestPid2 mTestClient2 |
| 169 | Vector<MediaResource> resources2; |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 170 | resources2.push_back(MediaResource(MediaResource::kNonSecureCodec, 1)); |
| 171 | resources2.push_back(MediaResource(MediaResource::kGraphicMemory, 300)); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 172 | mService->addResource(kTestPid2, kTestUid2, getId(mTestClient2), mTestClient2, resources2); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 173 | |
| 174 | // kTestPid2 mTestClient3 |
| 175 | Vector<MediaResource> resources3; |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 176 | mService->addResource(kTestPid2, kTestUid2, getId(mTestClient3), mTestClient3, resources3); |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 177 | resources3.push_back(MediaResource(MediaResource::kSecureCodec, 1)); |
| 178 | resources3.push_back(MediaResource(MediaResource::kGraphicMemory, 100)); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 179 | mService->addResource(kTestPid2, kTestUid2, getId(mTestClient3), mTestClient3, resources3); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 180 | |
| 181 | const PidResourceInfosMap &map = mService->mMap; |
| 182 | EXPECT_EQ(2u, map.size()); |
| 183 | ssize_t index1 = map.indexOfKey(kTestPid1); |
| 184 | ASSERT_GE(index1, 0); |
| 185 | const ResourceInfos &infos1 = map[index1]; |
| 186 | EXPECT_EQ(1u, infos1.size()); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 187 | expectEqResourceInfo(infos1[0], kTestUid1, mTestClient1, resources1); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 188 | |
| 189 | ssize_t index2 = map.indexOfKey(kTestPid2); |
| 190 | ASSERT_GE(index2, 0); |
| 191 | const ResourceInfos &infos2 = map[index2]; |
| 192 | EXPECT_EQ(2u, infos2.size()); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame^] | 193 | expectEqResourceInfo(infos2[0], kTestUid2, mTestClient2, resources2); |
| 194 | expectEqResourceInfo(infos2[1], kTestUid2, mTestClient3, resources3); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void testConfig() { |
| 198 | EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs); |
| 199 | EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec); |
| 200 | |
| 201 | Vector<MediaResourcePolicy> policies1; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 202 | policies1.push_back( |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 203 | MediaResourcePolicy( |
| 204 | String8(kPolicySupportsMultipleSecureCodecs), |
| 205 | String8("true"))); |
| 206 | policies1.push_back( |
| 207 | MediaResourcePolicy( |
| 208 | String8(kPolicySupportsSecureWithNonSecureCodec), |
| 209 | String8("false"))); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 210 | mService->config(policies1); |
| 211 | EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs); |
| 212 | EXPECT_FALSE(mService->mSupportsSecureWithNonSecureCodec); |
| 213 | |
| 214 | Vector<MediaResourcePolicy> policies2; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 215 | policies2.push_back( |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 216 | MediaResourcePolicy( |
| 217 | String8(kPolicySupportsMultipleSecureCodecs), |
| 218 | String8("false"))); |
| 219 | policies2.push_back( |
| 220 | MediaResourcePolicy( |
| 221 | String8(kPolicySupportsSecureWithNonSecureCodec), |
| 222 | String8("true"))); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 223 | mService->config(policies2); |
| 224 | EXPECT_FALSE(mService->mSupportsMultipleSecureCodecs); |
| 225 | EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec); |
| 226 | } |
| 227 | |
| 228 | void testRemoveResource() { |
| 229 | addResource(); |
| 230 | |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 231 | mService->removeResource(kTestPid2, getId(mTestClient2)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 232 | |
| 233 | const PidResourceInfosMap &map = mService->mMap; |
| 234 | EXPECT_EQ(2u, map.size()); |
| 235 | const ResourceInfos &infos1 = map.valueFor(kTestPid1); |
| 236 | const ResourceInfos &infos2 = map.valueFor(kTestPid2); |
| 237 | EXPECT_EQ(1u, infos1.size()); |
| 238 | EXPECT_EQ(1u, infos2.size()); |
| 239 | // mTestClient2 has been removed. |
| 240 | EXPECT_EQ(mTestClient3, infos2[0].client); |
| 241 | } |
| 242 | |
| 243 | void testGetAllClients() { |
| 244 | addResource(); |
| 245 | |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 246 | MediaResource::Type type = MediaResource::kSecureCodec; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 247 | Vector<sp<IResourceManagerClient> > clients; |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 248 | EXPECT_FALSE(mService->getAllClients_l(kLowPriorityPid, type, &clients)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 249 | // some higher priority process (e.g. kTestPid2) owns the resource, so getAllClients_l |
| 250 | // will fail. |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 251 | EXPECT_FALSE(mService->getAllClients_l(kMidPriorityPid, type, &clients)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 252 | EXPECT_TRUE(mService->getAllClients_l(kHighPriorityPid, type, &clients)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 253 | |
| 254 | EXPECT_EQ(2u, clients.size()); |
| 255 | EXPECT_EQ(mTestClient3, clients[0]); |
| 256 | EXPECT_EQ(mTestClient1, clients[1]); |
| 257 | } |
| 258 | |
| 259 | void testReclaimResourceSecure() { |
| 260 | Vector<MediaResource> resources; |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 261 | resources.push_back(MediaResource(MediaResource::kSecureCodec, 1)); |
| 262 | resources.push_back(MediaResource(MediaResource::kGraphicMemory, 150)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 263 | |
| 264 | // ### secure codec can't coexist and secure codec can coexist with non-secure codec ### |
| 265 | { |
| 266 | addResource(); |
| 267 | mService->mSupportsMultipleSecureCodecs = false; |
| 268 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 269 | |
| 270 | // priority too low |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 271 | EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources)); |
| 272 | EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 273 | |
| 274 | // reclaim all secure codecs |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 275 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 276 | verifyClients(true /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 277 | |
| 278 | // call again should reclaim one largest graphic memory from lowest process |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 279 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 280 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 281 | |
| 282 | // nothing left |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 283 | EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // ### secure codecs can't coexist and secure codec can't coexist with non-secure codec ### |
| 287 | { |
| 288 | addResource(); |
| 289 | mService->mSupportsMultipleSecureCodecs = false; |
| 290 | mService->mSupportsSecureWithNonSecureCodec = false; |
| 291 | |
| 292 | // priority too low |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 293 | EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources)); |
| 294 | EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 295 | |
| 296 | // reclaim all secure and non-secure codecs |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 297 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 298 | verifyClients(true /* c1 */, true /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 299 | |
| 300 | // nothing left |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 301 | EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | // ### secure codecs can coexist but secure codec can't coexist with non-secure codec ### |
| 306 | { |
| 307 | addResource(); |
| 308 | mService->mSupportsMultipleSecureCodecs = true; |
| 309 | mService->mSupportsSecureWithNonSecureCodec = false; |
| 310 | |
| 311 | // priority too low |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 312 | EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources)); |
| 313 | EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 314 | |
| 315 | // reclaim all non-secure codecs |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 316 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 317 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 318 | |
| 319 | // call again should reclaim one largest graphic memory from lowest process |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 320 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 321 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 322 | |
| 323 | // call again should reclaim another largest graphic memory from lowest process |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 324 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 325 | verifyClients(false /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 326 | |
| 327 | // nothing left |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 328 | EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // ### secure codecs can coexist and secure codec can coexist with non-secure codec ### |
| 332 | { |
| 333 | addResource(); |
| 334 | mService->mSupportsMultipleSecureCodecs = true; |
| 335 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 336 | |
| 337 | // priority too low |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 338 | EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 339 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 340 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 341 | // one largest graphic memory from lowest process got reclaimed |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 342 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 343 | |
| 344 | // call again should reclaim another graphic memory from lowest process |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 345 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 346 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 347 | |
| 348 | // call again should reclaim another graphic memory from lowest process |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 349 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 350 | verifyClients(false /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 351 | |
| 352 | // nothing left |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 353 | EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 354 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 355 | |
| 356 | // ### secure codecs can coexist and secure codec can coexist with non-secure codec ### |
| 357 | { |
| 358 | addResource(); |
| 359 | mService->mSupportsMultipleSecureCodecs = true; |
| 360 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 361 | |
| 362 | Vector<MediaResource> resources; |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 363 | resources.push_back(MediaResource(MediaResource::kSecureCodec, 1)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 364 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 365 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 366 | // secure codec from lowest process got reclaimed |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 367 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 368 | |
| 369 | // call again should reclaim another secure codec from lowest process |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 370 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 371 | verifyClients(false /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 372 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 373 | // no more secure codec, non-secure codec will be reclaimed. |
| 374 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 375 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 376 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void testReclaimResourceNonSecure() { |
| 380 | Vector<MediaResource> resources; |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 381 | resources.push_back(MediaResource(MediaResource::kNonSecureCodec, 1)); |
| 382 | resources.push_back(MediaResource(MediaResource::kGraphicMemory, 150)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 383 | |
| 384 | // ### secure codec can't coexist with non-secure codec ### |
| 385 | { |
| 386 | addResource(); |
| 387 | mService->mSupportsSecureWithNonSecureCodec = false; |
| 388 | |
| 389 | // priority too low |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 390 | EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources)); |
| 391 | EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 392 | |
| 393 | // reclaim all secure codecs |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 394 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 395 | verifyClients(true /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 396 | |
| 397 | // call again should reclaim one graphic memory from lowest process |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 398 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 399 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 400 | |
| 401 | // nothing left |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 402 | EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | |
| 406 | // ### secure codec can coexist with non-secure codec ### |
| 407 | { |
| 408 | addResource(); |
| 409 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 410 | |
| 411 | // priority too low |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 412 | EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 413 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 414 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 415 | // one largest graphic memory from lowest process got reclaimed |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 416 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 417 | |
| 418 | // call again should reclaim another graphic memory from lowest process |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 419 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 420 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 421 | |
| 422 | // call again should reclaim another graphic memory from lowest process |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 423 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 424 | verifyClients(false /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 425 | |
| 426 | // nothing left |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 427 | EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 428 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 429 | |
| 430 | // ### secure codec can coexist with non-secure codec ### |
| 431 | { |
| 432 | addResource(); |
| 433 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 434 | |
| 435 | Vector<MediaResource> resources; |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 436 | resources.push_back(MediaResource(MediaResource::kNonSecureCodec, 1)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 437 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 438 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 439 | // one non secure codec from lowest process got reclaimed |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 440 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 441 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 442 | // no more non-secure codec, secure codec from lowest priority process will be reclaimed |
| 443 | EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources)); |
| 444 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 445 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 446 | // clean up client 3 which still left |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 447 | mService->removeResource(kTestPid2, getId(mTestClient3)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 448 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | void testGetLowestPriorityBiggestClient() { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 452 | MediaResource::Type type = MediaResource::kGraphicMemory; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 453 | sp<IResourceManagerClient> client; |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 454 | EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 455 | |
| 456 | addResource(); |
| 457 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 458 | EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kLowPriorityPid, type, &client)); |
| 459 | EXPECT_TRUE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 460 | |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 461 | // kTestPid1 is the lowest priority process with MediaResource::kGraphicMemory. |
| 462 | // mTestClient1 has the largest MediaResource::kGraphicMemory within kTestPid1. |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 463 | EXPECT_EQ(mTestClient1, client); |
| 464 | } |
| 465 | |
| 466 | void testGetLowestPriorityPid() { |
| 467 | int pid; |
| 468 | int priority; |
| 469 | TestProcessInfo processInfo; |
| 470 | |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 471 | MediaResource::Type type = MediaResource::kGraphicMemory; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 472 | EXPECT_FALSE(mService->getLowestPriorityPid_l(type, &pid, &priority)); |
| 473 | |
| 474 | addResource(); |
| 475 | |
| 476 | EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority)); |
| 477 | EXPECT_EQ(kTestPid1, pid); |
| 478 | int priority1; |
| 479 | processInfo.getPriority(kTestPid1, &priority1); |
| 480 | EXPECT_EQ(priority1, priority); |
| 481 | |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 482 | type = MediaResource::kNonSecureCodec; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 483 | EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority)); |
| 484 | EXPECT_EQ(kTestPid2, pid); |
| 485 | int priority2; |
| 486 | processInfo.getPriority(kTestPid2, &priority2); |
| 487 | EXPECT_EQ(priority2, priority); |
| 488 | } |
| 489 | |
| 490 | void testGetBiggestClient() { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 491 | MediaResource::Type type = MediaResource::kGraphicMemory; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 492 | sp<IResourceManagerClient> client; |
| 493 | EXPECT_FALSE(mService->getBiggestClient_l(kTestPid2, type, &client)); |
| 494 | |
| 495 | addResource(); |
| 496 | |
| 497 | EXPECT_TRUE(mService->getBiggestClient_l(kTestPid2, type, &client)); |
| 498 | EXPECT_EQ(mTestClient2, client); |
| 499 | } |
| 500 | |
| 501 | void testIsCallingPriorityHigher() { |
| 502 | EXPECT_FALSE(mService->isCallingPriorityHigher_l(101, 100)); |
| 503 | EXPECT_FALSE(mService->isCallingPriorityHigher_l(100, 100)); |
| 504 | EXPECT_TRUE(mService->isCallingPriorityHigher_l(99, 100)); |
| 505 | } |
| 506 | |
| 507 | sp<ResourceManagerService> mService; |
| 508 | sp<IResourceManagerClient> mTestClient1; |
| 509 | sp<IResourceManagerClient> mTestClient2; |
| 510 | sp<IResourceManagerClient> mTestClient3; |
| 511 | }; |
| 512 | |
| 513 | TEST_F(ResourceManagerServiceTest, config) { |
| 514 | testConfig(); |
| 515 | } |
| 516 | |
| 517 | TEST_F(ResourceManagerServiceTest, addResource) { |
| 518 | addResource(); |
| 519 | } |
| 520 | |
| 521 | TEST_F(ResourceManagerServiceTest, removeResource) { |
| 522 | testRemoveResource(); |
| 523 | } |
| 524 | |
| 525 | TEST_F(ResourceManagerServiceTest, reclaimResource) { |
| 526 | testReclaimResourceSecure(); |
| 527 | testReclaimResourceNonSecure(); |
| 528 | } |
| 529 | |
| 530 | TEST_F(ResourceManagerServiceTest, getAllClients_l) { |
| 531 | testGetAllClients(); |
| 532 | } |
| 533 | |
| 534 | TEST_F(ResourceManagerServiceTest, getLowestPriorityBiggestClient_l) { |
| 535 | testGetLowestPriorityBiggestClient(); |
| 536 | } |
| 537 | |
| 538 | TEST_F(ResourceManagerServiceTest, getLowestPriorityPid_l) { |
| 539 | testGetLowestPriorityPid(); |
| 540 | } |
| 541 | |
| 542 | TEST_F(ResourceManagerServiceTest, getBiggestClient_l) { |
| 543 | testGetBiggestClient(); |
| 544 | } |
| 545 | |
| 546 | TEST_F(ResourceManagerServiceTest, isCallingPriorityHigher_l) { |
| 547 | testIsCallingPriorityHigher(); |
| 548 | } |
| 549 | |
| 550 | } // namespace android |