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