• 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 
16 #include <fcntl.h>
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 #include <iostream>
20 #include <string>
21 #include <unistd.h>
22 
23 #include "file.h"
24 #include "trace_streamer_selector.h"
25 constexpr size_t G_FILE_PERMISSION = 664;
26 
27 using namespace testing::ext;
28 using namespace SysTuning;
29 using namespace SysTuning::TraceStreamer;
30 namespace SysTuning {
31 namespace TraceStreamer {
32 class ParserTest : public testing::Test {
33 protected:
SetUpTestCase()34     static void SetUpTestCase() {}
TearDownTestCase()35     static void TearDownTestCase() {}
36 };
37 
38 /**
39  * @tc.name: BytraceParserTest
40  * @tc.desc: Test bytrace parsing TXT file to export database
41  * @tc.type: FUNC
42  */
43 HWTEST_F(ParserTest, BytraceParserTest, TestSize.Level1)
44 {
45     TS_LOGI("test25-1");
46     const std::string tracePath = "data/resource/ut_bytrace_input_full.txt";
47     const std::string utGoldDb = "data/resource/ut_bytrace_input_full_gold.db";
48     const std::string dbPath = "data/resource/out_db1.db";
49     constexpr size_t readSize = 1024 * 1024;
50     constexpr uint32_t lineLength = 256;
51 
52     if (access(tracePath.c_str(), F_OK) == 0) {
53         std::unique_ptr<SysTuning::TraceStreamer::TraceStreamerSelector> ta =
54             std::make_unique<SysTuning::TraceStreamer::TraceStreamerSelector>();
55         ta->EnableMetaTable(false);
56         ta->SetCleanMode(false);
57         int fd(base::OpenFile(tracePath, O_RDONLY, G_FILE_PERMISSION));
58         while (true) {
59             std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(readSize);
60             auto rsize = base::Read(fd, buf.get(), readSize);
61             if (rsize == 0) {
62                 break;
63             }
64             if (rsize < 0) {
65                 TS_LOGD("Reading trace file failed (errno: %d, %s)", errno, strerror(errno));
66                 break;
67             }
68             if (!ta->ParseTraceDataSegment(std::move(buf), rsize)) {
69                 break;
70             };
71         }
72         ta->WaitForParserEnd();
73         close(fd);
74         ta->ExportDatabase(dbPath);
75         ta->Clear();
76         EXPECT_TRUE(access(dbPath.c_str(), F_OK) == 0);
77     } else {
78         EXPECT_TRUE(false);
79     }
80 
81     if (access(utGoldDb.c_str(), F_OK) == 0) {
82         FILE* file1 = nullptr;
83         FILE* file2 = nullptr;
84         char line1[lineLength];
85         char line2[lineLength];
86         const std::string command1 = "md5sum data/resource/ut_bytrace_input_full_gold.db";
87         const std::string md5DbPath = "md5sum "+ dbPath;
88         file1 = popen(command1.c_str(), "r");
89         file2 = popen(md5DbPath.c_str(), "r");
90         if (file1 && file2) {
91             if (fgets(line1, lineLength, file1) != nullptr && fgets(line2, lineLength, file2) != nullptr) {
92                 std::string str1(line1);
93                 std::string str2(line2);
94                 str1 = str1.substr(0, str1.find_first_of(' '));
95                 str2 = str2.substr(0, str2.find_first_of(' '));
96                 EXPECT_TRUE(str1.compare(str2) == 0);
97             }
98         }
99     } else {
100         EXPECT_TRUE(false);
101     }
102 
103     if (access(dbPath.c_str(), F_OK) == 0) {
104         remove(dbPath.c_str());
105     }
106 }
107 
108 /**
109  * @tc.name: HtraceParserTest
110  * @tc.desc: Test htrace parsing binary file export database
111  * @tc.type: FUNC
112  */
113 HWTEST_F(ParserTest, HtraceParserTest, TestSize.Level1)
114 {
115     TS_LOGI("test25-2");
116     const std::string tracePath = "data/resource/htrace.bin";
117     const std::string utGoldDb = "data/resource/htrace_gold.db";
118     const std::string dbPath = "data/resource/out_db2.db";
119     constexpr size_t readSize = 1024;
120     constexpr uint32_t lineLength = 256;
121 
122     if (access(tracePath.c_str(), F_OK) == 0) {
123         std::unique_ptr<SysTuning::TraceStreamer::TraceStreamerSelector> ta =
124             std::make_unique<SysTuning::TraceStreamer::TraceStreamerSelector>();
125         ta->EnableMetaTable(false);
126         ta->SetCleanMode(false);
127         int fd(base::OpenFile(tracePath, O_RDONLY, G_FILE_PERMISSION));
128         while (true) {
129             std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(readSize);
130             auto rsize = base::Read(fd, buf.get(), readSize);
131 
132             if (rsize == 0) {
133                 break;
134             }
135             if (rsize < 0) {
136                 TS_LOGD("Reading trace file over (errno: %d, %s)", errno, strerror(errno));
137                 break;
138             }
139             if (!ta->ParseTraceDataSegment(std::move(buf), rsize)) {
140                 break;
141             };
142         }
143         ta->WaitForParserEnd();
144         close(fd);
145         ta->ExportDatabase(dbPath);
146         ta->Clear();
147         EXPECT_TRUE(access(dbPath.c_str(), F_OK) == 0);
148     } else {
149         EXPECT_TRUE(false);
150     }
151 
152     if (access(utGoldDb.c_str(), F_OK) == 0) {
153         FILE* file1 = nullptr;
154         FILE* file2 = nullptr;
155         char line1[lineLength];
156         char line2[lineLength];
157         const std::string command1 = "md5sum data/resource/htrace_gold.db";
158         const std::string md5DbPath = "md5sum "+ dbPath;
159         file1 = popen(command1.c_str(), "r");
160         file2 = popen(md5DbPath.c_str(), "r");
161         if (file1 && file2) {
162             if (fgets(line1, lineLength, file1) != nullptr && fgets(line2, lineLength, file2) != nullptr) {
163                 std::string str1(line1);
164                 std::string str2(line2);
165                 str1 = str1.substr(0, str1.find_first_of(' '));
166                 str2 = str2.substr(0, str2.find_first_of(' '));
167                 EXPECT_TRUE(str1.compare(str2) == 0);
168             }
169         }
170     } else {
171         EXPECT_TRUE(false);
172     }
173 
174     if (access(dbPath.c_str(), F_OK) == 0) {
175         remove(dbPath.c_str());
176     }
177 }
178 
179 /**
180  * @tc.name: HtraceAndPerfParserTest
181  * @tc.desc: Test parsing htrace and perf binary file export database
182  * @tc.type: FUNC
183  */
184 HWTEST_F(ParserTest, HtraceAndPerfParserTest, TestSize.Level1)
185 {
186     TS_LOGI("test25-3");
187     const std::string tracePath = "data/resource/htrace_perf.bin";
188     const std::string utGoldDb = "data/resource/htrace_perf_gold.db";
189     const std::string dbPath = "data/resource/out_db3.db";
190     constexpr size_t readSize = 1024;
191     constexpr uint32_t lineLength = 256;
192 
193     if (access(tracePath.c_str(), F_OK) == 0) {
194         std::unique_ptr<SysTuning::TraceStreamer::TraceStreamerSelector> ta =
195             std::make_unique<SysTuning::TraceStreamer::TraceStreamerSelector>();
196         ta->EnableMetaTable(false);
197         ta->SetCleanMode(false);
198         int fd(base::OpenFile(tracePath, O_RDONLY, G_FILE_PERMISSION));
199         while (true) {
200             std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(readSize);
201             auto rsize = base::Read(fd, buf.get(), readSize);
202 
203             if (rsize == 0) {
204                 break;
205             }
206             if (rsize < 0) {
207                 TS_LOGD("Reading trace file over (errno: %d, %s)", errno, strerror(errno));
208                 break;
209             }
210             if (!ta->ParseTraceDataSegment(std::move(buf), rsize)) {
211                 break;
212             };
213         }
214         ta->WaitForParserEnd();
215         close(fd);
216         ta->ExportDatabase(dbPath);
217         ta->Clear();
218         EXPECT_TRUE(access(dbPath.c_str(), F_OK) == 0);
219     } else {
220         EXPECT_TRUE(false);
221     }
222 
223     if (access(utGoldDb.c_str(), F_OK) == 0) {
224         FILE* file1 = nullptr;
225         FILE* file2 = nullptr;
226         char line1[lineLength];
227         char line2[lineLength];
228         const std::string command1 = "md5sum data/resource/htrace_perf_gold.db";
229         const std::string md5DbPath = "md5sum "+ dbPath;
230         file1 = popen(command1.c_str(), "r");
231         file2 = popen(md5DbPath.c_str(), "r");
232         if (file1 && file2) {
233             if (fgets(line1, lineLength, file1) != nullptr && fgets(line2, lineLength, file2) != nullptr) {
234                 std::string str1(line1);
235                 std::string str2(line2);
236                 str1 = str1.substr(0, str1.find_first_of(' '));
237                 str2 = str2.substr(0, str2.find_first_of(' '));
238                 EXPECT_TRUE(str1.compare(str2) == 0);
239             }
240         }
241     } else {
242         EXPECT_TRUE(false);
243     }
244 
245     if (access(dbPath.c_str(), F_OK) == 0) {
246         remove(dbPath.c_str());
247     }
248 }
249 
250 /**
251  * @tc.name: HtraceAndEbpfParserTest
252  * @tc.desc: Test parsing htrace and ebpf binary file export database
253  * @tc.type: FUNC
254  */
255 HWTEST_F(ParserTest, HtraceAndEbpfParserTest, TestSize.Level1)
256 {
257     TS_LOGI("test25-4");
258     const std::string tracePath = "data/resource/htrace_ebpf.bin";
259     const std::string utGoldDb = "data/resource/htrace_ebpf_gold.db";
260     const std::string dbPath = "data/resource/out_db4.db";
261     constexpr size_t readSize = 1024;
262     constexpr uint32_t lineLength = 256;
263 
264     if (access(tracePath.c_str(), F_OK) == 0) {
265         std::unique_ptr<SysTuning::TraceStreamer::TraceStreamerSelector> ta =
266             std::make_unique<SysTuning::TraceStreamer::TraceStreamerSelector>();
267         ta->EnableMetaTable(false);
268         ta->SetCleanMode(false);
269         int fd(base::OpenFile(tracePath, O_RDONLY, G_FILE_PERMISSION));
270         while (true) {
271             std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(readSize);
272             auto rsize = base::Read(fd, buf.get(), readSize);
273 
274             if (rsize == 0) {
275                 break;
276             }
277             if (rsize < 0) {
278                 TS_LOGD("Reading trace file over (errno: %d, %s)", errno, strerror(errno));
279                 break;
280             }
281             if (!ta->ParseTraceDataSegment(std::move(buf), rsize)) {
282                 break;
283             };
284         }
285         ta->WaitForParserEnd();
286         close(fd);
287         ta->ExportDatabase(dbPath);
288         ta->Clear();
289         EXPECT_TRUE(access(dbPath.c_str(), F_OK) == 0);
290     } else {
291         EXPECT_TRUE(false);
292     }
293 
294     if (access(utGoldDb.c_str(), F_OK) == 0) {
295         FILE* file1 = nullptr;
296         FILE* file2 = nullptr;
297         char line1[lineLength];
298         char line2[lineLength];
299         const std::string command1 = "md5sum data/resource/htrace_ebpf_gold.db";
300         const std::string md5DbPath = "md5sum "+ dbPath;
301         file1 = popen(command1.c_str(), "r");
302         file2 = popen(md5DbPath.c_str(), "r");
303         if (file1 && file2) {
304             if (fgets(line1, lineLength, file1) != nullptr && fgets(line2, lineLength, file2) != nullptr) {
305                 std::string str1(line1);
306                 std::string str2(line2);
307                 str1 = str1.substr(0, str1.find_first_of(' '));
308                 str2 = str2.substr(0, str2.find_first_of(' '));
309                 EXPECT_TRUE(str1.compare(str2) == 0);
310             }
311         }
312     } else {
313         EXPECT_TRUE(false);
314     }
315 
316     if (access(dbPath.c_str(), F_OK) == 0) {
317         remove(dbPath.c_str());
318     }
319 }
320 } // namespace TraceStreamer
321 } // namespace SysTuning
322