• 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 #include "ffrt_inner.h"
18 #include "dfx/bbox/bbox.h"
19 #include "c/queue_ext.h"
20 #include "../common.h"
21 
22 using namespace ffrt;
23 
24 extern void SaveTheBbox();
25 
26 using namespace testing;
27 #ifdef HWTEST_TESTING_EXT_ENABLE
28 using namespace testing::ext;
29 #endif
30 
31 class DfxTest : public testing::Test {
32 protected:
SetUpTestCase()33     static void SetUpTestCase()
34     {
35     }
36 
TearDownTestCase()37     static void TearDownTestCase()
38     {
39     }
40 
SetUp()41     void SetUp() override
42     {
43     }
44 
TearDown()45     void TearDown() override
46     {
47     }
48 };
49 
50 HWTEST_F(DfxTest, tracetest, TestSize.Level1)
51 {
52     int x = 0;
53     ffrt::submit(
__anon4585afc10102() 54         [&]() {
55             ffrt::set_trace_tag("task");
56             x++;
57             ffrt::clear_trace_tag();
58         }, {}, {});
59     ffrt::wait();
60     EXPECT_EQ(x, 1);
61 }
62 
63 static struct sigaction s_oldSa[SIGSYS + 1]; // SIGSYS = 31
64 
SignalHandler(int signo,siginfo_t * info,void * context)65 static void SignalHandler(int signo, siginfo_t* info, void* context __attribute__((unused)))
66 {
67     SaveTheBbox();
68 
69     // we need to deregister our signal handler for that signal before continuing.
70     sigaction(signo, &s_oldSa[signo], nullptr);
71 }
72 
SignalReg(int signo)73 static void SignalReg(int signo)
74 {
75     sigaction(signo, nullptr, &s_oldSa[signo]);
76     struct sigaction newAction;
77     newAction.sa_flags = SA_RESTART | SA_SIGINFO;
78     newAction.sa_sigaction = SignalHandler;
79     sigaction(signo, &newAction, nullptr);
80 }
81 
82 HWTEST_F(DfxTest, queue_dfx_bbox_normal_task_0001, TestSize.Level1)
83 {
84     // 异常信号用例,测试bbox功能正常;
85     int x = 0;
86     ffrt::mutex lock;
87 
88     pid_t pid = fork();
89     if (!pid) {
90         printf("pid = %d, thread id= %d  start\n", getpid(), pid);
91 
__anon4585afc10402() 92         auto basic1Func = [&]() {
93             lock.lock();
94             ffrt_usleep(2000);
95             x = x + 1;
96             lock.unlock();
97         };
98 
__anon4585afc10502() 99         auto basic2Func = [&]() {
100             ffrt_usleep(3000);
101             SignalReg(SIGABRT);
102             raise(SIGABRT);  // 向自身进程发送SIGABR
103         };
104 
__anon4585afc10602() 105         auto basic3Func = [&]() {
106             ffrt_usleep(5000);
107             x = x + 1;
108         };
109 
110         for (int i = 0; i < 20; i++) {
111             ffrt::submit(basic1Func, {}, {}, ffrt::task_attr().qos(static_cast<int>(ffrt::qos_default)));
112         }
113         for (int i = 0; i < 10; i++) {
114             ffrt::submit(basic3Func, {}, {}, ffrt::task_attr().qos(static_cast<int>(ffrt::qos_default)));
115         }
116         auto task = ffrt::submit_h(basic2Func, {}, {}, ffrt::task_attr().qos(static_cast<int>(ffrt::qos_background)));
117 
118         ffrt::wait({task});
119         printf("pid = %d, thread id= %d  end\n", getpid(), pid);
120         exit(0);
121     }
122     sleep(1);
123 }
124 
125 HWTEST_F(DfxTest, queue_dfx_bbox_queue_task_0001, TestSize.Level1)
126 {
127     // 异常信号用例,测试bbox功能正常;
128     int x = 0;
129     ffrt::mutex lock;
130 
131     pid_t pid = fork();
132     if (!pid) {
133         printf("pid = %d, thread id= %d  start\n", getpid(), pid);
134         ffrt_queue_attr_t queue_attr;
135         ffrt_queue_attr_t queue_attr2;
136         (void)ffrt_queue_attr_init(&queue_attr); // 初始化属性,必须
137         ffrt_queue_t queue_handle = ffrt_queue_create(ffrt_queue_serial, "test_queue", &queue_attr);
138         (void)ffrt_queue_attr_init(&queue_attr2); // 初始化属性,必须
139         ffrt_queue_t queue_handle2 = ffrt_queue_create(ffrt_queue_serial, "test_queue", &queue_attr2);
__anon4585afc10702() 140         std::function<void()> basic1Func = [&]() {
141             lock.lock();
142             ffrt_usleep(5000);
143             x = x + 1;
144             lock.unlock();
145         };
146 
__anon4585afc10802() 147         auto basic3Func = [&]() {
148             lock.lock();
149             sleep(2);
150             lock.unlock();
151         };
152 
153         auto task = ffrt::submit_h(basic3Func, {}, {}, ffrt::task_attr().qos(static_cast<int>(ffrt::qos_background)));
154         ffrt_queue_submit(queue_handle, create_function_wrapper(basic1Func, ffrt_function_kind_queue), nullptr);
155         ffrt_queue_submit(queue_handle2, create_function_wrapper(basic1Func, ffrt_function_kind_queue), nullptr);
156 
157         SaveTheBbox();
158         ffrt::wait({task});
159         ffrt_queue_attr_destroy(&queue_attr);
160         ffrt_queue_destroy(queue_handle);
161         ffrt_queue_attr_destroy(&queue_attr2);
162         ffrt_queue_destroy(queue_handle2);
163         printf("pid = %d, thread id= %d  end\n", getpid(), pid);
164         exit(0);
165     }
166     sleep(1);
167 }
168