1 /*
2 * Copyright (C) 2021 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 <memory>
16 #include <iostream>
17 #include "dump_usage.h"
18
19 using namespace std;
20 using namespace OHOS::HiviewDFX;
21
GetPss(const int & pid)22 static uint64_t GetPss(const int &pid)
23 {
24 unique_ptr<DumpUsage> dumpUsage = make_unique<DumpUsage>();
25 return dumpUsage->GetPss(pid);
26 }
27
GetPrivateDirty(const int & pid)28 static uint64_t GetPrivateDirty(const int &pid)
29 {
30 unique_ptr<DumpUsage> dumpUsage = make_unique<DumpUsage>();
31 return dumpUsage->GetPrivateDirty(pid);
32 }
33
GetSharedDirty(const int & pid)34 static uint64_t GetSharedDirty(const int &pid)
35 {
36 unique_ptr<DumpUsage> dumpUsage = make_unique<DumpUsage>();
37 return dumpUsage->GetSharedDirty(pid);
38 }
39
GetMemInfo(const int & pid,OHOS::HiviewDFX::MemInfoData::MemInfo & data)40 static bool GetMemInfo(const int &pid, OHOS::HiviewDFX::MemInfoData::MemInfo &data)
41 {
42 unique_ptr<DumpUsage> dumpUsage = make_unique<DumpUsage>();
43 return dumpUsage->GetMemInfo(pid, data);
44 }
45
46 #ifdef HIDUMPER_HIVIEWDFX_HIVIEW_ENABLE
GetCpuUsage(const int & pid)47 static float GetCpuUsage(const int &pid)
48 {
49 unique_ptr<DumpUsage> dumpUsage = make_unique<DumpUsage>();
50 return dumpUsage->GetCpuUsage(pid);
51 }
52 #endif
53
main(int argc,char * argv[])54 int main(int argc, char *argv[])
55 {
56 int pid;
57 cout << "Please input pid:";
58 cin >> pid;
59
60 uint64_t pss = GetPss(pid);
61 std::cout << "pss:" << pss << std::endl;
62
63 uint64_t privateDirty = GetPrivateDirty(pid);
64 std::cout << "privateDirty:" << privateDirty << std::endl;
65
66 uint64_t shareDirty = GetSharedDirty(pid);
67 std::cout << "shareDirty:" << shareDirty << std::endl;
68
69 #ifdef HIDUMPER_HIVIEWDFX_HIVIEW_ENABLE
70 float cpuUsage = GetCpuUsage(pid);
71 printf("cpuUsage:%.1f\n", cpuUsage);
72 #endif
73
74 OHOS::HiviewDFX::MemInfoData::MemInfo info;
75 bool success = GetMemInfo(pid, info);
76 std::cout << "GetMemInfo success:" << success << ",rss:" << info.rss << ",pss:" << info.pss
77 << ",sharedClean:" << info.sharedClean << ",sharedDirty:" << info.sharedDirty
78 << ",privateClean:" << info.privateClean << ",privateDirty:" << info.privateDirty << ",swap:" << info.swap
79 << ",swapPss:" << info.swapPss << std::endl;
80 }
81