• 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 "agent/profiler_impl.h"
17 
18 #include "base/pt_events.h"
19 #include "protocol_channel.h"
20 
21 #include "ecmascript/napi/include/dfx_jsnapi.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_DEBUGGER(DEBUG) << "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     if (profile == nullptr) {
74         SendResponse(request, response);
75         return;
76     }
77 
78     StopReturns result(std::move(profile));
79     SendResponse(request, response, result);
80 }
81 
SetSamplingInterval(const DispatchRequest & request)82 void ProfilerImpl::DispatcherImpl::SetSamplingInterval(const DispatchRequest &request)
83 {
84     std::unique_ptr<SetSamplingIntervalParams> params = SetSamplingIntervalParams::Create(request.GetParams());
85     if (params == nullptr) {
86         SendResponse(request, DispatchResponse::Fail("wrong params"));
87         return;
88     }
89     DispatchResponse response = profiler_->SetSamplingInterval(*params);
90     SendResponse(request, response);
91 }
92 
GetBestEffortCoverage(const DispatchRequest & request)93 void ProfilerImpl::DispatcherImpl::GetBestEffortCoverage(const DispatchRequest &request)
94 {
95     DispatchResponse response = profiler_->GetBestEffortCoverage();
96     SendResponse(request, response);
97 }
98 
StopPreciseCoverage(const DispatchRequest & request)99 void ProfilerImpl::DispatcherImpl::StopPreciseCoverage(const DispatchRequest &request)
100 {
101     DispatchResponse response = profiler_->StopPreciseCoverage();
102     SendResponse(request, response);
103 }
104 
TakePreciseCoverage(const DispatchRequest & request)105 void ProfilerImpl::DispatcherImpl::TakePreciseCoverage(const DispatchRequest &request)
106 {
107     DispatchResponse response = profiler_->TakePreciseCoverage();
108     SendResponse(request, response);
109 }
110 
StartPreciseCoverage(const DispatchRequest & request)111 void ProfilerImpl::DispatcherImpl::StartPreciseCoverage(const DispatchRequest &request)
112 {
113     std::unique_ptr<StartPreciseCoverageParams> params = StartPreciseCoverageParams::Create(request.GetParams());
114     if (params == nullptr) {
115         SendResponse(request, DispatchResponse::Fail("wrong params"));
116         return;
117     }
118     DispatchResponse response = profiler_->StartPreciseCoverage(*params);
119     SendResponse(request, response);
120 }
121 
StartTypeProfile(const DispatchRequest & request)122 void ProfilerImpl::DispatcherImpl::StartTypeProfile(const DispatchRequest &request)
123 {
124     DispatchResponse response = profiler_->StartTypeProfile();
125     SendResponse(request, response);
126 }
127 
StopTypeProfile(const DispatchRequest & request)128 void ProfilerImpl::DispatcherImpl::StopTypeProfile(const DispatchRequest &request)
129 {
130     DispatchResponse response = profiler_->StopTypeProfile();
131     SendResponse(request, response);
132 }
133 
TakeTypeProfile(const DispatchRequest & request)134 void ProfilerImpl::DispatcherImpl::TakeTypeProfile(const DispatchRequest &request)
135 {
136     DispatchResponse response = profiler_->TakeTypeProfile();
137     SendResponse(request, response);
138 }
139 
AllowNotify() const140 bool ProfilerImpl::Frontend::AllowNotify() const
141 {
142     return channel_ != nullptr;
143 }
144 
PreciseCoverageDeltaUpdate()145 void ProfilerImpl::Frontend::PreciseCoverageDeltaUpdate()
146 {
147     if (!AllowNotify()) {
148         return;
149     }
150 
151     tooling::PreciseCoverageDeltaUpdate preciseCoverageDeltaUpdate;
152     channel_->SendNotification(preciseCoverageDeltaUpdate);
153 }
154 
Disable()155 DispatchResponse ProfilerImpl::Disable()
156 {
157     return DispatchResponse::Ok();
158 }
159 
Enable()160 DispatchResponse ProfilerImpl::Enable()
161 {
162     return DispatchResponse::Ok();
163 }
164 
Start()165 DispatchResponse ProfilerImpl::Start()
166 {
167     panda::DFXJSNApi::StartCpuProfilerForInfo(vm_);
168     return DispatchResponse::Ok();
169 }
170 
Stop(std::unique_ptr<Profile> * profile)171 DispatchResponse ProfilerImpl::Stop(std::unique_ptr<Profile> *profile)
172 {
173     auto profileInfo = panda::DFXJSNApi::StopCpuProfilerForInfo(vm_);
174     if (profileInfo == nullptr) {
175         LOG_DEBUGGER(ERROR) << "Transfer DFXJSNApi::StopCpuProfilerImpl is failure";
176         return DispatchResponse::Fail("Stop is failure");
177     }
178     *profile = Profile::FromProfileInfo(*profileInfo);
179     return DispatchResponse::Ok();
180 }
181 
SetSamplingInterval(const SetSamplingIntervalParams & params)182 DispatchResponse ProfilerImpl::SetSamplingInterval(const SetSamplingIntervalParams &params)
183 {
184     panda::DFXJSNApi::SetCpuSamplingInterval(vm_, params.GetInterval());
185     return DispatchResponse::Ok();
186 }
187 
GetBestEffortCoverage()188 DispatchResponse ProfilerImpl::GetBestEffortCoverage()
189 {
190     return DispatchResponse::Fail("GetBestEffortCoverage not support now");
191 }
192 
StopPreciseCoverage()193 DispatchResponse ProfilerImpl::StopPreciseCoverage()
194 {
195     return DispatchResponse::Fail("StopPreciseCoverage not support now");
196 }
197 
TakePreciseCoverage()198 DispatchResponse ProfilerImpl::TakePreciseCoverage()
199 {
200     return DispatchResponse::Fail("TakePreciseCoverage not support now");
201 }
202 
StartPreciseCoverage(const StartPreciseCoverageParams & params)203 DispatchResponse ProfilerImpl::StartPreciseCoverage([[maybe_unused]] const StartPreciseCoverageParams &params)
204 {
205     return DispatchResponse::Fail("StartPreciseCoverage not support now");
206 }
207 
StartTypeProfile()208 DispatchResponse ProfilerImpl::StartTypeProfile()
209 {
210     return DispatchResponse::Fail("StartTypeProfile not support now");
211 }
212 
StopTypeProfile()213 DispatchResponse ProfilerImpl::StopTypeProfile()
214 {
215     return DispatchResponse::Fail("StopTypeProfile not support now");
216 }
217 
TakeTypeProfile()218 DispatchResponse ProfilerImpl::TakeTypeProfile()
219 {
220     return DispatchResponse::Fail("TakeTypeProfile not support now");
221 }
222 }  // namespace panda::ecmascript::tooling
223