1 /*
2 * Copyright (c) 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
16 #include "dump_usage.h"
17
18 #include <gtest/gtest.h>
19 #include <unistd.h>
20
21 using namespace testing::ext;
22 namespace OHOS {
23 namespace HiviewDFX {
24 static constexpr int MALLOC_SIZE = 1024;
25 class HiDumperInnerkitsTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 void SetUp();
30 void TearDown();
31 void StartTestProcess(int& pid);
32 void StopProcess(int processNum);
33 };
34
SetUpTestCase(void)35 void HiDumperInnerkitsTest::SetUpTestCase(void)
36 {
37 }
TearDownTestCase(void)38 void HiDumperInnerkitsTest::TearDownTestCase(void)
39 {
40 }
SetUp(void)41 void HiDumperInnerkitsTest::SetUp(void)
42 {
43 }
TearDown(void)44 void HiDumperInnerkitsTest::TearDown(void)
45 {
46 }
47
StartTestProcess(int & pid)48 void HiDumperInnerkitsTest::StartTestProcess(int& pid)
49 {
50 int processNum = fork();
51 if (processNum == 0) {
52 while (true) {
53 void* p = malloc(MALLOC_SIZE);
54 if (p == nullptr) {
55 const int bufSize = 256;
56 char buf[bufSize] = { 0 };
57 (void)strerror_r(errno, buf, bufSize);
58 printf("malloc failure, errno(%d:%s)", errno, buf);
59 return;
60 }
61 free(p);
62 }
63 } else {
64 pid = processNum;
65 }
66 }
67
StopProcess(int processNum)68 void HiDumperInnerkitsTest::StopProcess(int processNum)
69 {
70 std::string stopCmd = "kill -9 " + std::to_string(processNum);
71 system(stopCmd.c_str());
72 }
73
74 /**
75 * @tc.name: GetMemInfoTest001
76 * @tc.desc: Test GetMemInfo.
77 * @tc.type: FUNC
78 * @tc.require: issueI5NWZQ
79 */
80 HWTEST_F(HiDumperInnerkitsTest, GetMemInfoTest001, TestSize.Level1)
81 {
82 int pid = -1;
83 StartTestProcess(pid);
84 if (pid < 0) {
85 printf("fork process failure!");
86 return;
87 }
88 std::unique_ptr<DumpUsage> dumpUsage = std::make_unique<DumpUsage>();
89 MemInfoData::MemInfo info;
90 EXPECT_TRUE(dumpUsage->GetMemInfo(pid, info));
91 EXPECT_GT(info.pss, 0);
92 StopProcess(pid);
93 }
94
95 /**
96 * @tc.name: GetPssTest001
97 * @tc.desc: Test GetPss.
98 * @tc.type: FUNC
99 * @tc.require: issueI5NWZQ
100 */
101 HWTEST_F(HiDumperInnerkitsTest, GetPssTest001, TestSize.Level1)
102 {
103 int pid = -1;
104 StartTestProcess(pid);
105 if (pid < 0) {
106 printf("fork process failure!");
107 return;
108 }
109 std::unique_ptr<DumpUsage> dumpUsage = std::make_unique<DumpUsage>();
110 EXPECT_GT(dumpUsage->GetPss(pid), 0);
111 StopProcess(pid);
112 }
113
114 /**
115 * @tc.name: GetPrivateDirtyTest001
116 * @tc.desc: Test GetPrivateDirty.
117 * @tc.type: FUNC
118 * @tc.require: issueI5NWZQ
119 */
120 HWTEST_F(HiDumperInnerkitsTest, GetPrivateDirtyTest001, TestSize.Level1)
121 {
122 int pid = getpid();
123 std::unique_ptr<DumpUsage> dumpUsage = std::make_unique<DumpUsage>();
124 EXPECT_GE(dumpUsage->GetPrivateDirty(pid), 0);
125 }
126
127 /**
128 * @tc.name: GetSharedDirtyTest001
129 * @tc.desc: Test GetSharedDirty.
130 * @tc.type: FUNC
131 * @tc.require: issueI5NWZQ
132 */
133 HWTEST_F(HiDumperInnerkitsTest, GetSharedDirtyTest001, TestSize.Level1)
134 {
135 int pid = getpid();
136 std::unique_ptr<DumpUsage> dumpUsage = std::make_unique<DumpUsage>();
137 EXPECT_GE(dumpUsage->GetSharedDirty(pid), 0);
138 }
139
140 /**
141 * @tc.name: GetCpuUsage001
142 * @tc.desc: Test GetCpuUsage.
143 * @tc.type: FUNC
144 * @tc.require: issueI5NWZQ
145 */
146 HWTEST_F(HiDumperInnerkitsTest, GetCpuUsage001, TestSize.Level1)
147 {
148 int pid = -1;
149 StartTestProcess(pid);
150 if (pid < 0) {
151 printf("fork process failure!");
152 return;
153 }
154 sleep(1);
155 std::unique_ptr<DumpUsage> dumpUsage = std::make_unique<DumpUsage>();
156 EXPECT_GT(dumpUsage->GetCpuUsage(pid), 0);
157 StopProcess(pid);
158 }
159 } // namespace HiviewDFX
160 } // namespace OHOS
161