1 /*
2 * Copyright (c) 2025 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/cjheap_memory_dumper.h"
16 #include "dump_common_utils.h"
17
18 using namespace std;
19 namespace OHOS {
20 namespace HiviewDFX {
CjHeapMemoryDumper()21 CjHeapMemoryDumper::CjHeapMemoryDumper()
22 {
23 if (cjHeapInfo_ == nullptr) {
24 cjHeapInfo_ = std::make_unique<DumpCjHeapInfo>();
25 }
26 }
27
~CjHeapMemoryDumper()28 CjHeapMemoryDumper::~CjHeapMemoryDumper()
29 {
30 }
31
PreExecute(const shared_ptr<DumperParameter> & parameter,StringMatrix dumpDatas)32 DumpStatus CjHeapMemoryDumper::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 if (parameter->GetOpts().isDumpCjHeapMemGC_) {
41 needSnapshot_ = false;
42 DUMPER_HILOGD(MODULE_SERVICE, "trigger gc");
43 }
44 pid_ = static_cast<uint32_t>(parameter->GetOpts().dumpCjHeapMemPid_);
45 dumpDatas_ = dumpDatas;
46 return DumpStatus::DUMP_OK;
47 }
48
Execute()49 DumpStatus CjHeapMemoryDumper::Execute()
50 {
51 OHOS::AppExecFwk::CjHeapDumpInfo info;
52 info.pid = pid_;
53 info.needGc = needGc_;
54 info.needSnapshot = needSnapshot_;
55
56 if (dumpDatas_ != nullptr && cjHeapInfo_ != nullptr) {
57 bool ret = cjHeapInfo_->DumpCjHeapMemory(info);
58 ret ? status_ = DumpStatus::DUMP_OK : status_ = DumpStatus::DUMP_FAIL;
59 } else {
60 DUMPER_HILOGE(MODULE_SERVICE, "CjHeapMemoryDumper Execute nullptr");
61 status_ = DumpStatus::DUMP_FAIL;
62 }
63 return status_;
64 }
65
AfterExecute()66 DumpStatus CjHeapMemoryDumper::AfterExecute()
67 {
68 return status_;
69 }
70 } // namespace HiviewDFX
71 } // namespace OHOS