1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #define private public
17 #define protected public
18 #include <gtest/gtest.h>
19 #include <thread>
20 #include "eu/cpu_worker.h"
21 #include "eu/scpuworker_manager.h"
22 #include "eu/scpu_monitor.h"
23 #include "eu/cpu_manager_strategy.h"
24 #include "eu/worker_thread.h"
25 #include "qos.h"
26 #undef private
27 #undef protected
28 
29 namespace OHOS {
30 namespace FFRT_TEST {
31 constexpr int QOS_DEFAULT = 5;
32 constexpr int TASK_NOTIFY_TYPE_DEFAULT = 1;
33 using namespace testing;
34 using namespace testing::ext;
35 using namespace OHOS::FFRT_TEST;
36 using namespace ffrt;
37 using namespace std;
38 
39 
40 class CpuMonitorTest : public testing::Test {
41 protected:
SetUpTestCase()42     static void SetUpTestCase()
43     {
44     }
45 
TearDownTestCase()46     static void TearDownTestCase()
47     {
48     }
49 
SetUp()50     virtual void SetUp()
51     {
52     }
53 
TearDown()54     virtual void TearDown()
55     {
56     }
57 };
58 
59 /**
60  * @tc.name: IntoSleep
61  * @tc.desc: Test whether the IntoSleep interface are normal.
62  * @tc.type: FUNC
63  *
64  *
65  */
TEST_F(CpuMonitorTest,IntoSleep)66 TEST_F(CpuMonitorTest, IntoSleep)
67 {
68     CPUWorkerManager *it = new SCPUWorkerManager();
69 
70     SCPUMonitor cpu({
71         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
72         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
73         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
74 
75     cpu.IntoSleep(QoS(QOS_DEFAULT));
76 }
77 
78 /**
79  * @tc.name: WakeupSleep
80  * @tc.desc: Test whether the WakeupCount interface are normal.
81  * @tc.type: FUNC
82  *
83  *
84  */
TEST_F(CpuMonitorTest,WakeupSleep)85 TEST_F(CpuMonitorTest, WakeupSleep)
86 {
87     CPUWorkerManager *it = new SCPUWorkerManager();
88 
89     SCPUMonitor cpu({
90         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
91         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
92         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
93 
94     cpu.WakeupSleep(QoS(QOS_DEFAULT));
95 }
96 
97 /**
98  * @tc.name: TimeoutCount
99  * @tc.desc: Test whether the TimeoutCount interface are normal.
100  * @tc.type: FUNC
101  *
102  *
103  */
TEST_F(CpuMonitorTest,TimeoutCount)104 TEST_F(CpuMonitorTest, TimeoutCount)
105 {
106     CPUWorkerManager *it = new SCPUWorkerManager();
107 
108     SCPUMonitor cpu({
109         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
110         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
111         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
112 
113     cpu.TimeoutCount(QoS(QOS_DEFAULT));
114 }
115 
116 /**
117  * @tc.name: Notify
118  * @tc.desc: Test whether the Notify interface are normal.
119  * @tc.type: FUNC
120  *
121  *
122  */
TEST_F(CpuMonitorTest,Notify)123 TEST_F(CpuMonitorTest, Notify)
124 {
125     CPUWorkerManager *it = new SCPUWorkerManager();
126 
127     SCPUMonitor cpu({
128         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
129         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
130         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1),
131         std::bind(&CPUWorkerManager::GetWorkerCount, it, std::placeholders::_1),
132         CPUMonitor::HandleTaskNotifyDefault});
133 
134     cpu.Notify(QoS(QOS_DEFAULT), TaskNotifyType(TASK_NOTIFY_TYPE_DEFAULT));
135 }
136 
137 /**
138  * @tc.name: IntoDeepSleep
139  * @tc.desc: Test whether the IntoDeepSleep interface are normal.
140  * @tc.type: FUNC
141  *
142  *
143  */
TEST_F(CpuMonitorTest,IntoDeepSleep)144 TEST_F(CpuMonitorTest, IntoDeepSleep)
145 {
146     CPUWorkerManager *it = new SCPUWorkerManager();
147     SCPUMonitor cpu({
148         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
149         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
150         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
151 
152     cpu.IntoDeepSleep(QoS(QOS_DEFAULT));
153 }
154 
155 /**
156  * @tc.name: WakeupDeepSleep
157  * @tc.desc: Test whether the OutOfDeepSleep interface are normal.
158  * @tc.type: FUNC
159  *
160  *
161  */
TEST_F(CpuMonitorTest,WakeupDeepSleep)162 TEST_F(CpuMonitorTest, WakeupDeepSleep)
163 {
164     CPUWorkerManager *it = new SCPUWorkerManager();
165     SCPUMonitor cpu({
166         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
167         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
168         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
169 
170     cpu.WakeupDeepSleep(QoS(QOS_DEFAULT));
171 }
172 
173 /**
174  * @tc.name: IsExceedDeepSleepThreshold
175  * @tc.desc: Test whether the IsExceedDeepSleepThreshold interface are normal.
176  * @tc.type: FUNC
177  *
178  *
179  */
TEST_F(CpuMonitorTest,IsExceedDeepSleepThreshold)180 TEST_F(CpuMonitorTest, IsExceedDeepSleepThreshold)
181 {
182     CPUWorkerManager *it = new SCPUWorkerManager();
183     SCPUMonitor cpu({
184         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
185         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
186         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
187 
188     bool ret = cpu.IsExceedDeepSleepThreshold();
189 }
190 }
191 }