• 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_NAPI_INCLUDE_DFX_JSNAPI_H
17 #define ECMASCRIPT_NAPI_INCLUDE_DFX_JSNAPI_H
18 
19 #include <cassert>
20 #include <cstdint>
21 #include <functional>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "ecmascript/common.h"
27 #include "ecmascript/dfx/hprof/file_stream.h"
28 #include "ecmascript/dfx/hprof/heap_profiler_interface.h"
29 
30 #include "libpandabase/macros.h"
31 
32 namespace panda {
33 namespace ecmascript {
34 class EcmaVM;
35 class JSTaggedValue;
36 template<typename T>
37 class JSHandle;
38 class Stream;
39 class Progress;
40 struct ProfileInfo;
41 struct JsFrameInfo;
42 struct SamplingInfo;
43 struct TraceEvent;
44 enum class RawHeapDumpCropLevel;
45 }
46 class DFXJSNApi;
47 class JSValueRef;
48 template<typename T>
49 class Local;
50 using EcmaVM = ecmascript::EcmaVM;
51 using JSTaggedValue = ecmascript::JSTaggedValue;
52 using Stream = ecmascript::Stream;
53 using Progress = ecmascript::Progress;
54 using ProfileInfo = ecmascript::ProfileInfo;
55 using JsFrameInfo = ecmascript::JsFrameInfo;
56 using SamplingInfo = ecmascript::SamplingInfo;
57 using DebuggerPostTask = std::function<void(std::function<void()> &&)>;
58 using TraceEvent = ecmascript::TraceEvent;
59 using AppFreezeFilterCallback =
60     std::function<bool(const int32_t pid, const bool needDecreaseQuota, std::string &eventConfig)>;
61 using DumpSnapShotOption = ecmascript::DumpSnapShotOption;
62 using DumpFormat = ecmascript::DumpFormat;
63 using CropLevel = ecmascript::RawHeapDumpCropLevel;
64 struct DumpForSnapShotStruct {
65     const EcmaVM *vm;
66     DumpSnapShotOption dumpOption;
67 };
68 
69 class PUBLIC_API DFXJSNApi {
70 public:
71     // progress pointer is used to report the object number for IDE.
72     // isVmMode means the internal class in vm is visible. isPrivate means the number and string is not visible.
73     static void DumpHeapSnapshot(const EcmaVM *vm, const std::string &path, const DumpSnapShotOption &dumpOption,
74                                  const std::function<void(uint8_t)> &callback = nullptr);
75     static void DumpHeapSnapshot(const EcmaVM *vm, Stream *stream, const DumpSnapShotOption &dumpOption,
76                                  Progress *progress = nullptr,
77                                  std::function<void(uint8_t)> callback = [] (uint8_t) {});
78     static void DumpCpuProfile(const EcmaVM *vm);
79     static void DumpHeapSnapshot(const EcmaVM *vm, const DumpSnapShotOption &dumpOption);
80     static void DumpHeapSnapshot(const EcmaVM *vm, const DumpSnapShotOption &dumpOption, uint32_t tid);
81     static void DumpHeapSnapshotWithVm(const EcmaVM *vm, const DumpSnapShotOption &dumpOption, uint32_t tid);
82     static void TriggerGC(const EcmaVM *vm, uint32_t tid);
83     static void TriggerGCWithVm(const EcmaVM *vm);
84     static void TriggerSharedGCWithVm(const EcmaVM *vm);
85     static void DestroyHeapProfiler(const EcmaVM *vm);
86 
87     static bool BuildNativeAndJsStackTrace(const EcmaVM *vm, std::string &stackTraceStr);
88     static bool BuildJsStackTrace(const EcmaVM *vm, std::string &stackTraceStr);
89     static bool StartHeapTracking(const EcmaVM *vm, double timeInterval, bool isVmMode = true,
90                                   Stream *stream = nullptr, bool traceAllocation = false, bool newThread = true);
91     static bool UpdateHeapTracking(const EcmaVM *vm, Stream *stream);
92     static bool StopHeapTracking(const EcmaVM *vm, const std::string &filePath, bool newThread = true);
93     static bool StopHeapTracking(const EcmaVM *vm, Stream *stream, Progress *progress = nullptr, bool newThread = true);
94     static void PrintStatisticResult(const EcmaVM *vm);
95     static void StartRuntimeStat(EcmaVM *vm);
96     static void StopRuntimeStat(EcmaVM *vm);
97     static size_t GetArrayBufferSize(const EcmaVM *vm);
98     static size_t GetHeapTotalSize(const EcmaVM *vm);
99     // GetHeapUsedSize only support running on vm thread and provide an accurate value.
100     // GetHeapObjectSize provide a rough estimate.
101     static size_t GetHeapUsedSize(const EcmaVM *vm);
102     static size_t GetHeapObjectSize(const EcmaVM *vm);
103     static size_t GetHeapLimitSize(const EcmaVM *vm);
104     static size_t GetProcessHeapLimitSize();
105     static size_t GetGCCount(const EcmaVM *vm);
106     static size_t GetGCDuration(const EcmaVM *vm);
107     static size_t GetAccumulatedAllocateSize(const EcmaVM *vm);
108     static size_t GetAccumulatedFreeSize(const EcmaVM *vm);
109     static size_t GetFullGCLongTimeCount(const EcmaVM *vm);
110     static void GetHeapPrepare(const EcmaVM *vm);
111     static void NotifyApplicationState(EcmaVM *vm, bool inBackground);
112     static void NotifyIdleStatusControl(const EcmaVM *vm, std::function<void(bool)> callback);
113     static void NotifyIdleTime(const EcmaVM *vm, int idleMicroSec);
114     static void NotifyMemoryPressure(EcmaVM *vm, bool inHighMemoryPressure);
115     static void NotifyFinishColdStart(EcmaVM *vm, [[maybe_unused]] bool isConvinced);
116     static void NotifyHighSensitive(EcmaVM *vm, bool isStart);
117     static void NotifyWarmStart(EcmaVM *vm);
118     static bool AllowWarmStartGcRestrain(EcmaVM *vm);
119     static bool BuildJsStackInfoList(const EcmaVM *hostVm, uint32_t tid, std::vector<JsFrameInfo>& jsFrames);
120     static int32_t GetObjectHash(const EcmaVM *vm, Local<JSValueRef> nativeObject);
121     static int32_t GetObjectHashCode(const EcmaVM *vm, Local<JSValueRef> nativeObject);
122 
123     // cpuprofiler
124     static bool StopCpuProfilerForColdStart(const EcmaVM *vm);
125     static bool CpuProfilerSamplingAnyTime(const EcmaVM *vm);
126     static void CpuProfilerAnyTimeMainThread(const EcmaVM *vm);
127     static void SetJsDumpThresholds(EcmaVM *vm, size_t thresholds);
128     static void SetAppFreezeFilterCallback(const EcmaVM *vm, AppFreezeFilterCallback cb);
129     static bool StartCpuProfilerForFile(const EcmaVM *vm, const std::string &fileName,
130                                         int interval = 500); // 500:Default Sampling interval 500 microseconds
131     static void StopCpuProfilerForFile(const EcmaVM *vm);
132     static bool StartCpuProfilerForInfo(const EcmaVM *vm,
133                                         int interval = 500); // 500:Default Sampling interval 500 microseconds
134     static std::unique_ptr<ProfileInfo> StopCpuProfilerForInfo(const EcmaVM *vm);
135     static void EnableSeriliazationTimeoutCheck(const EcmaVM *ecmaVM, int32_t threshhold);
136     static void DisableSeriliazationTimeoutCheck(const EcmaVM *ecmaVM);
137 
138     enum class PUBLIC_API ProfilerType : uint8_t { CPU_PROFILER, HEAP_PROFILER };
139 
140     struct ProfilerOption {
141         const char *libraryPath;
142         int interval = 500; // 500:Default Sampling interval 500 microseconds
143         ProfilerType profilerType = ProfilerType::CPU_PROFILER;
144     };
145     // To be compatible with old process.
146     static bool StartProfiler(EcmaVM *vm, const ProfilerOption &option, int tid,
147                               int32_t instanceId, const DebuggerPostTask &debuggerPostTask, bool isDebugApp);
148     static void SetCpuSamplingInterval(const EcmaVM *vm, int interval);
149     static bool StartSampling(const EcmaVM *vm, uint64_t samplingInterval);
150     static const SamplingInfo *GetAllocationProfile(const EcmaVM *vm);
151     static void StopSampling(const EcmaVM *vm);
152 
153     static void ResumeVM(const EcmaVM *vm);
154     static bool SuspendVM(const EcmaVM *vm);
155     static bool IsSuspended(const EcmaVM *vm);
156     static void TerminateExecution(const EcmaVM *vm);
157     static bool CheckSafepoint(const EcmaVM *vm);
158     static void ResumeVMById(EcmaVM *vm, uint32_t tid);
159     static bool SuspendVMById(EcmaVM *vm, uint32_t tid);
160 
161     // tracing
162     static bool StartTracing(const EcmaVM *vm, std::string &categories);
163     static std::unique_ptr<std::vector<TraceEvent>> StopTracing(const EcmaVM *vm);
164     static void GetTracingBufferUseage(const EcmaVM *vm, double &percentFull, uint32_t &eventCount, double &value);
165     static void TranslateJSStackInfo(const EcmaVM *vm, std::string &url, int32_t &line, int32_t &column,
166         std::string &packageName);
167 
168     static uint32_t GetCurrentThreadId();
169     static void RegisterAsyncDetectCallBack(const EcmaVM *vm);
170     static void GetMainThreadStackTrace(const EcmaVM *vm, std::string &stackTraceStr);
171     static void SetJsRawHeapCropLevel(CropLevel level);
172 
173     static ecmascript::JSHandle<JSTaggedValue> FindFunctionForHook(const EcmaVM *vm,
174         const std::string &recordName, const std::string &namespaceName,
175         const std::string &className, const std::string &funcName);
176     static void ReplaceFunctionForHook(const EcmaVM *vm, ecmascript::JSHandle<JSTaggedValue> &target,
177         ecmascript::JSHandle<JSTaggedValue> &hook, ecmascript::JSHandle<JSTaggedValue> &backup);
178     static bool LoadHookModule(const EcmaVM *vm);
179 };
180 }
181 #endif