• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hiview_logger.h"
21 #include "parameter_ex.h"
22 #include "trace_collector.h"
23 
24 using namespace OHOS::HiviewDFX::UCollectUtil;
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_TAG("Hiview-CpuCollectionTask");
29 struct CpuThresholdItem {
30     std::string processName;
31     UCollect::TraceCaller caller;
32     double cpuLoadThreshold = 0.0;
33     bool hasOverThreshold = false;
34 };
35 
CpuCollectionTask(const std::string & workPath)36 CpuCollectionTask::CpuCollectionTask(const std::string& workPath) : workPath_(workPath)
37 {
38     InitCpuCollector();
39     if (Parameter::IsBetaVersion()) {
40         InitCpuStorage();
41     }
42 #ifdef HAS_HIPERF
43     InitCpuPerfDump();
44 #endif
45 }
46 
Collect()47 void CpuCollectionTask::Collect()
48 {
49     if (Parameter::IsBetaVersion()) {
50         ReportCpuCollectionEvent();
51     }
52     CollectCpuData();
53 }
54 
InitCpuCollector()55 void CpuCollectionTask::InitCpuCollector()
56 {
57     cpuCollector_ = UCollectUtil::CpuCollector::Create();
58     threadCpuCollector_ = cpuCollector_->CreateThreadCollector(getprocpid());
59 }
60 
InitCpuStorage()61 void CpuCollectionTask::InitCpuStorage()
62 {
63     cpuStorage_ = std::make_shared<CpuStorage>(workPath_);
64 }
65 
66 #ifdef HAS_HIPERF
InitCpuPerfDump()67 void CpuCollectionTask::InitCpuPerfDump()
68 {
69     cpuPerfDump_ = std::make_shared<CpuPerfDump>();
70 }
71 #endif
72 
ReportCpuCollectionEvent()73 void CpuCollectionTask::ReportCpuCollectionEvent()
74 {
75     cpuStorage_->Report();
76 }
77 
CheckAndDumpTraceData()78 void CpuCollectionTask::CheckAndDumpTraceData()
79 {
80     static std::vector<CpuThresholdItem> checkItems = {
81         {"hiview", UCollect::TraceCaller::HIVIEW, 0.07, false}, // 0.07 : 7% cpu load
82     };
83 
84     for (auto &item : checkItems) {
85         int32_t pid = CommonUtils::GetPidByName(item.processName);
86         if (pid <= 0) {
87             HIVIEW_LOGW("get pid failed, process:%{public}s", item.processName.c_str());
88             continue;
89         }
90         auto processCpuStatInfo = cpuCollector_->CollectProcessCpuStatInfo(pid);
91         double cpuLoad = processCpuStatInfo.data.cpuLoad;
92         if (!item.hasOverThreshold && cpuLoad >= item.cpuLoadThreshold) {
93             HIVIEW_LOGI("over threshold, current cpu load:%{public}f", cpuLoad);
94             item.hasOverThreshold = true;
95             return;
96         }
97 
98         // when cpu load restore to normal, start capture history trace
99         if (item.hasOverThreshold && cpuLoad < item.cpuLoadThreshold) {
100             HIVIEW_LOGI("capture history trace");
101             auto traceCollector = TraceCollector::Create();
102             traceCollector->DumpTrace(item.caller);
103             item.hasOverThreshold = false;
104         }
105     }
106 }
107 
CollectCpuData()108 void CpuCollectionTask::CollectCpuData()
109 {
110     auto cpuCollectionsResult = cpuCollector_->CollectProcessCpuStatInfos(true);
111     if (cpuCollectionsResult.retCode == UCollect::UcError::SUCCESS) {
112         if (Parameter::IsBetaVersion()) {
113             cpuStorage_->StoreProcessDatas(cpuCollectionsResult.data);
114         }
115 
116 #ifdef HAS_HIPERF
117         cpuPerfDump_->CheckAndDumpPerfData(cpuCollectionsResult.data);
118 #endif
119     }
120     if (threadCpuCollector_ != nullptr) {
121         auto threadCpuCollectResult = threadCpuCollector_ ->CollectThreadStatInfos(true);
122         if (Parameter::IsBetaVersion() && threadCpuCollectResult.retCode == UCollect::UcError::SUCCESS) {
123             cpuStorage_->StoreThreadDatas(threadCpuCollectResult.data);
124         }
125     }
126     // collect the system cpu usage periodically for hidumper
127     cpuCollector_->CollectSysCpuUsage(true);
128 
129 #ifdef CATCH_TRACE_FOR_CPU_HIGH_LOAD
130     if (Parameter::IsBetaVersion()) {
131         CheckAndDumpTraceData();
132     }
133 #endif
134 }
135 }  // namespace HiviewDFX
136 }  // namespace OHOS
137