• 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 #ifndef PARSE_SMAPS_INFO_H
16 #define PARSE_SMAPS_INFO_H
17 #include <map>
18 #include <string>
19 #include <vector>
20 #include "executor/memory/memory_filter.h"
21 #include "executor/memory/parse/meminfo_data.h"
22 #include "memory_collector.h"
23 namespace OHOS {
24 namespace HiviewDFX {
25 struct MemoryData {
26     std::string name = "[anon]";
27     std::string memoryClass;
28     std::string startAddr;  // 566ab4e000
29     std::string endAddr;    // 566ab7c000
30     std::string permission; // r-xp
31     int counts = 0; // number of occurrences of the same name
32     uint64_t size = 0; // calculate the sum of sizes corresponding to the same name.
33     uint64_t rss = 0;
34     uint64_t pss = 0;
35     uint64_t swapPss = 0;
36     uint64_t swap = 0;
37     uint64_t sharedDirty = 0;
38     uint64_t privateDirty = 0;
39     uint64_t sharedClean = 0;
40     uint64_t privateClean = 0;
41     uint64_t iNode = 0;
42 };
43 class ParseSmapsInfo {
44 public:
45     ParseSmapsInfo();
46     ~ParseSmapsInfo();
47 
48     using ValueMap = std::map<std::string, uint64_t>;
49     using GroupMap = std::map<std::string, ValueMap>;
50 
51     bool GetInfo(const MemoryFilter::MemoryType &memType, const int &pid, GroupMap &nativeMap, GroupMap &result);
52     // countSameNameMemMap---key: name, value: MemoryData
53     bool GetSmapsData(const int &pid, bool isShowSmapsAddress,
54         std::map<std::string, MemoryData>& countSameNameMemMap, std::vector<MemoryData>& showAddressMemInfoVec);
55 
56 private:
57     const std::vector<std::string> MEMORY_CLASS_VEC = {
58         "graph", "ark ts heap", ".db", "dev", "dmabuf", "guard", ".hap",
59         "native heap", ".so", "stack", ".ttf", "other"
60     };
61     std::string memGroup_ = "";
62     std::string nativeMemGroup_ = "";
63 
64     bool GetValue(const MemoryFilter::MemoryType &memType, const std::string &str, std::string &type, uint64_t &value);
65     bool GetHasPidValue(const std::string &str, std::string &type, uint64_t &value);
66     bool GetNoPidValue(const std::string &str, std::string &type, uint64_t &value);
67     void UpdateShowAddressMemInfoVec(const std::vector<MemoryItem>& memoryItems, const std::string& memoryClassStr,
68         std::vector<MemoryData>& showAddressMemInfoVec);
69     void UpdateCountSameNameMemMap(const std::vector<MemoryItem>& memoryItems, const std::string& memoryClassStr,
70         std::map<std::string, MemoryData>& countSameNameMemMap);
71     void SetMemoryData(MemoryData &memoryData, const MemoryItem &memoryItem, const std::string& memoryClassStr);
72 };
73 } // namespace HiviewDFX
74 } // namespace OHOS
75 #endif