1e509ee18Sopenharmony_ci/*
2e509ee18Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3e509ee18Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e509ee18Sopenharmony_ci * you may not use this file except in compliance with the License.
5e509ee18Sopenharmony_ci * You may obtain a copy of the License at
6e509ee18Sopenharmony_ci *
7e509ee18Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e509ee18Sopenharmony_ci *
9e509ee18Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e509ee18Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e509ee18Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e509ee18Sopenharmony_ci * See the License for the specific language governing permissions and
13e509ee18Sopenharmony_ci * limitations under the License.
14e509ee18Sopenharmony_ci */
15e509ee18Sopenharmony_ci
16e509ee18Sopenharmony_ci#ifndef ECMASCRIPT_TOOLING_AGENT_TRACING_IMPL_H
17e509ee18Sopenharmony_ci#define ECMASCRIPT_TOOLING_AGENT_TRACING_IMPL_H
18e509ee18Sopenharmony_ci
19e509ee18Sopenharmony_ci#if defined(ECMASCRIPT_SUPPORT_TRACING)
20e509ee18Sopenharmony_ci#include <uv.h>
21e509ee18Sopenharmony_ci#endif
22e509ee18Sopenharmony_ci
23e509ee18Sopenharmony_ci#include "tooling/base/pt_params.h"
24e509ee18Sopenharmony_ci#include "tooling/base/pt_returns.h"
25e509ee18Sopenharmony_ci#include "dispatcher.h"
26e509ee18Sopenharmony_ci
27e509ee18Sopenharmony_ci#include "ecmascript/dfx/cpu_profiler/samples_record.h"
28e509ee18Sopenharmony_ci#include "ecmascript/dfx/tracing/tracing.h"
29e509ee18Sopenharmony_ci#include "libpandabase/macros.h"
30e509ee18Sopenharmony_ci
31e509ee18Sopenharmony_cinamespace panda::ecmascript::tooling {
32e509ee18Sopenharmony_ciclass TracingImpl final {
33e509ee18Sopenharmony_cipublic:
34e509ee18Sopenharmony_ci    explicit TracingImpl(const EcmaVM *vm, ProtocolChannel *channel) : vm_(vm), frontend_(channel)
35e509ee18Sopenharmony_ci    {
36e509ee18Sopenharmony_ci    }
37e509ee18Sopenharmony_ci    ~TracingImpl() = default;
38e509ee18Sopenharmony_ci
39e509ee18Sopenharmony_ci    std::unique_ptr<std::vector<TraceEvent>> End();
40e509ee18Sopenharmony_ci    DispatchResponse GetCategories(std::vector<std::string> categories);
41e509ee18Sopenharmony_ci    DispatchResponse RecordClockSyncMarker(std::string syncId);
42e509ee18Sopenharmony_ci    DispatchResponse RequestMemoryDump(std::unique_ptr<RequestMemoryDumpParams> params,
43e509ee18Sopenharmony_ci                                       std::string dumpGuid,  bool success);
44e509ee18Sopenharmony_ci    DispatchResponse Start(std::unique_ptr<StartParams> params);
45e509ee18Sopenharmony_ci#if defined(ECMASCRIPT_SUPPORT_TRACING)
46e509ee18Sopenharmony_ci    static void TracingBufferUsageReport(uv_timer_t* handle);
47e509ee18Sopenharmony_ci#endif
48e509ee18Sopenharmony_ci
49e509ee18Sopenharmony_ci    class DispatcherImpl final : public DispatcherBase {
50e509ee18Sopenharmony_ci    public:
51e509ee18Sopenharmony_ci        DispatcherImpl(ProtocolChannel *channel, std::unique_ptr<TracingImpl> tracing)
52e509ee18Sopenharmony_ci            : DispatcherBase(channel), tracing_(std::move(tracing)) {}
53e509ee18Sopenharmony_ci        ~DispatcherImpl() override = default;
54e509ee18Sopenharmony_ci        void Dispatch(const DispatchRequest &request) override;
55e509ee18Sopenharmony_ci        void End(const DispatchRequest &request);
56e509ee18Sopenharmony_ci        void GetCategories(const DispatchRequest &request);
57e509ee18Sopenharmony_ci        void RecordClockSyncMarker(const DispatchRequest &request);
58e509ee18Sopenharmony_ci        void RequestMemoryDump(const DispatchRequest &request);
59e509ee18Sopenharmony_ci        void Start(const DispatchRequest &request);
60e509ee18Sopenharmony_ci
61e509ee18Sopenharmony_ci        enum class Method {
62e509ee18Sopenharmony_ci            END,
63e509ee18Sopenharmony_ci            GET_CATEGORIES,
64e509ee18Sopenharmony_ci            RECORD_CLOCK_SYNC_MARKER,
65e509ee18Sopenharmony_ci            REQUEST_MEMORY_DUMP,
66e509ee18Sopenharmony_ci            START,
67e509ee18Sopenharmony_ci            UNKNOWN
68e509ee18Sopenharmony_ci        };
69e509ee18Sopenharmony_ci        Method GetMethodEnum(const std::string& method);
70e509ee18Sopenharmony_ci
71e509ee18Sopenharmony_ci    private:
72e509ee18Sopenharmony_ci        NO_COPY_SEMANTIC(DispatcherImpl);
73e509ee18Sopenharmony_ci        NO_MOVE_SEMANTIC(DispatcherImpl);
74e509ee18Sopenharmony_ci
75e509ee18Sopenharmony_ci        std::unique_ptr<TracingImpl> tracing_ {};
76e509ee18Sopenharmony_ci    };
77e509ee18Sopenharmony_ci
78e509ee18Sopenharmony_ci    class Frontend {
79e509ee18Sopenharmony_ci    public:
80e509ee18Sopenharmony_ci        explicit Frontend(ProtocolChannel *channel) : channel_(channel) {}
81e509ee18Sopenharmony_ci        ~Frontend() = default;
82e509ee18Sopenharmony_ci
83e509ee18Sopenharmony_ci        void BufferUsage(double percentFull, int32_t eventCount, double value);
84e509ee18Sopenharmony_ci        void DataCollected(std::unique_ptr<std::vector<TraceEvent>> traceEvents);
85e509ee18Sopenharmony_ci        void TracingComplete();
86e509ee18Sopenharmony_ci
87e509ee18Sopenharmony_ci    private:
88e509ee18Sopenharmony_ci        bool AllowNotify() const;
89e509ee18Sopenharmony_ci        ProtocolChannel *channel_ {nullptr};
90e509ee18Sopenharmony_ci    };
91e509ee18Sopenharmony_ci
92e509ee18Sopenharmony_ciprivate:
93e509ee18Sopenharmony_ci    NO_COPY_SEMANTIC(TracingImpl);
94e509ee18Sopenharmony_ci    NO_MOVE_SEMANTIC(TracingImpl);
95e509ee18Sopenharmony_ci
96e509ee18Sopenharmony_ci    const EcmaVM *vm_ {nullptr};
97e509ee18Sopenharmony_ci    Frontend frontend_;
98e509ee18Sopenharmony_ci#if defined(ECMASCRIPT_SUPPORT_TRACING)
99e509ee18Sopenharmony_ci    uv_timer_t handle_ {};
100e509ee18Sopenharmony_ci#endif
101e509ee18Sopenharmony_ci};
102e509ee18Sopenharmony_ci}  // namespace panda::ecmascript::tooling
103e509ee18Sopenharmony_ci#endif