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