• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * SPDX-License-Identifier: GPL-2.0
4  *
5  * Unless required by applicable law or agreed to in writing, software
6  * distributed under the License is distributed on an "AS IS" BASIS,
7  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8  * See the License for the specific language governing permissions and
9  * limitations under the License.
10  */
11 
12 #include "rtg_test.h"
13 #include <cstdio>
14 #include <cstdlib>
15 #include <fcntl.h>
16 #include <cerrno>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/mman.h>
21 #include <sys/wait.h>
22 #include <sys/ioctl.h>
23 #include <ctime>
24 #include <climits>
25 #include <pthread.h>
26 #include <sys/syscall.h>
27 #include <securec.h>
28 #include <string>
29 #include <vector>
30 
31 using namespace testing::ext;
32 using namespace std;
33 
34 constexpr size_t MAX_LENGTH = 100;
35 constexpr size_t MAX_STR_LEN = 100;
36 const char RTG_SCHED_IPC_MAGIC = 0xAB;
37 const int RTG_ERR = -1;
38 const int RTG_SUCC = 0;
39 const int MAX_TID_NUM = 5;
40 const int MULTI_FRAME_NUM = 5;
41 
42 #define CMD_ID_SET_ENABLE \
43     _IOWR(RTG_SCHED_IPC_MAGIC, SET_ENABLE, struct RtgEnableData)
44 #define CMD_ID_SET_RTG \
45     _IOWR(RTG_SCHED_IPC_MAGIC, SET_RTG, struct RtgStrData)
46 #define CMD_ID_BEGIN_FRAME_FREQ \
47     _IOWR(RTG_SCHED_IPC_MAGIC, BEGIN_FRAME_FREQ, struct ProcStateData)
48 #define CMD_ID_END_FRAME_FREQ \
49     _IOWR(RTG_SCHED_IPC_MAGIC, END_FRAME_FREQ, struct ProcStateData)
50 #define CMD_ID_END_SCENE \
51     _IOWR(RTG_SCHED_IPC_MAGIC, END_SCENE, struct ProcStateData)
52 #define CMD_ID_SET_MIN_UTIL \
53     _IOWR(RTG_SCHED_IPC_MAGIC, SET_MIN_UTIL, struct ProcStateData)
54 #define CMD_ID_SET_MARGIN \
55     _IOWR(RTG_SCHED_IPC_MAGIC, SET_MARGIN, struct ProcStateData)
56 #define CMD_ID_LIST_RTG_THREAD \
57     _IOWR(RTG_SCHED_IPC_MAGIC, LIST_RTG_THREAD, struct RtgGrpData)
58 #define CMD_ID_LIST_RTG \
59     _IOWR(RTG_SCHED_IPC_MAGIC, LIST_RTG, struct RtgInfo)
60 #define CMD_ID_SET_RTG_ATTR \
61     _IOWR(RTG_SCHED_IPC_MAGIC, SET_RTG_ATTR, struct RtgStrData)
62 #define CMD_ID_SET_CONFIG \
63     _IOWR(RTG_SCHED_IPC_MAGIC, SET_CONFIG, struct RtgStrData)
64 
65 struct RtgEnableData {
66     int enable;
67     int len;
68     char *data;
69 };
70 
71 struct RtgStrData {
72     int type;
73     int len;
74     char *data;
75 };
76 
77 struct ProcStateData {
78     int grpId;
79     int stateParam;
80 };
81 
82 enum GrpCtrlCmd {
83     CMD_CREATE_RTG_GRP,
84     CMD_ADD_RTG_THREAD,
85     CMD_REMOVE_RTG_THREAD,
86     CMD_CLEAR_RTG_GRP,
87     CMD_DESTROY_RTG_GRP
88 };
89 
90 struct RtgGrpData {
91     int rtgCmd;
92     int grpId;
93     int prioType;
94     int rtCnt;
95     int tidNum;
96     int tids[MAX_TID_NUM];
97 };
98 
99 struct RtgInfo {
100     int rtgNum;
101     int rtgs[MULTI_FRAME_NUM];
102 };
103 
104 enum RtgSchedCmdid {
105     SET_ENABLE = 1,
106     SET_RTG,
107     SET_CONFIG,
108     SET_RTG_ATTR,
109     BEGIN_FRAME_FREQ = 5,
110     END_FRAME_FREQ,
111     END_SCENE,
112     SET_MIN_UTIL,
113     SET_MARGIN,
114     LIST_RTG = 10,
115     LIST_RTG_THREAD,
116     SEARCH_RTG,
117     GET_ENABLE,
118     RTG_CTRL_MAX_NR,
119 };
120 
121 enum RtgType : int {
122     VIP = 0,
123     TOP_TASK_KEY,
124     NORMAL_TASK,
125     RTG_TYPE_MAX,
126 };
127 
BasicOpenRtgNode()128 static int BasicOpenRtgNode()
129 {
130     char fileName[] = "/proc/self/sched_rtg_ctrl";
131     int fd = open(fileName, O_RDWR);
132     if (fd < 0) {
133         cout << "open node err." << endl;
134     }
135     return fd;
136 }
137 
EnableRtg(bool flag)138 static int EnableRtg(bool flag)
139 {
140     struct RtgEnableData enableData;
141     char configStr[] = "load_freq_switch:1;sched_cycle:1";
142 
143     enableData.enable = flag;
144     enableData.len = sizeof(configStr);
145     enableData.data = configStr;
146     int fd = BasicOpenRtgNode();
147     if (fd < 0) {
148         return RTG_ERR;
149     }
150     if (ioctl(fd, CMD_ID_SET_ENABLE, &enableData)) {
151         close(fd);
152         return RTG_ERR;
153     }
154 
155     close(fd);
156     return 0;
157 }
158 
CreateNewRtgGrp(int prioType,int rtNum)159 static int CreateNewRtgGrp(int prioType, int rtNum)
160 {
161     struct RtgGrpData grpData;
162     int ret;
163     int fd = BasicOpenRtgNode();
164     if (fd < 0) {
165         return RTG_ERR;
166     }
167     (void)memset_s(&grpData, sizeof(struct RtgGrpData), 0, sizeof(struct RtgGrpData));
168     if ((prioType > 0) && (prioType < RTG_TYPE_MAX)) {
169         grpData.prioType = prioType;
170     }
171     if (rtNum > 0) {
172         grpData.rtCnt = rtNum;
173     }
174     grpData.rtgCmd = CMD_CREATE_RTG_GRP;
175     ret = ioctl(fd, CMD_ID_SET_RTG, &grpData);
176 
177     close(fd);
178     return ret;
179 }
180 
DestroyRtgGrp(int grpId)181 static int DestroyRtgGrp(int grpId)
182 {
183     struct RtgGrpData grpData;
184     int ret;
185     int fd = BasicOpenRtgNode();
186     if (fd < 0) {
187         return fd;
188     }
189     (void)memset_s(&grpData, sizeof(struct RtgGrpData), 0, sizeof(struct RtgGrpData));
190     grpData.rtgCmd = CMD_DESTROY_RTG_GRP;
191     grpData.grpId = grpId;
192     ret = ioctl(fd, CMD_ID_SET_RTG, &grpData);
193 
194     close(fd);
195     return ret;
196 }
197 
AddThreadToRtg(int tid,int grpId,int prioType)198 static int AddThreadToRtg(int tid, int grpId, int prioType)
199 {
200     struct RtgGrpData grpData;
201     int ret;
202     int fd = BasicOpenRtgNode();
203     if (fd < 0) {
204         return fd;
205     }
206     (void)memset_s(&grpData, sizeof(struct RtgGrpData), 0, sizeof(struct RtgGrpData));
207     grpData.tidNum = 1;
208     grpData.tids[0] = tid;
209     grpData.grpId = grpId;
210     grpData.rtgCmd = CMD_ADD_RTG_THREAD;
211     grpData.prioType = prioType;
212     ret = ioctl(fd, CMD_ID_SET_RTG, &grpData);
213 
214     close(fd);
215     return ret;
216 }
217 
ClearRtgGrp(int grpId)218 static int ClearRtgGrp(int grpId)
219 {
220     struct RtgGrpData grpData;
221     int ret;
222     int fd = BasicOpenRtgNode();
223     if (fd < 0) {
224         return fd;
225     }
226     (void)memset_s(&grpData, sizeof(struct RtgGrpData), 0, sizeof(struct RtgGrpData));
227     grpData.rtgCmd = CMD_CLEAR_RTG_GRP;
228     grpData.grpId = grpId;
229     ret = ioctl(fd, CMD_ID_SET_RTG, &grpData);
230     if (ret < 0) {
231         return ret;
232     }
233 
234     close(fd);
235     return ret;
236 };
237 
BeginFrameFreq(int grpId,int stateParam)238 static int BeginFrameFreq(int grpId, int stateParam)
239 {
240     int ret = 0;
241     struct ProcStateData stateData;
242     stateData.grpId = grpId;
243     stateData.stateParam = stateParam;
244     int fd = BasicOpenRtgNode();
245     if (fd < 0) {
246         return fd;
247     }
248     ret = ioctl(fd, CMD_ID_BEGIN_FRAME_FREQ, &stateData);
249 
250     close(fd);
251     return ret;
252 }
253 
EndFrameFreq(int grpId)254 static int EndFrameFreq(int grpId)
255 {
256     int ret = 0;
257     struct ProcStateData stateData;
258     stateData.grpId = grpId;
259     stateData.stateParam = 0;
260     int fd = BasicOpenRtgNode();
261     if (fd < 0) {
262         return fd;
263     }
264     ret = ioctl(fd, CMD_ID_END_FRAME_FREQ, &stateData);
265 
266     close(fd);
267     return ret;
268 }
269 
EndScene(int grpId)270 static int EndScene(int grpId)
271 {
272     int ret = 0;
273     struct ProcStateData stateData;
274     stateData.grpId = grpId;
275 
276     int fd = BasicOpenRtgNode();
277     if (fd < 0) {
278         return fd;
279     }
280     ret = ioctl(fd, CMD_ID_END_SCENE, &stateData);
281     close(fd);
282     return ret;
283 }
284 
SetStateParam(unsigned int cmd,int grpId,int stateParam)285 static int SetStateParam(unsigned int cmd, int grpId, int stateParam)
286 {
287     int ret = 0;
288     struct ProcStateData stateData;
289     stateData.grpId = grpId;
290     stateData.stateParam = stateParam;
291 
292     int fd = BasicOpenRtgNode();
293     if (fd < 0) {
294         return fd;
295     }
296     ret = ioctl(fd, cmd, &stateData);
297 
298     close(fd);
299     return ret;
300 }
301 
302 
ListRtgThread(int grpId,vector<int> * rs)303 static int ListRtgThread(int grpId, vector<int> *rs)
304 {
305     int ret = 0;
306     struct RtgGrpData grpData;
307     int fd = BasicOpenRtgNode();
308     if (fd < 0) {
309         return fd;
310     }
311     if (!rs) {
312         return RTG_ERR;
313     }
314     (void)memset_s(&grpData, sizeof(struct RtgGrpData), 0, sizeof(struct RtgGrpData));
315     grpData.grpId = grpId;
316     ret = ioctl(fd, CMD_ID_LIST_RTG_THREAD, &grpData);
317     if (ret < 0) {
318         return ret;
319     } else {
320         rs->clear();
321         for (int i = 0; i < grpData.tidNum; i++) {
322             rs->push_back(grpData.tids[i]);
323         }
324     }
325 
326     close(fd);
327     return ret;
328 }
329 
ListRtgGroup(vector<int> * rs)330 static int ListRtgGroup(vector<int> *rs)
331 {
332     int ret = 0;
333     struct RtgInfo rtgInfo;
334     int fd = BasicOpenRtgNode();
335     if (fd < 0) {
336         return fd;
337     }
338     if (!rs) {
339         return RTG_ERR;
340     }
341     (void)memset_s(&rtgInfo, sizeof(struct RtgInfo), 0, sizeof(struct RtgInfo));
342     ret = ioctl(fd, CMD_ID_LIST_RTG, &rtgInfo);
343     if (ret < 0) {
344         return ret;
345     } else {
346         rs->clear();
347         for (int i = 0; i < rtgInfo.rtgNum; i++) {
348             rs->push_back(rtgInfo.rtgs[i]);
349         }
350     }
351 
352     close(fd);
353     return ret;
354 }
355 
SetFrameRateAndPrioType(int rtgId,int rate,int rtgType)356 static int SetFrameRateAndPrioType(int rtgId, int rate, int rtgType)
357 {
358     int ret = 0;
359     char strData[MAX_LENGTH] = {};
360     (void)sprintf_s(strData, sizeof(strData), "rtgId:%d;rate:%d;type:%d", rtgId, rate, rtgType);
361     struct RtgStrData rtgStrData;
362     rtgStrData.len = strlen(strData);
363     rtgStrData.data = strData;
364 
365     int fd = BasicOpenRtgNode();
366     if (fd < 0) {
367         return fd;
368     }
369     ret = ioctl(fd, CMD_ID_SET_RTG_ATTR, &rtgStrData);
370 
371     close(fd);
372     return ret;
373 }
374 
SetMaxVipRtgs(int rtframe)375 static int SetMaxVipRtgs(int rtframe)
376 {
377     int ret = 0;
378     char strData[MAX_STR_LEN] = {};
379     (void)sprintf_s(strData, sizeof(strData), "rtframe:%d", rtframe);
380     struct RtgStrData rtgStrData;
381     rtgStrData.len = strlen(strData);
382     rtgStrData.data = strData;
383 
384     int fd = BasicOpenRtgNode();
385     if (fd < 0) {
386         return fd;
387     }
388     ret = ioctl(fd, CMD_ID_SET_CONFIG, &rtgStrData);
389 
390     close(fd);
391     return ret;
392 }
393 
AddThreadsToRtg(vector<int> tids,int grpId,int prioType)394 static int AddThreadsToRtg(vector<int> tids, int grpId, int prioType)
395 {
396     struct RtgGrpData grpData;
397     int ret;
398     int fd = BasicOpenRtgNode();
399     if (fd < 0) {
400         return fd;
401     }
402     (void)memset_s(&grpData, sizeof(struct RtgGrpData), 0, sizeof(struct RtgGrpData));
403     int num = static_cast<int>(tids.size());
404     if (num > MAX_TID_NUM) {
405         return -1;
406     }
407     grpData.tidNum = num;
408     grpData.grpId = grpId;
409     grpData.rtgCmd = CMD_ADD_RTG_THREAD;
410     grpData.prioType = prioType;
411     for (int i = 0; i < num; i++) {
412         if (tids[i] < 0) {
413             return -1;
414         }
415         grpData.tids[i] = tids[i];
416     }
417     ret = ioctl(fd, CMD_ID_SET_RTG, &grpData);
418 
419     close(fd);
420     return ret;
421 }
422 
SetUp()423 void RtgTest::SetUp()
424 {
425     // must enable rtg before use the interface
426     int ret = EnableRtg(true);
427     ASSERT_EQ(RTG_SUCC, ret);
428 }
429 
TearDown()430 void RtgTest::TearDown()
431 {
432     // disable rtg after use the interface
433     int ret = EnableRtg(false);
434     ASSERT_EQ(RTG_SUCC, ret);
435 }
436 
SetUpTestCase()437 void RtgTest::SetUpTestCase() {}
438 
TearDownTestCase()439 void RtgTest::TearDownTestCase() {}
440 
441 /**
442  * @tc.name: setEnableSucc
443  * @tc.desc: Verify the enable rtg function.
444  * @tc.type: FUNC
445  */
446 HWTEST_F(RtgTest, setEnableSucc, Function | MediumTest | Level1)
447 {
448     int ret;
449 
450     // test set enable again
451     ret = EnableRtg(true);
452     ASSERT_EQ(RTG_SUCC, ret);
453 
454     // test set disable again
455     ret = EnableRtg(false);
456     ASSERT_EQ(RTG_SUCC, ret);
457 }
458 
459 /**
460  * @tc.name: createAndDestroyRtgSucc
461  * @tc.desc: Verify the create and destroy rtggrp function.
462  * @tc.type: FUNC
463  */
464 HWTEST_F(RtgTest, createAndDestroyRtgSucc, Function | MediumTest | Level1)
465 {
466     int ret;
467     int grpId;
468 
469     grpId = CreateNewRtgGrp(NORMAL_TASK, 0);
470     ASSERT_GT(grpId, 0);
471     ret = DestroyRtgGrp(grpId);
472     ASSERT_EQ(ret, 0);
473 }
474 
475 /**
476  * @tc.name: destoryErrorRtgGrp
477  * @tc.desc: Verify Destroy function with error param.
478  * @tc.type: FUNC
479  */
480 HWTEST_F(RtgTest, destoryErrorRtgGrp, Function | MediumTest | Level1)
481 {
482     int ret;
483     ret = DestroyRtgGrp(-1);
484     ASSERT_NE(RTG_SUCC, ret);
485 }
486 
487 /**
488  * @tc.name: addRtgGrpSucc
489  * @tc.desc: Verify add rtg function.
490  * @tc.type: FUNC
491  */
492 HWTEST_F(RtgTest, addRtgGrpSucc, Function | MediumTest | Level1)
493 {
494     int ret;
495     int grpId;
496     int pid = getpid();
497 
498     grpId = CreateNewRtgGrp(VIP, 0);
499     ASSERT_GT(grpId, 0);
500     ret = AddThreadToRtg(pid, grpId, VIP);
501     ASSERT_EQ(ret, 0);
502     ret = DestroyRtgGrp(grpId);
503     ASSERT_EQ(ret, 0);
504 }
505 
506 /**
507  * @tc.name: addRtgGrpFail
508  * @tc.desc: Verify add rtg function with error param.
509  * @tc.type: FUNC
510  */
511 HWTEST_F(RtgTest, addRtgGrpFail, Function | MediumTest | Level1)
512 {
513     int ret;
514     int grpId;
515     int pid = getpid();
516 
517     grpId = CreateNewRtgGrp(VIP, 0);
518     ASSERT_GT(grpId, 0);
519 
520     // error tid
521     ret = AddThreadToRtg(-1, grpId, VIP);
522     ASSERT_NE(ret, 0);
523 
524     // error grpid
525     ret = AddThreadToRtg(pid, -1, VIP);
526     ASSERT_NE(ret, RTG_SUCC);
527     ret = DestroyRtgGrp(grpId);
528     ASSERT_EQ(ret, 0);
529 }
530 
531 /**
532  * @tc.name: clearRtgSucc
533  * @tc.desc: Verify clear rtg function.
534  * @tc.type: FUNC
535  */
536 HWTEST_F(RtgTest, clearRtgSucc, Function | MediumTest | Level1)
537 {
538     int ret;
539     int grpId;
540     int pid = getpid();
541 
542     grpId = CreateNewRtgGrp(VIP, 0);
543     ASSERT_GT(grpId, 0);
544     ret = AddThreadToRtg(pid, grpId, VIP);
545     ASSERT_EQ(ret, RTG_SUCC);
546     ret = ClearRtgGrp(grpId);
547     ASSERT_EQ(ret, RTG_SUCC);
548     ret = DestroyRtgGrp(grpId);
549     ASSERT_EQ(ret, RTG_SUCC);
550 }
551 
552 /**
553  * @tc.name: clearRtgFail
554  * @tc.desc: Verify clear rtg function with error param.
555  * @tc.type: FUNC
556  */
557 HWTEST_F(RtgTest, clearRtgFail, Function | MediumTest | Level1)
558 {
559     int ret;
560 
561     ret = ClearRtgGrp(-1);
562     ASSERT_NE(ret, RTG_SUCC);
563 }
564 
565 /**
566  * @tc.name: begainFrameFreqSucc
567  * @tc.desc: Verify rtg frame start function.
568  * @tc.type: FUNC
569  */
570 HWTEST_F(RtgTest, begainFrameFreqSucc, Function | MediumTest | Level1)
571 {
572     int ret;
573     int grpId;
574 
575     grpId = CreateNewRtgGrp(VIP, 0);
576     ASSERT_GT(grpId, 0);
577     ret = BeginFrameFreq(grpId, 0);
578     ASSERT_EQ(ret, RTG_SUCC);
579     ret = DestroyRtgGrp(grpId);
580     ASSERT_EQ(ret, RTG_SUCC);
581 }
582 
583 /**
584  * @tc.name: begainFrameFreqFail
585  * @tc.desc: Verify rtg frame start function with error param.
586  * @tc.type: FUNC
587  */
588 HWTEST_F(RtgTest, begainFrameFreqFail, Function | MediumTest | Level1)
589 {
590     int ret;
591     ret = BeginFrameFreq(-1, 0);
592     ASSERT_NE(ret, RTG_SUCC);
593 }
594 
595 /**
596  * @tc.name: endFrameFreqSucc
597  * @tc.desc: Verify rtg frame end function.
598  * @tc.type: FUNC
599  */
600 HWTEST_F(RtgTest, endFrameFreqSucc, Function | MediumTest | Level1)
601 {
602     int ret;
603     int grpId;
604 
605     grpId = CreateNewRtgGrp(VIP, 0);
606     ASSERT_GT(grpId, 0);
607     ret = EndFrameFreq(grpId);
608     ASSERT_EQ(ret, RTG_SUCC);
609     ret = DestroyRtgGrp(grpId);
610     ASSERT_EQ(ret, RTG_SUCC);
611 }
612 
613 /**
614  * @tc.name: endFrameFreqFail
615  * @tc.desc: Verify rtg frame end function with error param.
616  * @tc.type: FUNC
617  */
618 HWTEST_F(RtgTest, endFrameFreqFail, Function | MediumTest | Level1)
619 {
620     int ret;
621     ret = EndFrameFreq(-1);
622     ASSERT_NE(ret, RTG_SUCC);
623 }
624 
625 /**
626  * @tc.name: endSceneSucc
627  * @tc.desc: Verify scene end function.
628  * @tc.type: FUNC
629  */
630 HWTEST_F(RtgTest, endSceneSucc, Function | MediumTest | Level1)
631 {
632     int ret;
633     int grpId;
634 
635     grpId = CreateNewRtgGrp(VIP, 0);
636     ASSERT_GT(grpId, 0);
637     ret = EndScene(grpId);
638     ASSERT_EQ(ret, RTG_SUCC);
639     ret = DestroyRtgGrp(grpId);
640     ASSERT_EQ(ret, RTG_SUCC);
641 }
642 
643 /**
644  * @tc.name: endSceneFail
645  * @tc.desc: Verify scene end function.
646  * @tc.type: FUNC
647  */
648 HWTEST_F(RtgTest, endSceneFail, Function | MediumTest | Level1)
649 {
650     int ret;
651 
652     ret = EndScene(-1);
653     ASSERT_NE(ret, RTG_SUCC);
654 }
655 
656 /**
657  * @tc.name: setMinUtilSucc
658  * @tc.desc: Verify set min util function.
659  * @tc.type: FUNC
660  */
661 HWTEST_F(RtgTest, setMinUtilSucc, Function | MediumTest | Level1)
662 {
663     int ret;
664     int grpId;
665 
666     grpId = CreateNewRtgGrp(VIP, 0);
667     ASSERT_GT(grpId, 0);
668     ret = SetStateParam(CMD_ID_SET_MIN_UTIL, grpId, 0);
669     ASSERT_EQ(ret, RTG_SUCC);
670     ret = DestroyRtgGrp(grpId);
671     ASSERT_EQ(ret, RTG_SUCC);
672 }
673 
674 /**
675  * @tc.name: setMinUtilFail
676  * @tc.desc: Verify set min util function with Error Param.
677  * @tc.type: FUNC
678  */
679 HWTEST_F(RtgTest, setMinUtilFail, Function | MediumTest | Level1)
680 {
681     int ret;
682 
683     ret = SetStateParam(CMD_ID_SET_MIN_UTIL, -1, 0);
684     ASSERT_NE(ret, RTG_SUCC);
685 }
686 
687 /**
688  * @tc.name: setMarginSucc
689  * @tc.desc: Verify set min margin function.
690  * @tc.type: FUNC
691  */
692 HWTEST_F(RtgTest, setMarginSucc, Function | MediumTest | Level1)
693 {
694     int ret;
695     int grpId;
696 
697     grpId = CreateNewRtgGrp(VIP, 0);
698     ASSERT_GT(grpId, 0);
699     ret = SetStateParam(CMD_ID_SET_MARGIN, grpId, 0);
700     ASSERT_EQ(ret, RTG_SUCC);
701     ret = DestroyRtgGrp(grpId);
702     ASSERT_EQ(ret, RTG_SUCC);
703 }
704 
705 /**
706  * @tc.name: setMarginFail
707  * @tc.desc: Verify set min margin function with error param.
708  * @tc.type: FUNC
709  */
710 HWTEST_F(RtgTest, setMarginFail, Function | MediumTest | Level1)
711 {
712     int ret;
713 
714     ret = SetStateParam(CMD_ID_SET_MARGIN, -1, 0);
715     ASSERT_NE(ret, RTG_SUCC);
716 }
717 
718 /**
719  * @tc.name: listRtgThreadSucc
720  * @tc.desc: Verify list rtg thread function.
721  * @tc.type: FUNC
722  */
723 HWTEST_F(RtgTest, ListRtgThreadSucc, Function | MediumTest | Level1)
724 {
725     int ret;
726     int grpId;
727     vector<int> rs;
728 
729     grpId = CreateNewRtgGrp(VIP, 0);
730     ASSERT_GT(grpId, 0);
731     ret = ListRtgThread(grpId, &rs);
732     ASSERT_EQ(ret, 0);
733     ret = DestroyRtgGrp(grpId);
734     ASSERT_EQ(ret, 0);
735 }
736 
737 /**
738  * @tc.name: listRtgThreadFail
739  * @tc.desc: Verify list rtg thread function with null vector input.
740  * @tc.type: FUNC
741  */
742 HWTEST_F(RtgTest, ListRtgThreadFail, Function | MediumTest | Level1)
743 {
744     int ret;
745     int grpId;
746     vector<int> rs;
747 
748     grpId = CreateNewRtgGrp(VIP, 0);
749     ASSERT_GT(grpId, 0);
750     ret = ListRtgThread(grpId, nullptr);
751     ASSERT_NE(ret, RTG_SUCC);
752     ret = ListRtgThread(-1, &rs);
753     ASSERT_NE(ret, RTG_SUCC);
754     ret = DestroyRtgGrp(grpId);
755     ASSERT_EQ(ret, RTG_SUCC);
756 }
757 
758 /**
759  * @tc.name: listRtgSucc
760  * @tc.desc: Verify list rtg function.
761  * @tc.type: FUNC
762  */
763 HWTEST_F(RtgTest, ListRtgSucc, Function | MediumTest | Level1)
764 {
765     int ret;
766     vector<int> rs;
767 
768     ret = ListRtgGroup(&rs);
769     ASSERT_EQ(ret, RTG_SUCC);
770 }
771 
772 /**
773  * @tc.name: listRtgFail
774  * @tc.desc: Verify list rtg function with error param.
775  * @tc.type: FUNC
776  */
777 HWTEST_F(RtgTest, ListRtgFail, Function | MediumTest | Level1)
778 {
779     int ret;
780 
781     ret = ListRtgGroup(nullptr);
782     ASSERT_NE(ret, RTG_SUCC);
783 }
784 
785 /**
786  * @tc.name: SetRtgAttrSucc
787  * @tc.desc: Verify rtg attr set function.
788  * @tc.type: FUNC
789  */
790 HWTEST_F(RtgTest, SetRtgAttrSucc, Function | MediumTest | Level1)
791 {
792     int ret;
793     int grpId;
794 
795     grpId = CreateNewRtgGrp(VIP, 0);
796     ASSERT_GT(grpId, 0);
797     ret = SetFrameRateAndPrioType(grpId, 60, VIP);
798     ASSERT_EQ(ret, RTG_SUCC);
799     ret = DestroyRtgGrp(grpId);
800     ASSERT_EQ(ret, RTG_SUCC);
801 }
802 
803 /**
804  * @tc.name: SetRtgAttrFail
805  * @tc.desc: Verify rtg attr set function with error param.
806  * @tc.type: FUNC
807  */
808 HWTEST_F(RtgTest, SetRtgAttrFail, Function | MediumTest | Level1)
809 {
810     int ret;
811     int grpId;
812     grpId = CreateNewRtgGrp(VIP, 0);
813     ASSERT_GT(grpId, 0);
814     ret = SetFrameRateAndPrioType(grpId, 90, -1);
815     ASSERT_NE(ret, RTG_SUCC);
816     ret = DestroyRtgGrp(grpId);
817     ASSERT_EQ(ret, RTG_SUCC);
818 }
819 
820 /**
821  * @tc.name: SetMaxVipRtgSucc
822  * @tc.desc: Verify rtg max vip num set function.
823  * @tc.type: FUNC
824  */
825 HWTEST_F(RtgTest, SetMaxVipRtgSucc, Function | MediumTest | Level1)
826 {
827     int ret;
828 
829     ret = SetMaxVipRtgs(2);
830     ASSERT_EQ(ret, RTG_SUCC);
831 }
832 
833 /**
834  * @tc.name: SetMaxVipRtgFail
835  * @tc.desc: Verify rtg max vip num set function with error param.
836  * @tc.type: FUNC
837  */
838 HWTEST_F(RtgTest, SetMaxVipRtgFail, Function | MediumTest | Level1)
839 {
840     int ret;
841 
842     // set 0 vip num
843     ret = SetMaxVipRtgs(0);
844     ASSERT_NE(ret, RTG_SUCC);
845 
846     // set large vip num
847     ret = SetMaxVipRtgs(50000);
848     ASSERT_NE(ret, RTG_SUCC);
849 }
850 
851 /**
852  * @tc.name: RtgAddMutipleThreadsSucc
853  * @tc.desc: Verify rtg multiple add function.
854  * @tc.type: FUNC
855  */
856 HWTEST_F(RtgTest, RtgAddMutipleThreadsSucc, Function | MediumTest | Level1)
857 {
858     int ret;
859     int pid[3];
860     vector<int> threads;
861     int grpId;
862 
863     for (int i = 0; i < 3; i++) {
864         pid[i] = fork();
865         ASSERT_TRUE(pid[i] >= 0) << "> parent: fork errno = " << errno;
866         if (pid[i] == 0) {
867             usleep(50000);
868             _Exit(0);
869         }
870         threads.push_back(pid[i]);
871     }
872     grpId = CreateNewRtgGrp(NORMAL_TASK, 0);
873     ASSERT_GT(grpId, 0);
874     ret = AddThreadsToRtg(threads, grpId, NORMAL_TASK);
875     ASSERT_EQ(ret, RTG_SUCC);
876     ret = DestroyRtgGrp(grpId);
877     ASSERT_EQ(ret, RTG_SUCC);
878 }
879 
880 /**
881  * @tc.name: RtgAddMutipleThreadsOutOfLimit
882  * @tc.desc: Verify rtg multiple add function with out of limit threads.
883  * @tc.type: FUNC
884  */
885 HWTEST_F(RtgTest, RtgAddMutipleThreadsOutOfLimit, Function | MediumTest | Level1)
886 {
887     int ret;
888     int pid[8];
889     vector<int> threads;
890     int grpId;
891 
892     for (int i = 0; i < 8; i++) {
893         pid[i] = fork();
894         ASSERT_TRUE(pid[i] >= 0) << "> parent: fork errno = " << errno;
895         if (pid[i] == 0) {
896             usleep(50000);
897             _Exit(0);
898         }
899     threads.push_back(pid[i]);
900     }
901     grpId = CreateNewRtgGrp(NORMAL_TASK, 0);
902     ASSERT_GT(grpId, 0);
903     ret = AddThreadsToRtg(threads, grpId, NORMAL_TASK);
904     ASSERT_NE(ret, RTG_SUCC);
905     ret = DestroyRtgGrp(grpId);
906     ASSERT_EQ(ret, RTG_SUCC);
907 }
908