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 #ifndef ECMASCRIPT_TOOLING_AGENT_PROFILER_IMPL_H 17 #define ECMASCRIPT_TOOLING_AGENT_PROFILER_IMPL_H 18 19 #include "tooling/base/pt_params.h" 20 #include "tooling/base/pt_returns.h" 21 #include "dispatcher.h" 22 23 #include "ecmascript/dfx/cpu_profiler/samples_record.h" 24 #include "libpandabase/macros.h" 25 26 namespace panda::ecmascript::tooling { 27 class ProfilerImpl final { 28 public: ProfilerImpl(const EcmaVM * vm,ProtocolChannel * channel)29 ProfilerImpl(const EcmaVM *vm, ProtocolChannel *channel) : vm_(vm), frontend_(channel) {} 30 ~ProfilerImpl() = default; 31 32 DispatchResponse Disable(); 33 DispatchResponse Enable(); 34 DispatchResponse Start(); 35 DispatchResponse Stop(std::unique_ptr<Profile> *profile); 36 DispatchResponse SetSamplingInterval(const SetSamplingIntervalParams ¶ms); 37 DispatchResponse GetBestEffortCoverage(); 38 DispatchResponse StopPreciseCoverage(); 39 DispatchResponse TakePreciseCoverage(); 40 DispatchResponse StartPreciseCoverage(const StartPreciseCoverageParams ¶ms); 41 DispatchResponse StartTypeProfile(); 42 DispatchResponse StopTypeProfile(); 43 DispatchResponse TakeTypeProfile(); 44 45 class DispatcherImpl final : public DispatcherBase { 46 public: DispatcherImpl(ProtocolChannel * channel,std::unique_ptr<ProfilerImpl> profiler)47 DispatcherImpl(ProtocolChannel *channel, std::unique_ptr<ProfilerImpl> profiler) 48 : DispatcherBase(channel), profiler_(std::move(profiler)) {} 49 ~DispatcherImpl() override = default; 50 51 void Dispatch(const DispatchRequest &request) override; 52 void Enable(const DispatchRequest &request); 53 void Disable(const DispatchRequest &request); 54 void Start(const DispatchRequest &request); 55 void Stop(const DispatchRequest &request); 56 void SetSamplingInterval(const DispatchRequest &request); 57 void GetBestEffortCoverage(const DispatchRequest &request); 58 void StopPreciseCoverage(const DispatchRequest &request); 59 void TakePreciseCoverage(const DispatchRequest &request); 60 void StartPreciseCoverage(const DispatchRequest &request); 61 void StartTypeProfile(const DispatchRequest &request); 62 void StopTypeProfile(const DispatchRequest &request); 63 void TakeTypeProfile(const DispatchRequest &request); 64 65 private: 66 NO_COPY_SEMANTIC(DispatcherImpl); 67 NO_MOVE_SEMANTIC(DispatcherImpl); 68 69 using AgentHandler = void (ProfilerImpl::DispatcherImpl::*)(const DispatchRequest &request); 70 std::unique_ptr<ProfilerImpl> profiler_ {}; 71 }; 72 73 class Frontend { 74 public: Frontend(ProtocolChannel * channel)75 explicit Frontend(ProtocolChannel *channel) : channel_(channel) {} 76 ~Frontend() = default; 77 78 void PreciseCoverageDeltaUpdate(); 79 80 private: 81 bool AllowNotify() const; 82 83 ProtocolChannel *channel_ {nullptr}; 84 }; 85 86 private: 87 NO_COPY_SEMANTIC(ProfilerImpl); 88 NO_MOVE_SEMANTIC(ProfilerImpl); 89 90 const EcmaVM *vm_ {nullptr}; 91 [[maybe_unused]] Frontend frontend_; 92 }; 93 } // namespace panda::ecmascript::tooling 94 #endif 95