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_HEAPPROFILER_CLIENT_H
17#define ECMASCRIPT_TOOLING_CLIENT_DOMAIN_HEAPPROFILER_CLIENT_H
18
19#include <iostream>
20#include <fstream>
21#include <map>
22
23#include "tooling/base/pt_json.h"
24
25using PtJson = panda::ecmascript::tooling::PtJson;
26
27namespace OHOS::ArkCompiler::Toolchain {
28enum HeapProfilerEvent {
29    DAFAULT_VALUE = 0,
30    ALLOCATION = 1,
31    ALLOCATION_STOP,
32    HEAPDUMP,
33    ENABLE,
34    DISABLE,
35    SAMPLING,
36    SAMPLING_STOP,
37    COLLECT_GARBAGE
38};
39class HeapProfilerClient final {
40public:
41    HeapProfilerClient(uint32_t sessionId) : sessionId_(sessionId) {}
42    ~HeapProfilerClient() = default;
43
44    bool DispatcherCmd(const std::string &cmd, const std::string &arg);
45    int HeapDumpCommand();
46    int AllocationTrackCommand();
47    int AllocationTrackStopCommand();
48    int Enable();
49    int Disable();
50    int Samping();
51    int SampingStop();
52    int CollectGarbage();
53    void RecvReply(std::unique_ptr<PtJson> json);
54    void SaveHeapSnapshotAndAllocationTrackData(const std::string &chunk);
55    void SaveHeapsamplingData(std::unique_ptr<PtJson> result);
56    bool WriteHeapProfilerForFile(const std::string &fileName, const std::string &data);
57
58private:
59    std::string fileName_;
60    std::map<uint32_t, HeapProfilerEvent> idEventMap_;
61    std::string path_;
62    bool isAllocationMsg_ {false};
63    uint32_t sessionId_;
64};
65} // OHOS::ArkCompiler::Toolchain
66#endif