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