• 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 #include "include/dump_usage.h"
16 
17 #include "executor/memory/parse/parse_smaps_rollup_info.h"
18 #include "util/dump_cpu_info_util.h"
19 
20 using namespace std;
21 namespace OHOS {
22 namespace HiviewDFX {
DumpUsage()23 DumpUsage::DumpUsage()
24 {
25 }
26 
~DumpUsage()27 DumpUsage::~DumpUsage()
28 {
29 }
30 
InitMemInfo(MemInfoData::MemInfo & memInfo)31 void DumpUsage::InitMemInfo(MemInfoData::MemInfo &memInfo)
32 {
33     memInfo.rss = 0;
34     memInfo.pss = 0;
35     memInfo.sharedClean = 0;
36     memInfo.sharedDirty = 0;
37     memInfo.privateClean = 0;
38     memInfo.privateDirty = 0;
39     memInfo.swap = 0;
40     memInfo.swapPss = 0;
41 }
42 
GetMemInfo(const int & pid,MemInfoData::MemInfo & info)43 bool DumpUsage::GetMemInfo(const int &pid, MemInfoData::MemInfo &info)
44 {
45     InitMemInfo(info);
46     unique_ptr<ParseSmapsRollupInfo> parseSmapsRollupInfo = make_unique<ParseSmapsRollupInfo>();
47     return parseSmapsRollupInfo->GetMemInfo(pid, info);
48 }
49 
GetPss(const int & pid)50 uint64_t DumpUsage::GetPss(const int &pid)
51 {
52     MemInfoData::MemInfo info;
53     return GetMemInfo(pid, info) ? info.pss : 0;
54 }
55 
GetPrivateDirty(const int & pid)56 uint64_t DumpUsage::GetPrivateDirty(const int &pid)
57 {
58     MemInfoData::MemInfo info;
59     return GetMemInfo(pid, info) ? info.privateDirty : 0;
60 }
61 
GetSharedDirty(const int & pid)62 uint64_t DumpUsage::GetSharedDirty(const int &pid)
63 {
64     MemInfoData::MemInfo info;
65     return GetMemInfo(pid, info) ? info.sharedDirty : 0;
66 }
67 
GetCpuUsage(const int & pid)68 float DumpUsage::GetCpuUsage(const int &pid)
69 {
70     float ret = DumpCpuInfoUtil::GetInstance().GetCpuUsage(pid);
71     return ret < 0 ? 0 : ret;
72 }
73 } // namespace HiviewDFX
74 } // namespace OHOS