• 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 "system_environment_information.h"
16 
17 #include <cstdio>
18 #include <cstdlib>
19 #include <iostream>
20 #include <map>
21 #include <memory>
22 #include <regex>
23 #include <string>
24 
25 #include "hilog_wrapper.h"
26 #include "kernel_system_memory_info.h"
27 #include "securec.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 namespace SystemEnv {
32 namespace {
33 static const int BYTES_KB = 1024;
34 }
Init(std::map<std::string,std::string> & memInfo)35 void KernelSystemMemoryInfo::Init(std::map<std::string, std::string> &memInfo)
36 {
37     auto findData = [&] (const std::string& key) -> std::string {
38         auto iter = memInfo.find(key);
39         if (iter != memInfo.end()) {
40             HILOG_DEBUG("key[%{public}s] data[%{public}s]", key.c_str(), iter->second.c_str());
41             return iter->second;
42         } else {
43             HILOG_ERROR("key[%{public}s]", key.c_str());
44             return std::string("");
45         }
46     };
47 
48     memTotal_ = std::stoll(findData(std::string("MemTotal"))) * BYTES_KB;
49     memFree_ = std::stoll(findData(std::string("MemFree"))) * BYTES_KB;
50     memAvailable_ = std::stoll(findData(std::string("MemAvailable"))) * BYTES_KB;
51     buffers_ = std::stoll(findData(std::string("Buffers"))) * BYTES_KB;
52     cached_ = std::stoll(findData(std::string("Cached"))) * BYTES_KB;
53     swapCached_ = std::stoll(findData(std::string("SwapCached"))) * BYTES_KB;
54 }
55 
GetMemTotal() const56 int64_t KernelSystemMemoryInfo::GetMemTotal() const
57 {
58     return memTotal_;
59 }
60 
GetMemFree() const61 int64_t KernelSystemMemoryInfo::GetMemFree() const
62 {
63     return memFree_;
64 }
65 
GetMemAvailable() const66 int64_t KernelSystemMemoryInfo::GetMemAvailable() const
67 {
68     return memAvailable_;
69 }
70 
GetBuffers() const71 int64_t KernelSystemMemoryInfo::GetBuffers() const
72 {
73     return buffers_;
74 }
75 
GetCached() const76 int64_t KernelSystemMemoryInfo::GetCached() const
77 {
78     return cached_;
79 }
80 
GetSwapCached() const81 int64_t KernelSystemMemoryInfo::GetSwapCached() const
82 {
83     return swapCached_;
84 }
85 
RequestSystemMemoryInfo(std::map<std::string,std::string> & memInfo)86 static void RequestSystemMemoryInfo(std::map<std::string, std::string> &memInfo)
87 {
88     std::regex rLabel("[\\w()]+");
89     std::regex rData("\\d+");
90     const int buffsize = 1024;
91     char buff[buffsize] = {0};
92 
93     FILE *fp = popen("cat /proc/meminfo", "r");
94     if (fp == nullptr) {
95         HILOG_ERROR("open meminfo failed");
96         return;
97     }
98 
99     while (fgets(buff, sizeof(buff), fp) != nullptr) {
100         std::string strbuf(buff);
101         (void)memset_s(buff, sizeof(buff), 0x00, sizeof(buff));
102         std::smatch sm;
103         std::smatch smData;
104         bool flag = false;
105         flag = std::regex_search(strbuf, sm, rLabel);
106         if (!flag) {
107             HILOG_ERROR("open meminfo failed");
108             continue;
109         }
110         std::string strLabel = sm[0];
111         strbuf = sm.suffix().str();
112         flag = std::regex_search(strbuf, sm, rData);
113         if (!flag) {
114             HILOG_ERROR("open meminfo failed");
115             continue;
116         }
117         std::string strData = sm[0];
118         memInfo[strLabel] = strData;
119     }
120 
121     pclose(fp);
122     fp = nullptr;
123 }
124 
GetMemInfo(KernelSystemMemoryInfo & memInfo)125 void GetMemInfo(KernelSystemMemoryInfo &memInfo)
126 {
127     std::map<std::string, std::string> memListInfo;
128     RequestSystemMemoryInfo(memListInfo);
129     memInfo.Init(memListInfo);
130 }
131 }  // namespace SystemEnv
132 }  // namespace AppExecFwk
133 }  // namespace OHOS
134