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 RPC_RPC_H 17 #define RPC_RPC_H 18 19 #include <functional> 20 #include <mutex> 21 #include "trace_streamer_selector.h" 22 #include "ffrt_converter.h" 23 namespace SysTuning { 24 namespace TraceStreamer { 25 class RpcServer { 26 public: 27 using ResultCallBack = std::function<void(const std::string /* result */, int32_t)>; 28 using ExportDatabaseCallback = std::function<void(const std::string /* result */, int32_t)>; 29 using ParseELFFileCallBack = std::function<void(const std::string, int32_t)>; 30 using SendDataCallBack = std::function<void(const char *, int32_t, int32_t)>; 31 using SplitFileCallBack = std::function<void(const std::string /* result */, int32_t, int32_t)>; 32 // In order to bind HTTP, maintain a unified interface, even if some parameters are useless 33 bool ParseData(const uint8_t *data, size_t len, ResultCallBack resultCallBack, bool isFinish); 34 bool ParseSplitFileData(const uint8_t *data, 35 size_t len, 36 int32_t isFinish, 37 SplitFileCallBack splitFileCallBack, 38 bool isSplitFile); 39 bool ParseDataOver(const uint8_t *data, size_t len, ResultCallBack resultCallBack); 40 bool SqlOperate(const uint8_t *data, size_t len, ResultCallBack resultCallBack); 41 bool SqlQuery(const uint8_t *data, size_t len, ResultCallBack resultCallBack); 42 bool Reset(const uint8_t *data, size_t len, ResultCallBack resultCallBack); 43 void CancelSqlQuery(); 44 bool ParseDataWithoutCallback(const uint8_t *data, size_t len, int32_t isFinish, bool isSplitFile); 45 46 // only for wasm, no callback 47 int32_t WasmSqlQuery(const uint8_t *data, size_t len, uint8_t *out, int32_t outLen); 48 bool SqlMetricsQueryWithCallback(const uint8_t *data, size_t len, ResultCallBack callback) const; 49 int32_t WasmSqlQueryWithCallback(const uint8_t *data, size_t len, ResultCallBack callback) const; 50 int32_t WasmSqlQueryToProtoCallback(const uint8_t *data, 51 size_t len, 52 SqllitePreparCacheData::TLVResultCallBack callback) const; 53 int32_t UpdateTraceTime(const uint8_t *data, int32_t len); 54 int32_t TraceStreamerInitThirdPartyConfig(const uint8_t *data, int32_t len); 55 int32_t WasmExportDatabase(ResultCallBack resultCallBack); 56 bool ParserConfig(std::string parserConfigJson); 57 bool SplitFile(std::string timeSnaps); 58 #ifdef ENABLE_NATIVE_HOOK 59 void ProcHookCommSplitResult(SplitFileCallBack splitFileCallBack); 60 #endif 61 #ifdef ENABLE_EBPF 62 void ProcEbpfSplitResult(SplitFileCallBack splitFileCallBack, bool isLast); 63 #endif 64 bool GetLongTraceTimeSnap(std::string dataString); 65 bool LongTraceSplitFile(const uint8_t *data, 66 size_t len, 67 int32_t isFinish, 68 uint32_t pageNum, 69 SplitFileCallBack splitFileCallBack); 70 bool GetTimeSnap(std::string dataString); GetFfrtConvertStatus()71 const bool GetFfrtConvertStatus() 72 { 73 return ffrtConvertEnabled_; 74 }; 75 bool DetermineSystrace(const uint8_t *data, size_t len); 76 77 #ifdef ENABLE_RAWTRACE 78 bool SendRawtraceSplitFileData(SplitFileCallBack splitFileCallBack, int32_t isFinish); 79 #endif 80 #ifdef IS_WASM 81 bool SaveAndParseFfrtData(const uint8_t *data, size_t len, ResultCallBack resultCallBack, bool isFinish); 82 bool ReadAndParseData(const std::string &filePath); 83 bool SendConvertedFfrtFile(const std::string &fileName, ResultCallBack resultCallBack); 84 void CreateFilePath(const std::string &filePath); 85 bool WriteToFile(const std::string &fileName, const uint8_t *data, size_t len); 86 bool ClearPathFile(string &symbolsPath, int32_t finish, ParseELFFileCallBack &parseELFFile); 87 bool DownloadELFCallback(const std::string &fileName, 88 size_t totalLen, 89 const uint8_t *data, 90 size_t len, 91 int32_t finish, 92 ParseELFFileCallBack parseELFFile); 93 #endif 94 std::map<int32_t, std::string> g_thirdPartyConfig; 95 96 private: 97 #ifdef ENABLE_HIPERF 98 void ProcPerfSplitResult(SplitFileCallBack splitFileCallBack, bool isLast); 99 #endif 100 void ProcPbreaderSplitResult(SplitFileCallBack splitFileCallBack); 101 bool SendBytraceSplitFileData(SplitFileCallBack splitFileCallBack, int32_t isFinish); 102 103 private: 104 std::unique_ptr<TraceStreamerSelector> ts_ = std::make_unique<TraceStreamerSelector>(); 105 size_t lenParseData_ = 0; 106 std::vector<std::string> symbolsPathFiles_; 107 std::vector<std::unique_ptr<TraceTimeSnap>> vTraceTimeSnap_; 108 bool ffrtConvertEnabled_ = false; 109 int64_t startParseTime_ = 0; 110 int64_t endParseTime_ = 0; 111 }; 112 } // namespace TraceStreamer 113 } // namespace SysTuning 114 #endif // RPC_RPC_H 115