• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <functional>
20 #include <memory>
21 
22 #include "libpandabase/macros.h"
23 
24 namespace panda::ecmascript {
25 class EcmaVM;
26 class TaggedObject;
27 class Progress;
28 class Stream;
29 struct SamplingInfo;
30 enum class DumpFormat { JSON, BINARY, OTHER };
31 struct DumpSnapShotOption {
32     DumpFormat dumpFormat; // dumpformat like JSON BINARY and OTHER
33     bool isVmMode = true; // vmMode do more dump.
34     bool isPrivate = false;
35     bool captureNumericValue = false; // heapdump add numeric object.
36     bool isFullGC = true; // whether do FullGC.
37     bool isSimplify = false; // whether trim heapdump snapshot.
38     bool isSync = true; // OOM and Ide dump need sync dump.
39     bool isBeforeFill = true; // whether do fillmap on main thread.
40     bool isDumpOOM = false; // whether dump oom heapdump.
41 };
42 
43 class HeapProfilerInterface {
44 public:
45     static HeapProfilerInterface *GetInstance(EcmaVM *vm);
46     static void Destroy(EcmaVM *vm);
47     // This is only used in OOM in SharedGC, since daemon thread does not have EcmaVM, so create a new instance
48     // to dump main JSThread.
49     static HeapProfilerInterface *CreateNewInstance(const EcmaVM *vm);
50     static void DestroyInstance(HeapProfilerInterface *heapProfiler);
51 
52     HeapProfilerInterface() = default;
53     virtual ~HeapProfilerInterface() = default;
54 
55     virtual size_t GetIdCount() = 0;
56     virtual void AllocationEvent(TaggedObject *address, size_t size) = 0;
57     virtual void MoveEvent(uintptr_t address, TaggedObject *forwardAddress, size_t size)= 0;
58     virtual bool DumpHeapSnapshot(Stream *stream, const DumpSnapShotOption &dumpOption,
59                                   Progress *progress = nullptr,
60                                   std::function<void(uint8_t)> callback = [] (uint8_t) {}) = 0;
61     // Provide an internal interface for oom dump.
62     // If `fromSharedGC` is set, means that OOM happened during SharedGC, and should do Dump then Fatal at once
63     // SharedGC complete, caller Must call this during `SuspendAll`.
64     virtual void DumpHeapSnapshotForOOM(const DumpSnapShotOption &dumpOption, bool fromSharedGC = false) = 0;
65     virtual bool GenerateHeapSnapshot(std::string &inputFilePath, std::string &outputPath) = 0;
66 
67     virtual bool StartHeapTracking(double timeInterval, bool isVmMode = true, Stream *stream = nullptr,
68                                    bool traceAllocation = false, bool newThread = true) = 0;
69     virtual bool UpdateHeapTracking(Stream *stream) = 0;
70     virtual bool StopHeapTracking(Stream *stream, Progress *progress = nullptr, bool newThread = true) = 0;
71     virtual bool StartHeapSampling(uint64_t samplingInterval, int stackDepth = 128) = 0;
72     virtual void StopHeapSampling() = 0;
73     virtual const struct SamplingInfo *GetAllocationProfile() = 0;
74 
75     NO_MOVE_SEMANTIC(HeapProfilerInterface);
76     NO_COPY_SEMANTIC(HeapProfilerInterface);
77 };
78 }  // namespace panda::ecmascript
79 #endif  // ECMASCRIPT_DFX_HPROF_HEAP_PROFILER_INTERFACE_H
80