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     needGc_ = true;
35     needSnapshot_ = true;
36     needLeakobj_ = parameter->GetOpts().isDumpJsHeapLeakobj_;
37     if (parameter->GetOpts().isDumpJsHeapMemGC_) {
38         needSnapshot_ = false;
39         needLeakobj_ = false;
40         DUMPER_HILOGD(MODULE_SERVICE, "trigger gc, set needSnapshot_ false, needLeakobj_ false");
41     }
42     pid_ = static_cast<uint32_t>(parameter->GetOpts().dumpJsHeapMemPid_);
43     tid_ = static_cast<uint32_t>(parameter->GetOpts().threadId_);
44     dumpDatas_ = dumpDatas;
45     return DumpStatus::DUMP_OK;
46 }
47 
Execute()48 DumpStatus JsHeapMemoryDumper::Execute()
49 {
50     OHOS::AppExecFwk::JsHeapDumpInfo info;
51     info.pid = pid_;
52     info.tid = tid_;
53     info.needGc = needGc_;
54     info.needSnapshot = needSnapshot_;
55     info.needLeakobj = needLeakobj_;
56 
57     if (dumpDatas_ != nullptr && jsHeapInfo_ != nullptr) {
58         bool ret = jsHeapInfo_->DumpJsHeapMemory(info);
59         ret ? status_ = DumpStatus::DUMP_OK : status_ = DumpStatus::DUMP_FAIL;
60     } else {
61         DUMPER_HILOGE(MODULE_SERVICE, "JsHeapMemoryDumper Execute nullptr");
62         status_ = DumpStatus::DUMP_FAIL;
63     }
64     return status_;
65 }
66 
AfterExecute()67 DumpStatus JsHeapMemoryDumper::AfterExecute()
68 {
69     return status_;
70 }
71 } // namespace HiviewDFX
72 } // namespace OHOS