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 "trace_streamer_selector.h"
17 #include <algorithm>
18 #include <chrono>
19 #include <cstdlib>
20 #include <functional>
21 #include <regex>
22 #include "animation_filter.h"
23 #include "app_start_filter.h"
24 #include "args_filter.h"
25 #include "binder_filter.h"
26 #include "clock_filter_ex.h"
27 #include "cpu_filter.h"
28 #include "file.h"
29 #include "filter_filter.h"
30 #include "frame_filter.h"
31 #ifdef ENABLE_HISYSEVENT
32 #include "hi_sysevent_measure_filter.h"
33 #endif
34 #include "irq_filter.h"
35 #include "measure_filter.h"
36 #include "task_pool_filter.h"
37 #include "ptreader_parser.h"
38 #include "pbreader_parser.h"
39 #ifdef ENABLE_RAWTRACE
40 #include "rawtrace_parser.h"
41 #endif
42 #ifdef ENABLE_HIPERF
43 #include "perf_data_filter.h"
44 #endif
45 #include "process_filter.h"
46 #include "slice_filter.h"
47 #include "stat_filter.h"
48 #include "string_help.h"
49 #include "system_event_measure_filter.h"
50
51 namespace {
52 const uint32_t CHUNK_SIZE = 1024 * 1024;
53 constexpr uint16_t RAW_TRACE_MAGIC_NUMBER = 57161;
54 } // namespace
55 using namespace SysTuning::base;
56 namespace SysTuning {
57 namespace TraceStreamer {
58 namespace {
IsHisysEventData(const std::string & bytraceMode)59 bool IsHisysEventData(const std::string &bytraceMode)
60 {
61 auto firstLine = std::find(bytraceMode.begin(), bytraceMode.end(), '}');
62 if (firstLine == bytraceMode.end()) {
63 return false;
64 }
65 std::string line(bytraceMode.begin(), ++firstLine);
66 json jMessage;
67 if (!jMessage.accept(line)) {
68 return false;
69 }
70 std::string startStr = R"({"domain_":)";
71 if (!StartWith(line, startStr)) {
72 return false;
73 }
74 return true;
75 }
GuessFileType(const uint8_t * data,size_t size)76 TraceFileType GuessFileType(const uint8_t *data, size_t size)
77 {
78 if (size == 0) {
79 return TRACE_FILETYPE_UN_KNOW;
80 }
81 std::string start(reinterpret_cast<const char *>(data), std::min<size_t>(size, 20));
82 if (start.find("# tracer") != std::string::npos) {
83 return TRACE_FILETYPE_BY_TRACE;
84 }
85 if (start.find("# TRACE") != std::string::npos) {
86 return TRACE_FILETYPE_BY_TRACE;
87 }
88 #ifdef ENABLE_RAWTRACE
89 uint16_t magicNumber = INVALID_UINT16;
90 int ret = memcpy_s(&magicNumber, sizeof(uint16_t), data, sizeof(uint16_t));
91 TS_CHECK_TRUE(ret == EOK, TRACE_FILETYPE_UN_KNOW, "Memcpy FAILED!Error code is %d, data size is %zu.", ret, size);
92 if (magicNumber == RAW_TRACE_MAGIC_NUMBER) {
93 return TRACE_FILETYPE_RAW_TRACE;
94 }
95 #endif
96 std::string lowerStart(start);
97 transform(start.begin(), start.end(), lowerStart.begin(), ::tolower);
98 if ((lowerStart.compare(0, std::string("<!doctype html>").length(), "<!doctype html>") == 0) ||
99 (lowerStart.compare(0, std::string("<html>").length(), "<html>") == 0)) {
100 return TRACE_FILETYPE_BY_TRACE;
101 }
102 if (start.compare(0, std::string("\x0a").length(), "\x0a") == 0) {
103 return TRACE_FILETYPE_UN_KNOW;
104 }
105 if (start.compare(0, std::string("OHOSPROF").length(), "OHOSPROF") == 0) {
106 return TRACE_FILETYPE_H_TRACE;
107 }
108 #ifdef ENABLE_HIPERF
109 if (start.compare(0, std::string("PERFILE2").length(), "PERFILE2") == 0) {
110 return TRACE_FILETYPE_PERF;
111 }
112 #endif
113 const std::regex bytraceMatcher = std::regex(R"(-(\d+)\s+\(?\s*(\d+|-+)?\)?\s?\[(\d+)\]\s*)"
114 R"([a-zA-Z0-9.]{0,5}\s+(\d+\.\d+):\s+(\S+):)");
115 std::smatch matcheLine;
116 std::string bytraceMode(reinterpret_cast<const char *>(data), size);
117 if (std::regex_search(bytraceMode, matcheLine, bytraceMatcher)) {
118 return TRACE_FILETYPE_BY_TRACE;
119 }
120
121 const std::regex hilogMatcher = std::regex(R"( *(\w+ )?([\-\d: ]+\.\d+) +(\d+) +(\d+) +([FEWID]) +(.+?): +(.+))");
122 if (std::regex_search(bytraceMode, matcheLine, hilogMatcher)) {
123 return TRACE_FILETYPE_HILOG;
124 }
125 // Identify hisysevent data
126 if (IsHisysEventData(bytraceMode)) {
127 return TRACE_FILETYPE_HI_SYSEVENT;
128 }
129 return TRACE_FILETYPE_UN_KNOW;
130 }
131 } // namespace
132
TraceStreamerSelector()133 TraceStreamerSelector::TraceStreamerSelector()
134 : ptreaderParser_(nullptr),
135 pbreaderParser_(nullptr),
136 #ifdef ENABLE_RAWTRACE
137 rawTraceParser_(nullptr),
138 #endif
139 fileType_(TRACE_FILETYPE_UN_KNOW)
140 {
141 InitFilter();
142 }
~TraceStreamerSelector()143 TraceStreamerSelector::~TraceStreamerSelector() {}
144
InitFilter()145 void TraceStreamerSelector::InitFilter()
146 {
147 streamFilters_ = std::make_unique<TraceStreamerFilters>();
148 traceDataCache_ = std::make_unique<TraceDataCache>();
149 streamFilters_->animationFilter_ = std::make_unique<AnimationFilter>(traceDataCache_.get(), streamFilters_.get());
150 streamFilters_->cpuFilter_ = std::make_unique<CpuFilter>(traceDataCache_.get(), streamFilters_.get());
151 streamFilters_->sliceFilter_ = std::make_unique<SliceFilter>(traceDataCache_.get(), streamFilters_.get());
152
153 streamFilters_->processFilter_ = std::make_unique<ProcessFilter>(traceDataCache_.get(), streamFilters_.get());
154 streamFilters_->clockFilter_ = std::make_unique<ClockFilterEx>(traceDataCache_.get(), streamFilters_.get());
155 streamFilters_->filterFilter_ = std::make_unique<FilterFilter>(traceDataCache_.get(), streamFilters_.get());
156 streamFilters_->cpuMeasureFilter_ =
157 std::make_unique<MeasureFilter>(traceDataCache_.get(), streamFilters_.get(), E_CPU_MEASURE_FILTER);
158 streamFilters_->processMeasureFilter_ =
159 std::make_unique<MeasureFilter>(traceDataCache_.get(), streamFilters_.get(), E_PROCESS_MEASURE_FILTER);
160 streamFilters_->statFilter_ = std::make_unique<StatFilter>(traceDataCache_.get(), streamFilters_.get());
161 streamFilters_->binderFilter_ = std::make_unique<BinderFilter>(traceDataCache_.get(), streamFilters_.get());
162 streamFilters_->argsFilter_ = std::make_unique<ArgsFilter>(traceDataCache_.get(), streamFilters_.get());
163 streamFilters_->irqFilter_ = std::make_unique<IrqFilter>(traceDataCache_.get(), streamFilters_.get());
164 streamFilters_->frameFilter_ = std::make_unique<FrameFilter>(traceDataCache_.get(), streamFilters_.get());
165 streamFilters_->clockRateFilter_ =
166 std::make_unique<MeasureFilter>(traceDataCache_.get(), streamFilters_.get(), E_CLOCK_RATE_FILTER);
167 streamFilters_->clockEnableFilter_ =
168 std::make_unique<MeasureFilter>(traceDataCache_.get(), streamFilters_.get(), E_CLOCK_ENABLE_FILTER);
169 streamFilters_->clockDisableFilter_ =
170 std::make_unique<MeasureFilter>(traceDataCache_.get(), streamFilters_.get(), E_CLOCK_DISABLE_FILTER);
171 streamFilters_->clkRateFilter_ =
172 std::make_unique<MeasureFilter>(traceDataCache_.get(), streamFilters_.get(), E_CLK_RATE_FILTER);
173 streamFilters_->clkEnableFilter_ =
174 std::make_unique<MeasureFilter>(traceDataCache_.get(), streamFilters_.get(), E_CLK_ENABLE_FILTER);
175 streamFilters_->clkDisableFilter_ =
176 std::make_unique<MeasureFilter>(traceDataCache_.get(), streamFilters_.get(), E_CLK_DISABLE_FILTER);
177 streamFilters_->sysEventMemMeasureFilter_ =
178 std::make_unique<SystemEventMeasureFilter>(traceDataCache_.get(), streamFilters_.get(), E_SYS_MEMORY_FILTER);
179 streamFilters_->sysEventVMemMeasureFilter_ = std::make_unique<SystemEventMeasureFilter>(
180 traceDataCache_.get(), streamFilters_.get(), E_SYS_VIRTUAL_MEMORY_FILTER);
181 streamFilters_->appStartupFilter_ = std::make_unique<APPStartupFilter>(traceDataCache_.get(), streamFilters_.get());
182 #ifdef ENABLE_HIPERF
183 streamFilters_->perfDataFilter_ = std::make_unique<PerfDataFilter>(traceDataCache_.get(), streamFilters_.get());
184 #endif
185 streamFilters_->sysEventSourceFilter_ = std::make_unique<SystemEventMeasureFilter>(
186 traceDataCache_.get(), streamFilters_.get(), E_SYS_EVENT_SOURCE_FILTER);
187 #ifdef ENABLE_HISYSEVENT
188 streamFilters_->hiSysEventMeasureFilter_ =
189 std::make_unique<HiSysEventMeasureFilter>(traceDataCache_.get(), streamFilters_.get());
190 #endif
191 streamFilters_->taskPoolFilter_ = std::make_unique<TaskPoolFilter>(traceDataCache_.get(), streamFilters_.get());
192 }
193
WaitForParserEnd()194 void TraceStreamerSelector::WaitForParserEnd()
195 {
196 if (fileType_ == TRACE_FILETYPE_H_TRACE) {
197 pbreaderParser_->WaitForParserEnd();
198 }
199 if (fileType_ == TRACE_FILETYPE_BY_TRACE || fileType_ == TRACE_FILETYPE_HILOG ||
200 fileType_ == TRACE_FILETYPE_HI_SYSEVENT) {
201 ptreaderParser_->WaitForParserEnd();
202 }
203 #ifdef ENABLE_HIPERF
204 if (fileType_ == TRACE_FILETYPE_PERF) {
205 pbreaderParser_->TraceDataSegmentEnd(false);
206 pbreaderParser_->WaitForParserEnd();
207 }
208 #endif
209 #ifdef ENABLE_RAWTRACE
210 if (fileType_ == TRACE_FILETYPE_RAW_TRACE) {
211 rawTraceParser_->WaitForParserEnd();
212 }
213 #endif
214 traceDataCache_->UpdateTraceRange();
215 if (traceDataCache_->AnimationTraceEnabled()) {
216 streamFilters_->animationFilter_->UpdateFrameInfo();
217 streamFilters_->animationFilter_->UpdateDynamicFrameInfo();
218 }
219 }
220
GetMetaData()221 MetaData *TraceStreamerSelector::GetMetaData()
222 {
223 return traceDataCache_->GetMetaData();
224 }
225
SetDataType(TraceFileType type)226 void TraceStreamerSelector::SetDataType(TraceFileType type)
227 {
228 fileType_ = type;
229 if (fileType_ == TRACE_FILETYPE_H_TRACE) {
230 pbreaderParser_ = std::make_unique<PbreaderParser>(traceDataCache_.get(), streamFilters_.get());
231 } else if (fileType_ == TRACE_FILETYPE_BY_TRACE) {
232 ptreaderParser_ = std::make_unique<PtreaderParser>(traceDataCache_.get(), streamFilters_.get());
233 }
234 #ifdef ENABLE_RAWTRACE
235 else if (fileType_ == TRACE_FILETYPE_RAW_TRACE) {
236 rawTraceParser_ = std::make_unique<RawTraceParser>(traceDataCache_.get(), streamFilters_.get());
237 }
238 #endif
239 }
240 // only support parse long trace profiler_data_xxxxxxxx_xxxxxx_x.htrace
BatchParseTraceDataSegment(std::unique_ptr<uint8_t[]> data,size_t size)241 bool TraceStreamerSelector::BatchParseTraceDataSegment(std::unique_ptr<uint8_t[]> data, size_t size)
242 {
243 if (size == 0) {
244 return true;
245 }
246 if (fileType_ == TRACE_FILETYPE_UN_KNOW) {
247 fileType_ = GuessFileType(data.get(), size);
248 if (fileType_ != TRACE_FILETYPE_H_TRACE) {
249 TS_LOGE("File type is not supported in this mode!");
250 return false;
251 }
252 pbreaderParser_ = std::make_unique<PbreaderParser>(traceDataCache_.get(), streamFilters_.get());
253 #ifdef ENABLE_HTRACE
254 pbreaderParser_->EnableOnlyParseFtrace();
255 #endif
256 }
257 pbreaderParser_->ParseTraceDataSegment(std::move(data), size);
258 return true;
259 }
260
GetMarkPositionData(std::unique_ptr<uint8_t[]> & data,size_t & size)261 void TraceStreamerSelector::GetMarkPositionData(std::unique_ptr<uint8_t[]> &data, size_t &size)
262 {
263 if (!markHeard_) {
264 std::string markStr(reinterpret_cast<const char *>(data.get()), size);
265 auto foundPos = markStr.find("MarkPositionJSON->");
266 if (foundPos == std::string::npos) {
267 // trace not MarkPosition,parse trace data
268 hasGotMarkFinish_ = true;
269 return;
270 }
271 // found MarkPosition
272 markHeard_ = true;
273 }
274 auto pos = std::find(data.get(), data.get() + size, '\n');
275 if (pos != data.get() + size) {
276 hasGotMarkFinish_ = true;
277 // Calculate the size of mark position information (include '\n')
278 auto curMarkSize = pos - data.get() + 1;
279 // Move the data pointer to the starting position of the remaining data
280 // The remaining data size is equal to the data size minus the current markinfo size
281 size -= curMarkSize;
282 auto remainingData = std::make_unique<uint8_t>(size);
283 memcpy_s(remainingData.get(), size, data.get() + curMarkSize, size);
284 data.reset(remainingData.release());
285 }
286 }
InitializeParser()287 void TraceStreamerSelector::InitializeParser()
288 {
289 if (fileType_ == TRACE_FILETYPE_H_TRACE || fileType_ == TRACE_FILETYPE_PERF) {
290 pbreaderParser_ = std::make_unique<PbreaderParser>(traceDataCache_.get(), streamFilters_.get());
291 #ifdef ENABLE_ARKTS
292 pbreaderParser_->EnableFileSeparate(enableFileSeparate_);
293 #endif
294 } else if (fileType_ == TRACE_FILETYPE_BY_TRACE || fileType_ == TRACE_FILETYPE_HI_SYSEVENT ||
295 fileType_ == TRACE_FILETYPE_HILOG) {
296 ptreaderParser_ = std::make_unique<PtreaderParser>(traceDataCache_.get(), streamFilters_.get(), fileType_);
297 #ifdef ENABLE_BYTRACE
298 ptreaderParser_->EnableBytrace(fileType_ == TRACE_FILETYPE_BY_TRACE);
299 #endif
300 #ifdef ENABLE_RAWTRACE
301 } else if (fileType_ == TRACE_FILETYPE_RAW_TRACE) {
302 rawTraceParser_ = std::make_unique<RawTraceParser>(traceDataCache_.get(), streamFilters_.get());
303 #endif
304 }
305 }
306
ProcessTraceData(std::unique_ptr<uint8_t[]> data,size_t size,int32_t isFinish)307 void TraceStreamerSelector::ProcessTraceData(std::unique_ptr<uint8_t[]> data, size_t size, int32_t isFinish)
308 {
309 if (fileType_ == TRACE_FILETYPE_H_TRACE) {
310 pbreaderParser_->ParseTraceDataSegment(std::move(data), size);
311 } else if (fileType_ == TRACE_FILETYPE_BY_TRACE || fileType_ == TRACE_FILETYPE_HI_SYSEVENT ||
312 fileType_ == TRACE_FILETYPE_HILOG) {
313 ptreaderParser_->ParseTraceDataSegment(std::move(data), size, isFinish);
314 return;
315 } else if (fileType_ == TRACE_FILETYPE_PERF) {
316 #ifdef ENABLE_HIPERF
317 pbreaderParser_->StoreTraceDataSegment(std::move(data), size, isFinish);
318 #endif
319 } else if (fileType_ == TRACE_FILETYPE_RAW_TRACE) {
320 #ifdef ENABLE_RAWTRACE
321 rawTraceParser_->ParseTraceDataSegment(std::move(data), size, isFinish);
322 #endif
323 }
324
325 SetAnalysisResult(TRACE_PARSER_NORMAL);
326 }
327
ParseTraceDataSegment(std::unique_ptr<uint8_t[]> data,size_t size,bool isSplitFile,int32_t isFinish)328 bool TraceStreamerSelector::ParseTraceDataSegment(std::unique_ptr<uint8_t[]> data,
329 size_t size,
330 bool isSplitFile,
331 int32_t isFinish)
332 {
333 if (size == 0) {
334 return true;
335 }
336 #if !IS_WASM
337 // if in the linux,hasGotMarkFinish_ = fasle, get markinfo
338 if (!hasGotMarkFinish_) {
339 GetMarkPositionData(data, size);
340 if (!hasGotMarkFinish_) {
341 // Believing that the markInfo data has not been sent completely,Waiting for next send
342 return true;
343 }
344 }
345 #endif
346
347 if (fileType_ == TRACE_FILETYPE_UN_KNOW) {
348 fileType_ = GuessFileType(data.get(), size);
349 if (fileType_ == TRACE_FILETYPE_UN_KNOW) {
350 SetAnalysisResult(TRACE_PARSER_FILE_TYPE_ERROR);
351 TS_LOGI(
352 "File type is not supported!,\nthe head content is:%s\n ---warning!!!---\n"
353 "File type is not supported!,\n",
354 data.get());
355 return false;
356 }
357 InitializeParser();
358 }
359 traceDataCache_->SetSplitFileMinTime(minTs_);
360 traceDataCache_->SetSplitFileMaxTime(maxTs_);
361 traceDataCache_->isSplitFile_ = isSplitFile;
362 ProcessTraceData(std::move(data), size, isFinish);
363
364 #if !IS_WASM
365 // in the linux,isFinish = 1,clear markinfo
366 if (isFinish) {
367 ClearMarkPositionInfo();
368 }
369 #endif
370 return true;
371 }
EnableMetaTable(bool enabled)372 void TraceStreamerSelector::EnableMetaTable(bool enabled)
373 {
374 traceDataCache_->EnableMetaTable(enabled);
375 }
376
EnableFileSave(bool enabled)377 void TraceStreamerSelector::EnableFileSave(bool enabled)
378 {
379 enableFileSeparate_ = enabled;
380 }
381
SetCleanMode(bool cleanMode)382 void TraceStreamerSelector::SetCleanMode(bool cleanMode)
383 {
384 g_curLogLevel = LOG_OFF;
385 }
386
ExportDatabase(const std::string & outputName,TraceDataDB::ResultCallBack resultCallBack)387 int32_t TraceStreamerSelector::ExportDatabase(const std::string &outputName, TraceDataDB::ResultCallBack resultCallBack)
388 {
389 traceDataCache_->UpdateTraceRange();
390 return traceDataCache_->ExportDatabase(outputName, resultCallBack);
391 }
CreatEmptyBatchDB(const std::string dbPath)392 int32_t TraceStreamerSelector::CreatEmptyBatchDB(const std::string dbPath)
393 {
394 return traceDataCache_->CreatEmptyBatchDB(dbPath);
395 }
BatchExportDatabase(const std::string & outputName)396 int32_t TraceStreamerSelector::BatchExportDatabase(const std::string &outputName)
397 {
398 traceDataCache_->UpdateTraceRange();
399 return traceDataCache_->BatchExportDatabase(outputName);
400 }
RevertTableName(const std::string & outputName)401 void TraceStreamerSelector::RevertTableName(const std::string &outputName)
402 {
403 return traceDataCache_->RevertTableName(outputName);
404 }
ExportPerfReadableText(const std::string & outputName,TraceDataDB::ResultCallBack resultCallBack)405 int32_t TraceStreamerSelector::ExportPerfReadableText(const std::string &outputName,
406 TraceDataDB::ResultCallBack resultCallBack)
407 {
408 traceDataCache_->UpdateTraceRange();
409 return traceDataCache_->ExportPerfReadableText(outputName, resultCallBack);
410 }
411
ExportHookReadableText(const std::string & outputName,TraceDataDB::ResultCallBack resultCallBack)412 int32_t TraceStreamerSelector::ExportHookReadableText(const std::string &outputName,
413 TraceDataDB::ResultCallBack resultCallBack)
414 {
415 traceDataCache_->UpdateTraceRange();
416 return traceDataCache_->ExportHookReadableText(outputName, resultCallBack);
417 }
418
ExportEbpfReadableText(const std::string & outputName,TraceDataDB::ResultCallBack resultCallBack)419 int32_t TraceStreamerSelector::ExportEbpfReadableText(const std::string &outputName,
420 TraceDataDB::ResultCallBack resultCallBack)
421 {
422 traceDataCache_->UpdateTraceRange();
423 return traceDataCache_->ExportEbpfReadableText(outputName, resultCallBack);
424 }
425
ReloadSymbolFiles(std::string & directory,std::vector<std::string> & symbolsPaths)426 bool TraceStreamerSelector::ReloadSymbolFiles(std::string &directory, std::vector<std::string> &symbolsPaths)
427 {
428 TS_LOGE("directory is %s", directory.c_str());
429 for (auto file : symbolsPaths) {
430 TS_LOGE("files is %s", file.c_str());
431 }
432 return pbreaderParser_->ReparseSymbolFilesAndResymbolization(directory, symbolsPaths);
433 }
Clear()434 void TraceStreamerSelector::Clear()
435 {
436 traceDataCache_->Prepare();
437 traceDataCache_->Clear();
438 }
SearchData()439 std::vector<std::string> TraceStreamerSelector::SearchData()
440 {
441 return traceDataCache_->SearchData();
442 }
OperateDatabase(const std::string & sql)443 int32_t TraceStreamerSelector::OperateDatabase(const std::string &sql)
444 {
445 return traceDataCache_->OperateDatabase(sql);
446 }
SearchDatabase(const std::string & sql,TraceDataDB::ResultCallBack resultCallBack)447 int32_t TraceStreamerSelector::SearchDatabase(const std::string &sql, TraceDataDB::ResultCallBack resultCallBack)
448 {
449 return traceDataCache_->SearchDatabase(sql, resultCallBack);
450 }
SearchDatabaseToProto(const std::string & data,SqllitePreparCacheData::TLVResultCallBack resultCallBack)451 int32_t TraceStreamerSelector::SearchDatabaseToProto(const std::string &data,
452 SqllitePreparCacheData::TLVResultCallBack resultCallBack)
453 {
454 return traceDataCache_->SearchDatabaseToProto(data, resultCallBack);
455 }
SearchDatabase(const std::string & sql,uint8_t * out,int32_t outLen)456 int32_t TraceStreamerSelector::SearchDatabase(const std::string &sql, uint8_t *out, int32_t outLen)
457 {
458 return traceDataCache_->SearchDatabase(sql, out, outLen);
459 }
SearchDatabase(std::string & sql,bool printf)460 int32_t TraceStreamerSelector::SearchDatabase(std::string &sql, bool printf)
461 {
462 return traceDataCache_->SearchDatabase(sql, printf);
463 }
SearchDatabase(const std::string & sql)464 std::string TraceStreamerSelector::SearchDatabase(const std::string &sql)
465 {
466 return traceDataCache_->SearchDatabase(sql);
467 }
InitMetricsMap(std::map<std::string,std::string> & metricsMap)468 void TraceStreamerSelector::InitMetricsMap(std::map<std::string, std::string> &metricsMap)
469 {
470 metricsMap.emplace(TRACE_MEM_UNAGG, MEM_UNAGG_QUERY);
471 metricsMap.emplace(TRACE_MEM, MEM_QUERY);
472 metricsMap.emplace(TRACE_MEM_TOP_TEN, MEM_TOP_QUERY);
473 metricsMap.emplace(TRACE_METADATA, META_DATA_QUERY);
474 metricsMap.emplace(SYS_CALLS, SYS_CALL_QUERY);
475 metricsMap.emplace(TRACE_STATS, TRACE_STATE_QUERY);
476 metricsMap.emplace(TRACE_TASK_NAMES, TRACE_TASK_NAME);
477 }
MetricsSqlQuery(const std::string & metrics)478 const std::string TraceStreamerSelector::MetricsSqlQuery(const std::string &metrics)
479 {
480 std::map<std::string, std::string> metricsMap;
481 InitMetricsMap(metricsMap);
482 auto itor = metricsMap.find(metrics);
483 if (itor == metricsMap.end()) {
484 TS_LOGE("metrics name error!!!");
485 return "";
486 }
487 return itor->second;
488 }
UpdateTraceRangeTime(uint8_t * data,int32_t len)489 int32_t TraceStreamerSelector::UpdateTraceRangeTime(uint8_t *data, int32_t len)
490 {
491 std::string traceRangeStr;
492 (void)memcpy_s(&traceRangeStr, len, data, len);
493 std::vector<std::string> vTraceRangeStr = SplitStringToVec(traceRangeStr, ";");
494 uint64_t minTs = std::stoull(vTraceRangeStr.at(0));
495 uint64_t maxTs = std::stoull(vTraceRangeStr.at(1));
496 traceDataCache_->UpdateTraceTime(minTs);
497 traceDataCache_->UpdateTraceTime(maxTs);
498 return 0;
499 }
SetCancel(bool cancel)500 void TraceStreamerSelector::SetCancel(bool cancel)
501 {
502 traceDataCache_->SetCancel(cancel);
503 }
UpdateBinderRunnableTraceStatus(bool status)504 void TraceStreamerSelector::UpdateBinderRunnableTraceStatus(bool status)
505 {
506 traceDataCache_->UpdateBinderRunnableTraceStatus(status);
507 }
UpdateAnimationTraceStatus(bool status)508 void TraceStreamerSelector::UpdateAnimationTraceStatus(bool status)
509 {
510 traceDataCache_->UpdateAnimationTraceStatus(status);
511 }
UpdateTaskPoolTraceStatus(bool status)512 void TraceStreamerSelector::UpdateTaskPoolTraceStatus(bool status)
513 {
514 traceDataCache_->UpdateTaskPoolTraceStatus(status);
515 }
UpdateAppStartTraceStatus(bool status)516 void TraceStreamerSelector::UpdateAppStartTraceStatus(bool status)
517 {
518 traceDataCache_->UpdateAppStartTraceStatus(status);
519 }
UpdateHMKernelTraceStatus(bool status)520 void TraceStreamerSelector::UpdateHMKernelTraceStatus(bool status)
521 {
522 traceDataCache_->UpdateHMKernelTraceStatus(status);
523 }
LoadQueryFile(const std::string & sqlOperator,std::vector<std::string> & sqlStrings)524 bool TraceStreamerSelector::LoadQueryFile(const std::string &sqlOperator, std::vector<std::string> &sqlStrings)
525 {
526 std::ifstream file(sqlOperator);
527 if (!file.is_open()) {
528 TS_LOGE("open file failed!");
529 }
530 std::string sqlString;
531 std::string line;
532 while (std::getline(file, line)) {
533 sqlString += line;
534 }
535 if (!sqlString.empty()) {
536 auto strVec = SplitStringToVec(sqlString, ";");
537 for (auto str : strVec) {
538 auto result = TrimInvisibleCharacters(str);
539 if (!result.empty()) {
540 sqlStrings.push_back(result);
541 }
542 }
543 }
544 file.close();
545 return true;
546 }
ReadSqlFileAndPrintResult(const std::string & sqlOperator)547 bool TraceStreamerSelector::ReadSqlFileAndPrintResult(const std::string &sqlOperator)
548 {
549 std::vector<std::string> sqlStrings;
550 if (!LoadQueryFile(sqlOperator, sqlStrings)) {
551 return false;
552 }
553 if (sqlStrings.empty()) {
554 TS_LOGE("%s is empty!", sqlOperator.c_str());
555 return false;
556 }
557 for (auto &str : sqlStrings) {
558 SearchDatabase(str, true);
559 }
560 return true;
561 }
ParserAndPrintMetrics(const std::string & metrics)562 bool TraceStreamerSelector::ParserAndPrintMetrics(const std::string &metrics)
563 {
564 auto metricsName = SplitStringToVec(metrics, ",");
565 for (const auto &itemName : metricsName) {
566 std::string result = SearchDatabase(MetricsSqlQuery(itemName));
567 if (result == "") {
568 return false;
569 }
570 Metrics metricsOperator;
571 metricsOperator.ParserJson(itemName, result);
572 for (auto item : metricsOperator.GetMetricsMap()) {
573 if (item.second == itemName) {
574 metricsOperator.PrintMetricsResult(item.first, nullptr);
575 continue;
576 }
577 }
578 }
579 return true;
580 }
581 } // namespace TraceStreamer
582 } // namespace SysTuning
583