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         enum class Method {
62             END,
63             GET_CATEGORIES,
64             RECORD_CLOCK_SYNC_MARKER,
65             REQUEST_MEMORY_DUMP,
66             START,
67             UNKNOWN
68         };
69         Method GetMethodEnum(const std::string& method);
70 
71     private:
72         NO_COPY_SEMANTIC(DispatcherImpl);
73         NO_MOVE_SEMANTIC(DispatcherImpl);
74 
75         std::unique_ptr<TracingImpl> tracing_ {};
76     };
77 
78     class Frontend {
79     public:
Frontend(ProtocolChannel *channel)80         explicit Frontend(ProtocolChannel *channel) : channel_(channel) {}
81         ~Frontend() = default;
82 
83         void BufferUsage(double percentFull, int32_t eventCount, double value);
84         void DataCollected(std::unique_ptr<std::vector<TraceEvent>> traceEvents);
85         void TracingComplete();
86 
87     private:
88         bool AllowNotify() const;
89         ProtocolChannel *channel_ {nullptr};
90     };
91 
92 private:
93     NO_COPY_SEMANTIC(TracingImpl);
94     NO_MOVE_SEMANTIC(TracingImpl);
95 
96     const EcmaVM *vm_ {nullptr};
97     Frontend frontend_;
98 #if defined(ECMASCRIPT_SUPPORT_TRACING)
99     uv_timer_t handle_ {};
100 #endif
101 };
102 }  // namespace panda::ecmascript::tooling
103 #endif