diff --git a/libmeminfo/include/meminfo.h b/libmeminfo/include/meminfo.h index 34aeb38218ed290c945e0cbdbff9c01363ce0dab..623258aeea9931951705c733d7b31d1465ef808f 100644 --- a/libmeminfo/include/meminfo.h +++ b/libmeminfo/include/meminfo.h @@ -21,12 +21,15 @@ namespace OHOS { namespace MemInfo { -// get rss refer to memmgr +// get Rss from statm uint64_t GetRssByPid(const int pid); -// get pss from smaps_rollup, include graphics memory +// get Pss from smaps_rollup uint64_t GetPssByPid(const int pid); +// get SwapPss from smaps_rollup +uint64_t GetSwapPssByPid(const int pid); + // get graphics memory from hdi bool GetGraphicsMemory(const int pid, uint64_t &gl, uint64_t &graph); } /* namespace MemInfo */ diff --git a/libmeminfo/src/meminfo.cpp b/libmeminfo/src/meminfo.cpp index 7bb2408953d0bce6e72e43daeec8631c744f3ea0..09b2b98359c32fce7b817dd5c8f2670d667837a8 100644 --- a/libmeminfo/src/meminfo.cpp +++ b/libmeminfo/src/meminfo.cpp @@ -29,31 +29,13 @@ constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001799, "MemInfo" }; constexpr int PAGE_TO_KB = 4; constexpr int BYTE_PER_KB = 1024; -// get rss refer to memmgr +// get Rss from statm uint64_t GetRssByPid(const int pid) { uint64_t size = 0; - std::string stat; std::string statm; - std::string statPid; std::string vss; std::string rss; - std::string name; - std::string status; - std::string statPath = "/proc/" + std::to_string(pid) + "/stat"; - // format like: - // 1 (init) S 0 0 0 0 -1 4210944 1 ... - if (!OHOS::LoadStringFromFile(statPath, stat)) { - HiviewDFX::HiLog::Error(LABEL, "stat file error!"); - return size; - } - std::istringstream isStat(stat); - isStat >> statPid >> name >> status; - - if (statPid != std::to_string(pid)) { - HiviewDFX::HiLog::Error(LABEL, "pid error!"); - return size; - } std::string statmPath = "/proc/" + std::to_string(pid) + "/statm"; // format like: @@ -69,7 +51,7 @@ uint64_t GetRssByPid(const int pid) return size; } -// get Pss and SwapPss from smaps_rollup, include graphics memory +// get Pss from smaps_rollup uint64_t GetPssByPid(const int pid) { uint64_t size = 0; @@ -85,22 +67,43 @@ uint64_t GetPssByPid(const int pid) std::string::size_type typePos = content.find(":"); if (typePos != content.npos) { std::string type = content.substr(0, typePos); - if (type == "Pss" || type == "SwapPss") { + if (type == "Pss") { std::string valueStr = content.substr(typePos + 1); const int base = 10; - size += strtoull(valueStr.c_str(), nullptr, base); + size = strtoull(valueStr.c_str(), nullptr, base); + break; } } } in.close(); + return size; +} - uint64_t gl = 0; - uint64_t graph = 0; - bool ret = GetGraphicsMemory(pid, gl, graph); - if (ret) { - size += gl; - size += graph; +// get SwapPss from smaps_rollup +uint64_t GetSwapPssByPid(const int pid) +{ + uint64_t size = 0; + std::string filename = "/proc/" + std::to_string(pid) + "/smaps_rollup"; + std::ifstream in(filename); + if (!in) { + HiviewDFX::HiLog::Error(LABEL, "File %{public}s not found.\n", filename.c_str()); + return size; + } + + std::string content; + while (in.good() && getline(in, content)) { + std::string::size_type typePos = content.find(":"); + if (typePos != content.npos) { + std::string type = content.substr(0, typePos); + if (type == "SwapPss") { + std::string valueStr = content.substr(typePos + 1); + const int base = 10; + size = strtoull(valueStr.c_str(), nullptr, base); + break; + } + } } + in.close(); return size; } diff --git a/libmeminfo/test/unittest/meminfo_test.cpp b/libmeminfo/test/unittest/meminfo_test.cpp index 379efe3102f2c9edb206393c6d73dc407418de9e..a7d32fa483693e0a913b79b91363c2a0982ac9b0 100644 --- a/libmeminfo/test/unittest/meminfo_test.cpp +++ b/libmeminfo/test/unittest/meminfo_test.cpp @@ -71,7 +71,6 @@ HWTEST_F(MemInfoTest, GetPssByPid_Test_001, TestSize.Level1) uint64_t size = 0; size = GetPssByPid(pid); std::cout << "size = " << size << std::endl; - system("hidumper --mem 1"); system("cat /proc/1/smaps_rollup"); ASSERT_EQ(size > 0, true); } @@ -83,5 +82,32 @@ HWTEST_F(MemInfoTest, GetPssByPid_Test_002, TestSize.Level1) size = GetPssByPid(pid); ASSERT_EQ(size == 0, true); } + +HWTEST_F(MemInfoTest, GetSwapPssByPid_Test_001, TestSize.Level1) +{ + int pid = 1; + uint64_t size = 0; + size = GetSwapPssByPid(pid); + std::cout << "size = " << size << std::endl; + system("cat /proc/1/smaps_rollup"); + ASSERT_EQ(size >= 0, true); +} + +HWTEST_F(MemInfoTest, GetSwapPssByPid_Test_002, TestSize.Level1) +{ + int pid = -1; + uint64_t size = 0; + size = GetSwapPssByPid(pid); + ASSERT_EQ(size == 0, true); +} + +HWTEST_F(MemInfoTest, GetGraphicsMemory_Test, TestSize.Level1) +{ + int pid = 1; + uint64_t gl = 0; + uint64_t graph = 0; + GetGraphicsMemory(pid, gl, graph); + ASSERT_EQ(gl == 0, true); +} } }