1484543d1Sopenharmony_ci/* 2484543d1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3484543d1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4484543d1Sopenharmony_ci * you may not use this file except in compliance with the License. 5484543d1Sopenharmony_ci * You may obtain a copy of the License at 6484543d1Sopenharmony_ci * 7484543d1Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8484543d1Sopenharmony_ci * 9484543d1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10484543d1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11484543d1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12484543d1Sopenharmony_ci * See the License for the specific language governing permissions and 13484543d1Sopenharmony_ci * limitations under the License. 14484543d1Sopenharmony_ci */ 15484543d1Sopenharmony_ci 16484543d1Sopenharmony_ci#include <gtest/gtest.h> 17484543d1Sopenharmony_ci#include <cinttypes> 18484543d1Sopenharmony_ci#include "ffrt_inner.h" 19484543d1Sopenharmony_ci#include "util.h" 20484543d1Sopenharmony_ci#include "c/deadline.h" 21484543d1Sopenharmony_ci#include "c/executor_task.h" 22484543d1Sopenharmony_ci#include "tm/scpu_task.h" 23484543d1Sopenharmony_ci#include "dfx/log/ffrt_log_api.h" 24484543d1Sopenharmony_ci#ifndef WITH_NO_MOCKER 25484543d1Sopenharmony_ciextern "C" int ffrt_set_cgroup_attr(ffrt_qos_t qos, ffrt_os_sched_attr *attr); 26484543d1Sopenharmony_ci#endif 27484543d1Sopenharmony_ci#include "../common.h" 28484543d1Sopenharmony_ci 29484543d1Sopenharmony_ciusing namespace std; 30484543d1Sopenharmony_ciusing namespace testing; 31484543d1Sopenharmony_ci#ifdef HWTEST_TESTING_EXT_ENABLE 32484543d1Sopenharmony_ciusing namespace testing::ext; 33484543d1Sopenharmony_ci#endif 34484543d1Sopenharmony_ci 35484543d1Sopenharmony_ciclass DependencyTest : public testing::Test { 36484543d1Sopenharmony_ciprotected: 37484543d1Sopenharmony_ci static void SetUpTestCase() 38484543d1Sopenharmony_ci { 39484543d1Sopenharmony_ci } 40484543d1Sopenharmony_ci 41484543d1Sopenharmony_ci static void TearDownTestCase() 42484543d1Sopenharmony_ci { 43484543d1Sopenharmony_ci } 44484543d1Sopenharmony_ci 45484543d1Sopenharmony_ci virtual void SetUp() 46484543d1Sopenharmony_ci { 47484543d1Sopenharmony_ci } 48484543d1Sopenharmony_ci 49484543d1Sopenharmony_ci virtual void TearDown() 50484543d1Sopenharmony_ci { 51484543d1Sopenharmony_ci } 52484543d1Sopenharmony_ci}; 53484543d1Sopenharmony_ci 54484543d1Sopenharmony_ciHWTEST_F(DependencyTest, dependency_success_01, TestSize.Level1) 55484543d1Sopenharmony_ci{ 56484543d1Sopenharmony_ci int x = 0; 57484543d1Sopenharmony_ci ffrt::submit([&]() { x = 2; }, {}, {&x}); 58484543d1Sopenharmony_ci ffrt::submit([&]() { x = x * 3; }, {&x}, {}); 59484543d1Sopenharmony_ci ffrt::wait(); 60484543d1Sopenharmony_ci EXPECT_EQ(x, 6); 61484543d1Sopenharmony_ci} 62484543d1Sopenharmony_ci 63484543d1Sopenharmony_ciHWTEST_F(DependencyTest, update_qos_success_02, TestSize.Level1) 64484543d1Sopenharmony_ci{ 65484543d1Sopenharmony_ci int ret = ffrt_task_attr_init(nullptr); 66484543d1Sopenharmony_ci EXPECT_EQ(ret, -1); 67484543d1Sopenharmony_ci ffrt_task_attr_get_name(nullptr); 68484543d1Sopenharmony_ci ffrt_task_attr_set_name(nullptr, "A"); 69484543d1Sopenharmony_ci ffrt_task_attr_set_qos(nullptr, static_cast<int>(ffrt::qos_user_initiated)); 70484543d1Sopenharmony_ci ffrt_task_attr_get_qos(nullptr); 71484543d1Sopenharmony_ci ffrt_task_attr_destroy(nullptr); 72484543d1Sopenharmony_ci ffrt_submit_base(nullptr, nullptr, nullptr, nullptr); 73484543d1Sopenharmony_ci ffrt_submit_h_base(nullptr, nullptr, nullptr, nullptr); 74484543d1Sopenharmony_ci ffrt::submit([] { 75484543d1Sopenharmony_ci printf("return %d\n", ffrt::this_task::update_qos(static_cast<int>(ffrt::qos_user_initiated))); 76484543d1Sopenharmony_ci printf("id is %" PRIu64 "\n", ffrt::this_task::get_id()); 77484543d1Sopenharmony_ci }); 78484543d1Sopenharmony_ci ffrt_this_task_get_id(); 79484543d1Sopenharmony_ci ffrt::wait(); 80484543d1Sopenharmony_ci ffrt_this_task_update_qos(static_cast<int>(ffrt::qos_user_initiated)); 81484543d1Sopenharmony_ci#ifndef WITH_NO_MOCKER 82484543d1Sopenharmony_ci ffrt_set_cgroup_attr(static_cast<int>(ffrt::qos_user_initiated), nullptr); 83484543d1Sopenharmony_ci#endif 84484543d1Sopenharmony_ci} 85484543d1Sopenharmony_ci 86484543d1Sopenharmony_ciHWTEST_F(DependencyTest, update_qos_success_03, TestSize.Level1) 87484543d1Sopenharmony_ci{ 88484543d1Sopenharmony_ci int ret = ffrt_task_attr_init(nullptr); 89484543d1Sopenharmony_ci EXPECT_EQ(ret, -1); 90484543d1Sopenharmony_ci ffrt::submit([] { 91484543d1Sopenharmony_ci printf("return %d\n", ffrt::this_task::update_qos(static_cast<int>(ffrt::qos_user_initiated))); 92484543d1Sopenharmony_ci }); 93484543d1Sopenharmony_ci ffrt_restore_qos_config(); 94484543d1Sopenharmony_ci} 95484543d1Sopenharmony_ci 96484543d1Sopenharmony_ciHWTEST_F(DependencyTest, update_qos_success_04, TestSize.Level1) 97484543d1Sopenharmony_ci{ 98484543d1Sopenharmony_ci int ret = ffrt_task_attr_init(nullptr); 99484543d1Sopenharmony_ci EXPECT_EQ(ret, -1); 100484543d1Sopenharmony_ci ffrt::submit([] { 101484543d1Sopenharmony_ci printf("return %d\n", ffrt::this_task::update_qos(static_cast<int>(ffrt::qos_user_initiated))); 102484543d1Sopenharmony_ci }); 103484543d1Sopenharmony_ci} 104484543d1Sopenharmony_ci 105484543d1Sopenharmony_ciHWTEST_F(DependencyTest, update_qos_success_05, TestSize.Level1) 106484543d1Sopenharmony_ci{ 107484543d1Sopenharmony_ci int x = 0; 108484543d1Sopenharmony_ci int ret = ffrt_task_attr_init(nullptr); 109484543d1Sopenharmony_ci EXPECT_EQ(ret, -1); 110484543d1Sopenharmony_ci ffrt_task_attr_get_name(nullptr); 111484543d1Sopenharmony_ci ffrt_task_attr_set_name(nullptr, "A"); 112484543d1Sopenharmony_ci ffrt_task_attr_set_qos(nullptr, static_cast<int>(ffrt::qos_user_initiated)); 113484543d1Sopenharmony_ci ffrt_task_attr_get_qos(nullptr); 114484543d1Sopenharmony_ci ffrt_task_attr_destroy(nullptr); 115484543d1Sopenharmony_ci ffrt_submit_base(nullptr, nullptr, nullptr, nullptr); 116484543d1Sopenharmony_ci ffrt_submit_h_base(nullptr, nullptr, nullptr, nullptr); 117484543d1Sopenharmony_ci ffrt::submit([&] { 118484543d1Sopenharmony_ci x++; 119484543d1Sopenharmony_ci printf("return %d\n", ffrt::this_task::update_qos(static_cast<int>(ffrt::qos_user_initiated))); 120484543d1Sopenharmony_ci printf("id is %" PRIu64 "\n", ffrt::this_task::get_id()); 121484543d1Sopenharmony_ci }); 122484543d1Sopenharmony_ci ffrt_this_task_get_id(); 123484543d1Sopenharmony_ci ffrt::wait(); 124484543d1Sopenharmony_ci ffrt_this_task_update_qos(static_cast<int>(ffrt::qos_user_initiated)); 125484543d1Sopenharmony_ci#ifndef WITH_NO_MOCKER 126484543d1Sopenharmony_ci ffrt_os_sched_attr attr = {100, 10, 99, 99, 9, "2-3"}; 127484543d1Sopenharmony_ci ffrt_set_cgroup_attr(static_cast<int>(ffrt::qos_user_initiated), &attr); 128484543d1Sopenharmony_ci#endif 129484543d1Sopenharmony_ci EXPECT_EQ(x, 1); 130484543d1Sopenharmony_ci} 131484543d1Sopenharmony_ci 132484543d1Sopenharmony_ciHWTEST_F(DependencyTest, update_qos_failed_01, TestSize.Level1) 133484543d1Sopenharmony_ci{ 134484543d1Sopenharmony_ci int x = 0; 135484543d1Sopenharmony_ci int ret = ffrt_task_attr_init(nullptr); 136484543d1Sopenharmony_ci EXPECT_EQ(ret, -1); 137484543d1Sopenharmony_ci ffrt_task_attr_get_name(nullptr); 138484543d1Sopenharmony_ci ffrt_task_attr_set_name(nullptr, "A"); 139484543d1Sopenharmony_ci ffrt_task_attr_set_qos(nullptr, static_cast<int>(ffrt::qos_user_initiated)); 140484543d1Sopenharmony_ci ffrt_task_attr_get_qos(nullptr); 141484543d1Sopenharmony_ci ffrt_task_attr_destroy(nullptr); 142484543d1Sopenharmony_ci ffrt_submit_base(nullptr, nullptr, nullptr, nullptr); 143484543d1Sopenharmony_ci ffrt_submit_h_base(nullptr, nullptr, nullptr, nullptr); 144484543d1Sopenharmony_ci ffrt::submit([&] { 145484543d1Sopenharmony_ci printf("return %d\n", ffrt::this_task::update_qos(static_cast<int>(ffrt::qos_user_initiated))); 146484543d1Sopenharmony_ci printf("id is %" PRIu64 "\n", ffrt::this_task::get_id()); 147484543d1Sopenharmony_ci int ret1 = ffrt_this_task_update_qos(static_cast<int>(ffrt::qos_default)); 148484543d1Sopenharmony_ci EXPECT_EQ(ret1, 0); 149484543d1Sopenharmony_ci x++; 150484543d1Sopenharmony_ci }); 151484543d1Sopenharmony_ci ffrt_this_task_get_id(); 152484543d1Sopenharmony_ci ffrt::wait(); 153484543d1Sopenharmony_ci#ifndef WITH_NO_MOCKER 154484543d1Sopenharmony_ci ffrt_set_cgroup_attr(static_cast<int>(ffrt::qos_user_initiated), nullptr); 155484543d1Sopenharmony_ci#endif 156484543d1Sopenharmony_ci EXPECT_EQ(x, 1); 157484543d1Sopenharmony_ci} 158484543d1Sopenharmony_ci 159484543d1Sopenharmony_ciHWTEST_F(DependencyTest, update_qos_failed_02, TestSize.Level1) 160484543d1Sopenharmony_ci{ 161484543d1Sopenharmony_ci int ret = ffrt_task_attr_init(nullptr); 162484543d1Sopenharmony_ci EXPECT_EQ(ret, -1); 163484543d1Sopenharmony_ci ffrt::submit([] { 164484543d1Sopenharmony_ci printf("return %d\n", ffrt::this_task::update_qos(static_cast<int>(ffrt::qos_user_initiated))); 165484543d1Sopenharmony_ci }); 166484543d1Sopenharmony_ci} 167484543d1Sopenharmony_ci 168484543d1Sopenharmony_ciHWTEST_F(DependencyTest, executor_task_submit_success_01, TestSize.Level1) 169484543d1Sopenharmony_ci{ 170484543d1Sopenharmony_ci ffrt_task_attr_t attr; 171484543d1Sopenharmony_ci static ffrt_executor_task_t work; 172484543d1Sopenharmony_ci work.wq[0] = &work.wq; 173484543d1Sopenharmony_ci work.wq[1] = &work.wq; 174484543d1Sopenharmony_ci work.type = reinterpret_cast<uintptr_t>(&attr); 175484543d1Sopenharmony_ci 176484543d1Sopenharmony_ci ffrt_executor_task_submit(&work, &attr); 177484543d1Sopenharmony_ci} 178484543d1Sopenharmony_ciHWTEST_F(DependencyTest, executor_task_submit_nullptr_01, TestSize.Level1) 179484543d1Sopenharmony_ci{ 180484543d1Sopenharmony_ci ffrt_executor_task_submit(nullptr, nullptr); 181484543d1Sopenharmony_ci} 182484543d1Sopenharmony_ci 183484543d1Sopenharmony_ciHWTEST_F(DependencyTest, executor_task_submit_cancel_01, TestSize.Level1) 184484543d1Sopenharmony_ci{ 185484543d1Sopenharmony_ci ffrt_executor_task_cancel(nullptr, static_cast<int>(ffrt::qos_user_initiated)); 186484543d1Sopenharmony_ci} 187484543d1Sopenharmony_ci 188484543d1Sopenharmony_ciHWTEST_F(DependencyTest, executor_task_submit_cancel_02, TestSize.Level1) 189484543d1Sopenharmony_ci{ 190484543d1Sopenharmony_ci ffrt_task_attr_t attr; 191484543d1Sopenharmony_ci ffrt_task_attr_init(&attr); 192484543d1Sopenharmony_ci ffrt_executor_task_t work; 193484543d1Sopenharmony_ci work.type = reinterpret_cast<uintptr_t>(&attr); 194484543d1Sopenharmony_ci 195484543d1Sopenharmony_ci ffrt_task_attr_set_qos(&attr, static_cast<int>(ffrt::qos_user_initiated)); 196484543d1Sopenharmony_ci ffrt_executor_task_submit(&work, &attr); 197484543d1Sopenharmony_ci usleep(10000); 198484543d1Sopenharmony_ci int cancelled = ffrt_executor_task_cancel(&work, static_cast<int>(ffrt::qos_user_initiated)); 199484543d1Sopenharmony_ci EXPECT_EQ(cancelled, 0); 200484543d1Sopenharmony_ci 201484543d1Sopenharmony_ci ffrt_task_attr_destroy(&attr); 202484543d1Sopenharmony_ci} 203484543d1Sopenharmony_ci 204484543d1Sopenharmony_ciHWTEST_F(DependencyTest, update_trace_tag_success_02, TestSize.Level1) 205484543d1Sopenharmony_ci{ 206484543d1Sopenharmony_ci ffrt::set_trace_tag("TASK A"); 207484543d1Sopenharmony_ci ffrt::clear_trace_tag(); 208484543d1Sopenharmony_ci} 209484543d1Sopenharmony_ci 210484543d1Sopenharmony_ciHWTEST_F(DependencyTest, task_attr_success_02, TestSize.Level1) 211484543d1Sopenharmony_ci{ 212484543d1Sopenharmony_ci ffrt::task_attr tmpTask; 213484543d1Sopenharmony_ci tmpTask.name("Task A"); 214484543d1Sopenharmony_ci tmpTask.qos(static_cast<int>(ffrt::qos_user_initiated)); 215484543d1Sopenharmony_ci tmpTask.name(); 216484543d1Sopenharmony_ci tmpTask.qos(); 217484543d1Sopenharmony_ci} 218484543d1Sopenharmony_ci 219484543d1Sopenharmony_ciHWTEST_F(DependencyTest, sample_pingpong_pipe_interval_checkpoint, TestSize.Level1) 220484543d1Sopenharmony_ci{ 221484543d1Sopenharmony_ci int loops = 5; 222484543d1Sopenharmony_ci int frame_num = 2; 223484543d1Sopenharmony_ci 224484543d1Sopenharmony_ci if (getenv("LOOP_NUM")) { 225484543d1Sopenharmony_ci loops = atoi(getenv("LOOP_NUM")); 226484543d1Sopenharmony_ci } 227484543d1Sopenharmony_ci if (getenv("FRAME_NUM")) { 228484543d1Sopenharmony_ci frame_num = atoi(getenv("FRAME_NUM")); 229484543d1Sopenharmony_ci } 230484543d1Sopenharmony_ci 231484543d1Sopenharmony_ci ffrt::submit([&]() { stall_us(10); }, {}, {}); 232484543d1Sopenharmony_ci 233484543d1Sopenharmony_ci auto it = ffrt::qos_interval_create(16, static_cast<int>(ffrt::qos_user_interactive)); 234484543d1Sopenharmony_ci for (int loop = 0; loop < loops; loop++) { 235484543d1Sopenharmony_ci constexpr int FRAME_NUM = 3; 236484543d1Sopenharmony_ci constexpr uint32_t BUFFER_NUM = 2; 237484543d1Sopenharmony_ci int x0[FRAME_NUM]; 238484543d1Sopenharmony_ci int x1[BUFFER_NUM]; 239484543d1Sopenharmony_ci int x2[BUFFER_NUM]; 240484543d1Sopenharmony_ci int x3[FRAME_NUM]; 241484543d1Sopenharmony_ci 242484543d1Sopenharmony_ci int stalls[10][4] = { 243484543d1Sopenharmony_ci {2000, 6000, 8000, 8000 + 6000 + 2000}, // 0 244484543d1Sopenharmony_ci {2125, 6375, 8500, 8500 + 6375 + 2125}, // 1 245484543d1Sopenharmony_ci {2222, 6666, 8888, 8888 + 6666 + 2222}, // 2 246484543d1Sopenharmony_ci {2250, 6750, 9000, 9000 + 6750 + 2250}, // 3 247484543d1Sopenharmony_ci {2375, 7125, 9500, 9500 + 7125 + 2375}, // 4 248484543d1Sopenharmony_ci {2500, 7500, 10000, 10000 + 7500 + 2500}, // 5 249484543d1Sopenharmony_ci {1875, 5625, 7500, 7500 + 5625 + 1875}, // 6 250484543d1Sopenharmony_ci {1750, 5250, 7000, 7000 + 5250 + 1750}, // 7 251484543d1Sopenharmony_ci {1625, 4875, 6500, 6500 + 4875 + 1625}, // 8 252484543d1Sopenharmony_ci {1500, 4500, 6000, 6000 + 4500 + 1500}, // 9 253484543d1Sopenharmony_ci }; 254484543d1Sopenharmony_ci 255484543d1Sopenharmony_ci auto start = std::chrono::system_clock::now(); 256484543d1Sopenharmony_ci ffrt::qos_interval_begin(it); 257484543d1Sopenharmony_ci for (int i = 0; i < frame_num; i++) { 258484543d1Sopenharmony_ci int pingpong = i % BUFFER_NUM; 259484543d1Sopenharmony_ci // task A 260484543d1Sopenharmony_ci ffrt::submit( 261484543d1Sopenharmony_ci [i, loop, stalls]() { 262484543d1Sopenharmony_ci FFRT_LOGI("%u", i); 263484543d1Sopenharmony_ci }, 264484543d1Sopenharmony_ci {x0 + i}, {x1 + pingpong}, ffrt::task_attr().name(("UI" + std::to_string(i)).c_str())); 265484543d1Sopenharmony_ci // task B 266484543d1Sopenharmony_ci ffrt::submit( 267484543d1Sopenharmony_ci [i, loop, stalls]() { 268484543d1Sopenharmony_ci FFRT_LOGI("%u", i); 269484543d1Sopenharmony_ci }, 270484543d1Sopenharmony_ci {x1 + pingpong}, {x2 + pingpong}, ffrt::task_attr().name(("Render" + std::to_string(i)).c_str())); 271484543d1Sopenharmony_ci // task C 272484543d1Sopenharmony_ci ffrt::submit( 273484543d1Sopenharmony_ci [i, loop, stalls]() { 274484543d1Sopenharmony_ci FFRT_LOGI("%u", i); 275484543d1Sopenharmony_ci }, 276484543d1Sopenharmony_ci {x2 + pingpong}, {x3 + i}, ffrt::task_attr().name(("surfaceflinger" + std::to_string(i)).c_str())); 277484543d1Sopenharmony_ci } 278484543d1Sopenharmony_ci ffrt::wait(); 279484543d1Sopenharmony_ci ffrt::qos_interval_end(it); 280484543d1Sopenharmony_ci } 281484543d1Sopenharmony_ci 282484543d1Sopenharmony_ci ffrt::qos_interval_destroy(it); 283484543d1Sopenharmony_ci} 284