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 29 #include "libpandabase/macros.h" 30 31 namespace panda { 32 namespace ecmascript { 33 class EcmaVM; 34 class Stream; 35 class Progress; 36 struct ProfileInfo; 37 struct JsFrameInfo; 38 struct SamplingInfo; 39 struct TraceEvent; 40 } 41 class DFXJSNApi; 42 using EcmaVM = ecmascript::EcmaVM; 43 using Stream = ecmascript::Stream; 44 using Progress = ecmascript::Progress; 45 using ProfileInfo = ecmascript::ProfileInfo; 46 using JsFrameInfo = ecmascript::JsFrameInfo; 47 using SamplingInfo = ecmascript::SamplingInfo; 48 using DebuggerPostTask = std::function<void(std::function<void()> &&)>; 49 using TraceEvent = ecmascript::TraceEvent; 50 51 struct DumpForSnapShotStruct { 52 const EcmaVM *vm; 53 int dumpFormat; 54 bool isVmMode = true; 55 bool isPrivate = false; 56 bool captureNumericValue = false; 57 bool isFullGC = true; 58 }; 59 60 class PUBLIC_API DFXJSNApi { 61 public: 62 // progress pointer is used to report the object number for IDE. 63 // isVmMode means the internal class in vm is visible. isPrivate means the number and string is not visible. 64 static void DumpHeapSnapshot(const EcmaVM *vm, int dumpFormat, const std::string &path, bool isVmMode = true, 65 bool isPrivate = false, bool captureNumericValue = false); 66 static void DumpHeapSnapshot(const EcmaVM *vm, int dumpFormat, Stream *stream, Progress *progress = nullptr, 67 bool isVmMode = true, bool isPrivate = false, bool captureNumericValue = false, 68 bool isFullGC = true); 69 static void DumpCpuProfile(const EcmaVM *vm, int dumpFormat, bool isVmMode = true, bool isPrivate = false, 70 bool captureNumericValue = false, bool isFullGC = true); 71 static void DumpHeapSnapshot(const EcmaVM *vm, int dumpFormat, bool isVmMode = true, bool isPrivate = false, 72 bool captureNumericValue = false, bool isFullGC = true); 73 static void DumpHeapSnapshotAllVMs(const EcmaVM *vm, int dumpFormat, bool isVmMode = true, bool isPrivate = false, 74 bool captureNumericValue = false, bool isFullGC = true); 75 static void DestroyHeapProfiler(const EcmaVM *vm); 76 77 static bool BuildNativeAndJsStackTrace(const EcmaVM *vm, std::string &stackTraceStr); 78 static bool BuildJsStackTrace(const EcmaVM *vm, std::string &stackTraceStr); 79 static bool StartHeapTracking(const EcmaVM *vm, double timeInterval, bool isVmMode = true, 80 Stream *stream = nullptr, bool traceAllocation = false, bool newThread = true); 81 static bool UpdateHeapTracking(const EcmaVM *vm, Stream *stream); 82 static bool StopHeapTracking(const EcmaVM *vm, const std::string &filePath, bool newThread = true); 83 static bool StopHeapTracking(const EcmaVM *vm, Stream *stream, Progress *progress = nullptr, bool newThread = true); 84 static void PrintStatisticResult(const EcmaVM *vm); 85 static void StartRuntimeStat(EcmaVM *vm); 86 static void StopRuntimeStat(EcmaVM *vm); 87 static size_t GetArrayBufferSize(const EcmaVM *vm); 88 static size_t GetHeapTotalSize(const EcmaVM *vm); 89 // GetHeapUsedSize only support running on vm thread and provide an accurate value. 90 // GetHeapObjectSize provide a rough estimate. 91 static size_t GetHeapUsedSize(const EcmaVM *vm); 92 static size_t GetHeapObjectSize(const EcmaVM *vm); 93 static size_t GetHeapLimitSize(const EcmaVM *vm); 94 static bool isOverLimit(const EcmaVM *vm); 95 static void SetOverLimit(EcmaVM *vm, bool state); 96 static void GetHeapPrepare(const EcmaVM *vm); 97 static void NotifyApplicationState(EcmaVM *vm, bool inBackground); 98 static void NotifyIdleStatusControl(const EcmaVM *vm, std::function<void(bool)> callback); 99 static void NotifyIdleTime(const EcmaVM *vm, int idleMicroSec); 100 static void NotifyMemoryPressure(EcmaVM *vm, bool inHighMemoryPressure); 101 static void NotifyFinishColdStart(EcmaVM *vm, bool isConvinced); 102 static void NotifyHighSensitive(EcmaVM *vm, bool isStart); 103 static bool BuildJsStackInfoList(const EcmaVM *hostVm, uint32_t tid, std::vector<JsFrameInfo>& jsFrames); 104 105 // cpuprofiler 106 static bool StopCpuProfilerForColdStart(const EcmaVM *vm); 107 static bool CpuProfilerSamplingAnyTime(const EcmaVM *vm); 108 static void StartCpuProfilerForFile(const EcmaVM *vm, const std::string &fileName, 109 int interval = 500); // 500:Default Sampling interval 500 microseconds 110 static void StopCpuProfilerForFile(const EcmaVM *vm); 111 static void StartCpuProfilerForInfo(const EcmaVM *vm, 112 int interval = 500); // 500:Default Sampling interval 500 microseconds 113 static std::unique_ptr<ProfileInfo> StopCpuProfilerForInfo(const EcmaVM *vm); 114 115 enum class PUBLIC_API ProfilerType : uint8_t { CPU_PROFILER, HEAP_PROFILER }; 116 117 struct ProfilerOption { 118 const char *libraryPath; 119 int interval = 500; // 500:Default Sampling interval 500 microseconds 120 ProfilerType profilerType = ProfilerType::CPU_PROFILER; 121 }; 122 // To be compatible with old process. 123 static bool StartProfiler(EcmaVM *vm, const ProfilerOption &option, int tid, 124 int32_t instanceId, const DebuggerPostTask &debuggerPostTask, bool isDebugApp); 125 static void SetCpuSamplingInterval(const EcmaVM *vm, int interval); 126 static bool StartSampling(const EcmaVM *vm, uint64_t samplingInterval); 127 static const SamplingInfo *GetAllocationProfile(const EcmaVM *vm); 128 static void StopSampling(const EcmaVM *vm); 129 130 static void ResumeVM(const EcmaVM *vm); 131 static bool SuspendVM(const EcmaVM *vm); 132 static bool IsSuspended(const EcmaVM *vm); 133 static void TerminateExecution(const EcmaVM *vm); 134 static bool CheckSafepoint(const EcmaVM *vm); 135 static void ResumeVMById(EcmaVM *vm, uint32_t tid); 136 static bool SuspendVMById(EcmaVM *vm, uint32_t tid); 137 138 // tracing 139 static bool StartTracing(const EcmaVM *vm, std::string &categories); 140 static std::unique_ptr<std::vector<TraceEvent>> StopTracing(const EcmaVM *vm); 141 static void GetTracingBufferUseage(const EcmaVM *vm, double &percentFull, uint32_t &eventCount, double &value); 142 }; 143 } 144 #endif