• 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 <vector>
17 #include <mutex>
18 #include <thread>
19 #include <condition_variable>
20 #include <atomic>
21 
22 #include <gtest/gtest.h>
23 
24 #include <sched/workgroup_internal.h>
25 #include "eu/rtg_ioctl.h"
26 #include "dfx/log/ffrt_log_api.h"
27 
28 #ifdef QOS_WORKER_FRAME_RTG
29 #include "sched/task_client_adapter.h"
30 #endif
31 
32 #ifndef WITH_NO_MOCKER
33 #include <mockcpp/mockcpp.hpp>
34 #endif
35 
36 using namespace ffrt;
37 
38 class MultiWorkgroupTest : public testing::Test {
39 protected:
SetUpTestCase()40     static void SetUpTestCase()
41     {
42     }
43 
TearDownTestCase()44     static void TearDownTestCase()
45     {
46     }
47 
SetUp()48     virtual void SetUp()
49     {
50     // x86环境下setuid是不生效的
51 #ifndef WITH_NO_MOCKER
52         MOCKER(getuid)
53             .stubs()
54             .will(returnValue(RS_UID));
55 #else
56         (void)setuid(RS_UID);
57 #endif
58         FFRT_LOGI("test,current uid:%d\n", getuid());
59     }
60 
TearDown()61     virtual void TearDown()
62     {
63     }
64 };
65 
66 #ifdef QOS_WORKER_FRAME_RTG
67 /**
68  * @tc.name: Compare_RS_UID
69  * @tc.desc: 看护ffrt中定义的RS_UID和QOS层的RS_UID必须相同
70  * @tc.type: FUNC
71  */
TEST_F(MultiWorkgroupTest,Compare_RS_UID)72 TEST_F(MultiWorkgroupTest, Compare_RS_UID)
73 {
74     FFRT_LOGI("testcase,current uid:%d\n", getuid());
75     FFRT_LOGI("Compare_RS_UID enter, 1\n");
76     IntervalReply rs;
77     rs.rtgId = -1;
78     rs.tid = -1;
79     CTC_QUERY_INTERVAL(QUERY_RENDER_SERVICE, rs);
80     FFRT_LOGI("Compare_RS_UID exit, rtgId:%d\n", rs.rtgId);
81     EXPECT_GT(rs.rtgId, 0u);
82 }
83 
84 /**
85  * @tc.name: RS_JoinWGTest
86  * @tc.desc: 看护 JoinWG 中加入RS的WG分组
87  * @tc.type: FUNC
88  */
TEST_F(MultiWorkgroupTest,RS_JoinWGTest)89 TEST_F(MultiWorkgroupTest, RS_JoinWGTest)
90 {
91     int qos = 6;
92     int tid = gettid();
93     {
94         FFRT_LOGI("wg not created, join wg with false return; uid:%d,tid:%d", getuid(), gettid());
95         bool ret = JoinWG(tid, qos);
96         EXPECT_FALSE(ret);
97     }
98     {
99         WorkGroup* workGroup = WorkgroupCreate(8333, qos);
100         FFRT_LOGI("wg created, first join wg with true return;");
101         bool ret = JoinWG(tid, qos);
102         FFRT_LOGI("wg tids:0:%d,1:%d\n", workGroup->tids[0], workGroup->tids[1]);
103         EXPECT_TRUE(ret);
104         EXPECT_TRUE(workGroup->tids[0] == tid);
105         EXPECT_TRUE(workGroup->tids[1] == -1);
106         FFRT_LOGI("join again, with true return,valid tids remain 1\n");
107         ret = JoinWG(tid, qos);
108         EXPECT_TRUE(ret);
109         EXPECT_TRUE(workGroup->tids[0] == tid);
110         EXPECT_TRUE(workGroup->tids[1] == -1);
111         LeaveWG(tid, qos);
112         WorkgroupClear(workGroup);
113     }
114 }
115 
TEST_F(MultiWorkgroupTest,RS_LeaveWGTest)116 TEST_F(MultiWorkgroupTest, RS_LeaveWGTest)
117 {
118     int qos = 6;
119     int tid = gettid();
120     {
121         // leave before join
122         FFRT_LOGI("wg not created,LeaveWG with false return\n");
123         bool ret = LeaveWG(tid, qos);
124         EXPECT_FALSE(ret);
125     }
126     WorkGroup* workGroup = WorkgroupCreate(8333, qos);
127     {
128         // leave after join
129         FFRT_LOGI("wg created,LeaveWG directly with true return and tids clean\n");
130         bool ret = LeaveWG(tid, qos);
131         EXPECT_TRUE(workGroup->tids[0] == -1);
132         EXPECT_TRUE(ret);
133     }
134     {
135         FFRT_LOGI("wg created, join first, and then leave with true return\n");
136         bool ret = JoinWG(tid, qos);
137         EXPECT_TRUE(ret);
138         EXPECT_TRUE(workGroup->tids[0] == tid);
139         EXPECT_TRUE(workGroup->tids[1] == -1);
140         ret = LeaveWG(tid, qos);
141         EXPECT_TRUE(workGroup->tids[0] == -1);
142         EXPECT_TRUE(ret);
143     }
144 }
145 
TEST_F(MultiWorkgroupTest,RS_WorkgroupCreateTest)146 TEST_F(MultiWorkgroupTest, RS_WorkgroupCreateTest)
147 {
148     int qos = 6;
149     int tid = gettid();
150     FFRT_LOGI("first create with not nullptr return\n");
151     WorkGroup* workGroup = WorkgroupCreate(8333, qos);
152     EXPECT_TRUE(workGroup != nullptr);
153     EXPECT_TRUE(workGroup->rtgId > 0);
154     FFRT_LOGI("again create with same ptr return\n");
155     WorkGroup* workGroup2 = WorkgroupCreate(8333, qos);
156     EXPECT_TRUE(workGroup == workGroup2);
157     WorkgroupClear(workGroup);
158 }
159 
TEST_F(MultiWorkgroupTest,RS_WorkgroupClearTest)160 TEST_F(MultiWorkgroupTest, RS_WorkgroupClearTest)
161 {
162     int qos = 6;
163     int tid = gettid();
164     {
165         FFRT_LOGI("[WorkgroupClear] wg created, return true\n");
166         WorkGroup* workGroup = WorkgroupCreate(8333, qos);
167         bool ret = WorkgroupClear(workGroup);
168         EXPECT_TRUE(ret);
169     }
170 }
171 
172 #endif