• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Level1)
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.Level1)
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.Level1)
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.Level1)
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.Level1)
154 {
155 #ifndef WITH_NO_MOCKER
156     int i = 0;
__anonc4fd61810102null157     auto handle = ffrt::submit_h([]{
158         QosConfig::Instance().setPolicySystem();
159         i++;
160     });
161     EXPECT_EQ(i, 1);
162 #endif
163 }
164 
165 class QosInterfaceTest : 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 HWTEST_F(QosInterfaceTest, QosPolicyTest, TestSize.Level1)
185 {
186     struct QosPolicyData qp = {0, 0, 0, 0, 0};
187     struct QosPolicyDatas policyDatas = {0, 0, {qp}};
188 
189     int ret = QosPolicy(&policyDatas);
190     EXPECT_NE(ret, 0);
191 }
192 
193 HWTEST_F(QosInterfaceTest, FFRTEnableRtgTest, TestSize.Level1)
194 {
195     bool flag = false;
196     FFRTEnableRtg(flag);
197     EXPECT_EQ(flag, false);
198 }
199 
200 HWTEST_F(QosInterfaceTest, FFRTAuthEnableTest, TestSize.Level1)
201 {
202     unsigned int uid = 3039;
203     unsigned int uaFlag = 0x1fff;
204     unsigned int status = 3;
205     FFRTAuthEnable(uid, uaFlag, status);
206     EXPECT_EQ(status, 3);
207 }
208 
209 HWTEST_F(QosInterfaceTest, FFRTAuthSwitchTest, TestSize.Level1)
210 {
211     unsigned int uid = 3039;
212     unsigned int rtgFlag = 0x1fff;
213     unsigned int qosFlag = 0x0003;
214     unsigned int status = 3;
215     FFRTAuthSwitch(uid, rtgFlag, qosFlag, status);
216     EXPECT_EQ(status, 3);
217 }
218 
219 HWTEST_F(QosInterfaceTest, FFRTAuthDeleteTest, TestSize.Level1)
220 {
221     unsigned int uid = 3039;
222     FFRTAuthDelete(uid);
223     EXPECT_EQ(uid, 3039);
224 }
225 
226 HWTEST_F(QosInterfaceTest, FFRTAuthPauseTest, TestSize.Level1)
227 {
228     unsigned int uid = 3039;
229     unsigned int uaFlag = 0x1fff;
230     unsigned int status = 3;
231     FFRTAuthEnable(uid, uaFlag, status);
232     FFRTAuthPause(uid);
233     EXPECT_EQ(uid, 3039);
234 }
235 
236 HWTEST_F(QosInterfaceTest, FFRTAuthGetTest, TestSize.Level1)
237 {
238     unsigned int uid = 3039;
239     unsigned int uaFlag = 0x1fff;
240     unsigned int status = 3;
241     int ret = 0;
242     ret = FFRTAuthEnable(uid, uaFlag, status);
243     FFRTAuthGet(uid, &uaFlag, &status);
244 
245     if (ret < 0) {
246         EXPECT_EQ(status, 0);
247     } else {
248         EXPECT_EQ(status, 3);
249     }
250 }
251 
252 HWTEST_F(QosInterfaceTest, FFRTQosApplyTest, TestSize.Level1)
253 {
254     unsigned int level = 1;
255 
256     FFRTQosApply(level);
257     EXPECT_EQ(level, 1);
258 }
259 
260 HWTEST_F(QosInterfaceTest, FFRTQosApplyForOtherTest, TestSize.Level1)
261 {
262     unsigned int level = 1;
263     int tid = 0;
264 
265     FFRTQosApplyForOther(level, tid);
266     EXPECT_EQ(level, 1);
267 }
268 
269 HWTEST_F(QosInterfaceTest, FFRTQosLeaveTest, TestSize.Level1)
270 {
271     int ret = FFRTQosLeave();
272     EXPECT_EQ(ret, 0);
273 }
274 
275 HWTEST_F(QosInterfaceTest, FFRTQosLeaveForOtherTest, TestSize.Level1)
276 {
277     unsigned int level = 1;
278     int tid = 0;
279     FFRTQosApplyForOther(level, tid);
280 
281     FFRTQosLeaveForOther(tid);
282     EXPECT_EQ(level, 1);
283 }
284 
285 HWTEST_F(QosInterfaceTest, FFRTQosConvertInt, TestSize.Level1)
286 {
287     QoS qos1 = 1;
288     QoS qos2 = 2;
289     QoS qos3 = qos1 + qos2;
290     printf("qos3=%d", qos3());
291     EXPECT_EQ(qos3, 3);
292 }