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