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 } 40 class DFXJSNApi; 41 using EcmaVM = ecmascript::EcmaVM; 42 using Stream = ecmascript::Stream; 43 using Progress = ecmascript::Progress; 44 using ProfileInfo = ecmascript::ProfileInfo; 45 using JsFrameInfo = ecmascript::JsFrameInfo; 46 using SamplingInfo = ecmascript::SamplingInfo; 47 using DebuggerPostTask = std::function<void(std::function<void()> &&)>; 48 49 class PUBLIC_API DFXJSNApi { 50 public: 51 // progress pointer is used to report the object number for IDE. 52 // isVmMode means the internal class in vm is visible. isPrivate means the number and string is not visible. 53 static void DumpHeapSnapshot(const EcmaVM *vm, int dumpFormat, const std::string &path, bool isVmMode = true, 54 bool isPrivate = false, bool captureNumericValue = false); 55 static void DumpHeapSnapshot(const EcmaVM *vm, int dumpFormat, Stream *stream, Progress *progress = nullptr, 56 bool isVmMode = true, bool isPrivate = false, bool captureNumericValue = false); 57 static void DumpHeapSnapshot(const EcmaVM *vm, int dumpFormat, bool isVmMode = true, bool isPrivate = false, 58 bool captureNumericValue = false); 59 static void DestroyHeapProfiler(const EcmaVM *vm); 60 61 static bool BuildNativeAndJsStackTrace(const EcmaVM *vm, std::string &stackTraceStr); 62 static bool BuildJsStackTrace(const EcmaVM *vm, std::string &stackTraceStr); 63 static bool StartHeapTracking(const EcmaVM *vm, double timeInterval, bool isVmMode = true, 64 Stream *stream = nullptr, bool traceAllocation = false, bool newThread = true); 65 static bool UpdateHeapTracking(const EcmaVM *vm, Stream *stream); 66 static bool StopHeapTracking(const EcmaVM *vm, const std::string &filePath, bool newThread = true); 67 static bool StopHeapTracking(const EcmaVM *vm, Stream *stream, Progress *progress = nullptr, bool newThread = true); 68 static void PrintStatisticResult(const EcmaVM *vm); 69 static void StartRuntimeStat(EcmaVM *vm); 70 static void StopRuntimeStat(EcmaVM *vm); 71 static size_t GetArrayBufferSize(const EcmaVM *vm); 72 static size_t GetHeapTotalSize(const EcmaVM *vm); 73 static size_t GetHeapUsedSize(const EcmaVM *vm); 74 static void NotifyApplicationState(EcmaVM *vm, bool inBackground); 75 static void NotifyIdleStatusControl(const EcmaVM *vm, std::function<void(bool)> callback); 76 static void NotifyIdleTime(const EcmaVM *vm, int idleMicroSec); 77 static void NotifyMemoryPressure(EcmaVM *vm, bool inHighMemoryPressure); 78 static bool BuildJsStackInfoList(const EcmaVM *hostVm, uint32_t tid, std::vector<JsFrameInfo>& jsFrames); 79 80 // cpuprofiler 81 static bool StopCpuProfilerForColdStart(const EcmaVM *vm); 82 static bool CpuProfilerSamplingAnyTime(const EcmaVM *vm); 83 static void StartCpuProfilerForFile(const EcmaVM *vm, const std::string &fileName, 84 int interval = 500); // 500:Default Sampling interval 500 microseconds 85 static void StopCpuProfilerForFile(const EcmaVM *vm); 86 static void StartCpuProfilerForInfo(const EcmaVM *vm, 87 int interval = 500); // 500:Default Sampling interval 500 microseconds 88 static std::unique_ptr<ProfileInfo> StopCpuProfilerForInfo(const EcmaVM *vm); 89 90 enum class PUBLIC_API ProfilerType : uint8_t { CPU_PROFILER, HEAP_PROFILER }; 91 92 struct ProfilerOption { 93 const char *libraryPath; 94 int interval = 500; // 500:Default Sampling interval 500 microseconds 95 ProfilerType profilerType = ProfilerType::CPU_PROFILER; 96 }; 97 static bool StartProfiler(EcmaVM *vm, const ProfilerOption &option, int32_t instanceId, 98 const DebuggerPostTask &debuggerPostTask); 99 static void SetCpuSamplingInterval(const EcmaVM *vm, int interval); 100 static bool StartSampling(const EcmaVM *vm, uint64_t samplingInterval); 101 static const SamplingInfo *GetAllocationProfile(const EcmaVM *vm); 102 static void StopSampling(const EcmaVM *vm); 103 104 static void ResumeVM(const EcmaVM *vm); 105 static bool SuspendVM(const EcmaVM *vm); 106 static bool IsSuspended(const EcmaVM *vm); 107 static bool CheckSafepoint(const EcmaVM *vm); 108 static void ResumeVMById(EcmaVM *vm, uint32_t tid); 109 static bool SuspendVMById(EcmaVM *vm, uint32_t tid); 110 }; 111 } 112 #endif