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
21using namespace std;
22namespace OHOS {
23namespace HiviewDFX {
24DumpUsage::DumpUsage()
25{
26}
27
28DumpUsage::~DumpUsage()
29{
30}
31
32bool 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
39uint64_t DumpUsage::GetPss(const int &pid)
40{
41    MemInfoData::MemInfo info;
42    return GetMemInfo(pid, info) ? info.pss + info.swapPss : 0;
43}
44
45uint64_t DumpUsage::GetPrivateDirty(const int &pid)
46{
47    MemInfoData::MemInfo info;
48    return GetMemInfo(pid, info) ? info.privateDirty : 0;
49}
50
51uint64_t DumpUsage::GetSharedDirty(const int &pid)
52{
53    MemInfoData::MemInfo info;
54    return GetMemInfo(pid, info) ? info.sharedDirty : 0;
55}
56
57double DumpUsage::GetCpuUsage(const int &pid)
58{
59    double cpuUsage = 0.00;
60#ifdef HIDUMPER_HIVIEWDFX_HIVIEW_ENABLE
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#endif
65    return cpuUsage;
66}
67} // namespace HiviewDFX
68} // namespace OHOS