• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ECMASCRIPT_TOOLING_AGENT_TRACING_IMPL_H
17 #define ECMASCRIPT_TOOLING_AGENT_TRACING_IMPL_H
18 
19 #if defined(ECMASCRIPT_SUPPORT_TRACING)
20 #include <uv.h>
21 #endif
22 
23 #include "tooling/base/pt_params.h"
24 #include "tooling/base/pt_returns.h"
25 #include "dispatcher.h"
26 
27 #include "ecmascript/dfx/cpu_profiler/samples_record.h"
28 #include "ecmascript/dfx/tracing/tracing.h"
29 #include "libpandabase/macros.h"
30 
31 namespace panda::ecmascript::tooling {
32 class TracingImpl final {
33 public:
TracingImpl(const EcmaVM * vm,ProtocolChannel * channel)34     explicit TracingImpl(const EcmaVM *vm, ProtocolChannel *channel) : vm_(vm), frontend_(channel)
35     {
36     }
37     ~TracingImpl() = default;
38 
39     std::unique_ptr<std::vector<TraceEvent>> End();
40     DispatchResponse GetCategories(std::vector<std::string> categories);
41     DispatchResponse RecordClockSyncMarker(std::string syncId);
42     DispatchResponse RequestMemoryDump(std::unique_ptr<RequestMemoryDumpParams> params,
43                                        std::string dumpGuid,  bool success);
44     DispatchResponse Start(std::unique_ptr<StartParams> params);
45 #if defined(ECMASCRIPT_SUPPORT_TRACING)
46     static void TracingBufferUsageReport(uv_timer_t* handle);
47 #endif
48 
49     class DispatcherImpl final : public DispatcherBase {
50     public:
DispatcherImpl(ProtocolChannel * channel,std::unique_ptr<TracingImpl> tracing)51         DispatcherImpl(ProtocolChannel *channel, std::unique_ptr<TracingImpl> tracing)
52             : DispatcherBase(channel), tracing_(std::move(tracing)) {}
53         ~DispatcherImpl() override = default;
54         void Dispatch(const DispatchRequest &request) override;
55         void End(const DispatchRequest &request);
56         void GetCategories(const DispatchRequest &request);
57         void RecordClockSyncMarker(const DispatchRequest &request);
58         void RequestMemoryDump(const DispatchRequest &request);
59         void Start(const DispatchRequest &request);
60 
61     private:
62         NO_COPY_SEMANTIC(DispatcherImpl);
63         NO_MOVE_SEMANTIC(DispatcherImpl);
64 
65         using AgentHandler = void (TracingImpl::DispatcherImpl::*)(const DispatchRequest &request);
66         std::unique_ptr<TracingImpl> tracing_ {};
67     };
68 
69     class Frontend {
70     public:
Frontend(ProtocolChannel * channel)71         explicit Frontend(ProtocolChannel *channel) : channel_(channel) {}
72         ~Frontend() = default;
73 
74         void BufferUsage(double percentFull, int32_t eventCount, double value);
75         void DataCollected(std::unique_ptr<std::vector<TraceEvent>> traceEvents);
76         void TracingComplete();
77 
78     private:
79         bool AllowNotify() const;
80         ProtocolChannel *channel_ {nullptr};
81     };
82 
83 private:
84     NO_COPY_SEMANTIC(TracingImpl);
85     NO_MOVE_SEMANTIC(TracingImpl);
86 
87     const EcmaVM *vm_ {nullptr};
88     Frontend frontend_;
89 #if defined(ECMASCRIPT_SUPPORT_TRACING)
90     uv_timer_t handle_ {};
91 #endif
92 };
93 }  // namespace panda::ecmascript::tooling
94 #endif