• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "executor/jsheap_memory_dumper.h"
16 #include "dump_common_utils.h"
17 
18 using namespace std;
19 namespace OHOS {
20 namespace HiviewDFX {
JsHeapMemoryDumper()21 JsHeapMemoryDumper::JsHeapMemoryDumper()
22 {
23     if (jsHeapInfo_ == nullptr) {
24         jsHeapInfo_ = std::make_unique<DumpJsHeapInfo>();
25     }
26 }
27 
~JsHeapMemoryDumper()28 JsHeapMemoryDumper::~JsHeapMemoryDumper()
29 {
30 }
31 
PreExecute(const shared_ptr<DumperParameter> & parameter,StringMatrix dumpDatas)32 DumpStatus JsHeapMemoryDumper::PreExecute(const shared_ptr<DumperParameter> &parameter, StringMatrix dumpDatas)
33 {
34     if (parameter == nullptr) {
35         DUMPER_HILOGE(MODULE_SERVICE, "parameter is nullptr");
36         return DumpStatus::DUMP_FAIL;
37     }
38     needGc_ = true;
39     needSnapshot_ = true;
40     needLeakobj_ = parameter->GetOpts().isDumpJsHeapLeakobj_;
41     /* In the arkts runtime, DumpFormat::BINARY is used to generate rawheap files. */
42     needBinary_ = parameter->GetOpts().dumpJsRawHeap_;
43     if (parameter->GetOpts().isDumpJsHeapMemGC_) {
44         needSnapshot_ = false;
45         needLeakobj_ = false;
46         needBinary_ = false;
47         DUMPER_HILOGD(MODULE_SERVICE, "trigger gc, set needSnapshot_ false, needLeakobj_ false");
48     }
49     pid_ = static_cast<uint32_t>(parameter->GetOpts().dumpJsHeapMemPid_);
50     tid_ = static_cast<uint32_t>(parameter->GetOpts().threadId_);
51     dumpDatas_ = dumpDatas;
52     return DumpStatus::DUMP_OK;
53 }
54 
Execute()55 DumpStatus JsHeapMemoryDumper::Execute()
56 {
57     OHOS::AppExecFwk::JsHeapDumpInfo info;
58     info.pid = pid_;
59     info.tid = tid_;
60     info.needGc = needGc_;
61     info.needSnapshot = needSnapshot_;
62     info.needLeakobj = needLeakobj_;
63     info.needBinary = needBinary_;
64 
65     if (dumpDatas_ != nullptr && jsHeapInfo_ != nullptr) {
66         bool ret = jsHeapInfo_->DumpJsHeapMemory(info);
67         ret ? status_ = DumpStatus::DUMP_OK : status_ = DumpStatus::DUMP_FAIL;
68     } else {
69         DUMPER_HILOGE(MODULE_SERVICE, "JsHeapMemoryDumper Execute nullptr");
70         status_ = DumpStatus::DUMP_FAIL;
71     }
72     return status_;
73 }
74 
AfterExecute()75 DumpStatus JsHeapMemoryDumper::AfterExecute()
76 {
77     return status_;
78 }
79 } // namespace HiviewDFX
80 } // namespace OHOS