1 /*
2 * Copyright (c) 2025 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 "dump_process_helper.h"
17
18 #include "file_ex.h"
19 #include "string_ex.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace DumpProcessHelper {
24 constexpr int JS_ERROR_RSS_MEMINFO_TOKEN_NUM = 2;
25 constexpr int JS_ERROR_RSS_MEMINFO_DECIMAL_BASE = 10;
26 constexpr int JS_ERROR_RSS_MEMINFO_PAGE_SIZE_KB = 4096;
27 constexpr int JS_ERROR_RSS_MEMINFO_SIZE_KB = 1000;
28
GetProcRssMemInfo()29 uint64_t GetProcRssMemInfo()
30 {
31 std::string statmPath = "/proc/self/statm";
32 std::string readContent;
33
34 if (!LoadStringFromFile(statmPath, readContent)) {
35 return 0;
36 }
37
38 std::vector<std::string> tokens;
39 SplitStr(readContent, " ", tokens);
40 if (tokens.size() < JS_ERROR_RSS_MEMINFO_TOKEN_NUM) {
41 return 0;
42 }
43
44 std::string rssStr = tokens.at(1);
45 uint64_t rss = static_cast<uint64_t>(strtoull(rssStr.c_str(), nullptr, JS_ERROR_RSS_MEMINFO_DECIMAL_BASE));
46 if (rss == 0) {
47 return 0;
48 }
49
50 rss = (rss * JS_ERROR_RSS_MEMINFO_PAGE_SIZE_KB) / JS_ERROR_RSS_MEMINFO_SIZE_KB;
51 return rss;
52 }
53 } // namespace DumpProcessHelper
54 } // namespace AppExecFwk
55 } // namespace OHOS