• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include <dlfcn.h>
16 #include <fcntl.h>
17 #include <fstream>
18 #include <hwext/gtest-ext.h>
19 #include <hwext/gtest-tag.h>
20 #include <string>
21 #include <thread>
22 #include <type_traits>
23 #include <unistd.h>
24 
25 #include "flow_controller.h"
26 #include "ftrace_module.h"
27 
28 using namespace testing::ext;
29 
30 namespace {
31 constexpr uint32_t BUFFER_SIZE_KB = 256;
32 constexpr uint32_t BUFFER_SIZE_MIN_KB = 63;
33 constexpr uint32_t BUFFER_SIZE_MAX_KB = 64 * 1024 + 1;
34 constexpr uint32_t FLUSH_INTERVAL_MS = 1000;
35 constexpr uint32_t FLUSH_THRESHOLD_KB = 1024;
36 constexpr uint32_t TRACE_PERIOD_MS = 500;
37 const std::string DEFAULT_TEST_PATH("/system/lib/");
38 using WriterStructPtr = std::unique_ptr<WriterStruct>::pointer;
39 using ConstVoidPtr = std::unique_ptr<const void>::pointer;
40 class FlowControllerTest : public ::testing::Test {
41 protected:
SetUp()42     void SetUp() override {}
TearDown()43     void TearDown() override {}
44 };
45 
WriteFunc(WriterStructPtr writer,ConstVoidPtr data,size_t size)46 long WriteFunc(WriterStructPtr writer, ConstVoidPtr data, size_t size)
47 {
48     if (writer == nullptr || data == nullptr || size <= 0) {
49         return -1;
50     }
51 
52     return 0;
53 }
54 
FlushFunc(WriterStructPtr writer)55 bool FlushFunc(WriterStructPtr writer)
56 {
57     if (writer == nullptr) {
58         return false;
59     }
60     return true;
61 }
62 
63 /*
64  * @tc.name: SetWriter
65  * @tc.desc: test FlowController::SetWriter.
66  * @tc.type: FUNC
67  */
68 HWTEST_F(FlowControllerTest, SetWriter, TestSize.Level1)
69 {
70     OHOS::Profiler::Plugins::FlowController controller;
71     WriterStruct writer = {WriteFunc, FlushFunc};
72     EXPECT_EQ(controller.SetWriter(static_cast<WriterStructPtr>(&writer)), 0);
73 }
74 
75 /*
76  * @tc.name: LoadConfig
77  * @tc.desc: test FlowController::LoadConfig.
78  * @tc.type: FUNC
79  */
80 HWTEST_F(FlowControllerTest, LoadConfig, TestSize.Level1)
81 {
82     OHOS::Profiler::Plugins::FlowController controller;
83     TracePluginConfig config;
84 
85     // set writer
86     WriterStruct writer = {WriteFunc, FlushFunc};
87     ASSERT_EQ(controller.SetWriter(static_cast<WriterStructPtr>(&writer)), 0);
88 
89     // set config
90     config.add_ftrace_events("sched/sched_switch");
91     config.set_buffer_size_kb(BUFFER_SIZE_KB);
92     config.set_flush_interval_ms(FLUSH_INTERVAL_MS);
93     config.set_flush_threshold_kb(FLUSH_THRESHOLD_KB);
94     config.set_parse_ksyms(true);
95     config.set_clock("global");
96     config.set_trace_period_ms(TRACE_PERIOD_MS);
97     config.set_raw_data_prefix("/data/local/tmp/raw_trace_");
98     std::vector<uint8_t> configData(config.ByteSizeLong());
99     int ret = config.SerializeToArray(configData.data(), configData.size());
100     ASSERT_GT(ret, 0);
101     EXPECT_EQ(controller.LoadConfig(configData.data(), configData.size()), 0);
102 }
103 
104 /*
105  * @tc.name: LoadConfig
106  * @tc.desc: test FlowController::LoadConfig.
107  * @tc.type: FUNC
108  */
109 HWTEST_F(FlowControllerTest, SetMinBufForLoadConfig, TestSize.Level1)
110 {
111     OHOS::Profiler::Plugins::FlowController controller;
112     TracePluginConfig config;
113 
114     // set writer
115     WriterStruct writer = {WriteFunc, FlushFunc};
116     ASSERT_EQ(controller.SetWriter(static_cast<WriterStructPtr>(&writer)), 0);
117 
118     // set config
119     config.set_buffer_size_kb(BUFFER_SIZE_MIN_KB);
120     std::vector<uint8_t> configData(config.ByteSizeLong());
121     int ret = config.SerializeToArray(configData.data(), configData.size());
122     ASSERT_GT(ret, 0);
123     EXPECT_EQ(controller.LoadConfig(configData.data(), configData.size()), 0);
124 }
125 
126 /*
127  * @tc.name: LoadConfig
128  * @tc.desc: test FlowController::LoadConfig.
129  * @tc.type: FUNC
130  */
131 HWTEST_F(FlowControllerTest, SetMaxBufForLoadConfig, TestSize.Level1)
132 {
133     OHOS::Profiler::Plugins::FlowController controller;
134     TracePluginConfig config;
135 
136     // set writer
137     WriterStruct writer = {WriteFunc, FlushFunc};
138     ASSERT_EQ(controller.SetWriter(static_cast<WriterStructPtr>(&writer)), 0);
139 
140     // set config
141     config.set_buffer_size_kb(BUFFER_SIZE_MAX_KB);
142     std::vector<uint8_t> configData(config.ByteSizeLong());
143     int ret = config.SerializeToArray(configData.data(), configData.size());
144     ASSERT_GT(ret, 0);
145     EXPECT_EQ(controller.LoadConfig(configData.data(), configData.size()), 0);
146 }
147 
148 /*
149  * @tc.name: LoadConfig
150  * @tc.desc: test FlowController::LoadConfig.
151  * @tc.type: FUNC
152  */
153 HWTEST_F(FlowControllerTest, SetTracePeriodForLoadConfig, TestSize.Level1)
154 {
155     OHOS::Profiler::Plugins::FlowController controller;
156     TracePluginConfig config;
157 
158     // set writer
159     WriterStruct writer = {WriteFunc, FlushFunc};
160     ASSERT_EQ(controller.SetWriter(static_cast<WriterStructPtr>(&writer)), 0);
161 
162     // set config
163     config.add_bytrace_apps("ftrace_plugin_ut");
164     config.add_bytrace_categories("idle");
165     std::vector<uint8_t> configData(config.ByteSizeLong());
166     int ret = config.SerializeToArray(configData.data(), configData.size());
167     ASSERT_GT(ret, 0);
168     EXPECT_EQ(controller.LoadConfig(configData.data(), configData.size()), 0);
169 }
170 
171 /*
172  * @tc.name: LoadConfig
173  * @tc.desc: test FlowController::LoadConfig.
174  * @tc.type: FUNC
175  */
176 HWTEST_F(FlowControllerTest, SetBytraceAppForLoadConfig, TestSize.Level1)
177 {
178     OHOS::Profiler::Plugins::FlowController controller;
179     TracePluginConfig config;
180 
181     // set writer
182     WriterStruct writer = {WriteFunc, FlushFunc};
183     ASSERT_EQ(controller.SetWriter(static_cast<WriterStructPtr>(&writer)), 0);
184 
185     // set config
186     config.add_ftrace_events("sched/sched_switch");
187     config.set_trace_period_ms(0);
188     std::vector<uint8_t> configData(config.ByteSizeLong());
189     int ret = config.SerializeToArray(configData.data(), configData.size());
190     ASSERT_GT(ret, 0);
191     EXPECT_EQ(controller.LoadConfig(configData.data(), configData.size()), 0);
192 }
193 
194 /*
195  * @tc.name: StartCapture
196  * @tc.desc: test FlowController::StartCapture.
197  * @tc.type: FUNC
198  */
199 HWTEST_F(FlowControllerTest, StartCapture, TestSize.Level1)
200 {
201     OHOS::Profiler::Plugins::FlowController controller;
202     TracePluginConfig config;
203 
204     // set writer
205     WriterStruct writer = {WriteFunc, FlushFunc};
206     ASSERT_EQ(controller.SetWriter(static_cast<WriterStructPtr>(&writer)), 0);
207 
208     // set config
209     config.add_ftrace_events("sched/sched_switch");
210     config.set_buffer_size_kb(BUFFER_SIZE_KB);
211     config.set_flush_interval_ms(FLUSH_INTERVAL_MS);
212     config.set_flush_threshold_kb(FLUSH_THRESHOLD_KB);
213     config.set_parse_ksyms(true);
214     config.set_clock("global");
215     config.set_trace_period_ms(TRACE_PERIOD_MS);
216     config.set_raw_data_prefix("/data/local/tmp/raw_trace_");
217     std::vector<uint8_t> configData(config.ByteSizeLong());
218     int ret = config.SerializeToArray(configData.data(), configData.size());
219     ASSERT_GT(ret, 0);
220     EXPECT_EQ(controller.LoadConfig(configData.data(), configData.size()), 0);
221 
222     EXPECT_EQ(controller.StartCapture(), 0);
223     EXPECT_EQ(controller.StopCapture(), 0);
224 }
225 
226 /*
227  * @tc.name: frace_module
228  * @tc.desc: test Framework.
229  * @tc.type: FUNC
230  */
231 HWTEST_F(FlowControllerTest, TestFramework, TestSize.Level1)
232 {
233     std::string path = DEFAULT_TEST_PATH + std::string("libftrace_plugin.z.so");
234     auto handle = dlopen(path.c_str(), RTLD_LAZY);
235     EXPECT_NE(handle, nullptr);
236     PluginModuleStruct* plugin = reinterpret_cast<PluginModuleStruct*>(dlsym(handle, "g_pluginModule"));
237     EXPECT_NE(plugin, nullptr);
238     EXPECT_STREQ(plugin->name, "ftrace-plugin");
239 
240     // set config
241     TracePluginConfig config;
242     config.add_ftrace_events("sched/sched_switch");
243     config.set_buffer_size_kb(BUFFER_SIZE_KB);
244     config.set_flush_interval_ms(FLUSH_INTERVAL_MS);
245     config.set_flush_threshold_kb(FLUSH_THRESHOLD_KB);
246     config.set_parse_ksyms(true);
247     config.set_clock("global");
248     config.set_trace_period_ms(TRACE_PERIOD_MS);
249     config.set_raw_data_prefix("/data/local/tmp/raw_trace_");
250     std::vector<uint8_t> configData(config.ByteSizeLong());
251     ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0);
252 
253     // test framework process
254     WriterStruct writer = {WriteFunc, FlushFunc};
255     std::vector<uint8_t> dataBuffer(plugin->resultBufferSizeHint);
256     EXPECT_EQ(plugin->callbacks->onRegisterWriterStruct(&writer), 0);
257     EXPECT_EQ(plugin->callbacks->onPluginSessionStart(configData.data(), configData.size()), 0);
258     EXPECT_EQ(plugin->callbacks->onPluginSessionStop(), 0);
259 }
260 } // namespace