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" |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 24 | #include <aidl/android/media/BnResourceManagerClient.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 25 | #include <media/MediaResource.h> |
| 26 | #include <media/MediaResourcePolicy.h> |
| 27 | #include <media/stagefright/foundation/ADebug.h> |
| 28 | #include <media/stagefright/ProcessInfoInterface.h> |
| 29 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 30 | namespace android { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 31 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 32 | using Status = ::ndk::ScopedAStatus; |
| 33 | using ::aidl::android::media::BnResourceManagerClient; |
| 34 | using ::aidl::android::media::IResourceManagerService; |
| 35 | using ::aidl::android::media::IResourceManagerClient; |
| 36 | |
| 37 | static int64_t getId(const std::shared_ptr<IResourceManagerClient>& client) { |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 38 | return (int64_t) client.get(); |
| 39 | } |
| 40 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 41 | struct TestProcessInfo : public ProcessInfoInterface { |
| 42 | TestProcessInfo() {} |
| 43 | virtual ~TestProcessInfo() {} |
| 44 | |
| 45 | virtual bool getPriority(int pid, int *priority) { |
| 46 | // For testing, use pid as priority. |
| 47 | // Lower the value higher the priority. |
| 48 | *priority = pid; |
| 49 | return true; |
| 50 | } |
| 51 | |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 52 | virtual bool isValidPid(int /* pid */) { |
| 53 | return true; |
| 54 | } |
| 55 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 56 | private: |
| 57 | DISALLOW_EVIL_CONSTRUCTORS(TestProcessInfo); |
| 58 | }; |
| 59 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 60 | struct TestSystemCallback : |
| 61 | public ResourceManagerService::SystemCallbackInterface { |
| 62 | TestSystemCallback() : |
| 63 | mLastEvent({EventType::INVALID, 0}), mEventCount(0) {} |
| 64 | |
| 65 | enum EventType { |
| 66 | INVALID = -1, |
| 67 | VIDEO_ON = 0, |
| 68 | VIDEO_OFF = 1, |
| 69 | VIDEO_RESET = 2, |
| 70 | CPUSET_ENABLE = 3, |
| 71 | CPUSET_DISABLE = 4, |
| 72 | }; |
| 73 | |
| 74 | struct EventEntry { |
| 75 | EventType type; |
| 76 | int arg; |
| 77 | }; |
| 78 | |
| 79 | virtual void noteStartVideo(int uid) override { |
| 80 | mLastEvent = {EventType::VIDEO_ON, uid}; |
| 81 | mEventCount++; |
| 82 | } |
| 83 | |
| 84 | virtual void noteStopVideo(int uid) override { |
| 85 | mLastEvent = {EventType::VIDEO_OFF, uid}; |
| 86 | mEventCount++; |
| 87 | } |
| 88 | |
| 89 | virtual void noteResetVideo() override { |
| 90 | mLastEvent = {EventType::VIDEO_RESET, 0}; |
| 91 | mEventCount++; |
| 92 | } |
| 93 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 94 | virtual bool requestCpusetBoost(bool enable) override { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 95 | mLastEvent = {enable ? EventType::CPUSET_ENABLE : EventType::CPUSET_DISABLE, 0}; |
| 96 | mEventCount++; |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | size_t eventCount() { return mEventCount; } |
| 101 | EventType lastEventType() { return mLastEvent.type; } |
| 102 | EventEntry lastEvent() { return mLastEvent; } |
| 103 | |
| 104 | protected: |
| 105 | virtual ~TestSystemCallback() {} |
| 106 | |
| 107 | private: |
| 108 | EventEntry mLastEvent; |
| 109 | size_t mEventCount; |
| 110 | |
| 111 | DISALLOW_EVIL_CONSTRUCTORS(TestSystemCallback); |
| 112 | }; |
| 113 | |
| 114 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 115 | struct TestClient : public BnResourceManagerClient { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 116 | TestClient(int pid, const std::shared_ptr<ResourceManagerService> &service) |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 117 | : mReclaimed(false), mPid(pid), mService(service) {} |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 118 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 119 | Status reclaimResource(bool* _aidl_return) override { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 120 | mService->removeClient(mPid, getId(ref<TestClient>())); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 121 | mReclaimed = true; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 122 | *_aidl_return = true; |
| 123 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 126 | Status getName(::std::string* _aidl_return) override { |
| 127 | *_aidl_return = "test_client"; |
| 128 | return Status::ok(); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 131 | bool reclaimed() const { |
| 132 | return mReclaimed; |
| 133 | } |
| 134 | |
| 135 | void reset() { |
| 136 | mReclaimed = false; |
| 137 | } |
| 138 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 139 | virtual ~TestClient() {} |
| 140 | |
| 141 | private: |
| 142 | bool mReclaimed; |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 143 | int mPid; |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 144 | std::shared_ptr<ResourceManagerService> mService; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 145 | DISALLOW_EVIL_CONSTRUCTORS(TestClient); |
| 146 | }; |
| 147 | |
| 148 | static const int kTestPid1 = 30; |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 149 | static const int kTestUid1 = 1010; |
| 150 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 151 | static const int kTestPid2 = 20; |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 152 | static const int kTestUid2 = 1011; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 153 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 154 | static const int kLowPriorityPid = 40; |
| 155 | static const int kMidPriorityPid = 25; |
| 156 | static const int kHighPriorityPid = 10; |
| 157 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 158 | using EventType = TestSystemCallback::EventType; |
| 159 | using EventEntry = TestSystemCallback::EventEntry; |
| 160 | bool operator== (const EventEntry& lhs, const EventEntry& rhs) { |
| 161 | return lhs.type == rhs.type && lhs.arg == rhs.arg; |
| 162 | } |
| 163 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 164 | #define CHECK_STATUS_TRUE(condition) \ |
| 165 | EXPECT_TRUE((condition).isOk() && (result)) |
| 166 | |
| 167 | #define CHECK_STATUS_FALSE(condition) \ |
| 168 | EXPECT_TRUE((condition).isOk() && !(result)) |
| 169 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 170 | class ResourceManagerServiceTest : public ::testing::Test { |
| 171 | public: |
| 172 | ResourceManagerServiceTest() |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 173 | : mSystemCB(new TestSystemCallback()), |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 174 | mService(::ndk::SharedRefBase::make<ResourceManagerService>( |
| 175 | new TestProcessInfo, mSystemCB)), |
| 176 | mTestClient1(::ndk::SharedRefBase::make<TestClient>(kTestPid1, mService)), |
| 177 | mTestClient2(::ndk::SharedRefBase::make<TestClient>(kTestPid2, mService)), |
| 178 | mTestClient3(::ndk::SharedRefBase::make<TestClient>(kTestPid2, mService)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | protected: |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 182 | static bool isEqualResources(const std::vector<MediaResourceParcel> &resources1, |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 183 | const ResourceList &resources2) { |
| 184 | // convert resource1 to ResourceList |
| 185 | ResourceList r1; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 186 | for (size_t i = 0; i < resources1.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 187 | const auto &res = resources1[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 188 | const auto resType = std::tuple(res.type, res.subType, res.id); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 189 | r1[resType] = res; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 190 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 191 | return r1 == resources2; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 194 | static void expectEqResourceInfo(const ResourceInfo &info, |
| 195 | int uid, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 196 | std::shared_ptr<IResourceManagerClient> client, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 197 | const std::vector<MediaResourceParcel> &resources) { |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 198 | EXPECT_EQ(uid, info.uid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 199 | EXPECT_EQ(client, info.client); |
| 200 | EXPECT_TRUE(isEqualResources(resources, info.resources)); |
| 201 | } |
| 202 | |
| 203 | void verifyClients(bool c1, bool c2, bool c3) { |
| 204 | TestClient *client1 = static_cast<TestClient*>(mTestClient1.get()); |
| 205 | TestClient *client2 = static_cast<TestClient*>(mTestClient2.get()); |
| 206 | TestClient *client3 = static_cast<TestClient*>(mTestClient3.get()); |
| 207 | |
| 208 | EXPECT_EQ(c1, client1->reclaimed()); |
| 209 | EXPECT_EQ(c2, client2->reclaimed()); |
| 210 | EXPECT_EQ(c3, client3->reclaimed()); |
| 211 | |
| 212 | client1->reset(); |
| 213 | client2->reset(); |
| 214 | client3->reset(); |
| 215 | } |
| 216 | |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 217 | // test set up |
| 218 | // --------------------------------------------------------------------------------- |
| 219 | // pid priority client type number |
| 220 | // --------------------------------------------------------------------------------- |
| 221 | // kTestPid1(30) 30 mTestClient1 secure codec 1 |
| 222 | // graphic memory 200 |
| 223 | // graphic memory 200 |
| 224 | // --------------------------------------------------------------------------------- |
| 225 | // kTestPid2(20) 20 mTestClient2 non-secure codec 1 |
| 226 | // graphic memory 300 |
| 227 | // ------------------------------------------- |
| 228 | // mTestClient3 secure codec 1 |
| 229 | // graphic memory 100 |
| 230 | // --------------------------------------------------------------------------------- |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 231 | void addResource() { |
| 232 | // kTestPid1 mTestClient1 |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 233 | std::vector<MediaResourceParcel> resources1; |
| 234 | resources1.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1)); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 235 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 236 | resources1.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 200)); |
| 237 | std::vector<MediaResourceParcel> resources11; |
| 238 | resources11.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 200)); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 239 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 240 | |
| 241 | // kTestPid2 mTestClient2 |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 242 | std::vector<MediaResourceParcel> resources2; |
| 243 | resources2.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1)); |
| 244 | resources2.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 300)); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 245 | mService->addResource(kTestPid2, kTestUid2, getId(mTestClient2), mTestClient2, resources2); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 246 | |
| 247 | // kTestPid2 mTestClient3 |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 248 | std::vector<MediaResourceParcel> resources3; |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 249 | mService->addResource(kTestPid2, kTestUid2, getId(mTestClient3), mTestClient3, resources3); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 250 | resources3.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1)); |
| 251 | resources3.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 100)); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 252 | mService->addResource(kTestPid2, kTestUid2, getId(mTestClient3), mTestClient3, resources3); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 253 | |
| 254 | const PidResourceInfosMap &map = mService->mMap; |
| 255 | EXPECT_EQ(2u, map.size()); |
| 256 | ssize_t index1 = map.indexOfKey(kTestPid1); |
| 257 | ASSERT_GE(index1, 0); |
| 258 | const ResourceInfos &infos1 = map[index1]; |
| 259 | EXPECT_EQ(1u, infos1.size()); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 260 | expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, resources1); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 261 | |
| 262 | ssize_t index2 = map.indexOfKey(kTestPid2); |
| 263 | ASSERT_GE(index2, 0); |
| 264 | const ResourceInfos &infos2 = map[index2]; |
| 265 | EXPECT_EQ(2u, infos2.size()); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 266 | expectEqResourceInfo(infos2.valueFor(getId(mTestClient2)), kTestUid2, mTestClient2, resources2); |
| 267 | expectEqResourceInfo(infos2.valueFor(getId(mTestClient3)), kTestUid2, mTestClient3, resources3); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 270 | void testCombineResourceWithNegativeValues() { |
| 271 | // kTestPid1 mTestClient1 |
| 272 | std::vector<MediaResourceParcel> resources1; |
| 273 | resources1.push_back(MediaResource(MediaResource::Type::kDrmSession, -100)); |
| 274 | resources1.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, -100)); |
| 275 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 276 | |
| 277 | // Expected result: |
| 278 | // 1) the client should have been added; |
| 279 | // 2) both resource entries should have been rejected, resource list should be empty. |
| 280 | const PidResourceInfosMap &map = mService->mMap; |
| 281 | EXPECT_EQ(1u, map.size()); |
| 282 | ssize_t index1 = map.indexOfKey(kTestPid1); |
| 283 | ASSERT_GE(index1, 0); |
| 284 | const ResourceInfos &infos1 = map[index1]; |
| 285 | EXPECT_EQ(1u, infos1.size()); |
| 286 | std::vector<MediaResourceParcel> expected; |
| 287 | expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected); |
| 288 | |
| 289 | resources1.clear(); |
| 290 | resources1.push_back(MediaResource(MediaResource::Type::kDrmSession, INT64_MAX)); |
| 291 | resources1.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, INT64_MAX)); |
| 292 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 293 | resources1.clear(); |
| 294 | resources1.push_back(MediaResource(MediaResource::Type::kDrmSession, 10)); |
| 295 | resources1.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 10)); |
| 296 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 297 | |
| 298 | // Expected result: |
| 299 | // Both values should saturate to INT64_MAX |
| 300 | expected.push_back(MediaResource(MediaResource::Type::kDrmSession, INT64_MAX)); |
| 301 | expected.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, INT64_MAX)); |
| 302 | expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected); |
| 303 | |
| 304 | resources1.clear(); |
| 305 | resources1.push_back(MediaResource(MediaResource::Type::kDrmSession, -10)); |
| 306 | resources1.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, -10)); |
| 307 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 308 | |
| 309 | // Expected result: |
| 310 | // 1) DrmSession resource should allow negative value addition, and value should drop accordingly |
| 311 | // 2) Non-drm session resource should ignore negative value addition. |
| 312 | expected.push_back(MediaResource(MediaResource::Type::kDrmSession, INT64_MAX - 10)); |
| 313 | expected.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, INT64_MAX)); |
| 314 | expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected); |
| 315 | |
| 316 | resources1.clear(); |
| 317 | resources1.push_back(MediaResource(MediaResource::Type::kDrmSession, INT64_MIN)); |
| 318 | expected.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, INT64_MIN)); |
| 319 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 320 | |
| 321 | // Expected result: |
| 322 | // 1) DrmSession resource value should drop to 0, but the entry shouldn't be removed. |
| 323 | // 2) Non-drm session resource should ignore negative value addition. |
| 324 | expected.clear(); |
| 325 | expected.push_back(MediaResource(MediaResource::Type::kDrmSession, 0)); |
| 326 | expected.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, INT64_MAX)); |
| 327 | expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected); |
| 328 | } |
| 329 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 330 | void testConfig() { |
| 331 | EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs); |
| 332 | EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec); |
| 333 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 334 | std::vector<MediaResourcePolicyParcel> policies1; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 335 | policies1.push_back( |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 336 | MediaResourcePolicy( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 337 | IResourceManagerService::kPolicySupportsMultipleSecureCodecs, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 338 | "true")); |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 339 | policies1.push_back( |
| 340 | MediaResourcePolicy( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 341 | IResourceManagerService::kPolicySupportsSecureWithNonSecureCodec, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 342 | "false")); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 343 | mService->config(policies1); |
| 344 | EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs); |
| 345 | EXPECT_FALSE(mService->mSupportsSecureWithNonSecureCodec); |
| 346 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 347 | std::vector<MediaResourcePolicyParcel> policies2; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 348 | policies2.push_back( |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 349 | MediaResourcePolicy( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 350 | IResourceManagerService::kPolicySupportsMultipleSecureCodecs, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 351 | "false")); |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 352 | policies2.push_back( |
| 353 | MediaResourcePolicy( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 354 | IResourceManagerService::kPolicySupportsSecureWithNonSecureCodec, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 355 | "true")); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 356 | mService->config(policies2); |
| 357 | EXPECT_FALSE(mService->mSupportsMultipleSecureCodecs); |
| 358 | EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec); |
| 359 | } |
| 360 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 361 | void testCombineResource() { |
| 362 | // kTestPid1 mTestClient1 |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 363 | std::vector<MediaResourceParcel> resources1; |
| 364 | resources1.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1)); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 365 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 366 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 367 | std::vector<MediaResourceParcel> resources11; |
| 368 | resources11.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 200)); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 369 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11); |
| 370 | |
| 371 | const PidResourceInfosMap &map = mService->mMap; |
| 372 | EXPECT_EQ(1u, map.size()); |
| 373 | ssize_t index1 = map.indexOfKey(kTestPid1); |
| 374 | ASSERT_GE(index1, 0); |
| 375 | const ResourceInfos &infos1 = map[index1]; |
| 376 | EXPECT_EQ(1u, infos1.size()); |
| 377 | |
| 378 | // test adding existing types to combine values |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 379 | resources1.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 100)); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 380 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 381 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 382 | std::vector<MediaResourceParcel> expected; |
| 383 | expected.push_back(MediaResource(MediaResource::Type::kSecureCodec, 2)); |
| 384 | expected.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 300)); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 385 | expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected); |
| 386 | |
| 387 | // test adding new types (including types that differs only in subType) |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 388 | resources11.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1)); |
| 389 | resources11.push_back(MediaResource(MediaResource::Type::kSecureCodec, MediaResource::SubType::kVideoCodec, 1)); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 390 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11); |
| 391 | |
| 392 | expected.clear(); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 393 | expected.push_back(MediaResource(MediaResource::Type::kSecureCodec, 2)); |
| 394 | expected.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1)); |
| 395 | expected.push_back(MediaResource(MediaResource::Type::kSecureCodec, MediaResource::SubType::kVideoCodec, 1)); |
| 396 | expected.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 500)); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 397 | expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected); |
| 398 | } |
| 399 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 400 | void testRemoveResource() { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 401 | // kTestPid1 mTestClient1 |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 402 | std::vector<MediaResourceParcel> resources1; |
| 403 | resources1.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1)); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 404 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 405 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 406 | std::vector<MediaResourceParcel> resources11; |
| 407 | resources11.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 200)); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 408 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11); |
| 409 | |
| 410 | const PidResourceInfosMap &map = mService->mMap; |
| 411 | EXPECT_EQ(1u, map.size()); |
| 412 | ssize_t index1 = map.indexOfKey(kTestPid1); |
| 413 | ASSERT_GE(index1, 0); |
| 414 | const ResourceInfos &infos1 = map[index1]; |
| 415 | EXPECT_EQ(1u, infos1.size()); |
| 416 | |
| 417 | // test partial removal |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 418 | resources11[0].value = 100; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 419 | mService->removeResource(kTestPid1, getId(mTestClient1), resources11); |
| 420 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 421 | std::vector<MediaResourceParcel> expected; |
| 422 | expected.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1)); |
| 423 | expected.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 100)); |
| 424 | expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected); |
| 425 | |
| 426 | // test removal request with negative value, should be ignored |
| 427 | resources11[0].value = -10000; |
| 428 | mService->removeResource(kTestPid1, getId(mTestClient1), resources11); |
| 429 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 430 | expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected); |
| 431 | |
| 432 | // test complete removal with overshoot value |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 433 | resources11[0].value = 1000; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 434 | mService->removeResource(kTestPid1, getId(mTestClient1), resources11); |
| 435 | |
| 436 | expected.clear(); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 437 | expected.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1)); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 438 | expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected); |
| 439 | } |
| 440 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 441 | void testOverridePid() { |
| 442 | |
| 443 | bool result; |
| 444 | std::vector<MediaResourceParcel> resources; |
| 445 | resources.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1)); |
| 446 | resources.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 150)); |
| 447 | |
| 448 | // ### secure codec can't coexist and secure codec can coexist with non-secure codec ### |
| 449 | { |
| 450 | addResource(); |
| 451 | mService->mSupportsMultipleSecureCodecs = false; |
| 452 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 453 | |
| 454 | // priority too low to reclaim resource |
| 455 | CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result)); |
| 456 | |
| 457 | // override Low Priority Pid with High Priority Pid |
| 458 | mService->overridePid(kLowPriorityPid, kHighPriorityPid); |
| 459 | CHECK_STATUS_TRUE(mService->reclaimResource(kLowPriorityPid, resources, &result)); |
| 460 | |
| 461 | // restore Low Priority Pid |
| 462 | mService->overridePid(kLowPriorityPid, -1); |
| 463 | CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result)); |
| 464 | } |
| 465 | } |
| 466 | |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 467 | void testMarkClientForPendingRemoval() { |
| 468 | bool result; |
| 469 | |
| 470 | { |
| 471 | addResource(); |
| 472 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 473 | |
| 474 | std::vector<MediaResourceParcel> resources; |
| 475 | resources.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1)); |
| 476 | |
| 477 | // Remove low priority clients |
| 478 | mService->removeClient(kTestPid1, getId(mTestClient1)); |
| 479 | |
| 480 | // no lower priority client |
| 481 | CHECK_STATUS_FALSE(mService->reclaimResource(kTestPid2, resources, &result)); |
| 482 | verifyClients(false /* c1 */, false /* c2 */, false /* c3 */); |
| 483 | |
| 484 | mService->markClientForPendingRemoval(kTestPid2, getId(mTestClient2)); |
| 485 | |
| 486 | // client marked for pending removal from the same process got reclaimed |
| 487 | CHECK_STATUS_TRUE(mService->reclaimResource(kTestPid2, resources, &result)); |
| 488 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
| 489 | |
| 490 | // clean up client 3 which still left |
| 491 | mService->removeClient(kTestPid2, getId(mTestClient3)); |
| 492 | } |
| 493 | |
| 494 | { |
| 495 | addResource(); |
| 496 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 497 | |
| 498 | std::vector<MediaResourceParcel> resources; |
| 499 | resources.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1)); |
| 500 | |
| 501 | mService->markClientForPendingRemoval(kTestPid2, getId(mTestClient2)); |
| 502 | |
| 503 | // client marked for pending removal from the same process got reclaimed |
| 504 | // first, even though there are lower priority process |
| 505 | CHECK_STATUS_TRUE(mService->reclaimResource(kTestPid2, resources, &result)); |
| 506 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
| 507 | |
| 508 | // lower priority client got reclaimed |
| 509 | CHECK_STATUS_TRUE(mService->reclaimResource(kTestPid2, resources, &result)); |
| 510 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
| 511 | |
| 512 | // clean up client 3 which still left |
| 513 | mService->removeClient(kTestPid2, getId(mTestClient3)); |
| 514 | } |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 515 | |
| 516 | { |
| 517 | addResource(); |
| 518 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 519 | |
| 520 | mService->markClientForPendingRemoval(kTestPid2, getId(mTestClient2)); |
| 521 | |
| 522 | // client marked for pending removal got reclaimed |
| 523 | EXPECT_TRUE(mService->reclaimResourcesFromClientsPendingRemoval(kTestPid2).isOk()); |
| 524 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
| 525 | |
| 526 | // No more clients marked for removal |
| 527 | EXPECT_TRUE(mService->reclaimResourcesFromClientsPendingRemoval(kTestPid2).isOk()); |
| 528 | verifyClients(false /* c1 */, false /* c2 */, false /* c3 */); |
| 529 | |
| 530 | mService->markClientForPendingRemoval(kTestPid2, getId(mTestClient3)); |
| 531 | |
| 532 | // client marked for pending removal got reclaimed |
| 533 | EXPECT_TRUE(mService->reclaimResourcesFromClientsPendingRemoval(kTestPid2).isOk()); |
| 534 | verifyClients(false /* c1 */, false /* c2 */, true /* c3 */); |
| 535 | |
| 536 | // clean up client 1 which still left |
| 537 | mService->removeClient(kTestPid1, getId(mTestClient1)); |
| 538 | } |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 541 | void testRemoveClient() { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 542 | addResource(); |
| 543 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 544 | mService->removeClient(kTestPid2, getId(mTestClient2)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 545 | |
| 546 | const PidResourceInfosMap &map = mService->mMap; |
| 547 | EXPECT_EQ(2u, map.size()); |
| 548 | const ResourceInfos &infos1 = map.valueFor(kTestPid1); |
| 549 | const ResourceInfos &infos2 = map.valueFor(kTestPid2); |
| 550 | EXPECT_EQ(1u, infos1.size()); |
| 551 | EXPECT_EQ(1u, infos2.size()); |
| 552 | // mTestClient2 has been removed. |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 553 | // (OK to use infos2[0] as there is only 1 entry) |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 554 | EXPECT_EQ(mTestClient3, infos2[0].client); |
| 555 | } |
| 556 | |
| 557 | void testGetAllClients() { |
| 558 | addResource(); |
| 559 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 560 | MediaResource::Type type = MediaResource::Type::kSecureCodec; |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 561 | Vector<std::shared_ptr<IResourceManagerClient> > clients; |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 562 | EXPECT_FALSE(mService->getAllClients_l(kLowPriorityPid, type, &clients)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 563 | // some higher priority process (e.g. kTestPid2) owns the resource, so getAllClients_l |
| 564 | // will fail. |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 565 | EXPECT_FALSE(mService->getAllClients_l(kMidPriorityPid, type, &clients)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 566 | EXPECT_TRUE(mService->getAllClients_l(kHighPriorityPid, type, &clients)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 567 | |
| 568 | EXPECT_EQ(2u, clients.size()); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 569 | // (OK to require ordering in clients[], as the pid map is sorted) |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 570 | EXPECT_EQ(mTestClient3, clients[0]); |
| 571 | EXPECT_EQ(mTestClient1, clients[1]); |
| 572 | } |
| 573 | |
| 574 | void testReclaimResourceSecure() { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 575 | bool result; |
| 576 | std::vector<MediaResourceParcel> resources; |
| 577 | resources.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1)); |
| 578 | resources.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 150)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 579 | |
| 580 | // ### secure codec can't coexist and secure codec can coexist with non-secure codec ### |
| 581 | { |
| 582 | addResource(); |
| 583 | mService->mSupportsMultipleSecureCodecs = false; |
| 584 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 585 | |
| 586 | // priority too low |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 587 | CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result)); |
| 588 | CHECK_STATUS_FALSE(mService->reclaimResource(kMidPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 589 | |
| 590 | // reclaim all secure codecs |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 591 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 592 | verifyClients(true /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 593 | |
| 594 | // call again should reclaim one largest graphic memory from lowest process |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 595 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 596 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 597 | |
| 598 | // nothing left |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 599 | CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // ### secure codecs can't coexist and secure codec can't coexist with non-secure codec ### |
| 603 | { |
| 604 | addResource(); |
| 605 | mService->mSupportsMultipleSecureCodecs = false; |
| 606 | mService->mSupportsSecureWithNonSecureCodec = false; |
| 607 | |
| 608 | // priority too low |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 609 | CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result)); |
| 610 | CHECK_STATUS_FALSE(mService->reclaimResource(kMidPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 611 | |
| 612 | // reclaim all secure and non-secure codecs |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 613 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 614 | verifyClients(true /* c1 */, true /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 615 | |
| 616 | // nothing left |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 617 | CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | |
| 621 | // ### secure codecs can coexist but secure codec can't coexist with non-secure codec ### |
| 622 | { |
| 623 | addResource(); |
| 624 | mService->mSupportsMultipleSecureCodecs = true; |
| 625 | mService->mSupportsSecureWithNonSecureCodec = false; |
| 626 | |
| 627 | // priority too low |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 628 | CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result)); |
| 629 | CHECK_STATUS_FALSE(mService->reclaimResource(kMidPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 630 | |
| 631 | // reclaim all non-secure codecs |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 632 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 633 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 634 | |
| 635 | // call again should reclaim one largest graphic memory from lowest process |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 636 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 637 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 638 | |
| 639 | // call again should reclaim another largest graphic memory from lowest process |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 640 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 641 | verifyClients(false /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 642 | |
| 643 | // nothing left |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 644 | CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | // ### secure codecs can coexist and secure codec can coexist with non-secure codec ### |
| 648 | { |
| 649 | addResource(); |
| 650 | mService->mSupportsMultipleSecureCodecs = true; |
| 651 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 652 | |
| 653 | // priority too low |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 654 | CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 655 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 656 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 657 | // one largest graphic memory from lowest process got reclaimed |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 658 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 659 | |
| 660 | // call again should reclaim another graphic memory from lowest process |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 661 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 662 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 663 | |
| 664 | // call again should reclaim another graphic memory from lowest process |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 665 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 666 | verifyClients(false /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 667 | |
| 668 | // nothing left |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 669 | CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 670 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 671 | |
| 672 | // ### secure codecs can coexist and secure codec can coexist with non-secure codec ### |
| 673 | { |
| 674 | addResource(); |
| 675 | mService->mSupportsMultipleSecureCodecs = true; |
| 676 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 677 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 678 | std::vector<MediaResourceParcel> resources; |
| 679 | resources.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 680 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 681 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 682 | // secure codec from lowest process got reclaimed |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 683 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 684 | |
| 685 | // call again should reclaim another secure codec from lowest process |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 686 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 687 | verifyClients(false /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 688 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 689 | // no more secure codec, non-secure codec will be reclaimed. |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 690 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 691 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 692 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | void testReclaimResourceNonSecure() { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 696 | bool result; |
| 697 | std::vector<MediaResourceParcel> resources; |
| 698 | resources.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1)); |
| 699 | resources.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 150)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 700 | |
| 701 | // ### secure codec can't coexist with non-secure codec ### |
| 702 | { |
| 703 | addResource(); |
| 704 | mService->mSupportsSecureWithNonSecureCodec = false; |
| 705 | |
| 706 | // priority too low |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 707 | CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result)); |
| 708 | CHECK_STATUS_FALSE(mService->reclaimResource(kMidPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 709 | |
| 710 | // reclaim all secure codecs |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 711 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 712 | verifyClients(true /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 713 | |
| 714 | // call again should reclaim one graphic memory from lowest process |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 715 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 716 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 717 | |
| 718 | // nothing left |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 719 | CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | |
| 723 | // ### secure codec can coexist with non-secure codec ### |
| 724 | { |
| 725 | addResource(); |
| 726 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 727 | |
| 728 | // priority too low |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 729 | CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 730 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 731 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 732 | // one largest graphic memory from lowest process got reclaimed |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 733 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 734 | |
| 735 | // call again should reclaim another graphic memory from lowest process |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 736 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 737 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 738 | |
| 739 | // call again should reclaim another graphic memory from lowest process |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 740 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 741 | verifyClients(false /* c1 */, false /* c2 */, true /* c3 */); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 742 | |
| 743 | // nothing left |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 744 | CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 745 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 746 | |
| 747 | // ### secure codec can coexist with non-secure codec ### |
| 748 | { |
| 749 | addResource(); |
| 750 | mService->mSupportsSecureWithNonSecureCodec = true; |
| 751 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 752 | std::vector<MediaResourceParcel> resources; |
| 753 | resources.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 754 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 755 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 756 | // one non secure codec from lowest process got reclaimed |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 757 | verifyClients(false /* c1 */, true /* c2 */, false /* c3 */); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 758 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 759 | // no more non-secure codec, secure codec from lowest priority process will be reclaimed |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 760 | CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result)); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 761 | verifyClients(true /* c1 */, false /* c2 */, false /* c3 */); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 762 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 763 | // clean up client 3 which still left |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 764 | mService->removeClient(kTestPid2, getId(mTestClient3)); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 765 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | void testGetLowestPriorityBiggestClient() { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 769 | MediaResource::Type type = MediaResource::Type::kGraphicMemory; |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 770 | std::shared_ptr<IResourceManagerClient> client; |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 771 | EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 772 | |
| 773 | addResource(); |
| 774 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 775 | EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kLowPriorityPid, type, &client)); |
| 776 | EXPECT_TRUE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 777 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 778 | // kTestPid1 is the lowest priority process with MediaResource::Type::kGraphicMemory. |
| 779 | // mTestClient1 has the largest MediaResource::Type::kGraphicMemory within kTestPid1. |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 780 | EXPECT_EQ(mTestClient1, client); |
| 781 | } |
| 782 | |
| 783 | void testGetLowestPriorityPid() { |
| 784 | int pid; |
| 785 | int priority; |
| 786 | TestProcessInfo processInfo; |
| 787 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 788 | MediaResource::Type type = MediaResource::Type::kGraphicMemory; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 789 | EXPECT_FALSE(mService->getLowestPriorityPid_l(type, &pid, &priority)); |
| 790 | |
| 791 | addResource(); |
| 792 | |
| 793 | EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority)); |
| 794 | EXPECT_EQ(kTestPid1, pid); |
| 795 | int priority1; |
| 796 | processInfo.getPriority(kTestPid1, &priority1); |
| 797 | EXPECT_EQ(priority1, priority); |
| 798 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 799 | type = MediaResource::Type::kNonSecureCodec; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 800 | EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority)); |
| 801 | EXPECT_EQ(kTestPid2, pid); |
| 802 | int priority2; |
| 803 | processInfo.getPriority(kTestPid2, &priority2); |
| 804 | EXPECT_EQ(priority2, priority); |
| 805 | } |
| 806 | |
| 807 | void testGetBiggestClient() { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 808 | MediaResource::Type type = MediaResource::Type::kGraphicMemory; |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 809 | std::shared_ptr<IResourceManagerClient> client; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 810 | EXPECT_FALSE(mService->getBiggestClient_l(kTestPid2, type, &client)); |
| 811 | |
| 812 | addResource(); |
| 813 | |
| 814 | EXPECT_TRUE(mService->getBiggestClient_l(kTestPid2, type, &client)); |
| 815 | EXPECT_EQ(mTestClient2, client); |
| 816 | } |
| 817 | |
| 818 | void testIsCallingPriorityHigher() { |
| 819 | EXPECT_FALSE(mService->isCallingPriorityHigher_l(101, 100)); |
| 820 | EXPECT_FALSE(mService->isCallingPriorityHigher_l(100, 100)); |
| 821 | EXPECT_TRUE(mService->isCallingPriorityHigher_l(99, 100)); |
| 822 | } |
| 823 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 824 | void testBatteryStats() { |
| 825 | // reset should always be called when ResourceManagerService is created (restarted) |
| 826 | EXPECT_EQ(1u, mSystemCB->eventCount()); |
| 827 | EXPECT_EQ(EventType::VIDEO_RESET, mSystemCB->lastEventType()); |
| 828 | |
| 829 | // new client request should cause VIDEO_ON |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 830 | std::vector<MediaResourceParcel> resources1; |
| 831 | resources1.push_back(MediaResource(MediaResource::Type::kBattery, MediaResource::SubType::kVideoCodec, 1)); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 832 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 833 | EXPECT_EQ(2u, mSystemCB->eventCount()); |
| 834 | EXPECT_EQ(EventEntry({EventType::VIDEO_ON, kTestUid1}), mSystemCB->lastEvent()); |
| 835 | |
| 836 | // each client should only cause 1 VIDEO_ON |
| 837 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 838 | EXPECT_EQ(2u, mSystemCB->eventCount()); |
| 839 | |
| 840 | // new client request should cause VIDEO_ON |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 841 | std::vector<MediaResourceParcel> resources2; |
| 842 | resources2.push_back(MediaResource(MediaResource::Type::kBattery, MediaResource::SubType::kVideoCodec, 2)); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 843 | mService->addResource(kTestPid2, kTestUid2, getId(mTestClient2), mTestClient2, resources2); |
| 844 | EXPECT_EQ(3u, mSystemCB->eventCount()); |
| 845 | EXPECT_EQ(EventEntry({EventType::VIDEO_ON, kTestUid2}), mSystemCB->lastEvent()); |
| 846 | |
| 847 | // partially remove mTestClient1's request, shouldn't be any VIDEO_OFF |
| 848 | mService->removeResource(kTestPid1, getId(mTestClient1), resources1); |
| 849 | EXPECT_EQ(3u, mSystemCB->eventCount()); |
| 850 | |
| 851 | // remove mTestClient1's request, should be VIDEO_OFF for kTestUid1 |
| 852 | // (use resource2 to test removing more instances than previously requested) |
| 853 | mService->removeResource(kTestPid1, getId(mTestClient1), resources2); |
| 854 | EXPECT_EQ(4u, mSystemCB->eventCount()); |
| 855 | EXPECT_EQ(EventEntry({EventType::VIDEO_OFF, kTestUid1}), mSystemCB->lastEvent()); |
| 856 | |
| 857 | // remove mTestClient2, should be VIDEO_OFF for kTestUid2 |
| 858 | mService->removeClient(kTestPid2, getId(mTestClient2)); |
| 859 | EXPECT_EQ(5u, mSystemCB->eventCount()); |
| 860 | EXPECT_EQ(EventEntry({EventType::VIDEO_OFF, kTestUid2}), mSystemCB->lastEvent()); |
| 861 | } |
| 862 | |
| 863 | void testCpusetBoost() { |
| 864 | // reset should always be called when ResourceManagerService is created (restarted) |
| 865 | EXPECT_EQ(1u, mSystemCB->eventCount()); |
| 866 | EXPECT_EQ(EventType::VIDEO_RESET, mSystemCB->lastEventType()); |
| 867 | |
| 868 | // new client request should cause CPUSET_ENABLE |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 869 | std::vector<MediaResourceParcel> resources1; |
| 870 | resources1.push_back(MediaResource(MediaResource::Type::kCpuBoost, 1)); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 871 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 872 | EXPECT_EQ(2u, mSystemCB->eventCount()); |
| 873 | EXPECT_EQ(EventType::CPUSET_ENABLE, mSystemCB->lastEventType()); |
| 874 | |
| 875 | // each client should only cause 1 CPUSET_ENABLE |
| 876 | mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1); |
| 877 | EXPECT_EQ(2u, mSystemCB->eventCount()); |
| 878 | |
| 879 | // new client request should cause CPUSET_ENABLE |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 880 | std::vector<MediaResourceParcel> resources2; |
| 881 | resources2.push_back(MediaResource(MediaResource::Type::kCpuBoost, 2)); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 882 | mService->addResource(kTestPid2, kTestUid2, getId(mTestClient2), mTestClient2, resources2); |
| 883 | EXPECT_EQ(3u, mSystemCB->eventCount()); |
| 884 | EXPECT_EQ(EventType::CPUSET_ENABLE, mSystemCB->lastEventType()); |
| 885 | |
| 886 | // remove mTestClient2 should not cause CPUSET_DISABLE, mTestClient1 still active |
| 887 | mService->removeClient(kTestPid2, getId(mTestClient2)); |
| 888 | EXPECT_EQ(3u, mSystemCB->eventCount()); |
| 889 | |
| 890 | // remove 1 cpuboost from mTestClient1, should not be CPUSET_DISABLE (still 1 left) |
| 891 | mService->removeResource(kTestPid1, getId(mTestClient1), resources1); |
| 892 | EXPECT_EQ(3u, mSystemCB->eventCount()); |
| 893 | |
| 894 | // remove 2 cpuboost from mTestClient1, should be CPUSET_DISABLE |
| 895 | // (use resource2 to test removing more than previously requested) |
| 896 | mService->removeResource(kTestPid1, getId(mTestClient1), resources2); |
| 897 | EXPECT_EQ(4u, mSystemCB->eventCount()); |
| 898 | EXPECT_EQ(EventType::CPUSET_DISABLE, mSystemCB->lastEventType()); |
| 899 | } |
| 900 | |
| 901 | sp<TestSystemCallback> mSystemCB; |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 902 | std::shared_ptr<ResourceManagerService> mService; |
| 903 | std::shared_ptr<IResourceManagerClient> mTestClient1; |
| 904 | std::shared_ptr<IResourceManagerClient> mTestClient2; |
| 905 | std::shared_ptr<IResourceManagerClient> mTestClient3; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 906 | }; |
| 907 | |
| 908 | TEST_F(ResourceManagerServiceTest, config) { |
| 909 | testConfig(); |
| 910 | } |
| 911 | |
| 912 | TEST_F(ResourceManagerServiceTest, addResource) { |
| 913 | addResource(); |
| 914 | } |
| 915 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 916 | TEST_F(ResourceManagerServiceTest, combineResource) { |
| 917 | testCombineResource(); |
| 918 | } |
| 919 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 920 | TEST_F(ResourceManagerServiceTest, combineResourceNegative) { |
| 921 | testCombineResourceWithNegativeValues(); |
| 922 | } |
| 923 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 924 | TEST_F(ResourceManagerServiceTest, removeResource) { |
| 925 | testRemoveResource(); |
| 926 | } |
| 927 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 928 | TEST_F(ResourceManagerServiceTest, removeClient) { |
| 929 | testRemoveClient(); |
| 930 | } |
| 931 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 932 | TEST_F(ResourceManagerServiceTest, reclaimResource) { |
| 933 | testReclaimResourceSecure(); |
| 934 | testReclaimResourceNonSecure(); |
| 935 | } |
| 936 | |
| 937 | TEST_F(ResourceManagerServiceTest, getAllClients_l) { |
| 938 | testGetAllClients(); |
| 939 | } |
| 940 | |
| 941 | TEST_F(ResourceManagerServiceTest, getLowestPriorityBiggestClient_l) { |
| 942 | testGetLowestPriorityBiggestClient(); |
| 943 | } |
| 944 | |
| 945 | TEST_F(ResourceManagerServiceTest, getLowestPriorityPid_l) { |
| 946 | testGetLowestPriorityPid(); |
| 947 | } |
| 948 | |
| 949 | TEST_F(ResourceManagerServiceTest, getBiggestClient_l) { |
| 950 | testGetBiggestClient(); |
| 951 | } |
| 952 | |
| 953 | TEST_F(ResourceManagerServiceTest, isCallingPriorityHigher_l) { |
| 954 | testIsCallingPriorityHigher(); |
| 955 | } |
| 956 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 957 | TEST_F(ResourceManagerServiceTest, testBatteryStats) { |
| 958 | testBatteryStats(); |
| 959 | } |
| 960 | |
| 961 | TEST_F(ResourceManagerServiceTest, testCpusetBoost) { |
| 962 | testCpusetBoost(); |
| 963 | } |
| 964 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 965 | TEST_F(ResourceManagerServiceTest, overridePid) { |
| 966 | testOverridePid(); |
| 967 | } |
| 968 | |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 969 | TEST_F(ResourceManagerServiceTest, markClientForPendingRemoval) { |
| 970 | testMarkClientForPendingRemoval(); |
| 971 | } |
| 972 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 973 | } // namespace android |