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 "ffrt_inner.h"
18 #include "util.h"
19 #include "c/deadline.h"
20 #include "c/executor_task.h"
21 #include "tm/scpu_task.h"
22 #include "dfx/log/ffrt_log_api.h"
23 #include "../common.h"
24 
25 #ifndef WITH_NO_MOCKER
26 extern "C" int ffrt_set_cgroup_attr(ffrt_qos_t qos, ffrt_os_sched_attr *attr);
27 #endif
28 using namespace std;
29 using namespace testing;
30 #ifdef HWTEST_TESTING_EXT_ENABLE
31 using namespace testing::ext;
32 #endif
33 
34 class DependencyTest : public testing::Test {
35 protected:
SetUpTestCase()36     static void SetUpTestCase()
37     {
38     }
39 
TearDownTestCase()40     static void TearDownTestCase()
41     {
42     }
43 
SetUp()44     virtual void SetUp()
45     {
46     }
47 
TearDown()48     virtual void TearDown()
49     {
50     }
51 };
52 
53 HWTEST_F(DependencyTest, dependency_success_01, TestSize.Level1)
54 {
55     int x = 0;
__anonf988f8560202() 56     ffrt::submit([&]() { x = 2; }, {}, {&x});
__anonf988f8560602() 57     ffrt::submit([&]() { x = x * 3; }, {&x}, {});
58     ffrt::wait();
59     EXPECT_EQ(x, 6);
60 }
61 
62 HWTEST_F(DependencyTest, update_qos_success_02, TestSize.Level1)
63 {
64     int ret = ffrt_task_attr_init(nullptr);
65     EXPECT_EQ(ret, -1);
66     ffrt_task_attr_get_name(nullptr);
67     ffrt_task_attr_set_name(nullptr, "A");
68     ffrt_task_attr_set_qos(nullptr, static_cast<int>(ffrt::qos_user_initiated));
69     ffrt_task_attr_get_qos(nullptr);
70     ffrt_task_attr_destroy(nullptr);
71     ffrt_submit_base(nullptr, nullptr, nullptr, nullptr);
72     ffrt_submit_h_base(nullptr, nullptr, nullptr, nullptr);
__anonf988f8560702null73     ffrt::submit([] {
74         printf("return %d\n", ffrt::this_task::update_qos(static_cast<int>(ffrt::qos_user_initiated)));
75     });
76     ffrt_this_task_get_id();
77     ffrt::wait();
78     ffrt_this_task_update_qos(static_cast<int>(ffrt::qos_user_initiated));
79 #ifndef WITH_NO_MOCKER
80     ffrt_set_cgroup_attr(static_cast<int>(ffrt::qos_user_initiated), nullptr);
81 #endif
82 }
83 
84 HWTEST_F(DependencyTest, update_trace_tag_success_02, TestSize.Level1)
85 {
86     ffrt::set_trace_tag("TASK A");
87     ffrt::clear_trace_tag();
88 }
89 
90 HWTEST_F(DependencyTest, task_attr_success_02, TestSize.Level1)
91 {
92     ffrt::task_attr tmpTask;
93     tmpTask.name("Task A");
94     tmpTask.qos(static_cast<int>(ffrt::qos_user_initiated));
95     tmpTask.name();
96     tmpTask.qos();
97 }
98 
99 HWTEST_F(DependencyTest, sample_pingpong_pipe_interval_checkpoint, TestSize.Level1)
100 {
101     int loops = 5;
102     int frame_num = 2;
103 
104     if (getenv("LOOP_NUM")) {
105         loops = atoi(getenv("LOOP_NUM"));
106     }
107     if (getenv("FRAME_NUM")) {
108         frame_num = atoi(getenv("FRAME_NUM"));
109     }
110 
__anonf988f8560a02() 111     ffrt::submit([&]() { stall_us(10); }, {}, {});
112 
113     auto it = ffrt::qos_interval_create(16, static_cast<int>(ffrt::qos_user_interactive));
114     for (int loop = 0; loop < loops; loop++) {
115         constexpr int FRAME_NUM = 3;
116         constexpr uint32_t BUFFER_NUM = 2;
117         int x0[FRAME_NUM];
118         int x1[BUFFER_NUM];
119         int x2[BUFFER_NUM];
120         int x3[FRAME_NUM];
121 
122         int stalls[10][4] = {
123             {2000, 6000, 8000, 8000 + 6000 + 2000}, // 0
124             {2125, 6375, 8500, 8500 + 6375 + 2125}, // 1
125             {2222, 6666, 8888, 8888 + 6666 + 2222}, // 2
126             {2250, 6750, 9000, 9000 + 6750 + 2250}, // 3
127             {2375, 7125, 9500, 9500 + 7125 + 2375}, // 4
128             {2500, 7500, 10000, 10000 + 7500 + 2500}, // 5
129             {1875, 5625, 7500, 7500 + 5625 + 1875}, // 6
130             {1750, 5250, 7000, 7000 + 5250 + 1750}, // 7
131             {1625, 4875, 6500, 6500 + 4875 + 1625}, // 8
132             {1500, 4500, 6000, 6000 + 4500 + 1500}, // 9
133         };
134 
135         auto start = std::chrono::system_clock::now();
136         ffrt::qos_interval_begin(it);
137         for (int i = 0; i < frame_num; i++) {
138             int pingpong = i % BUFFER_NUM;
139             // task A
140             ffrt::submit(
__anonf988f8560b02() 141                 [i, loop, stalls]() {
142                     FFRT_LOGI("%u", i);
143                 },
__anonf988f8560c02() 144                 {x0 + i}, {x1 + pingpong}, ffrt::task_attr().name(("UI" + std::to_string(i)).c_str()));
145             // task B
146             ffrt::submit(
__anonf988f8560e02() 147                 [i, loop, stalls]() {
148                     FFRT_LOGI("%u", i);
149                 },
__anonf988f8561002() 150                 {x1 + pingpong}, {x2 + pingpong}, ffrt::task_attr().name(("Render" + std::to_string(i)).c_str()));
151             // task C
152             ffrt::submit(
__anonf988f8561102() 153                 [i, loop, stalls]() {
154                     FFRT_LOGI("%u", i);
155                 },
__anonf988f8561302() 156                 {x2 + pingpong}, {x3 + i}, ffrt::task_attr().name(("surfaceflinger" + std::to_string(i)).c_str()));
157         }
158         ffrt::wait();
159         ffrt::qos_interval_end(it);
160     }
161 
162     ffrt::qos_interval_destroy(it);
163 }
164