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 "wasm_func.h"
17
18 #ifndef NAME_MAX
19 #define NAME_MAX 255
20 #endif
21 namespace SysTuning {
22 namespace TraceStreamer {
23 RpcServer g_wasmTraceStreamer;
24 extern "C" {
25 using ReplyFunction = void (*)(const char *data, uint32_t len, int32_t finish);
26 using TLVReplyFunction = void (*)(const char *data, uint32_t len, uint32_t type, int32_t finish);
27 ReplyFunction g_reply;
28 ReplyFunction g_ffrtConvertedReply;
29 TLVReplyFunction g_replyTLV;
30 uint8_t *g_reqBuf;
31 uint32_t g_reqBufferSize;
32
33 using SplitFileFunction = void (*)(const char *data, uint32_t len, int32_t dataType, int32_t isFinish);
34 SplitFileFunction g_splitFile;
35 uint8_t *g_splitFileBuf;
36 uint32_t g_splitFileBufferSize;
37
38 uint8_t *g_parserConfigBuf;
39 uint32_t g_parserConfigSize;
40
41 using SendDataCallBack = void (*)(const char *data, int32_t len, int32_t componentId);
42 SendDataCallBack g_sendData = nullptr;
43 uint8_t *g_sendDataBuf;
44 uint32_t g_sendDataBufSize;
45
46 using ExportDBCallback = void (*)(const char *data, uint32_t len, int32_t finish);
47 ExportDBCallback g_dbCallback;
48
49 using ParseELFFunction = void (*)(const char *data, uint32_t len, int32_t finish);
50 ParseELFFunction g_parseELFCallback;
51 uint8_t *g_fileNameBuf;
52 uint32_t g_fileNameSize;
53 bool g_isSystrace = false;
54 bool g_hasDeterminedSystrace = false;
55
ResultCallback(const std::string & jsonResult,int32_t finish)56 void ResultCallback(const std::string &jsonResult, int32_t finish)
57 {
58 g_reply(jsonResult.data(), jsonResult.size(), finish);
59 }
TLVResultCallback(const char * data,uint32_t len,uint32_t type,int32_t finish)60 void TLVResultCallback(const char *data, uint32_t len, uint32_t type, int32_t finish)
61 {
62 g_replyTLV(data, len, type, finish);
63 }
FfrtConvertedResultCallback(const std::string & content,int32_t finish)64 void FfrtConvertedResultCallback(const std::string &content, int32_t finish)
65 {
66 g_ffrtConvertedReply(content.data(), content.size(), finish);
67 }
SplitFileCallback(const std::string & jsonResult,int32_t dataType,int32_t finish)68 void SplitFileCallback(const std::string &jsonResult, int32_t dataType, int32_t finish)
69 {
70 g_splitFile(jsonResult.data(), jsonResult.size(), dataType, finish);
71 }
72
ParseELFCallback(const std::string & soDataResult,int32_t finish)73 void ParseELFCallback(const std::string &soDataResult, int32_t finish)
74 {
75 g_parseELFCallback(soDataResult.data(), soDataResult.size(), finish);
76 }
Initialize(uint32_t reqBufferSize,ReplyFunction replyFunction,TLVReplyFunction replyTLVFunction,ReplyFunction ffrtConvertedReply)77 EMSCRIPTEN_KEEPALIVE uint8_t *Initialize(uint32_t reqBufferSize,
78 ReplyFunction replyFunction,
79 TLVReplyFunction replyTLVFunction,
80 ReplyFunction ffrtConvertedReply)
81 {
82 g_reqBuf = new uint8_t[reqBufferSize];
83 g_reqBufferSize = reqBufferSize;
84 g_reply = replyFunction;
85 g_replyTLV = replyTLVFunction;
86 g_ffrtConvertedReply = ffrtConvertedReply;
87 return g_reqBuf;
88 }
89
InitializeSplitFile(SplitFileFunction splitFileFunction,uint32_t reqBufferSize)90 EMSCRIPTEN_KEEPALIVE uint8_t *InitializeSplitFile(SplitFileFunction splitFileFunction, uint32_t reqBufferSize)
91 {
92 g_splitFile = splitFileFunction;
93 g_splitFileBuf = new uint8_t[reqBufferSize];
94 g_splitFileBufferSize = reqBufferSize;
95 return g_splitFileBuf;
96 }
97
TraceStreamerSplitFileEx(int dataLen)98 EMSCRIPTEN_KEEPALIVE int TraceStreamerSplitFileEx(int dataLen)
99 {
100 std::string timeSnaps(reinterpret_cast<const char *>(g_splitFileBuf), dataLen);
101 if (g_wasmTraceStreamer.SplitFile(timeSnaps)) {
102 return 0;
103 }
104 return -1;
105 }
106
TraceStreamerReciveFileEx(int32_t dataLen,int32_t isFinish)107 EMSCRIPTEN_KEEPALIVE int TraceStreamerReciveFileEx(int32_t dataLen, int32_t isFinish)
108 {
109 if (g_wasmTraceStreamer.ParseSplitFileData(g_splitFileBuf, dataLen, isFinish, &SplitFileCallback, true)) {
110 return 0;
111 }
112 return -1;
113 }
114
InitializeParseConfig(uint32_t reqBufferSize)115 EMSCRIPTEN_KEEPALIVE uint8_t *InitializeParseConfig(uint32_t reqBufferSize)
116 {
117 g_parserConfigBuf = new uint8_t[reqBufferSize];
118 g_parserConfigSize = reqBufferSize;
119 return g_parserConfigBuf;
120 }
121
TraceStreamerParserConfigEx(int dataLen)122 EMSCRIPTEN_KEEPALIVE int TraceStreamerParserConfigEx(int dataLen)
123 {
124 std::string parserConfig(reinterpret_cast<const char *>(g_parserConfigBuf), dataLen);
125 if (g_wasmTraceStreamer.ParserConfig(parserConfig)) {
126 return 0;
127 }
128 return -1;
129 }
TraceStreamerGetLongTraceTimeSnapEx(int dataLen)130 EMSCRIPTEN_KEEPALIVE int TraceStreamerGetLongTraceTimeSnapEx(int dataLen)
131 {
132 std::string dataString(reinterpret_cast<const char *>(g_splitFileBuf), dataLen);
133 if (g_wasmTraceStreamer.GetLongTraceTimeSnap(dataString)) {
134 return 0;
135 }
136 return -1;
137 }
138
TraceStreamerLongTraceSplitFileEx(int dataLen,int32_t isFinish,uint32_t pageNum)139 EMSCRIPTEN_KEEPALIVE int TraceStreamerLongTraceSplitFileEx(int dataLen, int32_t isFinish, uint32_t pageNum)
140 {
141 if (g_wasmTraceStreamer.LongTraceSplitFile(g_splitFileBuf, dataLen, isFinish, pageNum, &SplitFileCallback)) {
142 return 0;
143 }
144 return -1;
145 }
146
InitFileName(ParseELFFunction parseELFCallback,uint32_t reqBufferSize)147 EMSCRIPTEN_KEEPALIVE uint8_t *InitFileName(ParseELFFunction parseELFCallback, uint32_t reqBufferSize)
148 {
149 g_parseELFCallback = parseELFCallback;
150 if (reqBufferSize > NAME_MAX) {
151 return nullptr;
152 }
153 g_fileNameBuf = new uint8_t[reqBufferSize];
154 if (!g_fileNameBuf) {
155 return nullptr;
156 }
157 g_fileNameSize = reqBufferSize;
158 return g_fileNameBuf;
159 }
160
UpdateTraceTime(int32_t len)161 EMSCRIPTEN_KEEPALIVE int32_t UpdateTraceTime(int32_t len)
162 {
163 return g_wasmTraceStreamer.UpdateTraceTime(g_reqBuf, len);
164 }
165
ThirdParySendDataCallback(const char * pluginData,int32_t len,int32_t componentId)166 void ThirdParySendDataCallback(const char *pluginData, int32_t len, int32_t componentId)
167 {
168 if (g_sendData) {
169 g_sendData(pluginData, len, componentId);
170 }
171 }
172
TraceStreamerSetThirdPartyDataDealer(SendDataCallBack sendDataCallBack,uint32_t reqBufferSize)173 EMSCRIPTEN_KEEPALIVE uint8_t *TraceStreamerSetThirdPartyDataDealer(SendDataCallBack sendDataCallBack,
174 uint32_t reqBufferSize)
175 {
176 g_sendData = sendDataCallBack;
177 g_sendDataBuf = new uint8_t[reqBufferSize];
178 g_sendDataBufSize = reqBufferSize;
179 return g_sendDataBuf;
180 }
181
TraceStreamerSetLogLevel(uint32_t level)182 EMSCRIPTEN_KEEPALIVE void TraceStreamerSetLogLevel(uint32_t level)
183 {
184 if (level >= LOG_DEBUG && level <= LOG_OFF) {
185 g_curLogLevel = static_cast<enum LogLevel>(level);
186 }
187 }
188
TraceStreamerPluginOutFilter(const char * pluginData,int32_t len,const std::string & componentName)189 int32_t TraceStreamerPluginOutFilter(const char *pluginData, int32_t len, const std::string &componentName)
190 {
191 std::map<int32_t, std::string>::iterator itor = g_wasmTraceStreamer.g_thirdPartyConfig.begin();
192 int32_t componentId;
193 for (; itor != g_wasmTraceStreamer.g_thirdPartyConfig.end(); ++itor) {
194 if (itor->second == componentName) {
195 componentId = itor->first;
196 return TraceStreamerPluginOutSendData(pluginData, len, componentId);
197 }
198 }
199 return -1;
200 }
201
202 // Tell js to call the corresponding third-party parser interface according to the compositeId
TraceStreamerPluginOutSendData(const char * pluginData,int32_t len,int32_t componentId)203 int32_t TraceStreamerPluginOutSendData(const char *pluginData, int32_t len, int32_t componentId)
204 {
205 ThirdParySendDataCallback(pluginData, len, componentId);
206 return 0;
207 }
208
TraceStreamerInitThirdPartyConfig(int32_t dataLen)209 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerInitThirdPartyConfig(int32_t dataLen)
210 {
211 return g_wasmTraceStreamer.TraceStreamerInitThirdPartyConfig(g_reqBuf, dataLen);
212 }
213
214 // return 0 while ok, -1 while failed
TraceStreamerParseData(const uint8_t * data,int32_t dataLen)215 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerParseData(const uint8_t *data, int32_t dataLen)
216 {
217 if (g_wasmTraceStreamer.ParseData(data, dataLen, nullptr, false)) {
218 return 0;
219 }
220 return -1;
221 }
222 // return 0 while ok, -1 while failed
TraceStreamerParseDataEx(int32_t dataLen,bool isFinish)223 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerParseDataEx(int32_t dataLen, bool isFinish)
224 {
225 if (!g_hasDeterminedSystrace) {
226 g_isSystrace = g_wasmTraceStreamer.DetermineSystrace(g_reqBuf, dataLen);
227 g_hasDeterminedSystrace = true;
228 }
229 if (g_wasmTraceStreamer.GetFfrtConvertStatus() && g_isSystrace) {
230 #if IS_WASM
231 return g_wasmTraceStreamer.SaveAndParseFfrtData(g_reqBuf, dataLen, &FfrtConvertedResultCallback, isFinish);
232 #endif
233 } else if (g_wasmTraceStreamer.ParseData(g_reqBuf, dataLen, nullptr, isFinish)) {
234 return 0;
235 }
236 return -1;
237 }
TraceStreamerDownloadELFEx(int32_t totalLen,int32_t fileNameLen,int32_t dataLen,int32_t finish)238 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerDownloadELFEx(int32_t totalLen,
239 int32_t fileNameLen,
240 int32_t dataLen,
241 int32_t finish)
242 {
243 std::string fileName(reinterpret_cast<const char *>(g_fileNameBuf), fileNameLen);
244 #if IS_WASM
245 if (g_wasmTraceStreamer.DownloadELFCallback(fileName, totalLen, g_reqBuf, dataLen, finish, &ParseELFCallback)) {
246 return 0;
247 }
248 #endif
249 return -1;
250 }
TraceStreamerParseDataOver()251 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerParseDataOver()
252 {
253 if (g_wasmTraceStreamer.ParseDataOver(nullptr, 0, nullptr)) {
254 return 0;
255 }
256 return -1;
257 }
TraceStreamerSqlOperate(const uint8_t * sql,int32_t sqlLen)258 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerSqlOperate(const uint8_t *sql, int32_t sqlLen)
259 {
260 if (g_wasmTraceStreamer.SqlOperate(sql, sqlLen, nullptr)) {
261 return 0;
262 }
263 return -1;
264 }
TraceStreamerSqlOperateEx(int32_t sqlLen)265 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerSqlOperateEx(int32_t sqlLen)
266 {
267 if (g_wasmTraceStreamer.SqlOperate(g_reqBuf, sqlLen, nullptr)) {
268 return 0;
269 }
270 return -1;
271 }
TraceStreamerReset()272 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerReset()
273 {
274 g_wasmTraceStreamer.Reset(nullptr, 0, nullptr);
275 return 0;
276 }
277 // return the length of result, -1 while failed
TraceStreamerSqlQuery(const uint8_t * sql,int32_t sqlLen,uint8_t * out,int32_t outLen)278 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerSqlQuery(const uint8_t *sql, int32_t sqlLen, uint8_t *out, int32_t outLen)
279 {
280 return g_wasmTraceStreamer.WasmSqlQuery(sql, sqlLen, out, outLen);
281 }
282 // return the length of result, -1 while failed
TraceStreamerSqlQueryEx(int32_t sqlLen)283 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerSqlQueryEx(int32_t sqlLen)
284 {
285 return g_wasmTraceStreamer.WasmSqlQueryWithCallback(g_reqBuf, sqlLen, &ResultCallback);
286 }
TraceStreamerSqlQueryToProtoCallback(int32_t sqlLen)287 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerSqlQueryToProtoCallback(int32_t sqlLen)
288 {
289 return g_wasmTraceStreamer.WasmSqlQueryToProtoCallback(g_reqBuf, sqlLen, &TLVResultCallback);
290 }
TraceStreamerSqlMetricsQuery(int32_t sqlLen)291 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerSqlMetricsQuery(int32_t sqlLen)
292 {
293 if (g_wasmTraceStreamer.SqlMetricsQueryWithCallback(g_reqBuf, sqlLen, &ResultCallback)) {
294 return 0;
295 }
296 return -1;
297 }
TraceStreamerCancel()298 EMSCRIPTEN_KEEPALIVE int32_t TraceStreamerCancel()
299 {
300 g_wasmTraceStreamer.CancelSqlQuery();
301 return 0;
302 }
303
ExportDatabaseCallback(const std::string & jsonResult,int32_t finish)304 void ExportDatabaseCallback(const std::string &jsonResult, int32_t finish)
305 {
306 g_dbCallback(jsonResult.data(), jsonResult.size(), finish);
307 }
308
WasmExportDatabase(ExportDBCallback fun)309 EMSCRIPTEN_KEEPALIVE int32_t WasmExportDatabase(ExportDBCallback fun)
310 {
311 g_dbCallback = fun;
312 return g_wasmTraceStreamer.WasmExportDatabase(&ExportDatabaseCallback);
313 }
314 } // extern "C"
315 } // namespace TraceStreamer
316 } // namespace SysTuning
317