1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 #include <thread>
18 
19 #include <gtest/gtest.h>
20 
21 #include <utils/SystemClock.h>
22 
23 #include "vhal_v2_0/VehicleObjectPool.h"
24 
25 namespace android {
26 namespace hardware {
27 namespace automotive {
28 namespace vehicle {
29 namespace V2_0 {
30 
31 namespace {
32 
33 class VehicleObjectPoolTest : public ::testing::Test {
34 protected:
SetUp()35     void SetUp() override {
36         stats = PoolStats::instance();
37         resetStats();
38         valuePool.reset(new VehiclePropValuePool);
39     }
40 
TearDown()41     void TearDown() override {
42         // At the end, all created objects should be either recycled or deleted.
43         // Some objects could be recycled multiple times, that's why it's <=
44         ASSERT_EQ(stats->Obtained, stats->Recycled);
45         ASSERT_LE(stats->Created, stats->Recycled);
46     }
47 private:
resetStats()48     void resetStats() {
49         stats->Obtained = 0;
50         stats->Created = 0;
51         stats->Recycled = 0;
52     }
53 
54 public:
55     PoolStats* stats;
56     std::unique_ptr<VehiclePropValuePool> valuePool;
57 };
58 
TEST_F(VehicleObjectPoolTest,valuePoolBasicCorrectness)59 TEST_F(VehicleObjectPoolTest, valuePoolBasicCorrectness) {
60     auto value = valuePool->obtain(VehiclePropertyType::INT32);
61     void* raw = value.get();
62     value.reset();
63     // At this point, value should be recycled and the only object in the pool.
64     ASSERT_EQ(raw, valuePool->obtain(VehiclePropertyType::INT32).get());
65     // Obtaining value of another type - should return a new object
66     ASSERT_NE(raw, valuePool->obtain(VehiclePropertyType::FLOAT).get());
67 
68     ASSERT_EQ(3u, stats->Obtained);
69     ASSERT_EQ(2u, stats->Created);
70 }
71 
TEST_F(VehicleObjectPoolTest,valuePoolStrings)72 TEST_F(VehicleObjectPoolTest, valuePoolStrings) {
73     valuePool->obtain(VehiclePropertyType::STRING);
74     auto vs = valuePool->obtain(VehiclePropertyType::STRING);
75     vs->value.stringValue = "Hello";
76     void* raw = vs.get();
77     vs.reset();  // delete the pointer
78 
79     auto vs2 = valuePool->obtain(VehiclePropertyType::STRING);
80     ASSERT_EQ(0u, vs2->value.stringValue.size());
81     ASSERT_NE(raw, valuePool->obtain(VehiclePropertyType::STRING).get());
82 
83     ASSERT_EQ(0u, stats->Obtained);
84 }
85 
TEST_F(VehicleObjectPoolTest,valuePoolMultithreadedBenchmark)86 TEST_F(VehicleObjectPoolTest, valuePoolMultithreadedBenchmark) {
87     // In this test we have T threads that concurrently in C cycles
88     // obtain and release O VehiclePropValue objects of FLOAT / INT32 types.
89 
90     const int T = 2;
91     const int C = 500;
92     const int O = 100;
93 
94     auto poolPtr = valuePool.get();
95 
96     std::vector<std::thread> threads;
97     auto start = elapsedRealtimeNano();
98     for (int i = 0; i < T; i++) {
99         threads.push_back(std::thread([&poolPtr] () {
100             for (int j = 0; j < C; j++) {
101                 std::vector<recyclable_ptr<VehiclePropValue>> vec;
102                 for (int k = 0; k < O; k++) {
103                     vec.push_back(
104                         poolPtr->obtain(k % 2 == 0
105                                         ? VehiclePropertyType::FLOAT
106                                         : VehiclePropertyType::INT32));
107                 }
108             }
109         }));
110     }
111 
112     for (auto& t : threads) {
113         t.join();
114     }
115     auto finish = elapsedRealtimeNano();
116 
117     ASSERT_EQ(static_cast<uint32_t>(T * C * O), stats->Obtained);
118     ASSERT_EQ(static_cast<uint32_t>(T * C * O), stats->Recycled);
119     // Created less than obtained.
120     ASSERT_GE(static_cast<uint32_t>(T * O), stats->Created);
121 
122     auto elapsedMs = (finish - start) / 1000000;
123     ASSERT_GE(1000, elapsedMs);  // Less a second to access 100K objects.
124                                  // Typically it takes about 0.1s on Nexus6P.
125 }
126 
127 }  // namespace anonymous
128 
129 }  // namespace V2_0
130 }  // namespace vehicle
131 }  // namespace automotive
132 }  // namespace hardware
133 }  // namespace android
134