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/dfx/hprof/heap_profiler_interface.h"
17 #include "ecmascript/dfx/hprof/heap_profiler.h"
18 #include "ecmascript/runtime.h"
19
20
21 namespace panda::ecmascript {
GetInstance(EcmaVM * vm)22 HeapProfilerInterface *HeapProfilerInterface::GetInstance(EcmaVM *vm)
23 {
24 return vm->GetOrNewHeapProfile();
25 }
26
Destroy(EcmaVM * vm)27 void HeapProfilerInterface::Destroy(EcmaVM *vm)
28 {
29 vm->DeleteHeapProfile();
30 }
31
CreateNewInstance(const EcmaVM * vm)32 HeapProfilerInterface *HeapProfilerInterface::CreateNewInstance(const EcmaVM *vm)
33 {
34 return new HeapProfiler(vm);
35 }
36
DestroyInstance(HeapProfilerInterface * heapProfiler)37 void HeapProfilerInterface::DestroyInstance(HeapProfilerInterface *heapProfiler)
38 {
39 delete heapProfiler;
40 }
41
DumpHeapSnapshotForCMCOOM(void * thread)42 void panda::ecmascript::HeapProfilerInterface::DumpHeapSnapshotForCMCOOM(void *thread)
43 {
44 #if defined(ECMASCRIPT_SUPPORT_SNAPSHOT) && defined(ENABLE_DUMP_IN_FAULTLOG)
45 EcmaVM *vm = Runtime::GetInstance()->GetMainThread()->GetEcmaVM();
46 if (thread != nullptr) {
47 vm = reinterpret_cast<JSThread *>(thread)->GetEcmaVM();
48 }
49
50 auto appfreezeCallback = Runtime::GetInstance()->GetAppFreezeFilterCallback();
51 std::string eventConfig;
52 bool shouldDump = appfreezeCallback == nullptr || appfreezeCallback(getprocpid(), true, eventConfig);
53 vm->GetEcmaGCKeyStats()->SendSysEventBeforeDump("OOMDump", 0, 0, eventConfig);
54 if (!shouldDump) {
55 LOG_ECMA(INFO) << "DumpHeapSnapshotForCMCOOM, no dump quota.";
56 return;
57 }
58
59 DumpSnapShotOption dumpOption;
60 dumpOption.dumpFormat = panda::ecmascript::DumpFormat::BINARY;
61 dumpOption.isFullGC = false;
62 dumpOption.isDumpOOM = true;
63
64 vm->GetOrNewHeapProfile()->DumpHeapSnapshotForOOM(dumpOption);
65 #endif
66 }
67 } // namespace panda::ecmascript
68