1/*
2 * Copyright (c) 2023 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_CLIENT_DOMAIN_PROFILER_CLIENT_H
17#define ECMASCRIPT_TOOLING_CLIENT_DOMAIN_PROFILER_CLIENT_H
18
19#include <iostream>
20#include <map>
21
22#include <vector>
23
24#include "tooling/base/pt_json.h"
25
26namespace OHOS::ArkCompiler::Toolchain {
27using PtJson = panda::ecmascript::tooling::PtJson;
28class ProfilerSingleton {
29public:
30    ProfilerSingleton() = default;
31
32    void AddCpuName(const std::string &data)
33    {
34        cpulist_.emplace_back(data);
35    }
36
37    void ShowCpuFile()
38    {
39        size_t size = cpulist_.size();
40        for (size_t i = 0; i < size; i++) {
41            std::cout << cpulist_[i] << std::endl;
42        }
43    }
44
45    void SetAddress(std::string address)
46    {
47        address_ = address;
48    }
49
50    const std::string& GetAddress() const
51    {
52        return address_;
53    }
54
55private:
56    std::vector<std::string> cpulist_;
57    std::string address_ = "/data/";
58    ProfilerSingleton(const ProfilerSingleton&) = delete;
59    ProfilerSingleton& operator=(const ProfilerSingleton&) = delete;
60};
61
62class ProfilerClient final {
63public:
64    ProfilerClient(uint32_t sessionId) : sessionId_(sessionId) {}
65    ~ProfilerClient() = default;
66
67    bool DispatcherCmd(const std::string &cmd);
68    int CpuprofileCommand();
69    int CpuprofileStopCommand();
70    int SetSamplingIntervalCommand();
71    int CpuprofileEnableCommand();
72    int CpuprofileDisableCommand();
73    bool WriteCpuProfileForFile(const std::string &fileName, const std::string &data);
74    void RecvProfilerResult(std::unique_ptr<PtJson> json);
75    void SetSamplingInterval(int interval);
76
77private:
78    int32_t interval_ = 0;
79    int32_t sessionId_;
80    std::map<uint32_t, std::string> idEventMap_ {};
81};
82} // OHOS::ArkCompiler::Toolchain
83#endif
84