• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 <climits>
16 #include <iostream>
17 #include <unistd.h>
18 
19 #include "cpu_collector.h"
20 
21 #include <gtest/gtest.h>
22 
23 using namespace testing::ext;
24 using namespace OHOS::HiviewDFX;
25 using namespace OHOS::HiviewDFX::UCollectUtil;
26 using namespace OHOS::HiviewDFX::UCollect;
27 
28 class CpuCollectorTest : public testing::Test {
29 public:
SetUp()30     void SetUp() {};
TearDown()31     void TearDown() {};
SetUpTestCase()32     static void SetUpTestCase() {};
TearDownTestCase()33     static void TearDownTestCase() {};
34 };
35 
36 #ifdef UNIFIED_COLLECTOR_CPU_ENABLE
37 /**
38  * @tc.name: CpuCollectorTest001
39  * @tc.desc: used to test CpuCollector.CollectSysCpuLoad
40  * @tc.type: FUNC
41 */
42 HWTEST_F(CpuCollectorTest, CpuCollectorTest001, TestSize.Level1)
43 {
44     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
45     CollectResult<SysCpuLoad> result = collector->CollectSysCpuLoad();
46     std::cout << "collect system cpu load result=" << result.retCode << std::endl;
47     ASSERT_TRUE(result.retCode == UcError::SUCCESS);
48 
49     const SysCpuLoad& sysCpuLoad = result.data;
50     std::cout << "collect system cpu load, avgLoad1=" << sysCpuLoad.avgLoad1 << std::endl;
51     std::cout << "collect system cpu load, avgLoad5=" << sysCpuLoad.avgLoad5 << std::endl;
52     std::cout << "collect system cpu load, avgLoad15=" << sysCpuLoad.avgLoad15 << std::endl;
53     ASSERT_TRUE(sysCpuLoad.avgLoad1 > 0);
54     ASSERT_TRUE(sysCpuLoad.avgLoad5 > 0);
55     ASSERT_TRUE(sysCpuLoad.avgLoad15 > 0);
56 }
57 
58 /**
59  * @tc.name: CpuCollectorTest002
60  * @tc.desc: used to test CpuCollector.CollectSysCpuUsage with updating
61  * @tc.type: FUNC
62 */
63 HWTEST_F(CpuCollectorTest, CpuCollectorTest002, TestSize.Level1)
64 {
65     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
66 
67     // first collection
68     sleep(1); // 1s
69     CollectResult<SysCpuUsage> result = collector->CollectSysCpuUsage(true);
70     std::cout << "collect1 system cpu usage result=" << result.retCode << std::endl;
71     ASSERT_TRUE(result.retCode == UcError::SUCCESS);
72 
73     const SysCpuUsage& sysCpuUsage = result.data;
74     ASSERT_GT(sysCpuUsage.startTime, 0);
75     ASSERT_GT(sysCpuUsage.endTime, sysCpuUsage.startTime);
76     ASSERT_GT(sysCpuUsage.cpuInfos.size(), 0);
77     for (const auto& cpuInfo : sysCpuUsage.cpuInfos) {
78         std::cout << cpuInfo.cpuId << ", userUsage=" << cpuInfo.userUsage
79             << ", niceUsage=" << cpuInfo.niceUsage
80             << ", systemUsage=" << cpuInfo.systemUsage
81             << ", idleUsage=" << cpuInfo.idleUsage
82             << ", ioWaitUsage=" << cpuInfo.ioWaitUsage
83             << ", irqUsage=" << cpuInfo.irqUsage
84             << ", softIrqUsage=" << cpuInfo.softIrqUsage
85             << std::endl;
86         ASSERT_FALSE(cpuInfo.cpuId.empty());
87     }
88 
89     // second collection
90     sleep(1); // 1s
91     CollectResult<SysCpuUsage> nextResult = collector->CollectSysCpuUsage(true);
92     std::cout << "collect2 system cpu usage result=" << nextResult.retCode << std::endl;
93     ASSERT_TRUE(nextResult.retCode == UcError::SUCCESS);
94     ASSERT_EQ(nextResult.data.startTime, sysCpuUsage.endTime);
95     ASSERT_GT(nextResult.data.endTime, nextResult.data.startTime);
96     ASSERT_EQ(nextResult.data.cpuInfos.size(), sysCpuUsage.cpuInfos.size());
97 }
98 
99 /**
100  * @tc.name: CpuCollectorTest003
101  * @tc.desc: used to test CpuCollector.CollectSysCpuUsage without updating
102  * @tc.type: FUNC
103 */
104 HWTEST_F(CpuCollectorTest, CpuCollectorTest003, TestSize.Level1)
105 {
106     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
107 
108     // first collection
109     sleep(1); // 1s
110     CollectResult<SysCpuUsage> result = collector->CollectSysCpuUsage(false);
111     std::cout << "collect1 system cpu usage result=" << result.retCode << std::endl;
112     ASSERT_TRUE(result.retCode == UcError::SUCCESS);
113 
114     // second collection
115     sleep(1); // 1s
116     CollectResult<SysCpuUsage> nextResult = collector->CollectSysCpuUsage(false);
117     std::cout << "collect2 system cpu usage result=" << nextResult.retCode << std::endl;
118     ASSERT_TRUE(nextResult.retCode == UcError::SUCCESS);
119     ASSERT_EQ(nextResult.data.startTime, result.data.startTime);
120     ASSERT_GT(nextResult.data.endTime, result.data.endTime);
121     ASSERT_EQ(nextResult.data.cpuInfos.size(), result.data.cpuInfos.size());
122 }
123 
124 /**
125  * @tc.name: CpuCollectorTest004
126  * @tc.desc: used to test CpuCollector.CollectCpuFrequency
127  * @tc.type: FUNC
128 */
129 HWTEST_F(CpuCollectorTest, CpuCollectorTest004, TestSize.Level1)
130 {
131     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
132     CollectResult<std::vector<CpuFreq>> result = collector->CollectCpuFrequency();
133     std::cout << "collect system cpu frequency result=" << result.retCode << std::endl;
134     ASSERT_TRUE(result.retCode == UcError::SUCCESS);
135 
136     const std::vector<CpuFreq>& cpuFreqs = result.data;
137     std::cout << "collect system cpu frequency, size=" << cpuFreqs.size() << std::endl;
138     ASSERT_GT(cpuFreqs.size(), 0);
139     for (size_t i = 0; i < cpuFreqs.size(); ++i) {
140         std::cout << "cpu" << cpuFreqs[i].cpuId << ", curFreq=" << cpuFreqs[i].curFreq << ", minFreq="
141             << cpuFreqs[i].minFreq << ", maxFreq=" << cpuFreqs[i].maxFreq << std::endl;
142         ASSERT_EQ(cpuFreqs[i].cpuId, i);
143         ASSERT_GT(cpuFreqs[i].curFreq, 0);
144         ASSERT_GT(cpuFreqs[i].minFreq, 0);
145         ASSERT_GT(cpuFreqs[i].maxFreq, 0);
146     }
147 }
148 
149 /**
150  * @tc.name: CpuCollectorTest009
151  * @tc.desc: used to test CpuCollector.CollectProcessCpuStatInfo
152  * @tc.type: FUNC
153 */
154 HWTEST_F(CpuCollectorTest, CpuCollectorTest009, TestSize.Level1)
155 {
156     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
157     constexpr int initPid = 1;
158     auto collectResult = collector->CollectProcessCpuStatInfo(initPid);
159     ASSERT_TRUE(collectResult.retCode == UcError::SUCCESS);
160 
161     std::cout << "proc.procName=" << collectResult.data.procName << std::endl;
162     std::cout << "proc.startTime=" << collectResult.data.startTime << std::endl;
163     std::cout << "proc.endTime=" << collectResult.data.endTime << std::endl;
164     std::cout << "proc.pid=" << collectResult.data.pid << std::endl;
165     std::cout << "proc.minFlt=" << collectResult.data.minFlt << std::endl;
166     std::cout << "proc.majFlt=" << collectResult.data.majFlt << std::endl;
167     std::cout << "proc.cpuLoad=" << collectResult.data.cpuLoad << std::endl;
168     std::cout << "proc.uCpuUsage=" << collectResult.data.uCpuUsage << std::endl;
169     std::cout << "proc.sCpuUsage=" << collectResult.data.sCpuUsage << std::endl;
170     std::cout << "proc.cpuUsage=" << collectResult.data.cpuUsage << std::endl;
171     ASSERT_GT(collectResult.data.startTime, 0);
172     ASSERT_GT(collectResult.data.endTime, 0);
173     ASSERT_EQ(collectResult.data.pid, initPid);
174     ASSERT_GE(collectResult.data.minFlt, 0);
175     ASSERT_GE(collectResult.data.majFlt, 0);
176     ASSERT_GE(collectResult.data.cpuLoad, 0);
177     ASSERT_GE(collectResult.data.uCpuUsage, 0);
178     ASSERT_GE(collectResult.data.sCpuUsage, 0);
179     ASSERT_GE(collectResult.data.cpuUsage, 0);
180     ASSERT_FALSE(collectResult.data.procName.empty());
181 
182     sleep(1); // 1s
183     auto nextCollectResult = collector->CollectProcessCpuStatInfo(initPid);
184     ASSERT_TRUE(nextCollectResult.retCode == UcError::SUCCESS);
185     ASSERT_EQ(nextCollectResult.data.startTime, collectResult.data.startTime);
186     ASSERT_GT(nextCollectResult.data.endTime, collectResult.data.endTime);
187 }
188 
189 /**
190  * @tc.name: CpuCollectorTest010
191  * @tc.desc: used to test CpuCollector.CollectProcessCpuStatInfo
192  * @tc.type: FUNC
193 */
194 HWTEST_F(CpuCollectorTest, CpuCollectorTest010, TestSize.Level1)
195 {
196     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
197     constexpr int notExistPid = INT_MAX;
198     auto collectResult = collector->CollectProcessCpuStatInfo(notExistPid);
199     ASSERT_TRUE(collectResult.retCode != UcError::SUCCESS);
200 }
201 
202 /**
203  * @tc.name: CpuCollectorTest011
204  * @tc.desc: used to test CpuCollector.CollectProcessCpuStatInfo
205  * @tc.type: FUNC
206 */
207 HWTEST_F(CpuCollectorTest, CpuCollectorTest011, TestSize.Level1)
208 {
209     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
210     constexpr int invalidPid = -1;
211     auto collectResult = collector->CollectProcessCpuStatInfo(invalidPid);
212     ASSERT_TRUE(collectResult.retCode != UcError::SUCCESS);
213 }
214 
215 /**
216  * @tc.name: CpuCollectorTest012
217  * @tc.desc: used to test CpuCollector.CollectProcessCpuStatInfos
218  * @tc.type: FUNC
219 */
220 HWTEST_F(CpuCollectorTest, CpuCollectorTest012, TestSize.Level1)
221 {
222     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
223     auto collectResult = collector->CollectProcessCpuStatInfos(true);
224     ASSERT_TRUE(collectResult.retCode == UcError::SUCCESS);
225     ASSERT_FALSE(collectResult.data.empty());
226 
227     sleep(1); // 1s
228     auto nextCollectResult = collector->CollectProcessCpuStatInfos();
229     ASSERT_TRUE(nextCollectResult.retCode == UcError::SUCCESS);
230     ASSERT_FALSE(nextCollectResult.data.empty());
231 
232     std::cout << "next collection startTime=" << nextCollectResult.data[0].startTime << std::endl;
233     std::cout << "next collection endTime=" << nextCollectResult.data[0].endTime << std::endl;
234     ASSERT_EQ(nextCollectResult.data[0].startTime, collectResult.data[0].endTime);
235     ASSERT_GT(nextCollectResult.data[0].endTime, nextCollectResult.data[0].startTime);
236 }
237 
238 /**
239  * @tc.name: CpuCollectorTest013
240  * @tc.desc: used to test CpuCollector.CollectProcessCpuStatInfos
241  * @tc.type: FUNC
242 */
243 HWTEST_F(CpuCollectorTest, CpuCollectorTest013, TestSize.Level1)
244 {
245     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
246     auto collectResult = collector->CollectProcessCpuStatInfos(true);
247     ASSERT_TRUE(collectResult.retCode == UcError::SUCCESS);
248     ASSERT_FALSE(collectResult.data.empty());
249 
250     sleep(1); // 1s
251     auto nextCollectResult = collector->CollectProcessCpuStatInfos(true);
252     ASSERT_TRUE(nextCollectResult.retCode == UcError::SUCCESS);
253     ASSERT_FALSE(nextCollectResult.data.empty());
254 
255     ASSERT_GT(nextCollectResult.data[0].startTime, collectResult.data[0].startTime);
256     ASSERT_GT(nextCollectResult.data[0].endTime, collectResult.data[0].endTime);
257 }
258 
259 /**
260  * @tc.name: CpuCollectorTest014
261  * @tc.desc: used to test CpuCollector.CollectProcessCpuStatInfos
262  * @tc.type: FUNC
263 */
264 HWTEST_F(CpuCollectorTest, CpuCollectorTest014, TestSize.Level1)
265 {
266     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
267     auto firstCollectResult = collector->CollectProcessCpuStatInfos(false);
268     ASSERT_TRUE(firstCollectResult.retCode == UcError::SUCCESS);
269     ASSERT_FALSE(firstCollectResult.data.empty());
270 
271     sleep(1); // 1s
272     auto secondCollectResult = collector->CollectProcessCpuStatInfos(false);
273     ASSERT_TRUE(secondCollectResult.retCode == UcError::SUCCESS);
274     ASSERT_FALSE(secondCollectResult.data.empty());
275 
276     ASSERT_EQ(firstCollectResult.data[0].startTime, secondCollectResult.data[0].startTime);
277     ASSERT_LT(firstCollectResult.data[0].endTime, secondCollectResult.data[0].endTime);
278 }
279 
280 /**
281  * @tc.name: CpuCollectorTest015
282  * @tc.desc: used to test the update function of CpuCollector.CollectProcessCpuStatInfo
283  * @tc.type: FUNC
284 */
285 HWTEST_F(CpuCollectorTest, CpuCollectorTest015, TestSize.Level1)
286 {
287     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
288     constexpr int initPid = 1;
289     auto collectResult = collector->CollectProcessCpuStatInfo(initPid, true);
290     ASSERT_TRUE(collectResult.retCode == UcError::SUCCESS);
291 
292     sleep(1); // 1s
293     auto nextCollectResult = collector->CollectProcessCpuStatInfo(initPid, true);
294     ASSERT_TRUE(nextCollectResult.retCode == UcError::SUCCESS);
295 
296     std::cout << "first collection startTime=" << collectResult.data.startTime << std::endl;
297     std::cout << "first collection endTime=" << collectResult.data.endTime << std::endl;
298     std::cout << "next collection startTime=" << nextCollectResult.data.startTime << std::endl;
299     std::cout << "next collection endTime=" << nextCollectResult.data.endTime << std::endl;
300     ASSERT_EQ(nextCollectResult.data.startTime, collectResult.data.endTime);
301     ASSERT_GT(nextCollectResult.data.endTime, collectResult.data.endTime);
302 }
303 
304 /**
305  * @tc.name: CpuCollectorTest016
306  * @tc.desc: used to test the function of CpuCollector.GetSysCpuUsage;
307  * @tc.type: FUNC
308 */
309 HWTEST_F(CpuCollectorTest, CpuCollectorTest016, TestSize.Level1)
310 {
311     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
312     auto collectResult = collector->GetSysCpuUsage();
313     ASSERT_TRUE(collectResult.retCode == UcError::SUCCESS);
314     ASSERT_TRUE(collectResult.data >= 0 && collectResult.data <= 1);
315 }
316 
317 /**
318  * @tc.name: CpuCollectorTest017
319  * @tc.desc: used to test the function of CreateThreadStatInfoCollector with self pid;
320  * @tc.type: FUNC
321 */
322 HWTEST_F(CpuCollectorTest, CpuCollectorTest017, TestSize.Level1)
323 {
324     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
325     auto threadCollector = collector->CreateThreadCollector(getpid());
326     sleep(1);
327     auto collectResult1 = threadCollector->CollectThreadStatInfos();
328     ASSERT_TRUE(collectResult1.retCode == UcError::SUCCESS);
329     sleep(1);
330     auto collectResult2 = threadCollector->CollectThreadStatInfos(true);
331     ASSERT_TRUE(collectResult2.retCode == UcError::SUCCESS);
332     ASSERT_TRUE(collectResult2.data.size() >= 1);
333     ASSERT_TRUE(collectResult2.data[0].cpuUsage >= 0);
334     ASSERT_TRUE(collectResult1.data[0].startTime == collectResult2.data[0].startTime);
335     sleep(1);
336     auto collectResult3 = threadCollector->CollectThreadStatInfos();
337     ASSERT_TRUE(collectResult3.retCode == UcError::SUCCESS);
338     ASSERT_TRUE(collectResult3.data.size() >= 1);
339     ASSERT_TRUE(collectResult3.data[0].cpuUsage >= 0);
340     ASSERT_TRUE(collectResult2.data[0].endTime == collectResult3.data[0].startTime);
341 }
342 
343 /**
344  * @tc.name: CpuCollectorTest018
345  * @tc.desc: used to test the function of CreateThreadStatInfoCollector with other pid;
346  * @tc.type: FUNC
347 */
348 HWTEST_F(CpuCollectorTest, CpuCollectorTest018, TestSize.Level1)
349 {
350     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
351     auto threadCollector = collector->CreateThreadCollector(1);
352     sleep(1);
353     auto collectResult1 = threadCollector->CollectThreadStatInfos(true);
354     ASSERT_TRUE(collectResult1.retCode == UcError::SUCCESS);
355     sleep(1);
356     auto collectResult2 = threadCollector->CollectThreadStatInfos(false);
357     ASSERT_TRUE(collectResult2.retCode == UcError::SUCCESS);
358     ASSERT_TRUE(collectResult2.data.size() >= 1);
359     ASSERT_TRUE(collectResult2.data[0].cpuUsage >= 0);
360     ASSERT_TRUE(collectResult1.data[0].endTime == collectResult2.data[0].startTime);
361     sleep(1);
362     auto collectResult3 = threadCollector->CollectThreadStatInfos();
363     ASSERT_TRUE(collectResult3.retCode == UcError::SUCCESS);
364     ASSERT_TRUE(collectResult3.data.size() >= 1);
365     ASSERT_TRUE(collectResult3.data[0].cpuUsage >= 0);
366     ASSERT_TRUE(collectResult2.data[0].startTime == collectResult3.data[0].startTime);
367 }
368 
369 /**
370  * @tc.name: CpuCollectorTest019
371  * @tc.desc: used to test the function of CpuCollector.Create;
372  * @tc.type: FUNC
373 */
374 HWTEST_F(CpuCollectorTest, CpuCollectorTest019, TestSize.Level1)
375 {
376     std::shared_ptr<CpuCollector> collector1 = CpuCollector::Create();
377     ASSERT_NE(collector1, nullptr);
378     std::shared_ptr<CpuCollector> collector2 = CpuCollector::Create();
379     ASSERT_NE(collector2, nullptr);
380     ASSERT_EQ(collector1, collector2);
381 
382     std::shared_ptr<CpuCollector> collector3 = CpuCollector::Create(false);
383     ASSERT_NE(collector3, nullptr);
384     std::shared_ptr<CpuCollector> collector4 = CpuCollector::Create(false);
385     ASSERT_NE(collector4, nullptr);
386     ASSERT_NE(collector3, collector4);
387 }
388 #else
389 /**
390  * @tc.name: CpuCollectorTest001
391  * @tc.desc: used to test empty CpuCollector
392  * @tc.type: FUNC
393 */
394 HWTEST_F(CpuCollectorTest, CpuCollectorTest001, TestSize.Level1)
395 {
396     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
397     auto result1 = collector->CollectSysCpuLoad();
398     ASSERT_TRUE(result1.retCode == UcError::FEATURE_CLOSED);
399 
400     auto result2 = collector->CollectSysCpuUsage();
401     ASSERT_TRUE(result2.retCode == UcError::FEATURE_CLOSED);
402 
403     auto result3 = collector->GetSysCpuUsage();
404     ASSERT_TRUE(result3.retCode == UcError::FEATURE_CLOSED);
405 
406     auto result4 = collector->CollectProcessCpuStatInfo(0);
407     ASSERT_TRUE(result4.retCode == UcError::FEATURE_CLOSED);
408 
409     auto result5 = collector->CollectCpuFrequency();
410     ASSERT_TRUE(result5.retCode == UcError::FEATURE_CLOSED);
411 
412     auto result6 = collector->CollectProcessCpuStatInfos();
413     ASSERT_TRUE(result6.retCode == UcError::FEATURE_CLOSED);
414 
415     auto result7 = collector->CreateThreadCollector(0);
416     ASSERT_TRUE(result7 == nullptr);
417 }
418 #endif
419