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 #include "dump_manager_cpu_client.h"
17 #include "executor/memory/parse/parse_smaps_rollup_info.h"
18 #include "executor/memory/memory_util.h"
19 #include "executor/memory/dma_info.h"
20 #include "hilog_wrapper.h"
21
22 using namespace std;
23 namespace OHOS {
24 namespace HiviewDFX {
DumpUsage()25 DumpUsage::DumpUsage()
26 {
27 }
28
~DumpUsage()29 DumpUsage::~DumpUsage()
30 {
31 }
32
GetMemInfo(const int & pid,MemInfoData::MemInfo & info)33 bool DumpUsage::GetMemInfo(const int &pid, MemInfoData::MemInfo &info)
34 {
35 MemoryUtil::GetInstance().InitMemInfo(info);
36 unique_ptr<ParseSmapsRollupInfo> parseSmapsRollupInfo = make_unique<ParseSmapsRollupInfo>();
37 return parseSmapsRollupInfo->GetMemInfo(pid, info);
38 }
39
GetPss(const int & pid)40 uint64_t DumpUsage::GetPss(const int &pid)
41 {
42 MemInfoData::MemInfo info;
43 return GetMemInfo(pid, info) ? info.pss + info.swapPss : 0;
44 }
45
GetPrivateDirty(const int & pid)46 uint64_t DumpUsage::GetPrivateDirty(const int &pid)
47 {
48 MemInfoData::MemInfo info;
49 return GetMemInfo(pid, info) ? info.privateDirty : 0;
50 }
51
GetSharedDirty(const int & pid)52 uint64_t DumpUsage::GetSharedDirty(const int &pid)
53 {
54 MemInfoData::MemInfo info;
55 return GetMemInfo(pid, info) ? info.sharedDirty : 0;
56 }
57
GetCpuUsage(const int & pid)58 double DumpUsage::GetCpuUsage(const int &pid)
59 {
60 double cpuUsage = 0.00;
61 auto& dumpManagerCpuClient = DumpManagerCpuClient::GetInstance();
62 dumpManagerCpuClient.GetCpuUsageByPid(pid, cpuUsage);
63 DUMPER_HILOGD(MODULE_CPU_SERVICE, "GetCpuUsage end, pid = %{public}d, cpuUsage = %{public}f", pid, cpuUsage);
64 return cpuUsage;
65 }
66
GetDma(const int & pid)67 uint64_t DumpUsage::GetDma(const int& pid)
68 {
69 DmaInfo dmaInfo;
70 return dmaInfo.ParseDmaInfo() ? dmaInfo.GetDmaByPid(pid) : 0;
71 }
72 } // namespace HiviewDFX
73 } // namespace OHOS