1 /* 2 * Copyright (c) 2021 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 <memory> 22 #include <string> 23 #include <vector> 24 25 #include "ecmascript/common.h" 26 #include "ecmascript/dfx/hprof/file_stream.h" 27 28 #include "libpandabase/macros.h" 29 30 namespace panda { 31 namespace ecmascript { 32 class EcmaVM; 33 class Stream; 34 class Progress; 35 struct ProfileInfo; 36 struct JsFrameInfo; 37 } 38 class DFXJSNApi; 39 using EcmaVM = ecmascript::EcmaVM; 40 using Stream = ecmascript::Stream; 41 using Progress = ecmascript::Progress; 42 using ProfileInfo = ecmascript::ProfileInfo; 43 using JsFrameInfo = ecmascript::JsFrameInfo; 44 45 class PUBLIC_API DFXJSNApi { 46 public: 47 // progress pointer is used to report the object number for IDE. 48 // isVmMode means the internal class in vm is visible. isPrivate means the number and string is not visible. 49 static void DumpHeapSnapshot(const EcmaVM *vm, int dumpFormat, const std::string &path, bool isVmMode = true, 50 bool isPrivate = false); 51 static void DumpHeapSnapshot(const EcmaVM *vm, int dumpFormat, Stream *stream, Progress *progress = nullptr, 52 bool isVmMode = true, bool isPrivate = false); 53 static void DumpHeapSnapshot(const EcmaVM *vm, int dumpFormat, bool isVmMode = true, bool isPrivate = false); 54 55 static bool BuildNativeAndJsStackTrace(const EcmaVM *vm, std::string &stackTraceStr); 56 static bool BuildJsStackTrace(const EcmaVM *vm, std::string &stackTraceStr); 57 static bool StartHeapTracking(const EcmaVM *vm, double timeInterval, bool isVmMode = true, 58 Stream *stream = nullptr, bool traceAllocation = false, bool newThread = true); 59 static bool UpdateHeapTracking(const EcmaVM *vm, Stream *stream); 60 static bool StopHeapTracking(const EcmaVM *vm, const std::string &filePath, bool newThread = true); 61 static bool StopHeapTracking(const EcmaVM *vm, Stream *stream, Progress *progress = nullptr, bool newThread = true); 62 static void PrintStatisticResult(const EcmaVM *vm); 63 static void StartRuntimeStat(EcmaVM *vm); 64 static void StopRuntimeStat(EcmaVM *vm); 65 static size_t GetArrayBufferSize(const EcmaVM *vm); 66 static size_t GetHeapTotalSize(const EcmaVM *vm); 67 static size_t GetHeapUsedSize(const EcmaVM *vm); 68 static void NotifyApplicationState(EcmaVM *vm, bool inBackground); 69 static void NotifyIdleTime(const EcmaVM *vm, int idleMicroSec); 70 static void NotifyMemoryPressure(EcmaVM *vm, bool inHighMemoryPressure); 71 static bool BuildJsStackInfoList(const EcmaVM *hostVm, uint32_t tid, std::vector<JsFrameInfo>& jsFrames); 72 73 // profile generator 74 #if defined(ECMASCRIPT_SUPPORT_CPUPROFILER) 75 static void StartCpuProfilerForFile(const EcmaVM *vm, const std::string &fileName, 76 const int interval = 500); // 500:Default Sampling interval 500 microseconds 77 static void StopCpuProfilerForFile(const EcmaVM *vm); 78 static void StartCpuProfilerForInfo(const EcmaVM *vm, 79 const int interval = 500); // 500:Default Sampling interval 500 microseconds 80 static std::unique_ptr<ProfileInfo> StopCpuProfilerForInfo(const EcmaVM *vm); 81 static void SetCpuSamplingInterval(const EcmaVM *vm, int interval); 82 #endif 83 84 static void ResumeVM(const EcmaVM *vm); 85 static bool SuspendVM(const EcmaVM *vm); 86 static bool IsSuspended(const EcmaVM *vm); 87 static bool CheckSafepoint(const EcmaVM *vm); 88 }; 89 } 90 #endif