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