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