1 /*
2 * Copyright (C) 2021-2022 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 "executor/memory/get_heap_info.h"
17 #include "executor/memory/memory_util.h"
18 #include "executor/memory/memory_info.h"
19 #include "hilog_wrapper.h"
20 #include "util/string_utils.h"
21 using namespace std;
22 namespace OHOS {
23 namespace HiviewDFX {
GetHeapInfo()24 GetHeapInfo::GetHeapInfo()
25 {
26 }
27
~GetHeapInfo()28 GetHeapInfo::~GetHeapInfo()
29 {
30 }
31 #ifdef HIDUMPER_ABILITY_RUNTIME_ENABLE
GetAppManagerInstance()32 OHOS::sptr<OHOS::AppExecFwk::IAppMgr> GetHeapInfo::GetAppManagerInstance()
33 {
34 OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
35 OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
36 OHOS::sptr<OHOS::IRemoteObject> appObject = systemAbilityManager->GetSystemAbility(OHOS::APP_MGR_SERVICE_ID);
37 return OHOS::iface_cast<OHOS::AppExecFwk::IAppMgr>(appObject);
38 }
39 #endif
40
GetInfo(const MemoryFilter::MemoryType & memType,const int & pid,GroupMap & infos)41 bool GetHeapInfo::GetInfo(const MemoryFilter::MemoryType &memType, const int &pid, GroupMap &infos)
42 {
43 DUMPER_HILOGI(MODULE_SERVICE, "GetHeapInfo: GetInfo memType:%{public}d pid:%{public}d begin.", memType, pid);
44 struct MallHeapInfo heapInfo = {0};
45 #ifdef HIDUMPER_ABILITY_RUNTIME_ENABLE
46 OHOS::sptr<OHOS::AppExecFwk::IAppMgr> appManager = GetAppManagerInstance();
47 if (appManager == nullptr) {
48 DUMPER_HILOGE(MODULE_SERVICE, "GetHeapInfo: Get the appManager is nullptr.");
49 return false;
50 }
51 OHOS::AppExecFwk::MallocInfo mallocInfo;
52 int ret = appManager->DumpHeapMemory(pid, mallocInfo);
53 if (ret != ERR_OK) {
54 DUMPER_HILOGE(MODULE_SERVICE, "DumpHeapMemory return failed, ret is:%{public}d", ret);
55 } else {
56 heapInfo.size = mallocInfo.hblkhd / numberSys;
57 heapInfo.alloc = mallocInfo.uordblks / numberSys;
58 heapInfo.free = mallocInfo.fordblks / numberSys;
59 }
60 DUMPER_HILOGD(MODULE_SERVICE, "DumpHeapMemory result: %{public}i, uordblks: %{public}llu, fordblks: %{public}llu,"
61 "hblkhd: %{public}llu", ret, static_cast<unsigned long long>(mallocInfo.fordblks),
62 static_cast<unsigned long long>(mallocInfo.uordblks), static_cast<unsigned long long>(mallocInfo.hblkhd));
63 #endif
64 for (const auto &info : infos) {
65 vector<string> pageTag;
66 StringUtils::GetInstance().StringSplit(info.first, "#", pageTag);
67 if (pageTag.size() <= 1) {
68 continue;
69 }
70
71 string group;
72 if (pageTag[1] == "other") {
73 group = pageTag[0] == MemoryFilter::GetInstance().FILE_PAGE_TAG ? "FilePage other" : "AnonPage other";
74 } else {
75 group = pageTag[1];
76 }
77
78 if (groupNative == group) {
79 infos[info.first].insert(pair<string, uint64_t>(MEMINFO_HEAP_SIZE, heapInfo.size));
80 infos[info.first].insert(pair<string, uint64_t>(MEMINFO_HEAP_ALLOC, heapInfo.alloc));
81 infos[info.first].insert(pair<string, uint64_t>(MEMINFO_HEAP_FREE, heapInfo.free));
82 break;
83 }
84 }
85
86 DUMPER_HILOGI(MODULE_SERVICE, "GetHeapInfo: GetInfo memType:%{public}d pid:%{public}d end, success!", memType, pid);
87 return true;
88 }
89 } // namespace HiviewDFX
90 } // namespace OHOS
91