• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/napi/include/dfx_jsnapi.h"
17 #include "ecmascript/ecma_module.h"
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/hprof/heap_profiler.h"
20 #include "ecmascript/mem/c_string.h"
21 #include "ecmascript/mem/heap-inl.h"
22 namespace panda {
23 using ecmascript::CString;
24 using ecmascript::EcmaString;
25 using ecmascript::JSTaggedValue;
26 using ecmascript::GCStats;
27 template<typename T>
28 using JSHandle = ecmascript::JSHandle<T>;
29 
DumpHeapSnapShot(EcmaVM * vm,int dumpFormat,const std::string & path,bool isVmMode)30 void DFXJSNApi::DumpHeapSnapShot(EcmaVM *vm,  int dumpFormat, const std::string &path, bool isVmMode)
31 {
32     if (dumpFormat == 0) {
33         ecmascript::HeapProfilerInterface::DumpHeapSnapShot(vm->GetJSThread(), ecmascript::DumpFormat::JSON,
34                                                             path, isVmMode);
35     } else if (dumpFormat == 1) {
36         ecmascript::HeapProfilerInterface::DumpHeapSnapShot(vm->GetJSThread(), ecmascript::DumpFormat::BINARY,
37                                                             path, isVmMode);
38     } else if (dumpFormat == 2) { // 2: enum is 2
39         ecmascript::HeapProfilerInterface::DumpHeapSnapShot(vm->GetJSThread(), ecmascript::DumpFormat::OTHER,
40                                                             path, isVmMode);
41     }
42 }
43 
BuildNativeAndJsBackStackTrace(EcmaVM * vm)44 std::string DFXJSNApi::BuildNativeAndJsBackStackTrace(EcmaVM *vm)
45 {
46     std::string result = ecmascript::base::ErrorHelper::BuildNativeAndJsStackTrace(vm->GetJSThreadNoCheck());
47     return result;
48 }
49 
StartHeapTracking(EcmaVM * vm,double timeInterval,bool isVmMode)50 bool DFXJSNApi::StartHeapTracking(EcmaVM *vm, double timeInterval, bool isVmMode)
51 {
52     ecmascript::HeapProfilerInterface *heapProfile = ecmascript::HeapProfilerInterface::GetInstance(vm->GetJSThread());
53     return heapProfile->StartHeapTracking(vm->GetJSThread(), timeInterval, isVmMode);
54 }
55 
StopHeapTracking(EcmaVM * vm,int dumpFormat,const std::string & filePath)56 bool DFXJSNApi::StopHeapTracking(EcmaVM *vm,  int dumpFormat, const std::string &filePath)
57 {
58     bool result = false;
59     ecmascript::HeapProfilerInterface *heapProfile = ecmascript::HeapProfilerInterface::GetInstance(vm->GetJSThread());
60     if (dumpFormat == 0) {
61         result = heapProfile->StopHeapTracking(vm->GetJSThread(), ecmascript::DumpFormat::JSON, filePath);
62     }
63     if (dumpFormat == 1) {
64         result = heapProfile->StopHeapTracking(vm->GetJSThread(), ecmascript::DumpFormat::BINARY, filePath);
65     }
66     if (dumpFormat == 2) { // 2: enum is 2
67         result = heapProfile->StopHeapTracking(vm->GetJSThread(), ecmascript::DumpFormat::OTHER, filePath);
68     }
69     const ecmascript::Heap *heap = vm->GetJSThread()->GetEcmaVM()->GetHeap();
70     const_cast<ecmascript::NativeAreaAllocator *>(heap->GetNativeAreaAllocator())->Delete(heapProfile);
71     return result;
72 }
73 
PrintStatisticResult(const EcmaVM * vm)74 void DFXJSNApi::PrintStatisticResult(const EcmaVM *vm)
75 {
76     ecmascript::GCStats gcstats(vm->GetHeap());
77     gcstats.PrintStatisticResult(true);
78 }
79 
StartRuntimeStat(EcmaVM * vm)80 void DFXJSNApi::StartRuntimeStat(EcmaVM *vm)
81 {
82     vm->SetRuntimeStatEnable(true);
83 }
84 
StopRuntimeStat(EcmaVM * vm)85 void DFXJSNApi::StopRuntimeStat(EcmaVM *vm)
86 {
87     vm->SetRuntimeStatEnable(false);
88 }
89 
GetArrayBufferSize(EcmaVM * vm)90 size_t DFXJSNApi::GetArrayBufferSize(EcmaVM *vm)
91 {
92     return vm->GetHeap()->GetArrayBufferSize();
93 }
94 
GetHeapTotalSize(const EcmaVM * vm)95 size_t DFXJSNApi::GetHeapTotalSize(const EcmaVM *vm)
96 {
97     return vm->GetHeap()->GetCommittedSize();
98 }
99 
GetHeapUsedSize(const EcmaVM * vm)100 size_t DFXJSNApi::GetHeapUsedSize(const EcmaVM *vm)
101 {
102     return vm->GetHeap()->GetHeapObjectSize();
103 }
104 }
105