• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 SMAPS_MEMORY_INFO_H
16 #define SMAPS_MEMORY_INFO_H
17 #include <map>
18 #include <memory>
19 #include <future>
20 #include <string>
21 #include <vector>
22 #include "common.h"
23 #include "time.h"
24 #include "executor/memory/parse/meminfo_data.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace {
29 static const std::string SMAPS_MEMINFO_RSS = "Rss";
30 static const std::string SMAPS_MEMINFO_PSS = "Pss";
31 static const std::string SMAPS_MEMINFO_SHARED_CLEAN = "Shared_Clean";
32 static const std::string SMAPS_MEMINFO_SHARED_DIRTY = "Shared_Dirty";
33 static const std::string SMAPS_MEMINFO_PRIVATE_CLEAN = "Private_Clean";
34 static const std::string SMAPS_MEMINFO_PRIVATE_DIRTY = "Private_Dirty";
35 static const std::string SMAPS_MEMINFO_SWAP = "Swap";
36 static const std::string SMAPS_MEMINFO_SWAP_PSS = "SwapPss";
37 static const std::string SMAPS_MEMINFO_SIZE = "Size";
38 static const std::string SMAPS_MEMINFO_NAME = "Name";
39 static const std::string SMAPS_MEMINFO_COUNTS = "Counts";
40 }
41 class SmapsMemoryInfo {
42 public:
43     SmapsMemoryInfo();
44     ~SmapsMemoryInfo();
45 
46     using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>;
47     using ValueMap = std::map<std::string, uint64_t>;
48     using GroupMap = std::map<std::string, ValueMap>;
49     using MemSmapsFun = std::function<void(MemInfoData::MemSmapsInfo&, uint64_t)>;
50     bool ShowMemorySmapsByPid(const int &pid, StringMatrix result, bool isShowSmapsInfo);
51 
52 private:
53     enum Status {
54         SUCCESS_MORE_DATA = 1,
55         FAIL_MORE_DATA = 2,
56         SUCCESS_NO_MORE_DATA = 3,
57         FAIL_NO_MORE_DATA = 4,
58     };
59 
60     void InsertSmapsTitle(StringMatrix result, bool isShowSmapsInfo);
61     void BuildSmapsResult(const GroupMap &infos, StringMatrix result, bool isShowSmapsInfo,
62         std::vector<std::map<std::string, std::string>> vectMap);
63     void BuildSmapsInfo(StringMatrix result, std::vector<std::map<std::string, std::string>> vectMap);
64     bool CalcSmapsStatData(MemInfoData::MemSmapsInfo &memSmapsInfo, const GroupMap &infos);
65     bool CalcSmapsInfo(MemInfoData::MemSmapsInfo &memSmapsInfo,
66         std::vector<std::map<std::string, std::string>> vectMap);
67     void CalcSmapsGroup(const GroupMap &infos, StringMatrix result, MemInfoData::MemSmapsInfo &memSmapsInfo,
68         bool isShowSmapsInfo, std::vector<std::map<std::string, std::string>> vectMap);
69     void SetRss(MemInfoData::MemSmapsInfo &meminfo, uint64_t value);
70     void SetPss(MemInfoData::MemSmapsInfo &meminfo, uint64_t value);
71     void SetSharedClean(MemInfoData::MemSmapsInfo &meminfo, uint64_t value);
72     void SetSharedDirty(MemInfoData::MemSmapsInfo &meminfo, uint64_t value);
73     void SetPrivateClean(MemInfoData::MemSmapsInfo &meminfo, uint64_t value);
74     void SetPrivateDirty(MemInfoData::MemSmapsInfo &meminfo, uint64_t value);
75     void SetSwap(MemInfoData::MemSmapsInfo &meminfo, uint64_t value);
76     void SetSwapPss(MemInfoData::MemSmapsInfo &meminfo, uint64_t value);
77     void SetSize(MemInfoData::MemSmapsInfo &meminfo, uint64_t value);
78     void SetName(MemInfoData::MemSmapsInfo &meminfo, const std::string &value);
79     void SetCounts(MemInfoData::MemSmapsInfo &meminfo, uint64_t value);
80     std::vector<std::pair<std::string, MemSmapsFun>> sMapsMethodVec_;
81 };
82 } // namespace HiviewDFX
83 } // namespace OHOS
84 #endif
85