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
GetCpuUsage(const int & pid)46 static float GetCpuUsage(const int &pid)
47 {
48 unique_ptr<DumpUsage> dumpUsage = make_unique<DumpUsage>();
49 return dumpUsage->GetCpuUsage(pid);
50 }
51
main(int argc,char * argv[])52 int main(int argc, char *argv[])
53 {
54 int pid;
55 cout << "Please input pid:";
56 cin >> pid;
57
58 uint64_t pss = GetPss(pid);
59 std::cout << "pss:" << pss << std::endl;
60
61 uint64_t privateDirty = GetPrivateDirty(pid);
62 std::cout << "privateDirty:" << privateDirty << std::endl;
63
64 uint64_t shareDirty = GetSharedDirty(pid);
65 std::cout << "shareDirty:" << shareDirty << std::endl;
66
67 float cpuUsage = GetCpuUsage(pid);
68 printf("cpuUsage:%.1f\n", cpuUsage);
69
70 OHOS::HiviewDFX::MemInfoData::MemInfo info;
71 bool success = GetMemInfo(pid, info);
72 std::cout << "GetMemInfo success:" << success << ",rss:" << info.rss << ",pss:" << info.pss
73 << ",sharedClean:" << info.sharedClean << ",sharedDirty:" << info.sharedDirty
74 << ",privateClean:" << info.privateClean << ",privateDirty:" << info.privateDirty << ",swap:" << info.swap
75 << ",swapPss:" << info.swapPss << std::endl;
76 }
77