• 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 #include "libpandabase/macros.h"
22 
23 namespace panda::ecmascript {
24 class EcmaVM;
25 class TaggedObject;
26 class Progress;
27 class Stream;
28 struct SamplingInfo;
29 enum class DumpFormat { JSON, BINARY, OTHER };
30 struct 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     bool isJSLeakWatcher = false;  // whether dump from jsleak wather.
41 };
42 
43 enum class RawHeapDumpCropLevel {
44     LEVEL_V1,
45     LEVEL_V2,
46     DEFAULT = LEVEL_V1
47 };
48 
49 class HeapProfilerInterface {
50 public:
51     static HeapProfilerInterface *GetInstance(EcmaVM *vm);
52     static void Destroy(EcmaVM *vm);
53     // This is only used in OOM in SharedGC, since daemon thread does not have EcmaVM, so create a new instance
54     // to dump main JSThread.
55     static HeapProfilerInterface *CreateNewInstance(const EcmaVM *vm);
56     static void DestroyInstance(HeapProfilerInterface *heapProfiler);
57 
58     static void DumpHeapSnapshotForCMCOOM(void *thread);
59 
60     HeapProfilerInterface() = default;
61     virtual ~HeapProfilerInterface() = default;
62 
63     virtual size_t GetIdCount() = 0;
64     virtual void AllocationEvent(TaggedObject *address, size_t size) = 0;
65     virtual void MoveEvent(uintptr_t address, TaggedObject *forwardAddress, size_t size)= 0;
66     virtual bool DumpHeapSnapshot(Stream *stream, const DumpSnapShotOption &dumpOption,
67                                   Progress *progress = nullptr,
68                                   std::function<void(uint8_t)> callback = [] (uint8_t) {}) = 0;
69     // Provide an internal interface for oom dump.
70     // If `fromSharedGC` is set, means that OOM happened during SharedGC, and should do Dump then Fatal at once
71     // SharedGC complete, caller Must call this during `SuspendAll`.
72     virtual void DumpHeapSnapshotForOOM(const DumpSnapShotOption &dumpOption, bool fromSharedGC = false) = 0;
73 
74     virtual bool StartHeapTracking(double timeInterval, bool isVmMode = true, Stream *stream = nullptr,
75                                    bool traceAllocation = false, bool newThread = true) = 0;
76     virtual bool UpdateHeapTracking(Stream *stream) = 0;
77     virtual bool StopHeapTracking(Stream *stream, Progress *progress = nullptr, bool newThread = true) = 0;
78     virtual bool StartHeapSampling(uint64_t samplingInterval, int stackDepth = 128) = 0;
79     virtual void StopHeapSampling() = 0;
80     virtual const struct SamplingInfo *GetAllocationProfile() = 0;
81 
82     NO_MOVE_SEMANTIC(HeapProfilerInterface);
83     NO_COPY_SEMANTIC(HeapProfilerInterface);
84 };
85 }  // namespace panda::ecmascript
86 #endif  // ECMASCRIPT_DFX_HPROF_HEAP_PROFILER_INTERFACE_H
87