1 /* 2 * Copyright (C) 2023-2025 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 #include <unistd.h> 18 19 #include "common_utils.h" 20 #ifdef CATCH_TRACE_FOR_CPU_HIGH_LOAD 21 #include "dump_trace_controller.h" 22 #endif 23 #include "hiview_logger.h" 24 #include "parameter_ex.h" 25 #include "time_util.h" 26 #include "trace_collector.h" 27 28 using namespace OHOS::HiviewDFX::UCollectUtil; 29 30 namespace OHOS { 31 namespace HiviewDFX { 32 DEFINE_LOG_TAG("Hiview-CpuCollectionTask"); 33 CpuCollectionTask(const std::string & workPath)34CpuCollectionTask::CpuCollectionTask(const std::string& workPath) : workPath_(workPath) 35 { 36 InitCpuCollector(); 37 if (Parameter::IsBetaVersion()) { 38 InitCpuStorage(); 39 } 40 #ifdef HAS_HIPERF 41 InitCpuPerfDump(); 42 #endif 43 44 #ifdef CATCH_TRACE_FOR_CPU_HIGH_LOAD 45 InitDumpTraceController(); 46 #endif 47 } 48 Collect()49void CpuCollectionTask::Collect() 50 { 51 if (Parameter::IsBetaVersion()) { 52 ReportCpuCollectionEvent(); 53 } 54 CollectCpuData(); 55 } 56 InitCpuCollector()57void CpuCollectionTask::InitCpuCollector() 58 { 59 cpuCollector_ = UCollectUtil::CpuCollector::Create(); 60 threadCpuCollector_ = cpuCollector_->CreateThreadCollector(getprocpid()); 61 } 62 InitCpuStorage()63void CpuCollectionTask::InitCpuStorage() 64 { 65 cpuStorage_ = std::make_shared<CpuStorage>(workPath_); 66 } 67 68 #ifdef HAS_HIPERF InitCpuPerfDump()69void CpuCollectionTask::InitCpuPerfDump() 70 { 71 cpuPerfDump_ = std::make_shared<CpuPerfDump>(); 72 } 73 #endif 74 ReportCpuCollectionEvent()75void CpuCollectionTask::ReportCpuCollectionEvent() 76 { 77 cpuStorage_->Report(); 78 } 79 80 #ifdef CATCH_TRACE_FOR_CPU_HIGH_LOAD InitDumpTraceController()81void CpuCollectionTask::InitDumpTraceController() 82 { 83 CpuThresholdItem hiviewCpuThresholdItem = { 84 .caller = UCollect::TraceCaller::HIVIEW, 85 .dumpTraceInterval = 120, // 120 : two minutes 86 .cpuLoadThreshold = 0.1, // 0.1 : 10% cpu load 87 .processName = "hiview" 88 }; 89 std::vector<CpuThresholdItem> items = {hiviewCpuThresholdItem}; 90 dumpTraceController_ = std::make_shared<DumpTraceController>(items); 91 } 92 #endif 93 CollectCpuData()94void CpuCollectionTask::CollectCpuData() 95 { 96 auto cpuCollectionsResult = cpuCollector_->CollectProcessCpuStatInfos(true); 97 if (cpuCollectionsResult.retCode == UCollect::UcError::SUCCESS) { 98 if (Parameter::IsBetaVersion()) { 99 cpuStorage_->StoreProcessDatas(cpuCollectionsResult.data); 100 } 101 102 #ifdef HAS_HIPERF 103 cpuPerfDump_->CheckAndDumpPerfData(cpuCollectionsResult.data); 104 #endif 105 } 106 if (threadCpuCollector_ != nullptr) { 107 auto threadCpuCollectResult = threadCpuCollector_ ->CollectThreadStatInfos(true); 108 if (Parameter::IsBetaVersion() && threadCpuCollectResult.retCode == UCollect::UcError::SUCCESS) { 109 cpuStorage_->StoreThreadDatas(threadCpuCollectResult.data); 110 } 111 } 112 // collect the system cpu usage periodically for hidumper 113 cpuCollector_->CollectSysCpuUsage(true); 114 115 #ifdef CATCH_TRACE_FOR_CPU_HIGH_LOAD 116 if (Parameter::IsBetaVersion()) { 117 dumpTraceController_->CheckAndDumpTrace(); 118 } 119 #endif 120 } 121 } // namespace HiviewDFX 122 } // namespace OHOS 123