14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_NAPI_INCLUDE_DFX_JSNAPI_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_NAPI_INCLUDE_DFX_JSNAPI_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <cassert>
204514f5e3Sopenharmony_ci#include <cstdint>
214514f5e3Sopenharmony_ci#include <functional>
224514f5e3Sopenharmony_ci#include <memory>
234514f5e3Sopenharmony_ci#include <string>
244514f5e3Sopenharmony_ci#include <vector>
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_ci#include "ecmascript/common.h"
274514f5e3Sopenharmony_ci#include "ecmascript/dfx/hprof/file_stream.h"
284514f5e3Sopenharmony_ci#include "ecmascript/dfx/hprof/heap_profiler_interface.h"
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_ci#include "libpandabase/macros.h"
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_cinamespace panda {
334514f5e3Sopenharmony_cinamespace ecmascript {
344514f5e3Sopenharmony_ciclass EcmaVM;
354514f5e3Sopenharmony_ciclass Stream;
364514f5e3Sopenharmony_ciclass Progress;
374514f5e3Sopenharmony_cistruct ProfileInfo;
384514f5e3Sopenharmony_cistruct JsFrameInfo;
394514f5e3Sopenharmony_cistruct SamplingInfo;
404514f5e3Sopenharmony_cistruct TraceEvent;
414514f5e3Sopenharmony_ci}
424514f5e3Sopenharmony_ciclass DFXJSNApi;
434514f5e3Sopenharmony_ciclass JSValueRef;
444514f5e3Sopenharmony_citemplate<typename T>
454514f5e3Sopenharmony_ciclass Local;
464514f5e3Sopenharmony_ciusing EcmaVM = ecmascript::EcmaVM;
474514f5e3Sopenharmony_ciusing Stream = ecmascript::Stream;
484514f5e3Sopenharmony_ciusing Progress = ecmascript::Progress;
494514f5e3Sopenharmony_ciusing ProfileInfo = ecmascript::ProfileInfo;
504514f5e3Sopenharmony_ciusing JsFrameInfo = ecmascript::JsFrameInfo;
514514f5e3Sopenharmony_ciusing SamplingInfo = ecmascript::SamplingInfo;
524514f5e3Sopenharmony_ciusing DebuggerPostTask = std::function<void(std::function<void()> &&)>;
534514f5e3Sopenharmony_ciusing TraceEvent = ecmascript::TraceEvent;
544514f5e3Sopenharmony_ciusing AppFreezeFilterCallback = std::function<bool(const int32_t pid)>;
554514f5e3Sopenharmony_ciusing DumpSnapShotOption = ecmascript::DumpSnapShotOption;
564514f5e3Sopenharmony_ciusing DumpFormat = ecmascript::DumpFormat;
574514f5e3Sopenharmony_cistruct DumpForSnapShotStruct {
584514f5e3Sopenharmony_ci    const EcmaVM *vm;
594514f5e3Sopenharmony_ci    DumpSnapShotOption dumpOption;
604514f5e3Sopenharmony_ci};
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ciclass PUBLIC_API DFXJSNApi {
634514f5e3Sopenharmony_cipublic:
644514f5e3Sopenharmony_ci    // progress pointer is used to report the object number for IDE.
654514f5e3Sopenharmony_ci    // isVmMode means the internal class in vm is visible. isPrivate means the number and string is not visible.
664514f5e3Sopenharmony_ci    static void DumpHeapSnapshot(const EcmaVM *vm, const std::string &path, const DumpSnapShotOption &dumpOption);
674514f5e3Sopenharmony_ci    static void DumpHeapSnapshot(const EcmaVM *vm, Stream *stream, const DumpSnapShotOption &dumpOption,
684514f5e3Sopenharmony_ci                                 Progress *progress = nullptr);
694514f5e3Sopenharmony_ci    static void DumpCpuProfile(const EcmaVM *vm);
704514f5e3Sopenharmony_ci    static void DumpHeapSnapshot(const EcmaVM *vm, const DumpSnapShotOption &dumpOption);
714514f5e3Sopenharmony_ci    static void DumpHeapSnapshot(const EcmaVM *vm, const DumpSnapShotOption &dumpOption, uint32_t tid);
724514f5e3Sopenharmony_ci    static void GenerateHeapSnapshotByBinFile(const EcmaVM *vm, std::string &inputFilePath, std::string &outputPath);
734514f5e3Sopenharmony_ci    static void DumpHeapSnapshotWithVm(const EcmaVM *vm, const DumpSnapShotOption &dumpOption, uint32_t tid);
744514f5e3Sopenharmony_ci    static void TriggerGC(const EcmaVM *vm, uint32_t tid);
754514f5e3Sopenharmony_ci    static void TriggerGCWithVm(const EcmaVM *vm);
764514f5e3Sopenharmony_ci    static void TriggerSharedGCWithVm(const EcmaVM *vm);
774514f5e3Sopenharmony_ci    static void DestroyHeapProfiler(const EcmaVM *vm);
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci    static bool BuildNativeAndJsStackTrace(const EcmaVM *vm, std::string &stackTraceStr);
804514f5e3Sopenharmony_ci    static bool BuildJsStackTrace(const EcmaVM *vm, std::string &stackTraceStr);
814514f5e3Sopenharmony_ci    static bool StartHeapTracking(const EcmaVM *vm, double timeInterval, bool isVmMode = true,
824514f5e3Sopenharmony_ci                                  Stream *stream = nullptr, bool traceAllocation = false, bool newThread = true);
834514f5e3Sopenharmony_ci    static bool UpdateHeapTracking(const EcmaVM *vm, Stream *stream);
844514f5e3Sopenharmony_ci    static bool StopHeapTracking(const EcmaVM *vm, const std::string &filePath, bool newThread = true);
854514f5e3Sopenharmony_ci    static bool StopHeapTracking(const EcmaVM *vm, Stream *stream, Progress *progress = nullptr, bool newThread = true);
864514f5e3Sopenharmony_ci    static void PrintStatisticResult(const EcmaVM *vm);
874514f5e3Sopenharmony_ci    static void StartRuntimeStat(EcmaVM *vm);
884514f5e3Sopenharmony_ci    static void StopRuntimeStat(EcmaVM *vm);
894514f5e3Sopenharmony_ci    static size_t GetArrayBufferSize(const EcmaVM *vm);
904514f5e3Sopenharmony_ci    static size_t GetHeapTotalSize(const EcmaVM *vm);
914514f5e3Sopenharmony_ci    // GetHeapUsedSize only support running on vm thread and provide an accurate value.
924514f5e3Sopenharmony_ci    // GetHeapObjectSize provide a rough estimate.
934514f5e3Sopenharmony_ci    static size_t GetHeapUsedSize(const EcmaVM *vm);
944514f5e3Sopenharmony_ci    static size_t GetHeapObjectSize(const EcmaVM *vm);
954514f5e3Sopenharmony_ci    static size_t GetHeapLimitSize(const EcmaVM *vm);
964514f5e3Sopenharmony_ci    static size_t GetProcessHeapLimitSize();
974514f5e3Sopenharmony_ci    static size_t GetGCCount(const EcmaVM *vm);
984514f5e3Sopenharmony_ci    static size_t GetGCDuration(const EcmaVM *vm);
994514f5e3Sopenharmony_ci    static size_t GetAccumulatedAllocateSize(const EcmaVM *vm);
1004514f5e3Sopenharmony_ci    static size_t GetAccumulatedFreeSize(const EcmaVM *vm);
1014514f5e3Sopenharmony_ci    static size_t GetFullGCLongTimeCount(const EcmaVM *vm);
1024514f5e3Sopenharmony_ci    static void GetHeapPrepare(const EcmaVM *vm);
1034514f5e3Sopenharmony_ci    static void NotifyApplicationState(EcmaVM *vm, bool inBackground);
1044514f5e3Sopenharmony_ci    static void NotifyIdleStatusControl(const EcmaVM *vm, std::function<void(bool)> callback);
1054514f5e3Sopenharmony_ci    static void NotifyIdleTime(const EcmaVM *vm, int idleMicroSec);
1064514f5e3Sopenharmony_ci    static void NotifyMemoryPressure(EcmaVM *vm, bool inHighMemoryPressure);
1074514f5e3Sopenharmony_ci    static void NotifyFinishColdStart(EcmaVM *vm, bool isConvinced);
1084514f5e3Sopenharmony_ci    static void NotifyHighSensitive(EcmaVM *vm, bool isStart);
1094514f5e3Sopenharmony_ci    static bool BuildJsStackInfoList(const EcmaVM *hostVm, uint32_t tid, std::vector<JsFrameInfo>& jsFrames);
1104514f5e3Sopenharmony_ci    static int32_t GetObjectHash(const EcmaVM *vm, Local<JSValueRef> nativeObject);
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ci    // cpuprofiler
1134514f5e3Sopenharmony_ci    static bool StopCpuProfilerForColdStart(const EcmaVM *vm);
1144514f5e3Sopenharmony_ci    static bool CpuProfilerSamplingAnyTime(const EcmaVM *vm);
1154514f5e3Sopenharmony_ci    static void CpuProfilerAnyTimeMainThread(const EcmaVM *vm);
1164514f5e3Sopenharmony_ci    static void SetJsDumpThresholds(EcmaVM *vm, size_t thresholds);
1174514f5e3Sopenharmony_ci    static void SetAppFreezeFilterCallback(const EcmaVM *vm, AppFreezeFilterCallback cb);
1184514f5e3Sopenharmony_ci    static bool StartCpuProfilerForFile(const EcmaVM *vm, const std::string &fileName,
1194514f5e3Sopenharmony_ci                                        int interval = 500); // 500:Default Sampling interval 500 microseconds
1204514f5e3Sopenharmony_ci    static void StopCpuProfilerForFile(const EcmaVM *vm);
1214514f5e3Sopenharmony_ci    static bool StartCpuProfilerForInfo(const EcmaVM *vm,
1224514f5e3Sopenharmony_ci                                        int interval = 500); // 500:Default Sampling interval 500 microseconds
1234514f5e3Sopenharmony_ci    static std::unique_ptr<ProfileInfo> StopCpuProfilerForInfo(const EcmaVM *vm);
1244514f5e3Sopenharmony_ci    static void EnableSeriliazationTimeoutCheck(const EcmaVM *ecmaVM, int32_t threshhold);
1254514f5e3Sopenharmony_ci    static void DisableSeriliazationTimeoutCheck(const EcmaVM *ecmaVM);
1264514f5e3Sopenharmony_ci
1274514f5e3Sopenharmony_ci    enum class PUBLIC_API ProfilerType : uint8_t { CPU_PROFILER, HEAP_PROFILER };
1284514f5e3Sopenharmony_ci
1294514f5e3Sopenharmony_ci    struct ProfilerOption {
1304514f5e3Sopenharmony_ci        const char *libraryPath;
1314514f5e3Sopenharmony_ci        int interval = 500; // 500:Default Sampling interval 500 microseconds
1324514f5e3Sopenharmony_ci        ProfilerType profilerType = ProfilerType::CPU_PROFILER;
1334514f5e3Sopenharmony_ci    };
1344514f5e3Sopenharmony_ci    // To be compatible with old process.
1354514f5e3Sopenharmony_ci    static bool StartProfiler(EcmaVM *vm, const ProfilerOption &option, int tid,
1364514f5e3Sopenharmony_ci                              int32_t instanceId, const DebuggerPostTask &debuggerPostTask, bool isDebugApp);
1374514f5e3Sopenharmony_ci    static void SetCpuSamplingInterval(const EcmaVM *vm, int interval);
1384514f5e3Sopenharmony_ci    static bool StartSampling(const EcmaVM *vm, uint64_t samplingInterval);
1394514f5e3Sopenharmony_ci    static const SamplingInfo *GetAllocationProfile(const EcmaVM *vm);
1404514f5e3Sopenharmony_ci    static void StopSampling(const EcmaVM *vm);
1414514f5e3Sopenharmony_ci
1424514f5e3Sopenharmony_ci    static void ResumeVM(const EcmaVM *vm);
1434514f5e3Sopenharmony_ci    static bool SuspendVM(const EcmaVM *vm);
1444514f5e3Sopenharmony_ci    static bool IsSuspended(const EcmaVM *vm);
1454514f5e3Sopenharmony_ci    static void TerminateExecution(const EcmaVM *vm);
1464514f5e3Sopenharmony_ci    static bool CheckSafepoint(const EcmaVM *vm);
1474514f5e3Sopenharmony_ci    static void ResumeVMById(EcmaVM *vm, uint32_t tid);
1484514f5e3Sopenharmony_ci    static bool SuspendVMById(EcmaVM *vm, uint32_t tid);
1494514f5e3Sopenharmony_ci
1504514f5e3Sopenharmony_ci    // tracing
1514514f5e3Sopenharmony_ci    static bool StartTracing(const EcmaVM *vm, std::string &categories);
1524514f5e3Sopenharmony_ci    static std::unique_ptr<std::vector<TraceEvent>> StopTracing(const EcmaVM *vm);
1534514f5e3Sopenharmony_ci    static void GetTracingBufferUseage(const EcmaVM *vm, double &percentFull, uint32_t &eventCount, double &value);
1544514f5e3Sopenharmony_ci    static void TranslateJSStackInfo(const EcmaVM *vm, std::string &url, int32_t &line, int32_t &column);
1554514f5e3Sopenharmony_ci
1564514f5e3Sopenharmony_ci    static uint32_t GetCurrentThreadId();
1574514f5e3Sopenharmony_ci};
1584514f5e3Sopenharmony_ci}
1594514f5e3Sopenharmony_ci#endif