• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ecmascript/tooling/agent/profiler_impl.h"
17 
18 #include "ecmascript/napi/include/dfx_jsnapi.h"
19 #include "ecmascript/tooling/base/pt_events.h"
20 #include "ecmascript/tooling/protocol_channel.h"
21 #include "libpandabase/utils/logger.h"
22 
23 namespace panda::ecmascript::tooling {
Dispatch(const DispatchRequest & request)24 void ProfilerImpl::DispatcherImpl::Dispatch(const DispatchRequest &request)
25 {
26     static std::unordered_map<std::string, AgentHandler> dispatcherTable {
27         { "disable", &ProfilerImpl::DispatcherImpl::Disable },
28         { "enable", &ProfilerImpl::DispatcherImpl::Enable },
29         { "start", &ProfilerImpl::DispatcherImpl::Start },
30         { "stop", &ProfilerImpl::DispatcherImpl::Stop },
31         { "SetSamplingInterval", &ProfilerImpl::DispatcherImpl::SetSamplingInterval },
32         { "getBestEffortCoverage", &ProfilerImpl::DispatcherImpl::GetBestEffortCoverage },
33         { "stopPreciseCoverage", &ProfilerImpl::DispatcherImpl::StopPreciseCoverage },
34         { "takePreciseCoverage", &ProfilerImpl::DispatcherImpl::TakePreciseCoverage },
35         { "startPreciseCoverage", &ProfilerImpl::DispatcherImpl::StartPreciseCoverage },
36         { "startTypeProfile", &ProfilerImpl::DispatcherImpl::StartTypeProfile },
37         { "stopTypeProfile", &ProfilerImpl::DispatcherImpl::StopTypeProfile },
38         { "takeTypeProfile", &ProfilerImpl::DispatcherImpl::TakeTypeProfile }
39     };
40 
41     const std::string &method = request.GetMethod();
42     LOG(DEBUG, DEBUGGER) << "dispatch [" << method << "] to ProfilerImpl";
43     auto entry = dispatcherTable.find(method);
44     if (entry != dispatcherTable.end() && entry->second != nullptr) {
45         (this->*(entry->second))(request);
46     } else {
47         SendResponse(request, DispatchResponse::Fail("Unknown method: " + method));
48     }
49 }
50 
Disable(const DispatchRequest & request)51 void ProfilerImpl::DispatcherImpl::Disable(const DispatchRequest &request)
52 {
53     DispatchResponse response = profiler_->Disable();
54     SendResponse(request, response);
55 }
56 
Enable(const DispatchRequest & request)57 void ProfilerImpl::DispatcherImpl::Enable(const DispatchRequest &request)
58 {
59     DispatchResponse response = profiler_->Enable();
60     SendResponse(request, response);
61 }
62 
Start(const DispatchRequest & request)63 void ProfilerImpl::DispatcherImpl::Start(const DispatchRequest &request)
64 {
65     DispatchResponse response = profiler_->Start();
66     SendResponse(request, response);
67 }
68 
Stop(const DispatchRequest & request)69 void ProfilerImpl::DispatcherImpl::Stop(const DispatchRequest &request)
70 {
71     std::unique_ptr<Profile> profile;
72     DispatchResponse response = profiler_->Stop(&profile);
73     StopReturns result(std::move(profile));
74     SendResponse(request, response, result);
75 }
76 
SetSamplingInterval(const DispatchRequest & request)77 void ProfilerImpl::DispatcherImpl::SetSamplingInterval(const DispatchRequest &request)
78 {
79     std::unique_ptr<SetSamplingIntervalParams> params = SetSamplingIntervalParams::Create(request.GetParams());
80     if (params == nullptr) {
81         SendResponse(request, DispatchResponse::Fail("wrong params"));
82         return;
83     }
84     DispatchResponse response = profiler_->SetSamplingInterval(*params);
85     SendResponse(request, response);
86 }
87 
GetBestEffortCoverage(const DispatchRequest & request)88 void ProfilerImpl::DispatcherImpl::GetBestEffortCoverage(const DispatchRequest &request)
89 {
90     DispatchResponse response = profiler_->GetBestEffortCoverage();
91     SendResponse(request, response);
92 }
93 
StopPreciseCoverage(const DispatchRequest & request)94 void ProfilerImpl::DispatcherImpl::StopPreciseCoverage(const DispatchRequest &request)
95 {
96     DispatchResponse response = profiler_->StopPreciseCoverage();
97     SendResponse(request, response);
98 }
99 
TakePreciseCoverage(const DispatchRequest & request)100 void ProfilerImpl::DispatcherImpl::TakePreciseCoverage(const DispatchRequest &request)
101 {
102     DispatchResponse response = profiler_->TakePreciseCoverage();
103     SendResponse(request, response);
104 }
105 
StartPreciseCoverage(const DispatchRequest & request)106 void ProfilerImpl::DispatcherImpl::StartPreciseCoverage(const DispatchRequest &request)
107 {
108     std::unique_ptr<StartPreciseCoverageParams> params = StartPreciseCoverageParams::Create(request.GetParams());
109     if (params == nullptr) {
110         SendResponse(request, DispatchResponse::Fail("wrong params"));
111         return;
112     }
113     DispatchResponse response = profiler_->StartPreciseCoverage(*params);
114     SendResponse(request, response);
115 }
116 
StartTypeProfile(const DispatchRequest & request)117 void ProfilerImpl::DispatcherImpl::StartTypeProfile(const DispatchRequest &request)
118 {
119     DispatchResponse response = profiler_->StartTypeProfile();
120     SendResponse(request, response);
121 }
122 
StopTypeProfile(const DispatchRequest & request)123 void ProfilerImpl::DispatcherImpl::StopTypeProfile(const DispatchRequest &request)
124 {
125     DispatchResponse response = profiler_->StopTypeProfile();
126     SendResponse(request, response);
127 }
128 
TakeTypeProfile(const DispatchRequest & request)129 void ProfilerImpl::DispatcherImpl::TakeTypeProfile(const DispatchRequest &request)
130 {
131     DispatchResponse response = profiler_->TakeTypeProfile();
132     SendResponse(request, response);
133 }
134 
AllowNotify() const135 bool ProfilerImpl::Frontend::AllowNotify() const
136 {
137     return channel_ != nullptr;
138 }
139 
ConsoleProfileFinished()140 void ProfilerImpl::Frontend::ConsoleProfileFinished()
141 {
142     if (!AllowNotify()) {
143         return;
144     }
145 
146     tooling::ConsoleProfileFinished consoleProfileFinished;
147     channel_->SendNotification(consoleProfileFinished);
148 }
149 
ConsoleProfileStarted()150 void ProfilerImpl::Frontend::ConsoleProfileStarted()
151 {
152     if (!AllowNotify()) {
153         return;
154     }
155 
156     tooling::ConsoleProfileStarted consoleProfileStarted;
157     channel_->SendNotification(consoleProfileStarted);
158 }
159 
PreciseCoverageDeltaUpdate()160 void ProfilerImpl::Frontend::PreciseCoverageDeltaUpdate()
161 {
162     if (!AllowNotify()) {
163         return;
164     }
165 
166     tooling::PreciseCoverageDeltaUpdate preciseCoverageDeltaUpdate;
167     channel_->SendNotification(preciseCoverageDeltaUpdate);
168 }
169 
Disable()170 DispatchResponse ProfilerImpl::Disable()
171 {
172     LOG(ERROR, DEBUGGER) << "Disable not support now.";
173     return DispatchResponse::Ok();
174 }
175 
Enable()176 DispatchResponse ProfilerImpl::Enable()
177 {
178     LOG(ERROR, DEBUGGER) << "Enable not support now.";
179     return DispatchResponse::Ok();
180 }
181 
Start()182 DispatchResponse ProfilerImpl::Start()
183 {
184     panda::DFXJSNApi::StartCpuProfilerForInfo(vm_);
185     return DispatchResponse::Ok();
186 }
187 
Stop(std::unique_ptr<Profile> * profile)188 DispatchResponse ProfilerImpl::Stop(std::unique_ptr<Profile> *profile)
189 {
190     auto profileInfo = panda::DFXJSNApi::StopCpuProfilerForInfo();
191     if (profileInfo == nullptr) {
192         LOG(ERROR, DEBUGGER) << "Transfer DFXJSNApi::StopCpuProfilerImpl is failure";
193         return DispatchResponse::Fail("Stop is failure");
194     }
195     *profile = Profile::FromProfileInfo(*profileInfo);
196     return DispatchResponse::Ok();
197 }
198 
SetSamplingInterval(const SetSamplingIntervalParams & params)199 DispatchResponse ProfilerImpl::SetSamplingInterval(const SetSamplingIntervalParams &params)
200 {
201     panda::DFXJSNApi::SetCpuSamplingInterval(params.GetInterval());
202     return DispatchResponse::Ok();
203 }
204 
GetBestEffortCoverage()205 DispatchResponse ProfilerImpl::GetBestEffortCoverage()
206 {
207     LOG(ERROR, DEBUGGER) << "GetBestEffortCoverage not support now.";
208     return DispatchResponse::Ok();
209 }
210 
StopPreciseCoverage()211 DispatchResponse ProfilerImpl::StopPreciseCoverage()
212 {
213     LOG(ERROR, DEBUGGER) << "StopPreciseCoverage not support now.";
214     return DispatchResponse::Ok();
215 }
216 
TakePreciseCoverage()217 DispatchResponse ProfilerImpl::TakePreciseCoverage()
218 {
219     LOG(ERROR, DEBUGGER) << "TakePreciseCoverage not support now.";
220     return DispatchResponse::Ok();
221 }
222 
StartPreciseCoverage(const StartPreciseCoverageParams & params)223 DispatchResponse ProfilerImpl::StartPreciseCoverage([[maybe_unused]] const StartPreciseCoverageParams &params)
224 {
225     LOG(ERROR, DEBUGGER) << "StartPreciseCoverage not support now.";
226     return DispatchResponse::Ok();
227 }
228 
StartTypeProfile()229 DispatchResponse ProfilerImpl::StartTypeProfile()
230 {
231     LOG(ERROR, DEBUGGER) << "StartTypeProfile not support now.";
232     return DispatchResponse::Ok();
233 }
234 
StopTypeProfile()235 DispatchResponse ProfilerImpl::StopTypeProfile()
236 {
237     LOG(ERROR, DEBUGGER) << "StopTypeProfile not support now.";
238     return DispatchResponse::Ok();
239 }
240 
TakeTypeProfile()241 DispatchResponse ProfilerImpl::TakeTypeProfile()
242 {
243     LOG(ERROR, DEBUGGER) << "TakeTypeProfile not support now.";
244     return DispatchResponse::Ok();
245 }
246 }  // namespace panda::ecmascript::tooling
247