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 #ifndef TRACE_STREAMER_SELECTOR_H 17 #define TRACE_STREAMER_SELECTOR_H 18 #include <functional> 19 #include <memory> 20 #include "metrics.h" 21 #include "trace_data_cache.h" 22 #include "trace_streamer_filters.h" 23 24 namespace SysTuning { 25 namespace TraceStreamer { 26 class PtreaderParser; 27 class PbreaderParser; 28 #ifdef ENABLE_RAWTRACE 29 class RawTraceParser; 30 #endif 31 class TraceStreamerSelector { 32 public: 33 TraceStreamerSelector(); 34 ~TraceStreamerSelector(); 35 bool ParseTraceDataSegment(std::unique_ptr<uint8_t[]> data, 36 size_t size, 37 bool isSplitFile, 38 int32_t isFinish, 39 bool isWasmReadFile = false); 40 void EnableMetaTable(bool enabled); 41 void EnableFileSave(bool enabled); 42 static void SetCleanMode(bool cleanMode); 43 int32_t ExportDatabase(const std::string &outputName, TraceDataDB::ResultCallBack resultCallBack = nullptr); 44 int32_t ExportPerfReadableText(const std::string &outputName, TraceDataDB::ResultCallBack resultCallBack = nullptr); 45 int32_t ExportHookReadableText(const std::string &outputName, TraceDataDB::ResultCallBack resultCallBack = nullptr); 46 int32_t ExportEbpfReadableText(const std::string &outputName, TraceDataDB::ResultCallBack resultCallBack = nullptr); 47 bool ReloadSymbolFiles(const std::string &directory, const std::vector<std::string> &fileNames); 48 std::vector<std::string> SearchData(); 49 int32_t OperateDatabase(const std::string &sql); 50 int32_t SearchDatabase(const std::string &sql, TraceDataDB::ResultCallBack resultCallBack); 51 int32_t SearchDatabase(const std::string &sql, uint8_t *out, int32_t outLen); 52 int32_t SearchDatabase(std::string &sql, bool printf); 53 int32_t SearchDatabaseToProto(const std::string &data, SqllitePreparCacheData::TLVResultCallBack resultCallBack); 54 std::string SearchDatabase(const std::string &sql); 55 int32_t UpdateTraceRangeTime(uint8_t *data, int32_t len); 56 void WaitForParserEnd(); 57 void Clear(); 58 MetaData *GetMetaData(); 59 void SetDataType(TraceFileType type); 60 void SetCancel(bool cancel); 61 bool ParserAndPrintMetrics(const std::string &metrics); 62 bool ReadSqlFileAndPrintResult(const std::string &sqlOperator); DataType()63 TraceFileType DataType() const 64 { 65 return fileType_; 66 } 67 void UpdateAnimationTraceStatus(bool status); 68 void UpdateTaskPoolTraceStatus(bool status); 69 void UpdateAppStartTraceStatus(bool status); 70 void UpdateBinderRunnableTraceStatus(bool status); 71 void UpdateHMKernelTraceStatus(bool status); 72 void UpdateRawTraceCutStartTsStatus(bool status); 73 void InitMetricsMap(std::map<std::string, std::string> &metricsMap); 74 const std::string MetricsSqlQuery(const std::string &metrics); GetPtreaderParser()75 auto GetPtreaderParser() 76 { 77 return ptreaderParser_.get(); 78 } 79 #ifdef ENABLE_RAWTRACE GetRawtraceData()80 auto GetRawtraceData() 81 { 82 return rawTraceParser_.get(); 83 } 84 #endif GetPbreaderParser()85 auto GetPbreaderParser() 86 { 87 return pbreaderParser_.get(); 88 } GetFileType()89 const auto GetFileType() 90 { 91 return fileType_; 92 } GetTraceDataCache()93 auto GetTraceDataCache() 94 { 95 return traceDataCache_.get(); 96 } GetStreamFilter()97 auto GetStreamFilter() 98 { 99 return streamFilters_.get(); 100 } 101 void InitializeParser(); 102 void ProcessTraceData(std::unique_ptr<uint8_t[]> data, size_t size, int32_t isFinish, bool isWasmReadFile); 103 104 // Used to obtain markinfo,skip under Linux ClearMarkPositionInfo()105 void ClearMarkPositionInfo() 106 { 107 hasGotMarkFinish_ = false; 108 markHeard_ = false; 109 }; 110 void GetMarkPositionData(std::unique_ptr<uint8_t[]> &data, size_t &size); 111 112 int32_t CreatEmptyBatchDB(const std::string dbPath); 113 int32_t BatchExportDatabase(const std::string &outputName); 114 bool BatchParseTraceDataSegment(std::unique_ptr<uint8_t[]> data, size_t size); 115 void RevertTableName(const std::string &outputName); 116 uint64_t minTs_ = INVALID_UINT64; 117 uint64_t maxTs_ = INVALID_UINT64; 118 119 private: 120 void InitFilter(); 121 bool LoadQueryFile(const std::string &sqlOperator, std::vector<std::string> &sqlStrings); 122 void ComputeDataDictStrHash(); 123 TraceFileType fileType_; 124 std::unique_ptr<TraceStreamerFilters> streamFilters_ = {}; 125 std::unique_ptr<TraceDataCache> traceDataCache_ = {}; 126 std::unique_ptr<PtreaderParser> ptreaderParser_; 127 std::unique_ptr<PbreaderParser> pbreaderParser_; 128 #ifdef ENABLE_RAWTRACE 129 std::unique_ptr<RawTraceParser> rawTraceParser_; 130 #endif 131 bool enableFileSeparate_ = false; 132 133 // Used to get markinfo,skip under Linux 134 bool hasGotMarkFinish_ = false; 135 bool markHeard_ = false; 136 }; 137 } // namespace TraceStreamer 138 } // namespace SysTuning 139 140 #endif // TRACE_STREAMER_SELECTOR_H 141