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 MEMORY_UTIL_H 16 #define MEMORY_UTIL_H 17 #include <map> 18 #include <memory> 19 #include <string> 20 #include <vector> 21 #include "singleton.h" 22 #include "executor/memory/parse/meminfo_data.h" 23 namespace OHOS { 24 namespace HiviewDFX { 25 class MemoryUtil : public Singleton<MemoryUtil> { 26 public: 27 MemoryUtil(); 28 ~MemoryUtil(); 29 30 MemoryUtil(MemoryUtil const &) = delete; 31 void operator=(MemoryUtil const &) = delete; 32 33 using ValueMap = std::map<std::string, uint64_t>; 34 using GroupMap = std::map<std::string, ValueMap>; 35 36 std::string KB_UNIT_ = " kB"; 37 uint64_t BYTE_TO_KB_ = 1024; 38 const int LINE_WIDTH_ = 14; 39 const int SMAPS_LINE_WIDTH_ = 12; 40 const char SEPARATOR_ = '-'; 41 const char BLANK_ = ' '; 42 43 void CalcGroup(const std::string &group, const std::string &type, const uint64_t &value, GroupMap &infos); 44 bool RunCMD(const std::string &cmd, std::vector<std::string> &result); 45 size_t GetMaxThreadNum(const size_t &threadNum); 46 bool IsNameLine(const std::string &str, std::string &name, uint64_t &iNode); 47 bool GetTypeValue(const std::string &str, const std::vector<std::string> &tag, std::string &type, uint64_t &value); 48 void InitMemInfo(MemInfoData::MemInfo &memInfo); 49 void InitMemSmapsInfo(MemInfoData::MemSmapsInfo &memInfo); 50 void InitMemUsage(MemInfoData::MemUsage &usage); 51 void InitGraphicsMemory(MemInfoData::GraphicsMemory &graphicsMemory); 52 bool GetTypeAndValue(const std::string &str, std::string &type, uint64_t &value); 53 void SetMemTotalValue(const std::string &value, std::vector<std::string> &lines, std::vector<std::string> &values, 54 bool flag = false); 55 std::string PermToString(const uint64_t iPerm); 56 uint64_t PermToInt(const std::string& perm); 57 58 private: 59 }; 60 } // namespace HiviewDFX 61 } // namespace OHOS 62 #endif 63