1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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 "rpc_server.h"
17
18 #include <cstdint>
19 #include <cstring>
20 #include <functional>
21
22 #include "log.h"
23 #include "meta.h"
24
25 #define UNUSED(expr) \
26 do { \
27 static_cast<void>(expr); \
28 } while (0)
29 namespace SysTuning {
30 namespace TraceStreamer {
ParseData(const uint8_t * data,size_t len,ResultCallBack resultCallBack)31 bool RpcServer::ParseData(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
32 {
33 g_loadSize += len;
34 size_t blockSize = 1024 * 1024;
35 do {
36 size_t parseSize = std::min(len, blockSize);
37 std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(parseSize);
38 std::copy(data, data + parseSize, buf.get());
39 if (!ts_->ParseTraceDataSegment(std::move(buf), parseSize)) {
40 if (resultCallBack) {
41 resultCallBack("formaterror\r\n", SEND_FINISH);
42 }
43 return false;
44 }
45 data += parseSize;
46 len -= parseSize;
47 lenParseData_ += parseSize;
48 } while (len > 0);
49 if (resultCallBack) {
50 resultCallBack("ok\r\n", SEND_FINISH);
51 }
52 return true;
53 }
54
UpdateTraceTime(const uint8_t * data,int len)55 int RpcServer::UpdateTraceTime(const uint8_t* data, int len)
56 {
57 std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(len);
58 std::copy(data, data + len, buf.get());
59 ts_->UpdateTraceRangeTime(buf.get(), len);
60 return 0;
61 }
62
TraceStreamer_Init_ThirdParty_Config(const uint8_t * data,int len)63 int RpcServer::TraceStreamer_Init_ThirdParty_Config(const uint8_t* data, int len)
64 {
65 TS_LOGE("TraceStreamer_Init_ThirdParty_Config is comming!");
66 std::string thirdPartyConfig = reinterpret_cast<const char*>(data);
67 TS_LOGE("thirdPartyConfig = %s", thirdPartyConfig.c_str());
68 int pos;
69 int size = thirdPartyConfig.size();
70 std::vector<std::string> vTraceRangeStr;
71 for (int i = 0; i < size; i++) {
72 pos = thirdPartyConfig.find(";", i);
73 if (pos == std::string::npos) {
74 break;
75 }
76 if (pos < size) {
77 std::string s = thirdPartyConfig.substr(i, pos - i);
78 vTraceRangeStr.push_back(s);
79 i = pos;
80 }
81 }
82 const int EVENT_COUNT_PAIR = 2;
83 if (vTraceRangeStr.size() % EVENT_COUNT_PAIR != 0) {
84 TS_LOGE("thirdPartyConfig is wrong!");
85 return -1;
86 }
87 for (int m = 0; m < vTraceRangeStr.size(); m += EVENT_COUNT_PAIR) {
88 int componentId = std::stoi(vTraceRangeStr.at(m));
89 std::string componentName = vTraceRangeStr.at(m + 1);
90 TS_LOGE("vTraceRangeStr[m] = %d, vTraceRangeStr[m + 1] = %s", componentId, componentName.c_str());
91 g_thirdPartyConfig.insert((std::map<int, std::string>::value_type(componentId, componentName)));
92 }
93 return 0;
94 }
95
ParseDataOver(const uint8_t * data,size_t len,ResultCallBack resultCallBack)96 bool RpcServer::ParseDataOver(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
97 {
98 UNUSED(data);
99 UNUSED(len);
100 MetaData* metaData = ts_->GetMetaData();
101 metaData->SetSourceFileName("input stream mode");
102 metaData->SetOutputFileName("wasm mode");
103 metaData->SetParserToolVersion(TRACE_STREAM_VERSION);
104 metaData->SetParserToolPublishDateTime(TRACE_STREAM_PUBLISHVERSION);
105 metaData->SetTraceDataSize(g_loadSize);
106 metaData->SetTraceType((ts_->DataType() == TRACE_FILETYPE_H_TRACE) ? "proto-based-trace" : "txt-based-trace");
107 TS_LOGI("RPC ParseDataOver, has parsed len %zu", lenParseData_);
108
109 ts_->WaitForParserEnd();
110 #ifndef USE_VTABLE
111 ts_->Clear();
112 #endif
113 if (resultCallBack) {
114 resultCallBack("ok\r\n", SEND_FINISH);
115 }
116 lenParseData_ = 0;
117 g_loadSize = 0;
118 return true;
119 }
120
SqlOperate(const uint8_t * data,size_t len,ResultCallBack resultCallBack)121 bool RpcServer::SqlOperate(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
122 {
123 ts_->SetCancel(false);
124 std::string sql(reinterpret_cast<const char*>(data), len);
125 TS_LOGI("RPC SqlOperate(%s, %zu)", sql.c_str(), len);
126
127 int ret = ts_->OperateDatabase(sql);
128 if (resultCallBack) {
129 std::string response = "ok\r\n";
130 if (ret != 0) {
131 response = "dberror\r\n";
132 }
133 resultCallBack(response, SEND_FINISH);
134 }
135 return (ret == 0);
136 }
137
SqlQuery(const uint8_t * data,size_t len,ResultCallBack resultCallBack)138 bool RpcServer::SqlQuery(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
139 {
140 ts_->SetCancel(false);
141 std::string sql(reinterpret_cast<const char*>(data), len);
142 TS_LOGI("RPC SqlQuery %zu:%s", len, sql.c_str());
143
144 int ret = ts_->SearchDatabase(sql, resultCallBack);
145 if (resultCallBack && ret != 0) {
146 resultCallBack("dberror\r\n", SEND_FINISH);
147 }
148 ts_->SetCancel(false);
149 return (ret == 0);
150 }
151
CancelSqlQuery()152 void RpcServer::CancelSqlQuery()
153 {
154 ts_->SetCancel(true);
155 }
156
Reset(const uint8_t * data,size_t len,ResultCallBack resultCallBack)157 bool RpcServer::Reset(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
158 {
159 UNUSED(data);
160 UNUSED(len);
161 TS_LOGI("RPC reset trace_streamer");
162
163 ts_->WaitForParserEnd();
164 ts_ = std::make_unique<TraceStreamerSelector>();
165 if (resultCallBack) {
166 resultCallBack("ok\r\n", SEND_FINISH);
167 }
168 return true;
169 }
170
WasmSqlQuery(const uint8_t * data,size_t len,uint8_t * out,int outLen)171 int RpcServer::WasmSqlQuery(const uint8_t* data, size_t len, uint8_t* out, int outLen)
172 {
173 ts_->SetCancel(false);
174 std::string sql(reinterpret_cast<const char*>(data), len);
175 TS_LOGI("WASM RPC SqlQuery out(%p:%d) sql(%zu:%s)", reinterpret_cast<void*>(out), outLen,
176 len, sql.c_str());
177
178 int ret = ts_->SearchDatabase(sql, out, outLen);
179 return ret;
180 }
WasmSqlQueryWithCallback(const uint8_t * data,size_t len,ResultCallBack callback) const181 int RpcServer::WasmSqlQueryWithCallback(const uint8_t* data, size_t len, ResultCallBack callback) const
182 {
183 ts_->SetCancel(false);
184 std::string sql(reinterpret_cast<const char*>(data), len);
185 TS_LOGI("WASM RPC SqlQuery sql(%zu:%s)", len, sql.c_str());
186
187 int ret = ts_->SearchDatabase(sql, callback);
188 return ret;
189 }
190 } // namespace TraceStreamer
191 } // namespace SysTuning
192