1/*
2 * Copyright (c) 2021-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_DFX_HPROF_HEAP_PROFILER_INTERFACE_H
17#define ECMASCRIPT_DFX_HPROF_HEAP_PROFILER_INTERFACE_H
18
19#include <memory>
20
21#include "libpandabase/macros.h"
22
23namespace panda::ecmascript {
24class EcmaVM;
25class TaggedObject;
26class Progress;
27class Stream;
28struct SamplingInfo;
29enum class DumpFormat { JSON, BINARY, OTHER };
30struct DumpSnapShotOption {
31    DumpFormat dumpFormat; // dumpformat like JSON BINARY and OTHER
32    bool isVmMode = true; // vmMode do more dump.
33    bool isPrivate = false;
34    bool captureNumericValue = false; // heapdump add numeric object.
35    bool isFullGC = true; // whether do FullGC.
36    bool isSimplify = false; // whether trim heapdump snapshot.
37    bool isSync = true; // OOM and Ide dump need sync dump.
38    bool isBeforeFill = true; // whether do fillmap on main thread.
39    bool isDumpOOM = false; // whether dump oom heapdump.
40};
41
42class HeapProfilerInterface {
43public:
44    static HeapProfilerInterface *GetInstance(EcmaVM *vm);
45    static void Destroy(EcmaVM *vm);
46
47    HeapProfilerInterface() = default;
48    virtual ~HeapProfilerInterface() = default;
49
50    virtual size_t GetIdCount() = 0;
51    virtual void AllocationEvent(TaggedObject *address, size_t size) = 0;
52    virtual void MoveEvent(uintptr_t address, TaggedObject *forwardAddress, size_t size)= 0;
53    virtual bool DumpHeapSnapshot(Stream *stream, const DumpSnapShotOption &dumpOption,
54                                  Progress *progress = nullptr) = 0;
55    // Provide an internal interface for oom dump.
56    virtual void DumpHeapSnapshot(const DumpSnapShotOption &dumpOption) = 0;
57    virtual bool GenerateHeapSnapshot(std::string &inputFilePath, std::string &outputPath) = 0;
58
59    virtual bool StartHeapTracking(double timeInterval, bool isVmMode = true, Stream *stream = nullptr,
60                                   bool traceAllocation = false, bool newThread = true) = 0;
61    virtual bool UpdateHeapTracking(Stream *stream) = 0;
62    virtual bool StopHeapTracking(Stream *stream, Progress *progress = nullptr, bool newThread = true) = 0;
63    virtual bool StartHeapSampling(uint64_t samplingInterval, int stackDepth = 128) = 0;
64    virtual void StopHeapSampling() = 0;
65    virtual const struct SamplingInfo *GetAllocationProfile() = 0;
66
67    NO_MOVE_SEMANTIC(HeapProfilerInterface);
68    NO_COPY_SEMANTIC(HeapProfilerInterface);
69};
70}  // namespace panda::ecmascript
71#endif  // ECMASCRIPT_DFX_HPROF_HEAP_PROFILER_INTERFACE_H
72