1 /*
2 * Copyright (c) 2022 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 "agent/tracing_impl.h"
17
18 #include "protocol_channel.h"
19
20 namespace panda::ecmascript::tooling {
Dispatch(const DispatchRequest & request)21 void TracingImpl::DispatcherImpl::Dispatch(const DispatchRequest &request)
22 {
23 Method method = GetMethodEnum(request.GetMethod());
24 LOG_DEBUGGER(DEBUG) << "dispatch [" << request.GetMethod() << "] to TracingImpl";
25 switch (method) {
26 case Method::END:
27 End(request);
28 break;
29 case Method::GET_CATEGORIES:
30 GetCategories(request);
31 break;
32 case Method::RECORD_CLOCK_SYNC_MARKER:
33 RecordClockSyncMarker(request);
34 break;
35 case Method::REQUEST_MEMORY_DUMP:
36 RequestMemoryDump(request);
37 break;
38 case Method::START:
39 Start(request);
40 break;
41 default:
42 SendResponse(request, DispatchResponse::Fail("Unknown method: " + request.GetMethod()));
43 break;
44 }
45 }
46
GetMethodEnum(const std::string & method)47 TracingImpl::DispatcherImpl::Method TracingImpl::DispatcherImpl::GetMethodEnum(const std::string& method)
48 {
49 if (method == "end") {
50 return Method::END;
51 } else if (method == "getCategories") {
52 return Method::GET_CATEGORIES;
53 } else if (method == "recordClockSyncMarker") {
54 return Method::RECORD_CLOCK_SYNC_MARKER;
55 } else if (method == "requestMemoryDump") {
56 return Method::REQUEST_MEMORY_DUMP;
57 } else if (method == "start") {
58 return Method::START;
59 } else {
60 return Method::UNKNOWN;
61 }
62 }
63
End(const DispatchRequest & request)64 void TracingImpl::DispatcherImpl::End(const DispatchRequest &request)
65 {
66 auto traceEvents = tracing_->End();
67 if (traceEvents == nullptr) {
68 LOG_DEBUGGER(ERROR) << "Transfer DFXJSNApi::StopTracing is failure";
69 SendResponse(request, DispatchResponse::Fail("Stop is failure"));
70 return;
71 }
72 SendResponse(request, DispatchResponse::Ok());
73
74 tracing_->frontend_.DataCollected(std::move(traceEvents));
75 tracing_->frontend_.TracingComplete();
76 }
77
GetCategories(const DispatchRequest & request)78 void TracingImpl::DispatcherImpl::GetCategories(const DispatchRequest &request)
79 {
80 std::vector<std::string> categories;
81 DispatchResponse response = tracing_->GetCategories(categories);
82 SendResponse(request, response);
83 }
84
RecordClockSyncMarker(const DispatchRequest & request)85 void TracingImpl::DispatcherImpl::RecordClockSyncMarker(const DispatchRequest &request)
86 {
87 std::string syncId;
88 DispatchResponse response = tracing_->RecordClockSyncMarker(syncId);
89 SendResponse(request, response);
90 }
91
RequestMemoryDump(const DispatchRequest & request)92 void TracingImpl::DispatcherImpl::RequestMemoryDump(const DispatchRequest &request)
93 {
94 std::unique_ptr<RequestMemoryDumpParams> params =
95 RequestMemoryDumpParams::Create(request.GetParams());
96 if (params == nullptr) {
97 SendResponse(request, DispatchResponse::Fail("wrong params"));
98 return;
99 }
100 std::string dumpGuid;
101 bool success = false;
102 DispatchResponse response = tracing_->RequestMemoryDump(std::move(params), dumpGuid, success);
103 SendResponse(request, response);
104 }
105
Start(const DispatchRequest & request)106 void TracingImpl::DispatcherImpl::Start(const DispatchRequest &request)
107 {
108 std::unique_ptr<StartParams> params =
109 StartParams::Create(request.GetParams());
110 if (params == nullptr) {
111 SendResponse(request, DispatchResponse::Fail("wrong params"));
112 return;
113 }
114 DispatchResponse response = tracing_->Start(std::move(params));
115 SendResponse(request, response);
116 }
117
AllowNotify() const118 bool TracingImpl::Frontend::AllowNotify() const
119 {
120 return channel_ != nullptr;
121 }
122
BufferUsage(double percentFull,int32_t eventCount,double value)123 void TracingImpl::Frontend::BufferUsage(double percentFull, int32_t eventCount, double value)
124 {
125 if (!AllowNotify()) {
126 return;
127 }
128
129 tooling::BufferUsage bufferUsage;
130 bufferUsage.SetPercentFull(percentFull).SetEventCount(eventCount).SetValue(value);
131 channel_->SendNotification(bufferUsage);
132 }
133
DataCollected(std::unique_ptr<std::vector<TraceEvent>> traceEvents)134 void TracingImpl::Frontend::DataCollected(std::unique_ptr<std::vector<TraceEvent>> traceEvents)
135 {
136 if (!AllowNotify()) {
137 return;
138 }
139
140 tooling::DataCollected dataCollected;
141 dataCollected.SetTraceEvents(std::move(traceEvents));
142
143 channel_->SendNotification(dataCollected);
144 }
145
TracingComplete()146 void TracingImpl::Frontend::TracingComplete()
147 {
148 if (!AllowNotify()) {
149 return;
150 }
151
152 tooling::TracingComplete tracingComplete;
153 channel_->SendNotification(tracingComplete);
154 }
155
End()156 std::unique_ptr<std::vector<TraceEvent>> TracingImpl::End()
157 {
158 #if defined(ECMASCRIPT_SUPPORT_TRACING)
159 if (handle_ != nullptr) {
160 uv_timer_stop(handle_);
161 }
162 #endif
163 auto traceEvents = panda::DFXJSNApi::StopTracing(vm_);
164 return traceEvents;
165 }
166
GetCategories(std::vector<std::string> categories)167 DispatchResponse TracingImpl::GetCategories([[maybe_unused]] std::vector<std::string> categories)
168 {
169 return DispatchResponse::Fail("GetCategories not support now.");
170 }
171
RecordClockSyncMarker(std::string syncId)172 DispatchResponse TracingImpl::RecordClockSyncMarker([[maybe_unused]] std::string syncId)
173 {
174 return DispatchResponse::Fail("RecordClockSyncMarker not support now.");
175 }
176
RequestMemoryDump(std::unique_ptr<RequestMemoryDumpParams> params,std::string dumpGuid,bool success)177 DispatchResponse TracingImpl::RequestMemoryDump([[maybe_unused]] std::unique_ptr<RequestMemoryDumpParams> params,
178 [[maybe_unused]] std::string dumpGuid, [[maybe_unused]] bool success)
179 {
180 return DispatchResponse::Fail("RequestMemoryDump not support now.");
181 }
182
Start(std::unique_ptr<StartParams> params)183 DispatchResponse TracingImpl::Start(std::unique_ptr<StartParams> params)
184 {
185 std::string categories = params->GetCategories();
186 if (!panda::DFXJSNApi::StartTracing(vm_, categories)) {
187 return DispatchResponse::Fail("Start tracing failed");
188 }
189 #if defined(ECMASCRIPT_SUPPORT_TRACING)
190 if (params->HasBufferUsageReportingInterval()) {
191 LOG_DEBUGGER(ERROR) << "HasBufferUsageReportingInterval " << params->GetBufferUsageReportingInterval();
192 if (handle_ != nullptr && uv_is_active(reinterpret_cast<uv_handle_t*>(handle_))) {
193 LOG_DEBUGGER(ERROR) << "uv_is_active!!!";
194 return DispatchResponse::Ok();
195 }
196
197 uv_loop_t *loop = reinterpret_cast<uv_loop_t *>(vm_->GetLoop());
198 if (loop == nullptr) {
199 return DispatchResponse::Fail("Loop is nullptr");
200 }
201 if (handle_ == nullptr) {
202 handle_ = new uv_timer_t;
203 }
204 uv_timer_init(loop, handle_);
205 handle_->data = this;
206 uv_timer_start(handle_, TracingBufferUsageReport, 0, params->GetBufferUsageReportingInterval());
207 if (DebuggerApi::IsMainThread()) {
208 uv_async_send(&loop->wq_async);
209 } else {
210 uv_work_t *work = new uv_work_t;
211 uv_queue_work(loop, work, [](uv_work_t *) { }, [](uv_work_t *work, int32_t) { delete work; });
212 }
213 }
214 #endif
215 return DispatchResponse::Ok();
216 }
217
~TracingImpl()218 TracingImpl::~TracingImpl()
219 {
220 #if defined(ECMASCRIPT_SUPPORT_TRACING)
221 uv_loop_t *loop = reinterpret_cast<uv_loop_t *>(vm_->GetLoop());
222 if (handle_!= nullptr && loop != nullptr) {
223 uv_close(reinterpret_cast<uv_handle_t*>(handle_), [](uv_handle_t* handle) {
224 delete reinterpret_cast<uv_timer_t*>(handle);
225 handle = nullptr;
226 });
227 }
228 #endif
229 }
230
231 #if defined(ECMASCRIPT_SUPPORT_TRACING)
TracingBufferUsageReport(uv_timer_t * handle)232 void TracingImpl::TracingBufferUsageReport(uv_timer_t* handle)
233 {
234 TracingImpl *tracing = static_cast<TracingImpl *>(handle->data);
235 if (tracing == nullptr) {
236 LOG_DEBUGGER(ERROR) << "tracing == nullptr";
237 return;
238 }
239
240 double percentFull = 0.0;
241 uint32_t eventCount = 0;
242 double value = 0.0;
243 panda::DFXJSNApi::GetTracingBufferUseage(tracing->vm_, percentFull, eventCount, value);
244 tracing->frontend_.BufferUsage(percentFull, eventCount, value);
245 }
246 #endif
247 } // namespace panda::ecmascript::tooling