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 #ifdef FFRT_GITEE
24 #include "eu/cpu_manager_interface.h"
25 #else
26 #include "eu/cpu_manager_strategy.h"
27 #endif
28 #include "eu/worker_thread.h"
29 #include "qos.h"
30 #include "common.h"
31 
32 #undef private
33 #undef protected
34 
35 namespace OHOS {
36 namespace FFRT_TEST {
37 using namespace testing;
38 #ifdef HWTEST_TESTING_EXT_ENABLE
39 using namespace testing::ext;
40 #endif
41 using namespace OHOS::FFRT_TEST;
42 using namespace ffrt;
43 using namespace std;
44 
45 
46 class CpuMonitorTest : public testing::Test {
47 protected:
SetUpTestCase()48     static void SetUpTestCase()
49     {
50     }
51 
TearDownTestCase()52     static void TearDownTestCase()
53     {
54     }
55 
SetUp()56     virtual void SetUp()
57     {
58     }
59 
TearDown()60     virtual void TearDown()
61     {
62     }
63 };
64 
65 /**
66  * @tc.name: IntoSleep
67  * @tc.desc: Test whether the IntoSleep interface are normal.
68  * @tc.type: FUNC
69  *
70  *
71  */
HWTEST_F(CpuMonitorTest, IntoSleep, TestSize.Level1)72 HWTEST_F(CpuMonitorTest, IntoSleep, TestSize.Level1)
73 {
74     CPUWorkerManager *it = new SCPUWorkerManager();
75     SCPUMonitor cpu({
76         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
77         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
78         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
79 
80     cpu.IntoSleep(QoS(5));
81 }
82 
83 #ifdef FFRT_GITEE
84 /**
85  * @tc.name: WakeupCount
86  * @tc.desc: Test whether the WakeupCount interface are normal.
87  * @tc.type: FUNC
88  *
89  *
90  */
HWTEST_F(CpuMonitorTest, WakeupCount, TestSize.Level1)91 HWTEST_F(CpuMonitorTest, WakeupCount, TestSize.Level1)
92 {
93     CPUWorkerManager *it = new SCPUWorkerManager();
94     SCPUMonitor cpu({
95         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
96         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
97         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
98 
99     cpu.WakeupCount(QoS(5));
100 }
101 #else
102 /**
103  * @tc.name: WakeupSleep
104  * @tc.desc: Test whether the WakeupSleep interface are normal.
105  * @tc.type: FUNC
106  *
107  *
108  */
HWTEST_F(CpuMonitorTest, WakeupSleep, TestSize.Level1)109 HWTEST_F(CpuMonitorTest, WakeupSleep, TestSize.Level1)
110 {
111     CPUWorkerManager *it = new SCPUWorkerManager();
112 
113     SCPUMonitor cpu({
114         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
115         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
116         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
117 
118     cpu.WakeupSleep(QoS(5));
119 }
120 #endif
121 
122 /**
123  * @tc.name: TimeoutCount
124  * @tc.desc: Test whether the TimeoutCount interface are normal.
125  * @tc.type: FUNC
126  *
127  *
128  */
HWTEST_F(CpuMonitorTest, TimeoutCount, TestSize.Level1)129 HWTEST_F(CpuMonitorTest, TimeoutCount, TestSize.Level1)
130 {
131     CPUWorkerManager *it = new SCPUWorkerManager();
132     SCPUMonitor cpu({
133         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
134         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
135         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
136 
137     cpu.TimeoutCount(QoS(5));
138 }
139 
140 /**
141  * @tc.name: Notify
142  * @tc.desc: Test whether the Notify interface are normal.
143  * @tc.type: FUNC
144  *
145  *
146  */
HWTEST_F(CpuMonitorTest, Notify, TestSize.Level1)147 HWTEST_F(CpuMonitorTest, Notify, TestSize.Level1)
148 {
149     CPUWorkerManager *it = new SCPUWorkerManager();
150     SCPUMonitor cpu({
151         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
152         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
153         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1),
154         std::bind(&SCPUWorkerManager::GetWorkerCount, it, std::placeholders::_1),
155         CPUMonitor::HandleTaskNotifyDefault});
156 
157     cpu.Notify(QoS(5), TaskNotifyType(1));
158 }
159 
160 /**
161  * @tc.name: IntoDeepSleep
162  * @tc.desc: Test whether the IntoDeepSleep interface are normal.
163  * @tc.type: FUNC
164  *
165  *
166  */
HWTEST_F(CpuMonitorTest, IntoDeepSleep, TestSize.Level1)167 HWTEST_F(CpuMonitorTest, IntoDeepSleep, TestSize.Level1)
168 {
169     CPUWorkerManager *it = new SCPUWorkerManager();
170     SCPUMonitor cpu({
171         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
172         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
173         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
174 
175     cpu.IntoDeepSleep(QoS(5));
176 }
177 
178 #ifdef FFRT_GITEE
179 /**
180  * @tc.name: OutOfDeepSleep
181  * @tc.desc: Test whether the OutOfDeepSleep interface are normal.
182  * @tc.type: FUNC
183  *
184  *
185  */
HWTEST_F(CpuMonitorTest, OutOfDeepSleep, TestSize.Level1)186 HWTEST_F(CpuMonitorTest, OutOfDeepSleep, TestSize.Level1)
187 {
188     CPUWorkerManager *it = new SCPUWorkerManager();
189     SCPUMonitor cpu({
190         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
191         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
192         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
193 
194     cpu.OutOfDeepSleep(QoS(5));
195 }
196 #else
HWTEST_F(CpuMonitorTest, WakeupDeepSleep, TestSize.Level1)197 HWTEST_F(CpuMonitorTest, WakeupDeepSleep, TestSize.Level1)
198 {
199     CPUWorkerManager *it = new SCPUWorkerManager();
200     SCPUMonitor cpu({
201         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
202         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
203         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
204 
205     cpu.WakeupDeepSleep(QoS(5));
206 }
207 #endif
208 
209 /**
210  * @tc.name: IsExceedDeepSleepThreshold
211  * @tc.desc: Test whether the IsExceedDeepSleepThreshold interface are normal.
212  * @tc.type: FUNC
213  *
214  *
215  */
HWTEST_F(CpuMonitorTest, IsExceedDeepSleepThreshold, TestSize.Level1)216 HWTEST_F(CpuMonitorTest, IsExceedDeepSleepThreshold, TestSize.Level1)
217 {
218     CPUWorkerManager *it = new SCPUWorkerManager();
219     SCPUMonitor cpu({
220         std::bind(&CPUWorkerManager::IncWorker, it, std::placeholders::_1),
221         std::bind(&CPUWorkerManager::WakeupWorkers, it, std::placeholders::_1),
222         std::bind(&CPUWorkerManager::GetTaskCount, it, std::placeholders::_1)});
223 
224     bool ret = cpu.IsExceedDeepSleepThreshold();
225 }
226 }
227 }