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 #ifndef WITH_NO_MOCKER 18 #include <mockcpp/mockcpp.hpp> 19 #endif 20 #include <fcntl.h> 21 #include "eu/osattr_manager.h" 22 #ifndef WITH_NO_MOCKER 23 #include "eu/qos_config.h" 24 #endif 25 #include "eu/qos_interface.h" 26 #include "../common.h" 27 28 using namespace testing; 29 #ifdef HWTEST_TESTING_EXT_ENABLE 30 using namespace testing::ext; 31 #endif 32 using namespace ffrt; 33 34 class CgroupQosTest : public testing::Test { 35 protected: SetUpTestCase()36 static void SetUpTestCase() 37 { 38 #ifndef WITH_NO_MOCKER 39 MOCKER(write).stubs().will(returnValue(0)); 40 MOCKER(read).stubs().will(returnValue(0)); 41 #endif 42 } 43 TearDownTestCase()44 static void TearDownTestCase() 45 { 46 #ifndef WITH_NO_MOCKER 47 GlobalMockObject::verify(); 48 #endif 49 } 50 SetUp()51 void SetUp() override 52 { 53 } 54 TearDown()55 void TearDown() override 56 { 57 } 58 }; 59 60 #ifndef FFRT_GITEE 61 HWTEST_F(CgroupQosTest, UpdateSchedAttr_test, TestSize.Level0) 62 { 63 ffrt_os_sched_attr attr = {100, 10, 99, 99, 9, "2-3"}; 64 int ret = 0; 65 #ifndef WITH_NO_MOCKER 66 ret = OSAttrManager::Instance()->UpdateSchedAttr(QoS(static_cast<int>(qos_defined_ive)), &attr); 67 EXPECT_EQ(ret, 0); 68 #endif 69 ret = OSAttrManager::Instance()->UpdateSchedAttr(QoS(static_cast<int>(qos_user_interactive)), &attr); 70 EXPECT_EQ(ret, -1); 71 } 72 #endif 73 74 HWTEST_F(CgroupQosTest, SetTidToCGroup_test, TestSize.Level1) 75 { 76 int32_t pid = 100; 77 OSAttrManager::Instance()->SetTidToCGroup(pid); 78 OSAttrManager::Instance()->SetTidToCGroup(-1); 79 EXPECT_EQ(pid, 100); 80 } 81 82 HWTEST_F(CgroupQosTest, SetCGroupCtlPara_test, TestSize.Level1) 83 { 84 int32_t value = 1; 85 OSAttrManager::Instance()->SetCGroupCtlPara("", value); 86 OSAttrManager::Instance()->SetCGroupCtlPara("test", value); 87 EXPECT_EQ(value, 1); 88 } 89 90 HWTEST_F(CgroupQosTest, SetCGroupSetPara_test, TestSize.Level0) 91 { 92 std::string value = "1"; 93 OSAttrManager::Instance()->SetCGroupSetPara("", value); 94 OSAttrManager::Instance()->SetCGroupSetPara("test", value); 95 EXPECT_EQ(value, "1"); 96 } 97 98 HWTEST_F(CgroupQosTest, SetTidToCGroupPrivate_test, TestSize.Level0) 99 { 100 int32_t pid = 100; 101 OSAttrManager::Instance()->SetTidToCGroupPrivate("test", pid); 102 OSAttrManager::Instance()->SetTidToCGroupPrivate("test", -1); 103 EXPECT_EQ(pid, 100); 104 } 105 106 HWTEST_F(CgroupQosTest, SetCGroupPara_test, TestSize.Level1) 107 { 108 int a = 100; 109 OSAttrManager::Instance()->SetCGroupPara("/proc/cpuinfo", a); 110 EXPECT_EQ(a, 100); 111 } 112 113 HWTEST_F(CgroupQosTest, SetCGroupPara_err_test, TestSize.Level0) 114 { 115 #ifndef WITH_NO_MOCKER 116 MOCKER(write).stubs().will(returnValue(0)); 117 MOCKER(read).stubs().will(returnValue(0)); 118 #endif 119 int a = 3; 120 OSAttrManager::Instance()->SetCGroupPara("/proc/cpuinfo", a); 121 EXPECT_EQ(a, 3); 122 #ifndef WITH_NO_MOCKER 123 MOCKER(write).stubs().will(returnValue(-1)); 124 MOCKER(read).stubs().will(returnValue(0)); 125 #endif 126 OSAttrManager::Instance()->SetCGroupPara("/proc/cpuinfo", a); 127 #ifndef WITH_NO_MOCKER 128 MOCKER(write).stubs().will(returnValue(0)); 129 MOCKER(read).stubs().will(returnValue(33)); 130 #endif 131 OSAttrManager::Instance()->SetCGroupPara("/proc/cpuinfo", a); 132 } 133 134 class QosTest : public testing::Test { 135 protected: SetUpTestCase()136 static void SetUpTestCase() 137 { 138 } 139 TearDownTestCase()140 static void TearDownTestCase() 141 { 142 } 143 SetUp()144 void SetUp() override 145 { 146 } 147 TearDown()148 void TearDown() override 149 { 150 } 151 }; 152 153 HWTEST_F(QosTest, QosConfig_test, TestSize.Level0) 154 { 155 #ifndef WITH_NO_MOCKER 156 int i = 0; __anon07643f230102null157 auto handle = ffrt::submit_h([]{ 158 QosConfig::Instance().setPolicySystem(); 159 i++; 160 }); 161 EXPECT_EQ(i, 1); 162 #endif 163 } 164 165 class QosInterfaceTest2 : public testing::Test { 166 protected: SetUpTestCase()167 static void SetUpTestCase() 168 { 169 } 170 TearDownTestCase()171 static void TearDownTestCase() 172 { 173 } 174 SetUp()175 void SetUp() override 176 { 177 } 178 TearDown()179 void TearDown() override 180 { 181 } 182 }; 183 184 #if !defined(SCHED_RGT_CTRL_DNE) 185 HWTEST_F(QosInterfaceTest2, FFRTEnableRtgTest, TestSize.Level1) 186 { 187 bool flag = false; 188 FFRTEnableRtg(flag); 189 EXPECT_EQ(flag, false); 190 } 191 #endif 192 193 #if !defined(AUTH_CTRL_DNE) 194 HWTEST_F(QosInterfaceTest2, FFRTAuthEnableTest, TestSize.Level1) 195 { 196 unsigned int uid = 3039; 197 unsigned int uaFlag = 0x1fff; 198 unsigned int status = 3; 199 FFRTAuthEnable(uid, uaFlag, status); 200 EXPECT_EQ(status, 3); 201 } 202 203 HWTEST_F(QosInterfaceTest2, FFRTAuthSwitchTest, TestSize.Level1) 204 { 205 unsigned int uid = 3039; 206 unsigned int rtgFlag = 0x1fff; 207 unsigned int qosFlag = 0x0003; 208 unsigned int status = 3; 209 FFRTAuthSwitch(uid, rtgFlag, qosFlag, status); 210 EXPECT_EQ(status, 3); 211 } 212 213 HWTEST_F(QosInterfaceTest2, FFRTAuthDeleteTest, TestSize.Level1) 214 { 215 unsigned int uid = 3039; 216 FFRTAuthDelete(uid); 217 EXPECT_EQ(uid, 3039); 218 } 219 220 HWTEST_F(QosInterfaceTest2, FFRTAuthPauseTest, TestSize.Level1) 221 { 222 unsigned int uid = 3039; 223 unsigned int uaFlag = 0x1fff; 224 unsigned int status = 3; 225 FFRTAuthEnable(uid, uaFlag, status); 226 FFRTAuthPause(uid); 227 EXPECT_EQ(uid, 3039); 228 } 229 230 HWTEST_F(QosInterfaceTest2, FFRTAuthGetTest, TestSize.Level1) 231 { 232 unsigned int uid = 3039; 233 unsigned int uaFlag = 0x1fff; 234 unsigned int status = 3; 235 int ret = 0; 236 ret = FFRTAuthEnable(uid, uaFlag, status); 237 FFRTAuthGet(uid, &uaFlag, &status); 238 239 if (ret < 0) { 240 EXPECT_EQ(status, 0); 241 } else { 242 EXPECT_EQ(status, 3); 243 } 244 } 245 #endif 246 247 #if !defined(SCHED_QOS_CTRL_DNE) 248 HWTEST_F(QosInterfaceTest2, QosPolicyTest, TestSize.Level0) 249 { 250 struct QosPolicyData qp = {0, 0, 0, 0, 0}; 251 struct QosPolicyDatas policyDatas = {0, 0, {qp}}; 252 253 int ret = QosPolicy(&policyDatas); 254 EXPECT_NE(ret, 0); 255 } 256 257 HWTEST_F(QosInterfaceTest2, FFRTQosApplyTest, TestSize.Level0) 258 { 259 unsigned int level = 1; 260 261 FFRTQosApply(level); 262 EXPECT_EQ(level, 1); 263 } 264 265 HWTEST_F(QosInterfaceTest2, FFRTQosApplyForOtherTest, TestSize.Level1) 266 { 267 unsigned int level = 1; 268 int tid = 0; 269 270 FFRTQosApplyForOther(level, tid); 271 EXPECT_EQ(level, 1); 272 } 273 274 HWTEST_F(QosInterfaceTest2, FFRTQosLeaveTest, TestSize.Level1) 275 { 276 int ret = FFRTQosLeave(); 277 EXPECT_EQ(ret, 0); 278 } 279 280 HWTEST_F(QosInterfaceTest2, FFRTQosLeaveForOtherTest, TestSize.Level1) 281 { 282 unsigned int level = 1; 283 int tid = 0; 284 FFRTQosApplyForOther(level, tid); 285 286 FFRTQosLeaveForOther(tid); 287 EXPECT_EQ(level, 1); 288 } 289 #endif 290 HWTEST_F(QosInterfaceTest2, FFRTQosConvertInt, TestSize.Level1) 291 { 292 QoS qos1 = 1; 293 QoS qos2 = 2; 294 QoS qos3 = qos1 + qos2; 295 printf("qos3=%d", qos3()); 296 EXPECT_EQ(qos3, 3); 297 } 298