• 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 "perf_file_writer_test.h"
17 
18 #include "virtual_runtime.h"
19 
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 namespace Developtools {
24 namespace HiPerf {
25 const uint TESTRECORDCOUNT = 10;
26 
27 class PerfFileWriterTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33 
34     static void TestEventDescInit(std::vector<AttrWithId> &eventDesc);
35     static const uint32_t TESTNUMBER1 = 1;
36     static const uint32_t TESTNUMBER2 = 2;
37     static const uint32_t TESTNUMBER3 = 3;
38     static const uint32_t TESTNUMBER4 = 4;
39     static const uint32_t TESTNUMBER5 = 5;
40 };
41 
SetUpTestCase()42 void PerfFileWriterTest::SetUpTestCase() {}
43 
TearDownTestCase()44 void PerfFileWriterTest::TearDownTestCase() {}
45 
SetUp()46 void PerfFileWriterTest::SetUp() {}
47 
TearDown()48 void PerfFileWriterTest::TearDown() {}
49 
50 /**
51  * @tc.name: Test
52  * @tc.desc: push several records of all type into the buffer
53  * @tc.type: FUNC
54  */
TestEventDescInit(std::vector<AttrWithId> & eventDesc)55 void PerfFileWriterTest::TestEventDescInit(std::vector<AttrWithId> &eventDesc)
56 {
57     for (uint32_t nr = TESTNUMBER2; nr > 0; nr--) {
58         auto &item = eventDesc.emplace_back();
59         item.attr = {};
60         item.attr.size = 0;
61         item.attr.type = TESTNUMBER1 * nr;
62         item.attr.config = TESTNUMBER2 * nr;
63         item.attr.__reserved_2 = TESTNUMBER3 * nr;
64         item.ids = {TESTNUMBER4 * nr, TESTNUMBER5 * nr};
65         item.name = "test event desc";
66     }
67 }
68 
69 HWTEST_F(PerfFileWriterTest, TestFileWriter_Normal, TestSize.Level1)
70 {
71     std::string filename = "./TestFileWriter_Normal";
72     PerfFileWriter fileWriter;
73     ASSERT_TRUE(fileWriter.Open(filename)) << "current path no write permission?";
74 
75     std::vector<AttrWithId> attrIds;
76     AttrWithId attrId;
77     perf_event_attr attr;
78     attrId.attr = attr;
79     attrId.ids.emplace_back(0);
80     attrIds.emplace_back(attrId);
81     ASSERT_TRUE(fileWriter.WriteAttrAndId(attrIds));
82 
83     uint64_t dataSize = 0;
84     for (uint i = 0; i < TESTRECORDCOUNT; i++) {
85         std::string testStr = "testFeature " + std::to_string(i);
86         PerfRecordMmap recordmmap(true, i, i, i, i, i, testStr);
87         dataSize += recordmmap.GetSize();
88         ASSERT_TRUE(fileWriter.WriteRecord(recordmmap));
89         ASSERT_TRUE(fileWriter.AddStringFeature(FEATURE::RESERVED, testStr));
90         ASSERT_TRUE(fileWriter.AddNrCpusFeature(FEATURE::RESERVED, 0, TESTNUMBER1));
91         ASSERT_TRUE(fileWriter.AddU64Feature(FEATURE::RESERVED, TESTNUMBER2));
92         std::vector<AttrWithId> eventDesces;
93         TestEventDescInit(eventDesces);
94         ASSERT_TRUE(fileWriter.AddEventDescFeature(FEATURE::RESERVED, eventDesces));
95         VirtualRuntime vr;
96         ASSERT_TRUE(fileWriter.AddSymbolsFeature(vr.GetSymbolsFiles()));
97     }
98     ASSERT_EQ(fileWriter.GetDataSize(), dataSize);
99     ASSERT_EQ(fileWriter.GetRecordCount(), TESTRECORDCOUNT);
100     ASSERT_TRUE(fileWriter.Close());
101     // check file
102     ASSERT_TRUE((access(filename.c_str(), F_OK) == 0));
103 }
104 
105 HWTEST_F(PerfFileWriterTest, TestFileWriter_Compress, TestSize.Level1)
106 {
107     std::string filename = "./TestFileWriter_Compress";
108     PerfFileWriter fileWriter;
109     ASSERT_TRUE(fileWriter.Open(filename, true)) << "current path no write permission?";
110 
111     std::vector<AttrWithId> attrIds;
112     AttrWithId attrId;
113     perf_event_attr attr;
114     attrId.attr = attr;
115     attrId.ids.emplace_back(0);
116     attrIds.emplace_back(attrId);
117     ASSERT_TRUE(fileWriter.WriteAttrAndId(attrIds));
118 
119     uint64_t dataSize = 0;
120     for (uint i = 0; i < TESTRECORDCOUNT; i++) {
121         std::string testStr = "testFeature " + std::to_string(i);
122         PerfRecordMmap recordmmap(true, i, i, i, i, i, testStr);
123         dataSize += recordmmap.GetSize();
124         ASSERT_TRUE(fileWriter.WriteRecord(recordmmap));
125         ASSERT_TRUE(fileWriter.AddStringFeature(FEATURE::RESERVED, testStr));
126         ASSERT_TRUE(fileWriter.AddNrCpusFeature(FEATURE::RESERVED, 0, TESTNUMBER1));
127         ASSERT_TRUE(fileWriter.AddU64Feature(FEATURE::RESERVED, TESTNUMBER2));
128         VirtualRuntime vr;
129         ASSERT_TRUE(fileWriter.AddSymbolsFeature(vr.GetSymbolsFiles()));
130     }
131     ASSERT_EQ(fileWriter.GetDataSize(), dataSize);
132     ASSERT_EQ(fileWriter.GetRecordCount(), TESTRECORDCOUNT);
133     ASSERT_TRUE(fileWriter.Close());
134     // check file
135     ASSERT_TRUE((access(filename.c_str(), F_OK) == 0));
136     filename += ".gz";
137     ASSERT_TRUE((access(filename.c_str(), F_OK) != 0));
138 }
139 
140 HWTEST_F(PerfFileWriterTest, TestFileWriter_AuxTraceInfo, TestSize.Level1)
141 {
142     std::string filename = "./TestFileWriter_AuxTraceInfo";
143     PerfFileWriter fileWriter;
144     ASSERT_TRUE(fileWriter.Open(filename)) << "current path no write permission?";
145 
146     std::vector<AttrWithId> attrIds;
147     AttrWithId attrId;
148     perf_event_attr attr;
149     attrId.attr = attr;
150     attrId.ids.emplace_back(0);
151     attrIds.emplace_back(attrId);
152     uint64_t dataSize = 0;
153     ASSERT_TRUE(fileWriter.WriteAttrAndId(attrIds, true));
154     dataSize = dataSize + sizeof(perf_event_header) + sizeof(PerfRecordTtimeConvData) +
155                sizeof(perf_event_header) + sizeof(PerfRecordAuxtraceInfoData) +
156                sizeof(perf_event_header) + sizeof(PerfRecordCpuMapData);
157 
158     for (uint i = 0; i < TESTRECORDCOUNT; i++) {
159         std::string testStr = "testFeature " + std::to_string(i);
160         PerfRecordMmap recordmmap(true, i, i, i, i, i, testStr);
161         dataSize += recordmmap.GetSize();
162         ASSERT_TRUE(fileWriter.WriteRecord(recordmmap));
163         ASSERT_TRUE(fileWriter.AddStringFeature(FEATURE::RESERVED, testStr));
164         ASSERT_TRUE(fileWriter.AddNrCpusFeature(FEATURE::RESERVED, 0, TESTNUMBER1));
165         ASSERT_TRUE(fileWriter.AddU64Feature(FEATURE::RESERVED, TESTNUMBER2));
166         std::vector<AttrWithId> eventDesces;
167         TestEventDescInit(eventDesces);
168         ASSERT_TRUE(fileWriter.AddEventDescFeature(FEATURE::RESERVED, eventDesces));
169         VirtualRuntime vr;
170         ASSERT_TRUE(fileWriter.AddSymbolsFeature(vr.GetSymbolsFiles()));
171     }
172     ASSERT_EQ(fileWriter.GetDataSize(), dataSize);
173     ASSERT_EQ(fileWriter.GetRecordCount(), TESTRECORDCOUNT);
174     ASSERT_TRUE(fileWriter.Close());
175     // check file
176     ASSERT_TRUE((access(filename.c_str(), F_OK) == 0));
177 }
178 
179 HWTEST_F(PerfFileWriterTest, TestFileWriter_NoWriteAttrAndId, TestSize.Level1)
180 {
181     std::string filename = "./TestFileWriter_NoWriteAttrAndId";
182     PerfFileWriter fileWriter;
183     ASSERT_TRUE(fileWriter.Open(filename)) << "current path no write permission?";
184 
185     for (uint i = 0; i < TESTRECORDCOUNT; i++) {
186         std::string testStr = "test " + std::to_string(i);
187         ASSERT_TRUE(fileWriter.AddStringFeature(FEATURE::RESERVED, testStr));
188         VirtualRuntime vr;
189         ASSERT_TRUE(fileWriter.AddSymbolsFeature(vr.GetSymbolsFiles()));
190     }
191 
192     std::string testStr = "testFeature ";
193     PerfRecordMmap recordmmap(true, 0, 0, 0, 0, 0, testStr);
194     ASSERT_FALSE(fileWriter.WriteRecord(recordmmap));
195     ASSERT_TRUE(fileWriter.Close());
196     // check file
197     ASSERT_TRUE((access(filename.c_str(), F_OK) == 0));
198 }
199 } // namespace HiPerf
200 } // namespace Developtools
201 } // namespace OHOS
202