• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "cpu_collection_task.h"
16 
17 namespace OHOS {
18 namespace HiviewDFX {
CpuCollectionTask(const std::string & workPath)19 CpuCollectionTask::CpuCollectionTask(const std::string& workPath) : workPath_(workPath)
20 {
21     InitCpuCollector();
22     InitCpuStorage();
23 #ifdef HAS_HIPERF
24     InitCpuPerfDump();
25 #endif
26 }
27 
Collect()28 void CpuCollectionTask::Collect()
29 {
30     ReportCpuCollectionEvent();
31     CollectCpuData();
32 }
33 
InitCpuCollector()34 void CpuCollectionTask::InitCpuCollector()
35 {
36     cpuCollector_ = UCollectUtil::CpuCollector::Create();
37 }
38 
InitCpuStorage()39 void CpuCollectionTask::InitCpuStorage()
40 {
41     cpuStorage_ = std::make_shared<CpuStorage>(workPath_);
42 }
43 
44 #ifdef HAS_HIPERF
InitCpuPerfDump()45 void CpuCollectionTask::InitCpuPerfDump()
46 {
47     cpuPerfDump_ = std::make_shared<CpuPerfDump>();
48 }
49 #endif
50 
ReportCpuCollectionEvent()51 void CpuCollectionTask::ReportCpuCollectionEvent()
52 {
53     cpuStorage_->Report();
54 }
55 
CollectCpuData()56 void CpuCollectionTask::CollectCpuData()
57 {
58     auto cpuCollectionsResult = cpuCollector_->CollectProcessCpuStatInfos(true);
59     if (cpuCollectionsResult.retCode == UCollect::UcError::SUCCESS) {
60         cpuStorage_->Store(cpuCollectionsResult.data);
61 #ifdef HAS_HIPERF
62         cpuPerfDump_->CheckAndDumpPerfData(cpuCollectionsResult.data);
63 #endif
64     }
65 
66     // collect the system cpu usage periodically for hidumper
67     cpuCollector_->CollectSysCpuUsage(true);
68 }
69 }  // namespace HiviewDFX
70 }  // namespace OHOS
71