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