• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include <sstream>
16 #include <fstream>
17 #include <climits>
18 #include "include/sp_utils.h"
19 #include "include/RAM.h"
20 namespace OHOS {
21 namespace SmartPerf {
ItemData()22 std::map<std::string, std::string> RAM::ItemData()
23 {
24     std::map<std::string, std::string> result;
25     std::map<std::string, std::string> ramInfo = RAM::GetRamInfo();
26     result = ramInfo;
27     return result;
28 }
SetProcessId(std::string pid)29 void RAM::SetProcessId(std::string pid)
30 {
31     processId = std::move(pid);
32 }
33 
GetRamInfo() const34 std::map<std::string, std::string> RAM::GetRamInfo() const
35 {
36     std::map<std::string, std::string> ramInfo;
37     std::string pssValue = "";
38     ramInfo["pss"] = "-1";
39     if (processId.size() > 0) {
40         const int zero = 0;
41         const int one = 1;
42         const int two = 2;
43         std::ostringstream cmdGrep;
44         cmdGrep.str("");
45         cmdGrep << "/proc/" << processId << "/smaps_rollup";
46         std::string cmdRam = cmdGrep.str();
47         char realPath[PATH_MAX] = {0x00};
48         if (realpath(cmdRam.c_str(), realPath) == nullptr) {
49             std::cout << "" << std::endl;
50         }
51         std::ifstream infile(realPath, std::ios::in);
52         if (!infile) {
53             return ramInfo;
54         }
55         std::string textline;
56         while (getline(infile, textline, '\n')) {
57             if (textline[zero] == 'P' && textline[one] == 's' && textline[two] == 's') {
58                 pssValue = textline;
59                 break;
60             }
61         }
62     }
63     if (pssValue.size() > 0) {
64         ramInfo["pss"] = SPUtils::ExtractNumber(pssValue.c_str());
65     }
66     return ramInfo;
67 }
68 }
69 }
70