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 <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 "file.h"
25 #include "trace_streamer_selector.h"
26
27 using namespace testing::ext;
28 using namespace SysTuning::TraceStreamer;
29
30 const std::string TRACE_PATH = "../../test/resource/trace_small_10.systrace";
31 constexpr size_t READ_SIZE = 1024;
32 constexpr uint32_t LINE_LENGTH = 256;
33 constexpr size_t G_FILE_PERMISSION = 664;
34
35 namespace SysTuning {
36 namespace TraceStreamer {
37 class QueryMetricsTest : public ::testing::Test {
38 protected:
SetUp()39 void SetUp() {}
TearDown()40 void TearDown() {}
41 };
42
ParseTraceFile(TraceStreamerSelector & ts)43 void ParseTraceFile(TraceStreamerSelector& ts)
44 {
45 int32_t fd(base::OpenFile(TRACE_PATH, O_RDONLY, G_FILE_PERMISSION));
46 if (fd < 0) {
47 TS_LOGD("Failed to open trace file (errno: %d, %s)", errno, strerror(errno));
48 return;
49 }
50
51 while (true) {
52 std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(READ_SIZE);
53 auto rsize = base::Read(fd, buf.get(), READ_SIZE);
54
55 if (rsize <= 0) {
56 break;
57 }
58
59 if (!ts.ParseTraceDataSegment(std::move(buf), rsize, 0, 1)) {
60 break;
61 }
62 }
63
64 close(fd);
65 ts.WaitForParserEnd();
66 }
67
ExecuteMetricsTest(TraceStreamerSelector & ts,std::string metricName)68 void ExecuteMetricsTest(TraceStreamerSelector& ts, std::string metricName)
69 {
70 ts.EnableMetaTable(false);
71 ts.SetCleanMode(false);
72 ParseTraceFile(ts);
73 bool result = ts.ParserAndPrintMetrics(metricName);
74 EXPECT_EQ(result, true);
75 }
76
77 /**
78 * @tc.name: QueryMetricsWithTraceMem
79 * @tc.desc: Query metrics with trace mem
80 * @tc.type: FUNC
81 */
82 HWTEST_F(QueryMetricsTest, QueryMetricsWithTraceMem, TestSize.Level1)
83 {
84 TS_LOGE("test42-1");
85 TraceStreamerSelector ts;
86 ExecuteMetricsTest(ts, TRACE_MEM);
87 }
88
89 /**
90 * @tc.name: QueryMetricsWithTraceMem
91 * @tc.desc: Query metrics with trace task names
92 * @tc.type: FUNC
93 */
94 HWTEST_F(QueryMetricsTest, QueryMetricsWithTraceMemTop10, TestSize.Level1)
95 {
96 TS_LOGE("test42-2");
97 TraceStreamerSelector ts;
98 ExecuteMetricsTest(ts, TRACE_MEM_TOP_TEN);
99 }
100
101 /**
102 * @tc.name: QueryMetricsWithTraceMem
103 * @tc.desc: Query metrics with trace task names
104 * @tc.type: FUNC
105 */
106 HWTEST_F(QueryMetricsTest, QueryMetricsWithTraceMemUnagg, TestSize.Level1)
107 {
108 TS_LOGE("test42-3");
109 TraceStreamerSelector ts;
110 ExecuteMetricsTest(ts, TRACE_MEM_UNAGG);
111 }
112
113 /**
114 * @tc.name: QueryMetricsWithTraceTaskNames
115 * @tc.desc: Query metrics with trace task names
116 * @tc.type: FUNC
117 */
118 HWTEST_F(QueryMetricsTest, QueryMetricsWithTraceTaskNames, TestSize.Level1)
119 {
120 TS_LOGE("test42-4");
121 TraceStreamerSelector ts;
122 ExecuteMetricsTest(ts, TRACE_TASK_NAMES);
123 }
124
125 /**
126 * @tc.name: QueryMetricsWithTraceStats
127 * @tc.desc: Query metrics with trace stats
128 * @tc.type: FUNC
129 */
130 HWTEST_F(QueryMetricsTest, QueryMetricsWithTraceStats, TestSize.Level1)
131 {
132 TS_LOGE("test42-5");
133 TraceStreamerSelector ts;
134 ExecuteMetricsTest(ts, TRACE_STATS);
135 }
136
137 /**
138 * @tc.name: QueryMetricsWithTraceMetaData
139 * @tc.desc: Query metrics with trace metadata
140 * @tc.type: FUNC
141 */
142 HWTEST_F(QueryMetricsTest, QueryMetricsWithTraceMetaData, TestSize.Level1)
143 {
144 TS_LOGE("test42-6");
145 TraceStreamerSelector ts;
146 ExecuteMetricsTest(ts, TRACE_METADATA);
147 }
148
149 /**
150 * @tc.name: QueryMetricsWithSysCalls
151 * @tc.desc: Query metrics with sys calls
152 * @tc.type: FUNC
153 */
154 HWTEST_F(QueryMetricsTest, QueryMetricsWithSysCalls, TestSize.Level1)
155 {
156 TS_LOGE("test42-7");
157 TraceStreamerSelector ts;
158 ExecuteMetricsTest(ts, SYS_CALLS);
159 }
160
161 } // namespace TraceStreamer
162 } // namespace SysTuning
163