1 /*
2 * Copyright (C) 2024 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 <gtest/gtest.h>
18
19 #include <vector>
20
21 #include "aidl/SessionRecords.h"
22
23 #define MS_TO_NS(x) (x * 1000 * 1000)
24 #define MS_TO_US(x) (x * 1000)
25
26 namespace aidl {
27 namespace google {
28 namespace hardware {
29 namespace power {
30 namespace impl {
31 namespace pixel {
32
33 class SessionRecordsTest : public ::testing::Test {
34 public:
SetUp()35 void SetUp() {
36 mRecords = std::make_shared<SessionRecords>(kMaxNumOfRecords, kJankCheckTimeFactor);
37 }
38
39 protected:
fakeWorkDurations(const std::vector<int32_t> fakedTotalDurationsMs)40 std::vector<WorkDuration> fakeWorkDurations(const std::vector<int32_t> fakedTotalDurationsMs) {
41 std::vector<WorkDuration> fakedWorkDurationsNs;
42 for (auto &d : fakedTotalDurationsMs) {
43 fakedWorkDurationsNs.emplace_back(0, MS_TO_NS(d));
44 }
45 return fakedWorkDurationsNs;
46 }
47
fakeWorkDurations(const std::vector<std::pair<int32_t,int32_t>> fakedReportedDurationsMs)48 std::vector<WorkDuration> fakeWorkDurations(
49 const std::vector<std::pair<int32_t, int32_t>> fakedReportedDurationsMs) {
50 std::vector<WorkDuration> fakedWorkDurationsNs;
51 for (auto &r : fakedReportedDurationsMs) {
52 fakedWorkDurationsNs.emplace_back(MS_TO_NS(r.first), MS_TO_NS(r.second));
53 }
54 return fakedWorkDurationsNs;
55 }
56
57 static constexpr int32_t kMaxNumOfRecords = 5;
58 static constexpr double kJankCheckTimeFactor = 1.5;
59 std::shared_ptr<SessionRecords> mRecords;
60 };
61
TEST_F(SessionRecordsTest,NoRecords)62 TEST_F(SessionRecordsTest, NoRecords) {
63 ASSERT_EQ(0, mRecords->getNumOfRecords());
64 ASSERT_FALSE(mRecords->getMaxDuration().has_value());
65 ASSERT_FALSE(mRecords->getAvgDuration().has_value());
66 ASSERT_EQ(0, mRecords->getNumOfMissedCycles());
67 }
68
TEST_F(SessionRecordsTest,addReportedDurations)69 TEST_F(SessionRecordsTest, addReportedDurations) {
70 mRecords->addReportedDurations(fakeWorkDurations({3, 4, 3, 2}), MS_TO_NS(3));
71 ASSERT_EQ(4, mRecords->getNumOfRecords());
72 ASSERT_EQ(MS_TO_US(4), mRecords->getMaxDuration().value());
73 ASSERT_EQ(MS_TO_US(3), mRecords->getAvgDuration().value());
74 ASSERT_EQ(0, mRecords->getNumOfMissedCycles());
75
76 // Push more records to override part of the old ones in the ring buffer
77 mRecords->addReportedDurations(fakeWorkDurations({2, 1, 2}), MS_TO_NS(3));
78 ASSERT_EQ(5, mRecords->getNumOfRecords());
79 ASSERT_EQ(MS_TO_US(3), mRecords->getMaxDuration().value());
80 ASSERT_EQ(MS_TO_US(2), mRecords->getAvgDuration().value());
81 ASSERT_EQ(0, mRecords->getNumOfMissedCycles());
82
83 // More records to override the ring buffer more rounds
84 mRecords->addReportedDurations(fakeWorkDurations({10, 2, 9, 8, 4, 5, 7, 6}), MS_TO_NS(3));
85 ASSERT_EQ(5, mRecords->getNumOfRecords());
86 ASSERT_EQ(MS_TO_US(8), mRecords->getMaxDuration().value());
87 ASSERT_EQ(MS_TO_US(6), mRecords->getAvgDuration().value());
88 ASSERT_EQ(4, mRecords->getNumOfMissedCycles());
89 }
90
TEST_F(SessionRecordsTest,checkLowFrameRate)91 TEST_F(SessionRecordsTest, checkLowFrameRate) {
92 ASSERT_FALSE(mRecords->isLowFrameRate(25));
93 mRecords->addReportedDurations(fakeWorkDurations({{0, 8}, {10, 9}, {20, 8}, {30, 8}}),
94 MS_TO_NS(10));
95 ASSERT_EQ(4, mRecords->getNumOfRecords());
96 ASSERT_FALSE(mRecords->isLowFrameRate(25));
97
98 mRecords->addReportedDurations(fakeWorkDurations({{130, 8}, {230, 9}}), MS_TO_NS(10));
99 ASSERT_EQ(5, mRecords->getNumOfRecords());
100 ASSERT_FALSE(mRecords->isLowFrameRate(25));
101
102 mRecords->addReportedDurations(fakeWorkDurations({{330, 8}, {430, 9}}), MS_TO_NS(10));
103 ASSERT_EQ(5, mRecords->getNumOfRecords());
104 ASSERT_TRUE(mRecords->isLowFrameRate(25));
105
106 mRecords->addReportedDurations(fakeWorkDurations({{440, 8}, {450, 9}}), MS_TO_NS(10));
107 ASSERT_EQ(5, mRecords->getNumOfRecords());
108 ASSERT_FALSE(mRecords->isLowFrameRate(25));
109 }
110
111 } // namespace pixel
112 } // namespace impl
113 } // namespace power
114 } // namespace hardware
115 } // namespace google
116 } // namespace aidl
117