• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "executor/memory/get_cma_info.h"
17 
18 #include <cstdlib>
19 
20 #include "executor/cmd_dumper.h"
21 #include "executor/memory/memory_util.h"
22 #include "util/string_utils.h"
23 #include "hilog_wrapper.h"
24 
25 using namespace std;
26 namespace OHOS {
27 namespace HiviewDFX {
GetCMAInfo()28 GetCMAInfo::GetCMAInfo()
29 {
30 }
31 
~GetCMAInfo()32 GetCMAInfo::~GetCMAInfo()
33 {
34 }
35 
GetUsed(uint64_t & value)36 bool GetCMAInfo::GetUsed(uint64_t &value)
37 {
38     string cmd = "cat /sys/kernel/debug/cma/*/used";
39     vector<string> usedInfos;
40     if (MemoryUtil::GetInstance().RunCMD(cmd, usedInfos)) {
41         const int base = 10;
42         for (size_t i = 0; i < usedInfos.size(); i++) {
43             string used = usedInfos.at(i);
44             if (StringUtils::GetInstance().IsNum(used)) {
45                 value += strtoull(usedInfos.at(i).c_str(), nullptr, base);
46             } else {
47                 DUMPER_HILOGE(MODULE_SERVICE, "CMA Get Used Data error\n");
48                 return false;
49             }
50         }
51     } else {
52         DUMPER_HILOGE(MODULE_SERVICE, "Get CMA used by run cmd failed\n");
53     }
54     return true;
55 }
56 } // namespace HiviewDFX
57 } // namespace OHOS
58