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, "Dumper GetInfo DumpHeapMemory result: %{public}i, hblkhd: %{public}i, uordblks: \
61 %{public}i, fordblks: %{public}i", ret, mallocInfo.hblkhd, mallocInfo.uordblks, mallocInfo.fordblks);
62 #endif
63 for (const auto &info : infos) {
64 vector<string> pageTag;
65 StringUtils::GetInstance().StringSplit(info.first, "#", pageTag);
66 if (pageTag.size() <= 1) {
67 continue;
68 }
69
70 string group;
71 if (pageTag[1] == "other") {
72 group = pageTag[0] == MemoryFilter::GetInstance().FILE_PAGE_TAG ? "FilePage other" : "AnonPage other";
73 } else {
74 group = pageTag[1];
75 }
76
77 if (groupNative == group) {
78 infos[info.first].insert(pair<string, uint64_t>(MEMINFO_HEAP_SIZE, heapInfo.size));
79 infos[info.first].insert(pair<string, uint64_t>(MEMINFO_HEAP_ALLOC, heapInfo.alloc));
80 infos[info.first].insert(pair<string, uint64_t>(MEMINFO_HEAP_FREE, heapInfo.free));
81 break;
82 }
83 }
84
85 DUMPER_HILOGI(MODULE_SERVICE, "GetHeapInfo: GetInfo memType:%{public}d pid:%{public}d end, success!", memType, pid);
86 return true;
87 }
88 } // namespace HiviewDFX
89 } // namespace OHOS
90