• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 obts_in 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  * limits_tions under the License.
14  */
15 
16 #include <hwext/gtest-ext.h>
17 #include <hwext/gtest-tag.h>
18 #include <fcntl.h>
19 #include <fstream>
20 #include <iostream>
21 #include <string>
22 #include <unistd.h>
23 
24 #include "export_test.h"
25 #include "file.h"
26 #include "trace_streamer_selector.h"
27 
28 using namespace testing::ext;
29 using namespace SysTuning::TraceStreamer;
30 
31 namespace SysTuning {
32 namespace TraceStreamer {
33 
34 class ExportTest : public ::testing::Test {
35 public:
SetUp()36     void SetUp()
37     {
38         ts_.InitFilter();
39     }
40 
TearDown()41     void TearDown() {}
42 
43 public:
44     TraceStreamerSelector ts_;
45 };
46 
ParseTraceFile(TraceStreamerSelector & ts,const std::string & tracePath)47 bool ParseTraceFile(TraceStreamerSelector &ts, const std::string &tracePath)
48 {
49     int32_t fd(base::OpenFile(tracePath, O_RDONLY, G_FILE_PERMISSION));
50     TS_CHECK_TRUE(fd >= 0, false, "Failed to open trace file (errno: %d, %s)", errno, strerror(errno));
51     struct stat statBuff;
52     stat(tracePath.c_str(), &statBuff);
53     size_t curFileSize = statBuff.st_size;
54     auto isFinish = false;
55     size_t curLoadSize = 0;
56     auto curParseCnt = 1;
57     while (true) {
58         // for rawtrace next parse.the first parse is for last comm dats_;
59         if (isFinish && ts.GetFileType() == TRACE_FILETYPE_RAW_TRACE && curParseCnt < RAW_TRACE_PARSE_MAX) {
60             ++curParseCnt;
61             isFinish = false;
62             curLoadSize = 0;
63             TS_CHECK_TRUE(lseek(fd, 0, SEEK_SET) != -1, false, "lseek error:%s", strerror(errno));
64         }
65         std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(G_CHUNK_SIZE);
66         auto rsize = read(fd, buf.get(), G_CHUNK_SIZE);
67         if (rsize == 0) {
68             break;
69         }
70 
71         if (rsize < 0) {
72             TS_LOGE("Reading trace file failed (errno: %d, %s)", errno, strerror(errno));
73             return false;
74         }
75         curLoadSize += rsize;
76         if (curLoadSize == curFileSize) {
77             isFinish = true;
78         }
79         if (!ts.ParseTraceDataSegment(std::move(buf), static_cast<size_t>(rsize), false, isFinish)) {
80             return false;
81         };
82         printf("\rLoadingFile:\t%.2f MB\r", static_cast<double>(curLoadSize) / 1E6);
83     }
84     close(fd);
85     ts.WaitForParserEnd();
86     return true;
87 }
88 
RemoveDirectory(const std::filesystem::path & dirPath)89 void RemoveDirectory(const std::filesystem::path &dirPath)
90 {
91     if (std::filesystem::exists(dirPath) && std::filesystem::is_directory(dirPath)) {
92         std::filesystem::remove_all(dirPath);
93         std::cout << "Successfully removed: " << dirPath << std::endl;
94     } else {
95         std::cout << "Directory does not exist: " << dirPath << std::endl;
96     }
97 }
98 
99 /**
100  * @tc.name: ExportPerfReadableText
101  * @tc.desc: Export Perf Readable Text
102  * @tc.type: FUNC
103  */
104 HWTEST_F(ExportTest, ExportPerfReadableText, TestSize.Level1)
105 {
106     TS_LOGE("test44-1");
107     std::string perfFilePath("../../test/resource/hiprofiler_data_perf.htrace");
108     EXPECT_TRUE(ParseTraceFile(ts_, perfFilePath));
109     std::string errFilePath("/err");
110     std::string sucessFilePath("export_hiprofiler_data_perf.txt");
111     EXPECT_EQ(ts_.ExportPerfReadableText(errFilePath), 1);
112     EXPECT_EQ(ts_.ExportPerfReadableText(sucessFilePath), 0);
113     if (access(sucessFilePath.c_str(), F_OK) == 0) {
114         remove(sucessFilePath.c_str());
115     }
116 }
117 } // namespace TraceStreamer
118 } // namespace SysTuning