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
16#include "meminfo.h"
17
18#include <fstream>
19#include <sstream>
20#include <v1_0/imemory_tracker_interface.h>
21
22#include "file_ex.h" // LoadStringFromFile
23#include "hilog/log.h"
24
25#undef LOG_TAG
26#define LOG_TAG "MemInfo"
27
28#undef LOG_DOMAIN
29#define LOG_DOMAIN 0xD001799
30
31
32namespace OHOS {
33namespace MemInfo {
34using namespace OHOS::HDI::Memorytracker::V1_0;
35constexpr int PAGE_TO_KB = 4;
36constexpr int BYTE_PER_KB = 1024;
37
38// get Rss from statm
39uint64_t GetRssByPid(const int pid)
40{
41    uint64_t size = 0;
42    std::string statm;
43    std::string vss;
44    std::string rss;
45
46    std::string statmPath = "/proc/" + std::to_string(pid) + "/statm";
47    // format like:
48    // 640 472 369 38 0 115 0
49    if (!OHOS::LoadStringFromFile(statmPath, statm)) {
50        HILOG_ERROR(LOG_CORE, "statm file error!");
51        return size;
52    }
53    std::istringstream isStatm(statm);
54    isStatm >> vss >> rss; // pages
55
56    size = static_cast<uint64_t>(atoi(rss.c_str()) * PAGE_TO_KB);
57    return size;
58}
59
60// get Pss from smaps_rollup
61uint64_t GetPssByPid(const int pid)
62{
63    uint64_t size = 0;
64    std::string filename = "/proc/" + std::to_string(pid) + "/smaps_rollup";
65    std::ifstream in(filename);
66    if (!in) {
67        HILOG_ERROR(LOG_CORE, "File %{public}s not found.\n", filename.c_str());
68        return size;
69    }
70
71    std::string content;
72    while (in.good() && getline(in, content)) {
73        std::string::size_type typePos = content.find(":");
74        if (typePos != content.npos) {
75            std::string type = content.substr(0, typePos);
76            if (type == "Pss") {
77                std::string valueStr = content.substr(typePos + 1);
78                const int base = 10;
79                size = strtoull(valueStr.c_str(), nullptr, base);
80                break;
81            }
82        }
83    }
84    in.close();
85    return size;
86}
87
88// get SwapPss from smaps_rollup
89uint64_t GetSwapPssByPid(const int pid)
90{
91    uint64_t size = 0;
92    std::string filename = "/proc/" + std::to_string(pid) + "/smaps_rollup";
93    std::ifstream in(filename);
94    if (!in) {
95        HILOG_ERROR(LOG_CORE, "File %{public}s not found.\n", filename.c_str());
96        return size;
97    }
98
99    std::string content;
100    while (in.good() && getline(in, content)) {
101        std::string::size_type typePos = content.find(":");
102        if (typePos != content.npos) {
103            std::string type = content.substr(0, typePos);
104            if (type == "SwapPss") {
105                std::string valueStr = content.substr(typePos + 1);
106                const int base = 10;
107                size = strtoull(valueStr.c_str(), nullptr, base);
108                break;
109            }
110        }
111    }
112    in.close();
113    return size;
114}
115
116// get graphics memory from hdi
117bool GetGraphicsMemory(const int pid, uint64_t &gl, uint64_t &graph)
118{
119    bool ret = false;
120    sptr<IMemoryTrackerInterface> memtrack = IMemoryTrackerInterface::Get(true);
121    if (memtrack == nullptr) {
122        HILOG_ERROR(LOG_CORE, "memtrack service is null");
123        return ret;
124    }
125    const std::vector<std::pair<MemoryTrackerType, std::string>> MEMORY_TRACKER_TYPES = {
126        {MEMORY_TRACKER_TYPE_GL, "GL"}, {MEMORY_TRACKER_TYPE_GRAPH, "Graph"},
127        {MEMORY_TRACKER_TYPE_OTHER, "Other"}
128    };
129
130    for (const auto &memTrackerType : MEMORY_TRACKER_TYPES) {
131        std::vector<MemoryRecord> records;
132        if (memtrack->GetDevMem(pid, memTrackerType.first, records) != HDF_SUCCESS) {
133            continue;
134        }
135        uint64_t value = 0;
136        for (const auto &record : records) {
137            if ((static_cast<uint32_t>(record.flags) & FLAG_UNMAPPED) == FLAG_UNMAPPED) {
138                value = static_cast<uint64_t>(record.size / BYTE_PER_KB);
139                break;
140            }
141        }
142        if (memTrackerType.first == MEMORY_TRACKER_TYPE_GL) {
143            gl = value;
144            ret = true;
145        } else if (memTrackerType.first == MEMORY_TRACKER_TYPE_GRAPH) {
146            graph = value;
147            ret = true;
148        }
149    }
150    return ret;
151}
152} /* namespace MemInfo */
153} /* namespace OHOS */
154