• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "executor/memory/get_ram_info.h"
16 #include "executor/memory/memory_util.h"
17 #include "executor/memory/memory_filter.h"
18 #include "util/string_utils.h"
19 using namespace std;
20 namespace OHOS {
21 namespace HiviewDFX {
GetRamInfo()22 GetRamInfo::GetRamInfo()
23 {
24 }
~GetRamInfo()25 GetRamInfo::~GetRamInfo()
26 {
27 }
28 
GetGroupMapValue(const GroupMap & infos,const vector<string> keys) const29 uint64_t GetRamInfo::GetGroupMapValue(const GroupMap &infos, const vector<string> keys) const
30 {
31     uint64_t totalValue = 0;
32     for (const auto &info : infos) {
33         auto &valueMap = info.second;
34         for (const auto &str : keys) {
35             auto it = valueMap.find(str);
36             if (it != valueMap.end()) {
37                 totalValue += it->second;
38             }
39         }
40     }
41     return totalValue;
42 }
43 
GetValueMapValue(const ValueMap & infos,const vector<string> strs) const44 uint64_t GetRamInfo::GetValueMapValue(const ValueMap &infos, const vector<string> strs) const
45 {
46     uint64_t totalValue = 0;
47     for (const auto &str : strs) {
48         auto it = infos.find(str);
49         if (it != infos.end()) {
50             if (str == "Mapped") {
51                 totalValue -= it->second;
52             } else {
53                 totalValue += it->second;
54             }
55         }
56     }
57     return totalValue;
58 }
59 
GetTotalPss(const GroupMap & infos) const60 uint64_t GetRamInfo::GetTotalPss(const GroupMap &infos) const
61 {
62     uint64_t totalValue = GetGroupMapValue(infos, MemoryFilter::GetInstance().CALC_TOTAL_PSS_);
63     return totalValue;
64 }
65 
GetTotalSwapPss(const GroupMap & infos) const66 uint64_t GetRamInfo::GetTotalSwapPss(const GroupMap &infos) const
67 {
68     uint64_t totalValue = GetGroupMapValue(infos, MemoryFilter::GetInstance().CALC_TOTAL_SWAP_PSS_);
69     return totalValue;
70 }
71 
GetFreeInfo(const ValueMap & infos) const72 uint64_t GetRamInfo::GetFreeInfo(const ValueMap &infos) const
73 {
74     uint64_t totalValue = GetValueMapValue(infos, MemoryFilter::GetInstance().CALC_FREE_);
75     return totalValue;
76 }
77 
GetKernelUsedInfo(const ValueMap & infos) const78 uint64_t GetRamInfo::GetKernelUsedInfo(const ValueMap &infos) const
79 {
80     uint64_t totalValue = GetValueMapValue(infos, MemoryFilter::GetInstance().CALC_KERNEL_USED_);
81     return totalValue;
82 }
83 
GetCachedInfo(const ValueMap & infos) const84 uint64_t GetRamInfo::GetCachedInfo(const ValueMap &infos) const
85 {
86     uint64_t totalValue = GetValueMapValue(infos, MemoryFilter::GetInstance().CALC_CACHED_);
87     return totalValue;
88 }
89 
GetTotalRam(const ValueMap & infos) const90 uint64_t GetRamInfo::GetTotalRam(const ValueMap &infos) const
91 {
92     uint64_t totalValue = GetValueMapValue(infos, MemoryFilter::GetInstance().CALC_TOTAL_);
93     return totalValue;
94 }
95 
GetZramTotalInfo(const ValueMap & infos) const96 uint64_t GetRamInfo::GetZramTotalInfo(const ValueMap &infos) const
97 {
98     uint64_t totalValue = GetValueMapValue(infos, MemoryFilter::GetInstance().CALC_ZARM_TOTAL_);
99     return totalValue;
100 }
101 
GetUsedRam(const GroupMap & smapsInfo,const ValueMap & meminfo,Ram & ram) const102 uint64_t GetRamInfo::GetUsedRam(const GroupMap &smapsInfo, const ValueMap &meminfo, Ram &ram) const
103 {
104     ram.totalPss = GetTotalPss(smapsInfo);
105     ram.kernelUsed = GetKernelUsedInfo(meminfo);
106     uint64_t totalValue = ram.totalPss + ram.kernelUsed;
107     return totalValue;
108 }
109 
GetFreeRam(const ValueMap & meminfo,Ram & ram) const110 uint64_t GetRamInfo::GetFreeRam(const ValueMap &meminfo, Ram &ram) const
111 {
112     ram.cachedInfo = GetCachedInfo(meminfo);
113     ram.freeInfo = GetFreeInfo(meminfo);
114     uint64_t totalValue = ram.cachedInfo + ram.freeInfo;
115     return totalValue;
116 }
117 
GetLostRam(const GroupMap & smapsInfo,const ValueMap & meminfo) const118 uint64_t GetRamInfo::GetLostRam(const GroupMap &smapsInfo, const ValueMap &meminfo) const
119 {
120     uint64_t totalValue = GetTotalRam(meminfo) - (GetTotalPss(smapsInfo) - GetTotalSwapPss(smapsInfo))
121                       - GetFreeInfo(meminfo) - GetCachedInfo(meminfo) - GetKernelUsedInfo(meminfo)
122                       - GetZramTotalInfo(meminfo);
123     return totalValue;
124 }
125 
GetRam(const GroupMap & smapsInfo,const ValueMap & meminfo) const126 GetRamInfo::Ram GetRamInfo::GetRam(const GroupMap &smapsInfo, const ValueMap &meminfo) const
127 {
128     Ram ram;
129 
130     ram.total = GetTotalRam(meminfo);
131     ram.used = GetUsedRam(smapsInfo, meminfo, ram);
132     ram.free = GetFreeRam(meminfo, ram);
133     ram.lost = GetLostRam(smapsInfo, meminfo);
134 
135     return ram;
136 }
137 } // namespace HiviewDFX
138 } // namespace OHOS