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