1f0bfeaa8Sopenharmony_ci/* 2f0bfeaa8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3f0bfeaa8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f0bfeaa8Sopenharmony_ci * you may not use this file except in compliance with the License. 5f0bfeaa8Sopenharmony_ci * You may obtain a copy of the License at 6f0bfeaa8Sopenharmony_ci * 7f0bfeaa8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f0bfeaa8Sopenharmony_ci * 9f0bfeaa8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f0bfeaa8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f0bfeaa8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f0bfeaa8Sopenharmony_ci * See the License for the specific language governing permissions and 13f0bfeaa8Sopenharmony_ci * limitations under the License. 14f0bfeaa8Sopenharmony_ci */ 15f0bfeaa8Sopenharmony_ci 16f0bfeaa8Sopenharmony_ci#include "meminfo.h" 17f0bfeaa8Sopenharmony_ci 18f0bfeaa8Sopenharmony_ci#include <fstream> 19f0bfeaa8Sopenharmony_ci#include <sstream> 20f0bfeaa8Sopenharmony_ci#include <v1_0/imemory_tracker_interface.h> 21f0bfeaa8Sopenharmony_ci 22f0bfeaa8Sopenharmony_ci#include "file_ex.h" // LoadStringFromFile 23f0bfeaa8Sopenharmony_ci#include "hilog/log.h" 24f0bfeaa8Sopenharmony_ci 25f0bfeaa8Sopenharmony_ci#undef LOG_TAG 26f0bfeaa8Sopenharmony_ci#define LOG_TAG "MemInfo" 27f0bfeaa8Sopenharmony_ci 28f0bfeaa8Sopenharmony_ci#undef LOG_DOMAIN 29f0bfeaa8Sopenharmony_ci#define LOG_DOMAIN 0xD001799 30f0bfeaa8Sopenharmony_ci 31f0bfeaa8Sopenharmony_ci 32f0bfeaa8Sopenharmony_cinamespace OHOS { 33f0bfeaa8Sopenharmony_cinamespace MemInfo { 34f0bfeaa8Sopenharmony_ciusing namespace OHOS::HDI::Memorytracker::V1_0; 35f0bfeaa8Sopenharmony_ciconstexpr int PAGE_TO_KB = 4; 36f0bfeaa8Sopenharmony_ciconstexpr int BYTE_PER_KB = 1024; 37f0bfeaa8Sopenharmony_ci 38f0bfeaa8Sopenharmony_ci// get Rss from statm 39f0bfeaa8Sopenharmony_ciuint64_t GetRssByPid(const int pid) 40f0bfeaa8Sopenharmony_ci{ 41f0bfeaa8Sopenharmony_ci uint64_t size = 0; 42f0bfeaa8Sopenharmony_ci std::string statm; 43f0bfeaa8Sopenharmony_ci std::string vss; 44f0bfeaa8Sopenharmony_ci std::string rss; 45f0bfeaa8Sopenharmony_ci 46f0bfeaa8Sopenharmony_ci std::string statmPath = "/proc/" + std::to_string(pid) + "/statm"; 47f0bfeaa8Sopenharmony_ci // format like: 48f0bfeaa8Sopenharmony_ci // 640 472 369 38 0 115 0 49f0bfeaa8Sopenharmony_ci if (!OHOS::LoadStringFromFile(statmPath, statm)) { 50f0bfeaa8Sopenharmony_ci HILOG_ERROR(LOG_CORE, "statm file error!"); 51f0bfeaa8Sopenharmony_ci return size; 52f0bfeaa8Sopenharmony_ci } 53f0bfeaa8Sopenharmony_ci std::istringstream isStatm(statm); 54f0bfeaa8Sopenharmony_ci isStatm >> vss >> rss; // pages 55f0bfeaa8Sopenharmony_ci 56f0bfeaa8Sopenharmony_ci size = static_cast<uint64_t>(atoi(rss.c_str()) * PAGE_TO_KB); 57f0bfeaa8Sopenharmony_ci return size; 58f0bfeaa8Sopenharmony_ci} 59f0bfeaa8Sopenharmony_ci 60f0bfeaa8Sopenharmony_ci// get Pss from smaps_rollup 61f0bfeaa8Sopenharmony_ciuint64_t GetPssByPid(const int pid) 62f0bfeaa8Sopenharmony_ci{ 63f0bfeaa8Sopenharmony_ci uint64_t size = 0; 64f0bfeaa8Sopenharmony_ci std::string filename = "/proc/" + std::to_string(pid) + "/smaps_rollup"; 65f0bfeaa8Sopenharmony_ci std::ifstream in(filename); 66f0bfeaa8Sopenharmony_ci if (!in) { 67f0bfeaa8Sopenharmony_ci HILOG_ERROR(LOG_CORE, "File %{public}s not found.\n", filename.c_str()); 68f0bfeaa8Sopenharmony_ci return size; 69f0bfeaa8Sopenharmony_ci } 70f0bfeaa8Sopenharmony_ci 71f0bfeaa8Sopenharmony_ci std::string content; 72f0bfeaa8Sopenharmony_ci while (in.good() && getline(in, content)) { 73f0bfeaa8Sopenharmony_ci std::string::size_type typePos = content.find(":"); 74f0bfeaa8Sopenharmony_ci if (typePos != content.npos) { 75f0bfeaa8Sopenharmony_ci std::string type = content.substr(0, typePos); 76f0bfeaa8Sopenharmony_ci if (type == "Pss") { 77f0bfeaa8Sopenharmony_ci std::string valueStr = content.substr(typePos + 1); 78f0bfeaa8Sopenharmony_ci const int base = 10; 79f0bfeaa8Sopenharmony_ci size = strtoull(valueStr.c_str(), nullptr, base); 80f0bfeaa8Sopenharmony_ci break; 81f0bfeaa8Sopenharmony_ci } 82f0bfeaa8Sopenharmony_ci } 83f0bfeaa8Sopenharmony_ci } 84f0bfeaa8Sopenharmony_ci in.close(); 85f0bfeaa8Sopenharmony_ci return size; 86f0bfeaa8Sopenharmony_ci} 87f0bfeaa8Sopenharmony_ci 88f0bfeaa8Sopenharmony_ci// get SwapPss from smaps_rollup 89f0bfeaa8Sopenharmony_ciuint64_t GetSwapPssByPid(const int pid) 90f0bfeaa8Sopenharmony_ci{ 91f0bfeaa8Sopenharmony_ci uint64_t size = 0; 92f0bfeaa8Sopenharmony_ci std::string filename = "/proc/" + std::to_string(pid) + "/smaps_rollup"; 93f0bfeaa8Sopenharmony_ci std::ifstream in(filename); 94f0bfeaa8Sopenharmony_ci if (!in) { 95f0bfeaa8Sopenharmony_ci HILOG_ERROR(LOG_CORE, "File %{public}s not found.\n", filename.c_str()); 96f0bfeaa8Sopenharmony_ci return size; 97f0bfeaa8Sopenharmony_ci } 98f0bfeaa8Sopenharmony_ci 99f0bfeaa8Sopenharmony_ci std::string content; 100f0bfeaa8Sopenharmony_ci while (in.good() && getline(in, content)) { 101f0bfeaa8Sopenharmony_ci std::string::size_type typePos = content.find(":"); 102f0bfeaa8Sopenharmony_ci if (typePos != content.npos) { 103f0bfeaa8Sopenharmony_ci std::string type = content.substr(0, typePos); 104f0bfeaa8Sopenharmony_ci if (type == "SwapPss") { 105f0bfeaa8Sopenharmony_ci std::string valueStr = content.substr(typePos + 1); 106f0bfeaa8Sopenharmony_ci const int base = 10; 107f0bfeaa8Sopenharmony_ci size = strtoull(valueStr.c_str(), nullptr, base); 108f0bfeaa8Sopenharmony_ci break; 109f0bfeaa8Sopenharmony_ci } 110f0bfeaa8Sopenharmony_ci } 111f0bfeaa8Sopenharmony_ci } 112f0bfeaa8Sopenharmony_ci in.close(); 113f0bfeaa8Sopenharmony_ci return size; 114f0bfeaa8Sopenharmony_ci} 115f0bfeaa8Sopenharmony_ci 116f0bfeaa8Sopenharmony_ci// get graphics memory from hdi 117f0bfeaa8Sopenharmony_cibool GetGraphicsMemory(const int pid, uint64_t &gl, uint64_t &graph) 118f0bfeaa8Sopenharmony_ci{ 119f0bfeaa8Sopenharmony_ci bool ret = false; 120f0bfeaa8Sopenharmony_ci sptr<IMemoryTrackerInterface> memtrack = IMemoryTrackerInterface::Get(true); 121f0bfeaa8Sopenharmony_ci if (memtrack == nullptr) { 122f0bfeaa8Sopenharmony_ci HILOG_ERROR(LOG_CORE, "memtrack service is null"); 123f0bfeaa8Sopenharmony_ci return ret; 124f0bfeaa8Sopenharmony_ci } 125f0bfeaa8Sopenharmony_ci const std::vector<std::pair<MemoryTrackerType, std::string>> MEMORY_TRACKER_TYPES = { 126f0bfeaa8Sopenharmony_ci {MEMORY_TRACKER_TYPE_GL, "GL"}, {MEMORY_TRACKER_TYPE_GRAPH, "Graph"}, 127f0bfeaa8Sopenharmony_ci {MEMORY_TRACKER_TYPE_OTHER, "Other"} 128f0bfeaa8Sopenharmony_ci }; 129f0bfeaa8Sopenharmony_ci 130f0bfeaa8Sopenharmony_ci for (const auto &memTrackerType : MEMORY_TRACKER_TYPES) { 131f0bfeaa8Sopenharmony_ci std::vector<MemoryRecord> records; 132f0bfeaa8Sopenharmony_ci if (memtrack->GetDevMem(pid, memTrackerType.first, records) != HDF_SUCCESS) { 133f0bfeaa8Sopenharmony_ci continue; 134f0bfeaa8Sopenharmony_ci } 135f0bfeaa8Sopenharmony_ci uint64_t value = 0; 136f0bfeaa8Sopenharmony_ci for (const auto &record : records) { 137f0bfeaa8Sopenharmony_ci if ((static_cast<uint32_t>(record.flags) & FLAG_UNMAPPED) == FLAG_UNMAPPED) { 138f0bfeaa8Sopenharmony_ci value = static_cast<uint64_t>(record.size / BYTE_PER_KB); 139f0bfeaa8Sopenharmony_ci break; 140f0bfeaa8Sopenharmony_ci } 141f0bfeaa8Sopenharmony_ci } 142f0bfeaa8Sopenharmony_ci if (memTrackerType.first == MEMORY_TRACKER_TYPE_GL) { 143f0bfeaa8Sopenharmony_ci gl = value; 144f0bfeaa8Sopenharmony_ci ret = true; 145f0bfeaa8Sopenharmony_ci } else if (memTrackerType.first == MEMORY_TRACKER_TYPE_GRAPH) { 146f0bfeaa8Sopenharmony_ci graph = value; 147f0bfeaa8Sopenharmony_ci ret = true; 148f0bfeaa8Sopenharmony_ci } 149f0bfeaa8Sopenharmony_ci } 150f0bfeaa8Sopenharmony_ci return ret; 151f0bfeaa8Sopenharmony_ci} 152f0bfeaa8Sopenharmony_ci} /* namespace MemInfo */ 153f0bfeaa8Sopenharmony_ci} /* namespace OHOS */ 154