blob: 168fde99495951568560e081871ad3fa592d6d54 [file] [log] [blame]
Ronghua Wu231c3d12015-03-11 15:10:32 -07001/*
Ronghua Wu67e7f542015-03-13 10:47:08 -07002 * Copyright 2015 The Android Open Source Project
Ronghua Wu231c3d12015-03-11 15:10:32 -07003 *
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 Zhangfdd512a2019-11-22 11:03:14 -080024#include <aidl/android/media/BnResourceManagerClient.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070025#include <media/MediaResource.h>
26#include <media/MediaResourcePolicy.h>
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/ProcessInfoInterface.h>
29
Chong Zhangfdd512a2019-11-22 11:03:14 -080030namespace aidl {
Ronghua Wu231c3d12015-03-11 15:10:32 -070031namespace android {
Chong Zhang181e6952019-10-09 13:23:39 -070032namespace media {
Chong Zhangfdd512a2019-11-22 11:03:14 -080033bool operator== (const MediaResourceParcel& lhs, const MediaResourceParcel& rhs) {
34 return lhs.type == rhs.type && lhs.subType == rhs.subType &&
35 lhs.id == rhs.id && lhs.value == rhs.value;
36}}}}
Chong Zhang181e6952019-10-09 13:23:39 -070037
Chong Zhangfdd512a2019-11-22 11:03:14 -080038namespace android {
Ronghua Wu231c3d12015-03-11 15:10:32 -070039
Chong Zhangfdd512a2019-11-22 11:03:14 -080040using Status = ::ndk::ScopedAStatus;
41using ::aidl::android::media::BnResourceManagerClient;
42using ::aidl::android::media::IResourceManagerService;
43using ::aidl::android::media::IResourceManagerClient;
44
45static int64_t getId(const std::shared_ptr<IResourceManagerClient>& client) {
Ronghua Wu37c89242015-07-15 12:23:48 -070046 return (int64_t) client.get();
47}
48
Ronghua Wu231c3d12015-03-11 15:10:32 -070049struct TestProcessInfo : public ProcessInfoInterface {
50 TestProcessInfo() {}
51 virtual ~TestProcessInfo() {}
52
53 virtual bool getPriority(int pid, int *priority) {
54 // For testing, use pid as priority.
55 // Lower the value higher the priority.
56 *priority = pid;
57 return true;
58 }
59
Ronghua Wud11c43a2016-01-27 16:26:12 -080060 virtual bool isValidPid(int /* pid */) {
61 return true;
62 }
63
Ronghua Wu231c3d12015-03-11 15:10:32 -070064private:
65 DISALLOW_EVIL_CONSTRUCTORS(TestProcessInfo);
66};
67
Chong Zhangdd726802019-08-21 17:24:13 -070068struct TestSystemCallback :
69 public ResourceManagerService::SystemCallbackInterface {
70 TestSystemCallback() :
71 mLastEvent({EventType::INVALID, 0}), mEventCount(0) {}
72
73 enum EventType {
74 INVALID = -1,
75 VIDEO_ON = 0,
76 VIDEO_OFF = 1,
77 VIDEO_RESET = 2,
78 CPUSET_ENABLE = 3,
79 CPUSET_DISABLE = 4,
80 };
81
82 struct EventEntry {
83 EventType type;
84 int arg;
85 };
86
87 virtual void noteStartVideo(int uid) override {
88 mLastEvent = {EventType::VIDEO_ON, uid};
89 mEventCount++;
90 }
91
92 virtual void noteStopVideo(int uid) override {
93 mLastEvent = {EventType::VIDEO_OFF, uid};
94 mEventCount++;
95 }
96
97 virtual void noteResetVideo() override {
98 mLastEvent = {EventType::VIDEO_RESET, 0};
99 mEventCount++;
100 }
101
Chong Zhangfdd512a2019-11-22 11:03:14 -0800102 virtual bool requestCpusetBoost(bool enable) override {
Chong Zhangdd726802019-08-21 17:24:13 -0700103 mLastEvent = {enable ? EventType::CPUSET_ENABLE : EventType::CPUSET_DISABLE, 0};
104 mEventCount++;
105 return true;
106 }
107
108 size_t eventCount() { return mEventCount; }
109 EventType lastEventType() { return mLastEvent.type; }
110 EventEntry lastEvent() { return mLastEvent; }
111
112protected:
113 virtual ~TestSystemCallback() {}
114
115private:
116 EventEntry mLastEvent;
117 size_t mEventCount;
118
119 DISALLOW_EVIL_CONSTRUCTORS(TestSystemCallback);
120};
121
122
Ronghua Wu231c3d12015-03-11 15:10:32 -0700123struct TestClient : public BnResourceManagerClient {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800124 TestClient(int pid, const std::shared_ptr<ResourceManagerService> &service)
Ronghua Wu37c89242015-07-15 12:23:48 -0700125 : mReclaimed(false), mPid(pid), mService(service) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700126
Chong Zhang181e6952019-10-09 13:23:39 -0700127 Status reclaimResource(bool* _aidl_return) override {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800128 mService->removeClient(mPid, getId(ref<TestClient>()));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700129 mReclaimed = true;
Chong Zhang181e6952019-10-09 13:23:39 -0700130 *_aidl_return = true;
131 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700132 }
133
Chong Zhang181e6952019-10-09 13:23:39 -0700134 Status getName(::std::string* _aidl_return) override {
135 *_aidl_return = "test_client";
136 return Status::ok();
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700137 }
138
Ronghua Wu231c3d12015-03-11 15:10:32 -0700139 bool reclaimed() const {
140 return mReclaimed;
141 }
142
143 void reset() {
144 mReclaimed = false;
145 }
146
Ronghua Wu231c3d12015-03-11 15:10:32 -0700147 virtual ~TestClient() {}
148
149private:
150 bool mReclaimed;
Ronghua Wu37c89242015-07-15 12:23:48 -0700151 int mPid;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800152 std::shared_ptr<ResourceManagerService> mService;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700153 DISALLOW_EVIL_CONSTRUCTORS(TestClient);
154};
155
156static const int kTestPid1 = 30;
Chong Zhangee33d642019-08-08 14:26:43 -0700157static const int kTestUid1 = 1010;
158
Ronghua Wu231c3d12015-03-11 15:10:32 -0700159static const int kTestPid2 = 20;
Chong Zhangee33d642019-08-08 14:26:43 -0700160static const int kTestUid2 = 1011;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700161
Ronghua Wu05d89f12015-07-07 16:47:42 -0700162static const int kLowPriorityPid = 40;
163static const int kMidPriorityPid = 25;
164static const int kHighPriorityPid = 10;
165
Chong Zhangdd726802019-08-21 17:24:13 -0700166using EventType = TestSystemCallback::EventType;
167using EventEntry = TestSystemCallback::EventEntry;
168bool operator== (const EventEntry& lhs, const EventEntry& rhs) {
169 return lhs.type == rhs.type && lhs.arg == rhs.arg;
170}
171
Chong Zhang181e6952019-10-09 13:23:39 -0700172#define CHECK_STATUS_TRUE(condition) \
173 EXPECT_TRUE((condition).isOk() && (result))
174
175#define CHECK_STATUS_FALSE(condition) \
176 EXPECT_TRUE((condition).isOk() && !(result))
177
Ronghua Wu231c3d12015-03-11 15:10:32 -0700178class ResourceManagerServiceTest : public ::testing::Test {
179public:
180 ResourceManagerServiceTest()
Chong Zhangdd726802019-08-21 17:24:13 -0700181 : mSystemCB(new TestSystemCallback()),
Chong Zhangfdd512a2019-11-22 11:03:14 -0800182 mService(::ndk::SharedRefBase::make<ResourceManagerService>(
183 new TestProcessInfo, mSystemCB)),
184 mTestClient1(::ndk::SharedRefBase::make<TestClient>(kTestPid1, mService)),
185 mTestClient2(::ndk::SharedRefBase::make<TestClient>(kTestPid2, mService)),
186 mTestClient3(::ndk::SharedRefBase::make<TestClient>(kTestPid2, mService)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700187 }
188
189protected:
Chong Zhang181e6952019-10-09 13:23:39 -0700190 static bool isEqualResources(const std::vector<MediaResourceParcel> &resources1,
Chong Zhangfb092d32019-08-12 09:45:44 -0700191 const ResourceList &resources2) {
192 // convert resource1 to ResourceList
193 ResourceList r1;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700194 for (size_t i = 0; i < resources1.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700195 const auto &res = resources1[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700196 const auto resType = std::tuple(res.type, res.subType, res.id);
Robert Shihc3af31b2019-09-20 21:45:01 -0700197 r1[resType] = res;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700198 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700199 return r1 == resources2;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700200 }
201
Chong Zhangee33d642019-08-08 14:26:43 -0700202 static void expectEqResourceInfo(const ResourceInfo &info,
203 int uid,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800204 std::shared_ptr<IResourceManagerClient> client,
Chong Zhang181e6952019-10-09 13:23:39 -0700205 const std::vector<MediaResourceParcel> &resources) {
Chong Zhangee33d642019-08-08 14:26:43 -0700206 EXPECT_EQ(uid, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700207 EXPECT_EQ(client, info.client);
208 EXPECT_TRUE(isEqualResources(resources, info.resources));
209 }
210
211 void verifyClients(bool c1, bool c2, bool c3) {
212 TestClient *client1 = static_cast<TestClient*>(mTestClient1.get());
213 TestClient *client2 = static_cast<TestClient*>(mTestClient2.get());
214 TestClient *client3 = static_cast<TestClient*>(mTestClient3.get());
215
216 EXPECT_EQ(c1, client1->reclaimed());
217 EXPECT_EQ(c2, client2->reclaimed());
218 EXPECT_EQ(c3, client3->reclaimed());
219
220 client1->reset();
221 client2->reset();
222 client3->reset();
223 }
224
Ronghua Wu67e7f542015-03-13 10:47:08 -0700225 // test set up
226 // ---------------------------------------------------------------------------------
227 // pid priority client type number
228 // ---------------------------------------------------------------------------------
229 // kTestPid1(30) 30 mTestClient1 secure codec 1
230 // graphic memory 200
231 // graphic memory 200
232 // ---------------------------------------------------------------------------------
233 // kTestPid2(20) 20 mTestClient2 non-secure codec 1
234 // graphic memory 300
235 // -------------------------------------------
236 // mTestClient3 secure codec 1
237 // graphic memory 100
238 // ---------------------------------------------------------------------------------
Ronghua Wu231c3d12015-03-11 15:10:32 -0700239 void addResource() {
240 // kTestPid1 mTestClient1
Chong Zhang181e6952019-10-09 13:23:39 -0700241 std::vector<MediaResourceParcel> resources1;
242 resources1.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1));
Chong Zhangee33d642019-08-08 14:26:43 -0700243 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
Chong Zhang181e6952019-10-09 13:23:39 -0700244 resources1.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 200));
245 std::vector<MediaResourceParcel> resources11;
246 resources11.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 200));
Chong Zhangee33d642019-08-08 14:26:43 -0700247 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700248
249 // kTestPid2 mTestClient2
Chong Zhang181e6952019-10-09 13:23:39 -0700250 std::vector<MediaResourceParcel> resources2;
251 resources2.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1));
252 resources2.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 300));
Chong Zhangee33d642019-08-08 14:26:43 -0700253 mService->addResource(kTestPid2, kTestUid2, getId(mTestClient2), mTestClient2, resources2);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700254
255 // kTestPid2 mTestClient3
Chong Zhang181e6952019-10-09 13:23:39 -0700256 std::vector<MediaResourceParcel> resources3;
Chong Zhangee33d642019-08-08 14:26:43 -0700257 mService->addResource(kTestPid2, kTestUid2, getId(mTestClient3), mTestClient3, resources3);
Chong Zhang181e6952019-10-09 13:23:39 -0700258 resources3.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1));
259 resources3.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 100));
Chong Zhangee33d642019-08-08 14:26:43 -0700260 mService->addResource(kTestPid2, kTestUid2, getId(mTestClient3), mTestClient3, resources3);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700261
262 const PidResourceInfosMap &map = mService->mMap;
263 EXPECT_EQ(2u, map.size());
264 ssize_t index1 = map.indexOfKey(kTestPid1);
265 ASSERT_GE(index1, 0);
266 const ResourceInfos &infos1 = map[index1];
267 EXPECT_EQ(1u, infos1.size());
Chong Zhangfb092d32019-08-12 09:45:44 -0700268 expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, resources1);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700269
270 ssize_t index2 = map.indexOfKey(kTestPid2);
271 ASSERT_GE(index2, 0);
272 const ResourceInfos &infos2 = map[index2];
273 EXPECT_EQ(2u, infos2.size());
Chong Zhangfb092d32019-08-12 09:45:44 -0700274 expectEqResourceInfo(infos2.valueFor(getId(mTestClient2)), kTestUid2, mTestClient2, resources2);
275 expectEqResourceInfo(infos2.valueFor(getId(mTestClient3)), kTestUid2, mTestClient3, resources3);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700276 }
277
Chong Zhang181e6952019-10-09 13:23:39 -0700278 void testCombineResourceWithNegativeValues() {
279 // kTestPid1 mTestClient1
280 std::vector<MediaResourceParcel> resources1;
281 resources1.push_back(MediaResource(MediaResource::Type::kDrmSession, -100));
282 resources1.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, -100));
283 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
284
285 // Expected result:
286 // 1) the client should have been added;
287 // 2) both resource entries should have been rejected, resource list should be empty.
288 const PidResourceInfosMap &map = mService->mMap;
289 EXPECT_EQ(1u, map.size());
290 ssize_t index1 = map.indexOfKey(kTestPid1);
291 ASSERT_GE(index1, 0);
292 const ResourceInfos &infos1 = map[index1];
293 EXPECT_EQ(1u, infos1.size());
294 std::vector<MediaResourceParcel> expected;
295 expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected);
296
297 resources1.clear();
298 resources1.push_back(MediaResource(MediaResource::Type::kDrmSession, INT64_MAX));
299 resources1.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, INT64_MAX));
300 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
301 resources1.clear();
302 resources1.push_back(MediaResource(MediaResource::Type::kDrmSession, 10));
303 resources1.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 10));
304 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
305
306 // Expected result:
307 // Both values should saturate to INT64_MAX
308 expected.push_back(MediaResource(MediaResource::Type::kDrmSession, INT64_MAX));
309 expected.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, INT64_MAX));
310 expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected);
311
312 resources1.clear();
313 resources1.push_back(MediaResource(MediaResource::Type::kDrmSession, -10));
314 resources1.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, -10));
315 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
316
317 // Expected result:
318 // 1) DrmSession resource should allow negative value addition, and value should drop accordingly
319 // 2) Non-drm session resource should ignore negative value addition.
320 expected.push_back(MediaResource(MediaResource::Type::kDrmSession, INT64_MAX - 10));
321 expected.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, INT64_MAX));
322 expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected);
323
324 resources1.clear();
325 resources1.push_back(MediaResource(MediaResource::Type::kDrmSession, INT64_MIN));
326 expected.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, INT64_MIN));
327 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
328
329 // Expected result:
330 // 1) DrmSession resource value should drop to 0, but the entry shouldn't be removed.
331 // 2) Non-drm session resource should ignore negative value addition.
332 expected.clear();
333 expected.push_back(MediaResource(MediaResource::Type::kDrmSession, 0));
334 expected.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, INT64_MAX));
335 expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected);
336 }
337
Ronghua Wu231c3d12015-03-11 15:10:32 -0700338 void testConfig() {
339 EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs);
340 EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec);
341
Chong Zhang181e6952019-10-09 13:23:39 -0700342 std::vector<MediaResourcePolicyParcel> policies1;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700343 policies1.push_back(
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700344 MediaResourcePolicy(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800345 IResourceManagerService::kPolicySupportsMultipleSecureCodecs,
Chong Zhang181e6952019-10-09 13:23:39 -0700346 "true"));
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700347 policies1.push_back(
348 MediaResourcePolicy(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800349 IResourceManagerService::kPolicySupportsSecureWithNonSecureCodec,
Chong Zhang181e6952019-10-09 13:23:39 -0700350 "false"));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700351 mService->config(policies1);
352 EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs);
353 EXPECT_FALSE(mService->mSupportsSecureWithNonSecureCodec);
354
Chong Zhang181e6952019-10-09 13:23:39 -0700355 std::vector<MediaResourcePolicyParcel> policies2;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700356 policies2.push_back(
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700357 MediaResourcePolicy(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800358 IResourceManagerService::kPolicySupportsMultipleSecureCodecs,
Chong Zhang181e6952019-10-09 13:23:39 -0700359 "false"));
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700360 policies2.push_back(
361 MediaResourcePolicy(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800362 IResourceManagerService::kPolicySupportsSecureWithNonSecureCodec,
Chong Zhang181e6952019-10-09 13:23:39 -0700363 "true"));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700364 mService->config(policies2);
365 EXPECT_FALSE(mService->mSupportsMultipleSecureCodecs);
366 EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec);
367 }
368
Chong Zhangfb092d32019-08-12 09:45:44 -0700369 void testCombineResource() {
370 // kTestPid1 mTestClient1
Chong Zhang181e6952019-10-09 13:23:39 -0700371 std::vector<MediaResourceParcel> resources1;
372 resources1.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1));
Chong Zhangfb092d32019-08-12 09:45:44 -0700373 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
374
Chong Zhang181e6952019-10-09 13:23:39 -0700375 std::vector<MediaResourceParcel> resources11;
376 resources11.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 200));
Chong Zhangfb092d32019-08-12 09:45:44 -0700377 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11);
378
379 const PidResourceInfosMap &map = mService->mMap;
380 EXPECT_EQ(1u, map.size());
381 ssize_t index1 = map.indexOfKey(kTestPid1);
382 ASSERT_GE(index1, 0);
383 const ResourceInfos &infos1 = map[index1];
384 EXPECT_EQ(1u, infos1.size());
385
386 // test adding existing types to combine values
Chong Zhang181e6952019-10-09 13:23:39 -0700387 resources1.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 100));
Chong Zhangfb092d32019-08-12 09:45:44 -0700388 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
389
Chong Zhang181e6952019-10-09 13:23:39 -0700390 std::vector<MediaResourceParcel> expected;
391 expected.push_back(MediaResource(MediaResource::Type::kSecureCodec, 2));
392 expected.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 300));
Chong Zhangfb092d32019-08-12 09:45:44 -0700393 expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected);
394
395 // test adding new types (including types that differs only in subType)
Chong Zhang181e6952019-10-09 13:23:39 -0700396 resources11.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1));
397 resources11.push_back(MediaResource(MediaResource::Type::kSecureCodec, MediaResource::SubType::kVideoCodec, 1));
Chong Zhangfb092d32019-08-12 09:45:44 -0700398 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11);
399
400 expected.clear();
Chong Zhang181e6952019-10-09 13:23:39 -0700401 expected.push_back(MediaResource(MediaResource::Type::kSecureCodec, 2));
402 expected.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1));
403 expected.push_back(MediaResource(MediaResource::Type::kSecureCodec, MediaResource::SubType::kVideoCodec, 1));
404 expected.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 500));
Chong Zhangfb092d32019-08-12 09:45:44 -0700405 expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected);
406 }
407
Ronghua Wu231c3d12015-03-11 15:10:32 -0700408 void testRemoveResource() {
Chong Zhangfb092d32019-08-12 09:45:44 -0700409 // kTestPid1 mTestClient1
Chong Zhang181e6952019-10-09 13:23:39 -0700410 std::vector<MediaResourceParcel> resources1;
411 resources1.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1));
Chong Zhangfb092d32019-08-12 09:45:44 -0700412 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
413
Chong Zhang181e6952019-10-09 13:23:39 -0700414 std::vector<MediaResourceParcel> resources11;
415 resources11.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 200));
Chong Zhangfb092d32019-08-12 09:45:44 -0700416 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11);
417
418 const PidResourceInfosMap &map = mService->mMap;
419 EXPECT_EQ(1u, map.size());
420 ssize_t index1 = map.indexOfKey(kTestPid1);
421 ASSERT_GE(index1, 0);
422 const ResourceInfos &infos1 = map[index1];
423 EXPECT_EQ(1u, infos1.size());
424
425 // test partial removal
Chong Zhang181e6952019-10-09 13:23:39 -0700426 resources11[0].value = 100;
Chong Zhangfb092d32019-08-12 09:45:44 -0700427 mService->removeResource(kTestPid1, getId(mTestClient1), resources11);
428
Chong Zhang181e6952019-10-09 13:23:39 -0700429 std::vector<MediaResourceParcel> expected;
430 expected.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1));
431 expected.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 100));
432 expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected);
433
434 // test removal request with negative value, should be ignored
435 resources11[0].value = -10000;
436 mService->removeResource(kTestPid1, getId(mTestClient1), resources11);
437
Chong Zhangfb092d32019-08-12 09:45:44 -0700438 expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected);
439
440 // test complete removal with overshoot value
Chong Zhang181e6952019-10-09 13:23:39 -0700441 resources11[0].value = 1000;
Chong Zhangfb092d32019-08-12 09:45:44 -0700442 mService->removeResource(kTestPid1, getId(mTestClient1), resources11);
443
444 expected.clear();
Chong Zhang181e6952019-10-09 13:23:39 -0700445 expected.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1));
Chong Zhangfb092d32019-08-12 09:45:44 -0700446 expectEqResourceInfo(infos1.valueFor(getId(mTestClient1)), kTestUid1, mTestClient1, expected);
447 }
448
449 void testRemoveClient() {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700450 addResource();
451
Chong Zhangfb092d32019-08-12 09:45:44 -0700452 mService->removeClient(kTestPid2, getId(mTestClient2));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700453
454 const PidResourceInfosMap &map = mService->mMap;
455 EXPECT_EQ(2u, map.size());
456 const ResourceInfos &infos1 = map.valueFor(kTestPid1);
457 const ResourceInfos &infos2 = map.valueFor(kTestPid2);
458 EXPECT_EQ(1u, infos1.size());
459 EXPECT_EQ(1u, infos2.size());
460 // mTestClient2 has been removed.
Chong Zhangfb092d32019-08-12 09:45:44 -0700461 // (OK to use infos2[0] as there is only 1 entry)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700462 EXPECT_EQ(mTestClient3, infos2[0].client);
463 }
464
465 void testGetAllClients() {
466 addResource();
467
Chong Zhang181e6952019-10-09 13:23:39 -0700468 MediaResource::Type type = MediaResource::Type::kSecureCodec;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800469 Vector<std::shared_ptr<IResourceManagerClient> > clients;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700470 EXPECT_FALSE(mService->getAllClients_l(kLowPriorityPid, type, &clients));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700471 // some higher priority process (e.g. kTestPid2) owns the resource, so getAllClients_l
472 // will fail.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700473 EXPECT_FALSE(mService->getAllClients_l(kMidPriorityPid, type, &clients));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700474 EXPECT_TRUE(mService->getAllClients_l(kHighPriorityPid, type, &clients));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700475
476 EXPECT_EQ(2u, clients.size());
Chong Zhangfb092d32019-08-12 09:45:44 -0700477 // (OK to require ordering in clients[], as the pid map is sorted)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700478 EXPECT_EQ(mTestClient3, clients[0]);
479 EXPECT_EQ(mTestClient1, clients[1]);
480 }
481
482 void testReclaimResourceSecure() {
Chong Zhang181e6952019-10-09 13:23:39 -0700483 bool result;
484 std::vector<MediaResourceParcel> resources;
485 resources.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1));
486 resources.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 150));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700487
488 // ### secure codec can't coexist and secure codec can coexist with non-secure codec ###
489 {
490 addResource();
491 mService->mSupportsMultipleSecureCodecs = false;
492 mService->mSupportsSecureWithNonSecureCodec = true;
493
494 // priority too low
Chong Zhang181e6952019-10-09 13:23:39 -0700495 CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result));
496 CHECK_STATUS_FALSE(mService->reclaimResource(kMidPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700497
498 // reclaim all secure codecs
Chong Zhang181e6952019-10-09 13:23:39 -0700499 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700500 verifyClients(true /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700501
502 // call again should reclaim one largest graphic memory from lowest process
Chong Zhang181e6952019-10-09 13:23:39 -0700503 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700504 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700505
506 // nothing left
Chong Zhang181e6952019-10-09 13:23:39 -0700507 CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700508 }
509
510 // ### secure codecs can't coexist and secure codec can't coexist with non-secure codec ###
511 {
512 addResource();
513 mService->mSupportsMultipleSecureCodecs = false;
514 mService->mSupportsSecureWithNonSecureCodec = false;
515
516 // priority too low
Chong Zhang181e6952019-10-09 13:23:39 -0700517 CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result));
518 CHECK_STATUS_FALSE(mService->reclaimResource(kMidPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700519
520 // reclaim all secure and non-secure codecs
Chong Zhang181e6952019-10-09 13:23:39 -0700521 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700522 verifyClients(true /* c1 */, true /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700523
524 // nothing left
Chong Zhang181e6952019-10-09 13:23:39 -0700525 CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700526 }
527
528
529 // ### secure codecs can coexist but secure codec can't coexist with non-secure codec ###
530 {
531 addResource();
532 mService->mSupportsMultipleSecureCodecs = true;
533 mService->mSupportsSecureWithNonSecureCodec = false;
534
535 // priority too low
Chong Zhang181e6952019-10-09 13:23:39 -0700536 CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result));
537 CHECK_STATUS_FALSE(mService->reclaimResource(kMidPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700538
539 // reclaim all non-secure codecs
Chong Zhang181e6952019-10-09 13:23:39 -0700540 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700541 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700542
543 // call again should reclaim one largest graphic memory from lowest process
Chong Zhang181e6952019-10-09 13:23:39 -0700544 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700545 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700546
547 // call again should reclaim another largest graphic memory from lowest process
Chong Zhang181e6952019-10-09 13:23:39 -0700548 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700549 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700550
551 // nothing left
Chong Zhang181e6952019-10-09 13:23:39 -0700552 CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700553 }
554
555 // ### secure codecs can coexist and secure codec can coexist with non-secure codec ###
556 {
557 addResource();
558 mService->mSupportsMultipleSecureCodecs = true;
559 mService->mSupportsSecureWithNonSecureCodec = true;
560
561 // priority too low
Chong Zhang181e6952019-10-09 13:23:39 -0700562 CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700563
Chong Zhang181e6952019-10-09 13:23:39 -0700564 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700565 // one largest graphic memory from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700566 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700567
568 // call again should reclaim another graphic memory from lowest process
Chong Zhang181e6952019-10-09 13:23:39 -0700569 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700570 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700571
572 // call again should reclaim another graphic memory from lowest process
Chong Zhang181e6952019-10-09 13:23:39 -0700573 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700574 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700575
576 // nothing left
Chong Zhang181e6952019-10-09 13:23:39 -0700577 CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700578 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700579
580 // ### secure codecs can coexist and secure codec can coexist with non-secure codec ###
581 {
582 addResource();
583 mService->mSupportsMultipleSecureCodecs = true;
584 mService->mSupportsSecureWithNonSecureCodec = true;
585
Chong Zhang181e6952019-10-09 13:23:39 -0700586 std::vector<MediaResourceParcel> resources;
587 resources.push_back(MediaResource(MediaResource::Type::kSecureCodec, 1));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700588
Chong Zhang181e6952019-10-09 13:23:39 -0700589 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700590 // secure codec from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700591 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700592
593 // call again should reclaim another secure codec from lowest process
Chong Zhang181e6952019-10-09 13:23:39 -0700594 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700595 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700596
Ronghua Wu05d89f12015-07-07 16:47:42 -0700597 // no more secure codec, non-secure codec will be reclaimed.
Chong Zhang181e6952019-10-09 13:23:39 -0700598 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700599 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700600 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700601 }
602
603 void testReclaimResourceNonSecure() {
Chong Zhang181e6952019-10-09 13:23:39 -0700604 bool result;
605 std::vector<MediaResourceParcel> resources;
606 resources.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1));
607 resources.push_back(MediaResource(MediaResource::Type::kGraphicMemory, 150));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700608
609 // ### secure codec can't coexist with non-secure codec ###
610 {
611 addResource();
612 mService->mSupportsSecureWithNonSecureCodec = false;
613
614 // priority too low
Chong Zhang181e6952019-10-09 13:23:39 -0700615 CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result));
616 CHECK_STATUS_FALSE(mService->reclaimResource(kMidPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700617
618 // reclaim all secure codecs
Chong Zhang181e6952019-10-09 13:23:39 -0700619 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700620 verifyClients(true /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700621
622 // call again should reclaim one graphic memory from lowest process
Chong Zhang181e6952019-10-09 13:23:39 -0700623 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700624 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700625
626 // nothing left
Chong Zhang181e6952019-10-09 13:23:39 -0700627 CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700628 }
629
630
631 // ### secure codec can coexist with non-secure codec ###
632 {
633 addResource();
634 mService->mSupportsSecureWithNonSecureCodec = true;
635
636 // priority too low
Chong Zhang181e6952019-10-09 13:23:39 -0700637 CHECK_STATUS_FALSE(mService->reclaimResource(kLowPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700638
Chong Zhang181e6952019-10-09 13:23:39 -0700639 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700640 // one largest graphic memory from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700641 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700642
643 // call again should reclaim another graphic memory from lowest process
Chong Zhang181e6952019-10-09 13:23:39 -0700644 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700645 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700646
647 // call again should reclaim another graphic memory from lowest process
Chong Zhang181e6952019-10-09 13:23:39 -0700648 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700649 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700650
651 // nothing left
Chong Zhang181e6952019-10-09 13:23:39 -0700652 CHECK_STATUS_FALSE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700653 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700654
655 // ### secure codec can coexist with non-secure codec ###
656 {
657 addResource();
658 mService->mSupportsSecureWithNonSecureCodec = true;
659
Chong Zhang181e6952019-10-09 13:23:39 -0700660 std::vector<MediaResourceParcel> resources;
661 resources.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700662
Chong Zhang181e6952019-10-09 13:23:39 -0700663 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700664 // one non secure codec from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700665 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700666
Ronghua Wu05d89f12015-07-07 16:47:42 -0700667 // no more non-secure codec, secure codec from lowest priority process will be reclaimed
Chong Zhang181e6952019-10-09 13:23:39 -0700668 CHECK_STATUS_TRUE(mService->reclaimResource(kHighPriorityPid, resources, &result));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700669 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700670
Ronghua Wu05d89f12015-07-07 16:47:42 -0700671 // clean up client 3 which still left
Chong Zhangfb092d32019-08-12 09:45:44 -0700672 mService->removeClient(kTestPid2, getId(mTestClient3));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700673 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700674 }
675
676 void testGetLowestPriorityBiggestClient() {
Chong Zhang181e6952019-10-09 13:23:39 -0700677 MediaResource::Type type = MediaResource::Type::kGraphicMemory;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800678 std::shared_ptr<IResourceManagerClient> client;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700679 EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700680
681 addResource();
682
Ronghua Wu05d89f12015-07-07 16:47:42 -0700683 EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kLowPriorityPid, type, &client));
684 EXPECT_TRUE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700685
Chong Zhang181e6952019-10-09 13:23:39 -0700686 // kTestPid1 is the lowest priority process with MediaResource::Type::kGraphicMemory.
687 // mTestClient1 has the largest MediaResource::Type::kGraphicMemory within kTestPid1.
Ronghua Wu231c3d12015-03-11 15:10:32 -0700688 EXPECT_EQ(mTestClient1, client);
689 }
690
691 void testGetLowestPriorityPid() {
692 int pid;
693 int priority;
694 TestProcessInfo processInfo;
695
Chong Zhang181e6952019-10-09 13:23:39 -0700696 MediaResource::Type type = MediaResource::Type::kGraphicMemory;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700697 EXPECT_FALSE(mService->getLowestPriorityPid_l(type, &pid, &priority));
698
699 addResource();
700
701 EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority));
702 EXPECT_EQ(kTestPid1, pid);
703 int priority1;
704 processInfo.getPriority(kTestPid1, &priority1);
705 EXPECT_EQ(priority1, priority);
706
Chong Zhang181e6952019-10-09 13:23:39 -0700707 type = MediaResource::Type::kNonSecureCodec;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700708 EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority));
709 EXPECT_EQ(kTestPid2, pid);
710 int priority2;
711 processInfo.getPriority(kTestPid2, &priority2);
712 EXPECT_EQ(priority2, priority);
713 }
714
715 void testGetBiggestClient() {
Chong Zhang181e6952019-10-09 13:23:39 -0700716 MediaResource::Type type = MediaResource::Type::kGraphicMemory;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800717 std::shared_ptr<IResourceManagerClient> client;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700718 EXPECT_FALSE(mService->getBiggestClient_l(kTestPid2, type, &client));
719
720 addResource();
721
722 EXPECT_TRUE(mService->getBiggestClient_l(kTestPid2, type, &client));
723 EXPECT_EQ(mTestClient2, client);
724 }
725
726 void testIsCallingPriorityHigher() {
727 EXPECT_FALSE(mService->isCallingPriorityHigher_l(101, 100));
728 EXPECT_FALSE(mService->isCallingPriorityHigher_l(100, 100));
729 EXPECT_TRUE(mService->isCallingPriorityHigher_l(99, 100));
730 }
731
Chong Zhangdd726802019-08-21 17:24:13 -0700732 void testBatteryStats() {
733 // reset should always be called when ResourceManagerService is created (restarted)
734 EXPECT_EQ(1u, mSystemCB->eventCount());
735 EXPECT_EQ(EventType::VIDEO_RESET, mSystemCB->lastEventType());
736
737 // new client request should cause VIDEO_ON
Chong Zhang181e6952019-10-09 13:23:39 -0700738 std::vector<MediaResourceParcel> resources1;
739 resources1.push_back(MediaResource(MediaResource::Type::kBattery, MediaResource::SubType::kVideoCodec, 1));
Chong Zhangdd726802019-08-21 17:24:13 -0700740 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
741 EXPECT_EQ(2u, mSystemCB->eventCount());
742 EXPECT_EQ(EventEntry({EventType::VIDEO_ON, kTestUid1}), mSystemCB->lastEvent());
743
744 // each client should only cause 1 VIDEO_ON
745 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
746 EXPECT_EQ(2u, mSystemCB->eventCount());
747
748 // new client request should cause VIDEO_ON
Chong Zhang181e6952019-10-09 13:23:39 -0700749 std::vector<MediaResourceParcel> resources2;
750 resources2.push_back(MediaResource(MediaResource::Type::kBattery, MediaResource::SubType::kVideoCodec, 2));
Chong Zhangdd726802019-08-21 17:24:13 -0700751 mService->addResource(kTestPid2, kTestUid2, getId(mTestClient2), mTestClient2, resources2);
752 EXPECT_EQ(3u, mSystemCB->eventCount());
753 EXPECT_EQ(EventEntry({EventType::VIDEO_ON, kTestUid2}), mSystemCB->lastEvent());
754
755 // partially remove mTestClient1's request, shouldn't be any VIDEO_OFF
756 mService->removeResource(kTestPid1, getId(mTestClient1), resources1);
757 EXPECT_EQ(3u, mSystemCB->eventCount());
758
759 // remove mTestClient1's request, should be VIDEO_OFF for kTestUid1
760 // (use resource2 to test removing more instances than previously requested)
761 mService->removeResource(kTestPid1, getId(mTestClient1), resources2);
762 EXPECT_EQ(4u, mSystemCB->eventCount());
763 EXPECT_EQ(EventEntry({EventType::VIDEO_OFF, kTestUid1}), mSystemCB->lastEvent());
764
765 // remove mTestClient2, should be VIDEO_OFF for kTestUid2
766 mService->removeClient(kTestPid2, getId(mTestClient2));
767 EXPECT_EQ(5u, mSystemCB->eventCount());
768 EXPECT_EQ(EventEntry({EventType::VIDEO_OFF, kTestUid2}), mSystemCB->lastEvent());
769 }
770
771 void testCpusetBoost() {
772 // reset should always be called when ResourceManagerService is created (restarted)
773 EXPECT_EQ(1u, mSystemCB->eventCount());
774 EXPECT_EQ(EventType::VIDEO_RESET, mSystemCB->lastEventType());
775
776 // new client request should cause CPUSET_ENABLE
Chong Zhang181e6952019-10-09 13:23:39 -0700777 std::vector<MediaResourceParcel> resources1;
778 resources1.push_back(MediaResource(MediaResource::Type::kCpuBoost, 1));
Chong Zhangdd726802019-08-21 17:24:13 -0700779 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
780 EXPECT_EQ(2u, mSystemCB->eventCount());
781 EXPECT_EQ(EventType::CPUSET_ENABLE, mSystemCB->lastEventType());
782
783 // each client should only cause 1 CPUSET_ENABLE
784 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
785 EXPECT_EQ(2u, mSystemCB->eventCount());
786
787 // new client request should cause CPUSET_ENABLE
Chong Zhang181e6952019-10-09 13:23:39 -0700788 std::vector<MediaResourceParcel> resources2;
789 resources2.push_back(MediaResource(MediaResource::Type::kCpuBoost, 2));
Chong Zhangdd726802019-08-21 17:24:13 -0700790 mService->addResource(kTestPid2, kTestUid2, getId(mTestClient2), mTestClient2, resources2);
791 EXPECT_EQ(3u, mSystemCB->eventCount());
792 EXPECT_EQ(EventType::CPUSET_ENABLE, mSystemCB->lastEventType());
793
794 // remove mTestClient2 should not cause CPUSET_DISABLE, mTestClient1 still active
795 mService->removeClient(kTestPid2, getId(mTestClient2));
796 EXPECT_EQ(3u, mSystemCB->eventCount());
797
798 // remove 1 cpuboost from mTestClient1, should not be CPUSET_DISABLE (still 1 left)
799 mService->removeResource(kTestPid1, getId(mTestClient1), resources1);
800 EXPECT_EQ(3u, mSystemCB->eventCount());
801
802 // remove 2 cpuboost from mTestClient1, should be CPUSET_DISABLE
803 // (use resource2 to test removing more than previously requested)
804 mService->removeResource(kTestPid1, getId(mTestClient1), resources2);
805 EXPECT_EQ(4u, mSystemCB->eventCount());
806 EXPECT_EQ(EventType::CPUSET_DISABLE, mSystemCB->lastEventType());
807 }
808
809 sp<TestSystemCallback> mSystemCB;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800810 std::shared_ptr<ResourceManagerService> mService;
811 std::shared_ptr<IResourceManagerClient> mTestClient1;
812 std::shared_ptr<IResourceManagerClient> mTestClient2;
813 std::shared_ptr<IResourceManagerClient> mTestClient3;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700814};
815
816TEST_F(ResourceManagerServiceTest, config) {
817 testConfig();
818}
819
820TEST_F(ResourceManagerServiceTest, addResource) {
821 addResource();
822}
823
Chong Zhangfb092d32019-08-12 09:45:44 -0700824TEST_F(ResourceManagerServiceTest, combineResource) {
825 testCombineResource();
826}
827
Chong Zhang181e6952019-10-09 13:23:39 -0700828TEST_F(ResourceManagerServiceTest, combineResourceNegative) {
829 testCombineResourceWithNegativeValues();
830}
831
Ronghua Wu231c3d12015-03-11 15:10:32 -0700832TEST_F(ResourceManagerServiceTest, removeResource) {
833 testRemoveResource();
834}
835
Chong Zhangfb092d32019-08-12 09:45:44 -0700836TEST_F(ResourceManagerServiceTest, removeClient) {
837 testRemoveClient();
838}
839
Ronghua Wu231c3d12015-03-11 15:10:32 -0700840TEST_F(ResourceManagerServiceTest, reclaimResource) {
841 testReclaimResourceSecure();
842 testReclaimResourceNonSecure();
843}
844
845TEST_F(ResourceManagerServiceTest, getAllClients_l) {
846 testGetAllClients();
847}
848
849TEST_F(ResourceManagerServiceTest, getLowestPriorityBiggestClient_l) {
850 testGetLowestPriorityBiggestClient();
851}
852
853TEST_F(ResourceManagerServiceTest, getLowestPriorityPid_l) {
854 testGetLowestPriorityPid();
855}
856
857TEST_F(ResourceManagerServiceTest, getBiggestClient_l) {
858 testGetBiggestClient();
859}
860
861TEST_F(ResourceManagerServiceTest, isCallingPriorityHigher_l) {
862 testIsCallingPriorityHigher();
863}
864
Chong Zhangdd726802019-08-21 17:24:13 -0700865TEST_F(ResourceManagerServiceTest, testBatteryStats) {
866 testBatteryStats();
867}
868
869TEST_F(ResourceManagerServiceTest, testCpusetBoost) {
870 testCpusetBoost();
871}
872
Ronghua Wu231c3d12015-03-11 15:10:32 -0700873} // namespace android