• 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/dynamic/base/pt_params.h"
24 #include "tooling/dynamic/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     ~TracingImpl();
36 
37     std::unique_ptr<std::vector<TraceEvent>> End();
38     DispatchResponse GetCategories(std::vector<std::string> categories);
39     DispatchResponse RecordClockSyncMarker(std::string syncId);
40     DispatchResponse RequestMemoryDump(std::unique_ptr<RequestMemoryDumpParams> params,
41                                        std::string dumpGuid,  bool success);
42     DispatchResponse Start(std::unique_ptr<StartParams> params);
43 #if defined(ECMASCRIPT_SUPPORT_TRACING)
44     static void TracingBufferUsageReport(uv_timer_t* handle);
45 #endif
46 
47     class DispatcherImpl final : public DispatcherBase {
48     public:
DispatcherImpl(ProtocolChannel * channel,std::unique_ptr<TracingImpl> tracing)49         DispatcherImpl(ProtocolChannel *channel, std::unique_ptr<TracingImpl> tracing)
50             : DispatcherBase(channel), tracing_(std::move(tracing)) {}
51         ~DispatcherImpl() override = default;
52         void Dispatch(const DispatchRequest &request) override;
53         void End(const DispatchRequest &request);
54         void GetCategories(const DispatchRequest &request);
55         void RecordClockSyncMarker(const DispatchRequest &request);
56         void RequestMemoryDump(const DispatchRequest &request);
57         void Start(const DispatchRequest &request);
58 
59         enum class Method {
60             END,
61             GET_CATEGORIES,
62             RECORD_CLOCK_SYNC_MARKER,
63             REQUEST_MEMORY_DUMP,
64             START,
65             UNKNOWN
66         };
67         Method GetMethodEnum(const std::string& method);
68 
69     private:
70         NO_COPY_SEMANTIC(DispatcherImpl);
71         NO_MOVE_SEMANTIC(DispatcherImpl);
72 
73         std::unique_ptr<TracingImpl> tracing_ {};
74     };
75 
76     class Frontend {
77     public:
Frontend(ProtocolChannel * channel)78         explicit Frontend(ProtocolChannel *channel) : channel_(channel) {}
79         ~Frontend() = default;
80 
81         void BufferUsage(double percentFull, int32_t eventCount, double value);
82         void DataCollected(std::unique_ptr<std::vector<TraceEvent>> traceEvents);
83         void TracingComplete();
84 
85     private:
86         bool AllowNotify() const;
87         ProtocolChannel *channel_ {nullptr};
88     };
89 
90 private:
91     NO_COPY_SEMANTIC(TracingImpl);
92     NO_MOVE_SEMANTIC(TracingImpl);
93 
94     const EcmaVM *vm_ {nullptr};
95     Frontend frontend_;
96 #if defined(ECMASCRIPT_SUPPORT_TRACING)
97     uv_timer_t *handle_ {nullptr};
98 #endif
99 };
100 }  // namespace panda::ecmascript::tooling
101 #endif